0N/A/*
2362N/A * Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/* @test
0N/A * @bug 4173528 5068772
0N/A * @summary Unit tests for java.util.UUID
0N/A */
0N/A
0N/Aimport java.util.*;
0N/A
0N/Apublic class UUIDTest {
0N/A
0N/A static Random generator = new Random();
0N/A
0N/A public static void main(String[] args) throws Exception {
0N/A containsTest();
0N/A randomUUIDTest();
0N/A nameUUIDFromBytesTest();
0N/A stringTest();
0N/A versionTest();
0N/A variantTest();
0N/A timestampTest();
0N/A clockSequenceTest();
0N/A nodeTest();
0N/A hashCodeEqualsTest();
0N/A compareTo();
0N/A }
0N/A
0N/A // Verify that list.contains detects UUID collisons
0N/A private static void containsTest() throws Exception {
0N/A List list = new LinkedList();
0N/A list.add(new UUID(4,4));
0N/A if (!list.contains(new UUID(4,4)))
0N/A throw new Exception("contains test did not work as expected");
0N/A }
0N/A
0N/A private static void randomUUIDTest() throws Exception {
0N/A List list = new LinkedList();
0N/A for (int i=0; i<100; i++) {
0N/A UUID u1 = UUID.randomUUID();
5019N/A if (4 != u1.version()) {
5019N/A throw new Exception("bad version");
5019N/A }
5019N/A if (2 != u1.variant()) {
5019N/A throw new Exception("bad variant");
5019N/A }
0N/A if (list.contains(u1))
0N/A throw new Exception("random UUID collision very unlikely");
0N/A list.add(u1);
0N/A }
0N/A }
0N/A
0N/A private static void nameUUIDFromBytesTest() throws Exception {
0N/A Random byteSource = new Random();
0N/A byte[] someBytes = new byte[12];
0N/A List list = new LinkedList();
0N/A for (int i=0; i<100; i++) {
0N/A byteSource.nextBytes(someBytes);
5019N/A UUID u1 = UUID.nameUUIDFromBytes(someBytes);
5019N/A if (3 != u1.version()) {
5019N/A throw new Exception("bad version");
5019N/A }
5019N/A if (2 != u1.variant()) {
5019N/A throw new Exception("bad variant");
5019N/A }
5019N/A if (list.contains(u1))
0N/A throw new Exception("byte UUID collision very unlikely");
5019N/A list.add(u1);
0N/A }
0N/A }
0N/A
0N/A private static void stringTest() throws Exception {
0N/A for (int i=0; i<100; i++) {
0N/A UUID u1 = UUID.randomUUID();
0N/A UUID u2 = UUID.fromString(u1.toString());
0N/A if (!u1.equals(u2))
0N/A throw new Exception("UUID -> string -> UUID failed");
0N/A }
0N/A }
0N/A
0N/A private static void versionTest() throws Exception {
0N/A UUID test = UUID.randomUUID();
0N/A if (test.version() != 4)
0N/A throw new Exception("randomUUID not type 4");
0N/A Random byteSource = new Random();
0N/A byte[] someBytes = new byte[12];
0N/A byteSource.nextBytes(someBytes);
0N/A test = UUID.nameUUIDFromBytes(someBytes);
0N/A if (test.version() != 3)
0N/A throw new Exception("nameUUIDFromBytes not type 3");
0N/A test = UUID.fromString("9835451d-e2e0-1e41-8a5a-be785f17dcda");
0N/A if (test.version() != 1)
0N/A throw new Exception("wrong version fromString 1");
0N/A test = UUID.fromString("9835451d-e2e0-2e41-8a5a-be785f17dcda");
0N/A if (test.version() != 2)
0N/A throw new Exception("wrong version fromString 2");
0N/A test = UUID.fromString("9835451d-e2e0-3e41-8a5a-be785f17dcda");
0N/A if (test.version() != 3)
0N/A throw new Exception("wrong version fromString 3");
0N/A test = UUID.fromString("9835451d-e2e0-4e41-8a5a-be785f17dcda");
0N/A if (test.version() != 4)
0N/A throw new Exception("wrong version fromString 4");
0N/A test = new UUID(0x0000000000001000L, 55L);
0N/A if (test.version() != 1)
0N/A throw new Exception("wrong version from bit set to 1");
0N/A test = new UUID(0x0000000000002000L, 55L);
0N/A if (test.version() != 2)
0N/A throw new Exception("wrong version from bit set to 2");
0N/A test = new UUID(0x0000000000003000L, 55L);
0N/A if (test.version() != 3)
0N/A throw new Exception("wrong version from bit set to 3");
0N/A test = new UUID(0x0000000000004000L, 55L);
0N/A if (test.version() != 4)
0N/A throw new Exception("wrong version from bit set to 4");
0N/A }
0N/A
0N/A private static void variantTest() throws Exception {
0N/A UUID test = UUID.randomUUID();
0N/A if (test.variant() != 2)
0N/A throw new Exception("randomUUID not variant 2");
0N/A Random byteSource = new Random();
0N/A byte[] someBytes = new byte[12];
0N/A byteSource.nextBytes(someBytes);
0N/A test = UUID.nameUUIDFromBytes(someBytes);
0N/A if (test.variant() != 2)
0N/A throw new Exception("nameUUIDFromBytes not variant 2");
0N/A test = new UUID(55L, 0x0000000000001000L);
0N/A if (test.variant() != 0)
0N/A throw new Exception("wrong variant from bit set to 0");
0N/A test = new UUID(55L, 0x8000000000001000L);
0N/A if (test.variant() != 2)
0N/A throw new Exception("wrong variant from bit set to 2");
0N/A test = new UUID(55L, 0xc000000000001000L);
0N/A if (test.variant() != 6)
0N/A throw new Exception("wrong variant from bit set to 6");
0N/A test = new UUID(55L, 0xe000000000001000L);
0N/A if (test.variant() != 7)
0N/A throw new Exception("wrong variant from bit set to 7");
0N/A }
0N/A
0N/A private static void timestampTest() throws Exception {
0N/A UUID test = UUID.randomUUID();
0N/A try {
0N/A test.timestamp();
0N/A throw new Exception("Expected exception not thrown");
0N/A } catch (UnsupportedOperationException uoe) {
0N/A // Correct result
0N/A }
0N/A test = UUID.fromString("00000001-0000-1000-8a5a-be785f17dcda");
0N/A if (test.timestamp() != 1)
0N/A throw new Exception("Incorrect timestamp");
0N/A test = UUID.fromString("00000400-0000-1000-8a5a-be785f17dcda");
0N/A if (test.timestamp() != 1024)
0N/A throw new Exception("Incorrect timestamp");
0N/A test = UUID.fromString("FFFFFFFF-FFFF-1FFF-8a5a-be785f17dcda");
0N/A if (test.timestamp() != Long.MAX_VALUE>>3)
0N/A throw new Exception("Incorrect timestamp");
0N/A }
0N/A
0N/A private static void clockSequenceTest() throws Exception {
0N/A UUID test = UUID.randomUUID();
0N/A try {
0N/A test.clockSequence();
0N/A throw new Exception("Expected exception not thrown");
0N/A } catch (UnsupportedOperationException uoe) {
0N/A // Correct result
0N/A }
0N/A test = UUID.fromString("00000001-0000-1000-8001-be785f17dcda");
0N/A if (test.clockSequence() != 1)
0N/A throw new Exception("Incorrect sequence");
0N/A test = UUID.fromString("00000001-0000-1000-8002-be785f17dcda");
0N/A if (test.clockSequence() != 2)
0N/A throw new Exception("Incorrect sequence");
0N/A test = UUID.fromString("00000001-0000-1000-8010-be785f17dcda");
0N/A if (test.clockSequence() != 16)
0N/A throw new Exception("Incorrect sequence");
0N/A test = UUID.fromString("00000001-0000-1000-bFFF-be785f17dcda");
0N/A if (test.clockSequence() != ((2L<<13)-1)) // 2^14 - 1
0N/A throw new Exception("Incorrect sequence");
0N/A }
0N/A
0N/A private static void nodeTest() throws Exception {
0N/A UUID test = UUID.randomUUID();
0N/A try {
0N/A test.node();
0N/A throw new Exception("Expected exception not thrown");
0N/A } catch (UnsupportedOperationException uoe) {
0N/A // Correct result
0N/A }
0N/A test = UUID.fromString("00000001-0000-1000-8001-000000000001");
0N/A if (test.node() != 1)
0N/A throw new Exception("Incorrect node");
0N/A test = UUID.fromString("00000001-0000-1000-8002-FFFFFFFFFFFF");
0N/A if (test.node() != ((2L<<47)-1)) // 2^48 - 1
0N/A throw new Exception("Incorrect node");
0N/A }
0N/A
0N/A private static void hashCodeEqualsTest() throws Exception {
0N/A // If two UUIDs are equal they must have the same hashCode
0N/A for (int i=0; i<100; i++) {
0N/A UUID u1 = UUID.randomUUID();
0N/A UUID u2 = UUID.fromString(u1.toString());
0N/A if (u1.hashCode() != u2.hashCode())
0N/A throw new Exception("Equal UUIDs with different hashcodes");
0N/A }
0N/A // Test equality of UUIDs with tampered bits
0N/A for (int i=0; i<1000; i++) {
0N/A long l = generator.nextLong();
0N/A long l2 = generator.nextLong();
0N/A int position = generator.nextInt(64);
0N/A UUID u1 = new UUID(l, l2);
0N/A l = l ^ (1L << position);
0N/A UUID u2 = new UUID(l, l2);
0N/A if (u1.equals(u2))
0N/A throw new Exception("UUIDs with different bits equal");
0N/A }
0N/A }
0N/A
0N/A private static void compareTo() throws Exception {
0N/A UUID id = new UUID(33L, 63L);
0N/A UUID id2 = new UUID(34L, 62L);
0N/A UUID id3 = new UUID(34L, 63L);
0N/A UUID id4 = new UUID(34L, 64L);
0N/A UUID id5 = new UUID(35L, 63L);
0N/A
0N/A if ((id.compareTo(id2) >= 0) ||
0N/A (id2.compareTo(id3) >= 0) ||
0N/A (id3.compareTo(id4) >= 0) ||
0N/A (id4.compareTo(id5) >= 0))
0N/A throw new RuntimeException("compareTo failure");
0N/A
0N/A if ((id5.compareTo(id4) <= 0) ||
0N/A (id4.compareTo(id3) <= 0) ||
0N/A (id3.compareTo(id2) <= 0) ||
0N/A (id2.compareTo(id) <= 0))
0N/A throw new RuntimeException("compareTo failure");
0N/A
0N/A if (id.compareTo(id) != 0)
0N/A throw new RuntimeException("compareTo failure");
0N/A
0N/A }
0N/A
0N/A}