2
|
1 |
/*
|
5506
|
2 |
* Copyright (c) 2003, 2004, Oracle and/or its affiliates. All rights reserved.
|
2
|
3 |
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
|
|
4 |
*
|
|
5 |
* This code is free software; you can redistribute it and/or modify it
|
|
6 |
* under the terms of the GNU General Public License version 2 only, as
|
|
7 |
* published by the Free Software Foundation.
|
|
8 |
*
|
|
9 |
* This code is distributed in the hope that it will be useful, but WITHOUT
|
|
10 |
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
11 |
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
12 |
* version 2 for more details (a copy is included in the LICENSE file that
|
|
13 |
* accompanied this code).
|
|
14 |
*
|
|
15 |
* You should have received a copy of the GNU General Public License version
|
|
16 |
* 2 along with this work; if not, write to the Free Software Foundation,
|
|
17 |
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
18 |
*
|
5506
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
|
|
20 |
* or visit www.oracle.com if you need additional information or have any
|
|
21 |
* questions.
|
2
|
22 |
*/
|
|
23 |
|
|
24 |
/* @test
|
|
25 |
* @bug 4173528 5068772
|
|
26 |
* @summary Unit tests for java.util.UUID
|
|
27 |
*/
|
|
28 |
|
|
29 |
import java.util.*;
|
|
30 |
|
|
31 |
public class UUIDTest {
|
|
32 |
|
|
33 |
static Random generator = new Random();
|
|
34 |
|
|
35 |
public static void main(String[] args) throws Exception {
|
|
36 |
containsTest();
|
|
37 |
randomUUIDTest();
|
|
38 |
nameUUIDFromBytesTest();
|
|
39 |
stringTest();
|
|
40 |
versionTest();
|
|
41 |
variantTest();
|
|
42 |
timestampTest();
|
|
43 |
clockSequenceTest();
|
|
44 |
nodeTest();
|
|
45 |
hashCodeEqualsTest();
|
|
46 |
compareTo();
|
|
47 |
}
|
|
48 |
|
|
49 |
// Verify that list.contains detects UUID collisons
|
|
50 |
private static void containsTest() throws Exception {
|
|
51 |
List list = new LinkedList();
|
|
52 |
list.add(new UUID(4,4));
|
|
53 |
if (!list.contains(new UUID(4,4)))
|
|
54 |
throw new Exception("contains test did not work as expected");
|
|
55 |
}
|
|
56 |
|
|
57 |
private static void randomUUIDTest() throws Exception {
|
|
58 |
List list = new LinkedList();
|
|
59 |
for (int i=0; i<100; i++) {
|
|
60 |
UUID u1 = UUID.randomUUID();
|
|
61 |
if (list.contains(u1))
|
|
62 |
throw new Exception("random UUID collision very unlikely");
|
|
63 |
list.add(u1);
|
|
64 |
}
|
|
65 |
}
|
|
66 |
|
|
67 |
private static void nameUUIDFromBytesTest() throws Exception {
|
|
68 |
Random byteSource = new Random();
|
|
69 |
byte[] someBytes = new byte[12];
|
|
70 |
List list = new LinkedList();
|
|
71 |
for (int i=0; i<100; i++) {
|
|
72 |
byteSource.nextBytes(someBytes);
|
|
73 |
UUID test = UUID.nameUUIDFromBytes(someBytes);
|
|
74 |
if (list.contains(test))
|
|
75 |
throw new Exception("byte UUID collision very unlikely");
|
|
76 |
list.add(test);
|
|
77 |
}
|
|
78 |
}
|
|
79 |
|
|
80 |
private static void stringTest() throws Exception {
|
|
81 |
for (int i=0; i<100; i++) {
|
|
82 |
UUID u1 = UUID.randomUUID();
|
|
83 |
UUID u2 = UUID.fromString(u1.toString());
|
|
84 |
if (!u1.equals(u2))
|
|
85 |
throw new Exception("UUID -> string -> UUID failed");
|
|
86 |
}
|
|
87 |
}
|
|
88 |
|
|
89 |
private static void versionTest() throws Exception {
|
|
90 |
UUID test = UUID.randomUUID();
|
|
91 |
if (test.version() != 4)
|
|
92 |
throw new Exception("randomUUID not type 4");
|
|
93 |
Random byteSource = new Random();
|
|
94 |
byte[] someBytes = new byte[12];
|
|
95 |
byteSource.nextBytes(someBytes);
|
|
96 |
test = UUID.nameUUIDFromBytes(someBytes);
|
|
97 |
if (test.version() != 3)
|
|
98 |
throw new Exception("nameUUIDFromBytes not type 3");
|
|
99 |
test = UUID.fromString("9835451d-e2e0-1e41-8a5a-be785f17dcda");
|
|
100 |
if (test.version() != 1)
|
|
101 |
throw new Exception("wrong version fromString 1");
|
|
102 |
test = UUID.fromString("9835451d-e2e0-2e41-8a5a-be785f17dcda");
|
|
103 |
if (test.version() != 2)
|
|
104 |
throw new Exception("wrong version fromString 2");
|
|
105 |
test = UUID.fromString("9835451d-e2e0-3e41-8a5a-be785f17dcda");
|
|
106 |
if (test.version() != 3)
|
|
107 |
throw new Exception("wrong version fromString 3");
|
|
108 |
test = UUID.fromString("9835451d-e2e0-4e41-8a5a-be785f17dcda");
|
|
109 |
if (test.version() != 4)
|
|
110 |
throw new Exception("wrong version fromString 4");
|
|
111 |
test = new UUID(0x0000000000001000L, 55L);
|
|
112 |
if (test.version() != 1)
|
|
113 |
throw new Exception("wrong version from bit set to 1");
|
|
114 |
test = new UUID(0x0000000000002000L, 55L);
|
|
115 |
if (test.version() != 2)
|
|
116 |
throw new Exception("wrong version from bit set to 2");
|
|
117 |
test = new UUID(0x0000000000003000L, 55L);
|
|
118 |
if (test.version() != 3)
|
|
119 |
throw new Exception("wrong version from bit set to 3");
|
|
120 |
test = new UUID(0x0000000000004000L, 55L);
|
|
121 |
if (test.version() != 4)
|
|
122 |
throw new Exception("wrong version from bit set to 4");
|
|
123 |
}
|
|
124 |
|
|
125 |
private static void variantTest() throws Exception {
|
|
126 |
UUID test = UUID.randomUUID();
|
|
127 |
if (test.variant() != 2)
|
|
128 |
throw new Exception("randomUUID not variant 2");
|
|
129 |
Random byteSource = new Random();
|
|
130 |
byte[] someBytes = new byte[12];
|
|
131 |
byteSource.nextBytes(someBytes);
|
|
132 |
test = UUID.nameUUIDFromBytes(someBytes);
|
|
133 |
if (test.variant() != 2)
|
|
134 |
throw new Exception("nameUUIDFromBytes not variant 2");
|
|
135 |
test = new UUID(55L, 0x0000000000001000L);
|
|
136 |
if (test.variant() != 0)
|
|
137 |
throw new Exception("wrong variant from bit set to 0");
|
|
138 |
test = new UUID(55L, 0x8000000000001000L);
|
|
139 |
if (test.variant() != 2)
|
|
140 |
throw new Exception("wrong variant from bit set to 2");
|
|
141 |
test = new UUID(55L, 0xc000000000001000L);
|
|
142 |
if (test.variant() != 6)
|
|
143 |
throw new Exception("wrong variant from bit set to 6");
|
|
144 |
test = new UUID(55L, 0xe000000000001000L);
|
|
145 |
if (test.variant() != 7)
|
|
146 |
throw new Exception("wrong variant from bit set to 7");
|
|
147 |
}
|
|
148 |
|
|
149 |
private static void timestampTest() throws Exception {
|
|
150 |
UUID test = UUID.randomUUID();
|
|
151 |
try {
|
|
152 |
test.timestamp();
|
|
153 |
throw new Exception("Expected exception not thrown");
|
|
154 |
} catch (UnsupportedOperationException uoe) {
|
|
155 |
// Correct result
|
|
156 |
}
|
|
157 |
test = UUID.fromString("00000001-0000-1000-8a5a-be785f17dcda");
|
|
158 |
if (test.timestamp() != 1)
|
|
159 |
throw new Exception("Incorrect timestamp");
|
|
160 |
test = UUID.fromString("00000400-0000-1000-8a5a-be785f17dcda");
|
|
161 |
if (test.timestamp() != 1024)
|
|
162 |
throw new Exception("Incorrect timestamp");
|
|
163 |
test = UUID.fromString("FFFFFFFF-FFFF-1FFF-8a5a-be785f17dcda");
|
|
164 |
if (test.timestamp() != Long.MAX_VALUE>>3)
|
|
165 |
throw new Exception("Incorrect timestamp");
|
|
166 |
}
|
|
167 |
|
|
168 |
private static void clockSequenceTest() throws Exception {
|
|
169 |
UUID test = UUID.randomUUID();
|
|
170 |
try {
|
|
171 |
test.clockSequence();
|
|
172 |
throw new Exception("Expected exception not thrown");
|
|
173 |
} catch (UnsupportedOperationException uoe) {
|
|
174 |
// Correct result
|
|
175 |
}
|
|
176 |
test = UUID.fromString("00000001-0000-1000-8001-be785f17dcda");
|
|
177 |
if (test.clockSequence() != 1)
|
|
178 |
throw new Exception("Incorrect sequence");
|
|
179 |
test = UUID.fromString("00000001-0000-1000-8002-be785f17dcda");
|
|
180 |
if (test.clockSequence() != 2)
|
|
181 |
throw new Exception("Incorrect sequence");
|
|
182 |
test = UUID.fromString("00000001-0000-1000-8010-be785f17dcda");
|
|
183 |
if (test.clockSequence() != 16)
|
|
184 |
throw new Exception("Incorrect sequence");
|
|
185 |
test = UUID.fromString("00000001-0000-1000-bFFF-be785f17dcda");
|
|
186 |
if (test.clockSequence() != ((2L<<13)-1)) // 2^14 - 1
|
|
187 |
throw new Exception("Incorrect sequence");
|
|
188 |
}
|
|
189 |
|
|
190 |
private static void nodeTest() throws Exception {
|
|
191 |
UUID test = UUID.randomUUID();
|
|
192 |
try {
|
|
193 |
test.node();
|
|
194 |
throw new Exception("Expected exception not thrown");
|
|
195 |
} catch (UnsupportedOperationException uoe) {
|
|
196 |
// Correct result
|
|
197 |
}
|
|
198 |
test = UUID.fromString("00000001-0000-1000-8001-000000000001");
|
|
199 |
if (test.node() != 1)
|
|
200 |
throw new Exception("Incorrect node");
|
|
201 |
test = UUID.fromString("00000001-0000-1000-8002-FFFFFFFFFFFF");
|
|
202 |
if (test.node() != ((2L<<47)-1)) // 2^48 - 1
|
|
203 |
throw new Exception("Incorrect node");
|
|
204 |
}
|
|
205 |
|
|
206 |
private static void hashCodeEqualsTest() throws Exception {
|
|
207 |
// If two UUIDs are equal they must have the same hashCode
|
|
208 |
for (int i=0; i<100; i++) {
|
|
209 |
UUID u1 = UUID.randomUUID();
|
|
210 |
UUID u2 = UUID.fromString(u1.toString());
|
|
211 |
if (u1.hashCode() != u2.hashCode())
|
|
212 |
throw new Exception("Equal UUIDs with different hashcodes");
|
|
213 |
}
|
|
214 |
// Test equality of UUIDs with tampered bits
|
|
215 |
for (int i=0; i<1000; i++) {
|
|
216 |
long l = generator.nextLong();
|
|
217 |
long l2 = generator.nextLong();
|
|
218 |
int position = generator.nextInt(64);
|
|
219 |
UUID u1 = new UUID(l, l2);
|
|
220 |
l = l ^ (1L << position);
|
|
221 |
UUID u2 = new UUID(l, l2);
|
|
222 |
if (u1.equals(u2))
|
|
223 |
throw new Exception("UUIDs with different bits equal");
|
|
224 |
}
|
|
225 |
}
|
|
226 |
|
|
227 |
private static void compareTo() throws Exception {
|
|
228 |
UUID id = new UUID(33L, 63L);
|
|
229 |
UUID id2 = new UUID(34L, 62L);
|
|
230 |
UUID id3 = new UUID(34L, 63L);
|
|
231 |
UUID id4 = new UUID(34L, 64L);
|
|
232 |
UUID id5 = new UUID(35L, 63L);
|
|
233 |
|
|
234 |
if ((id.compareTo(id2) >= 0) ||
|
|
235 |
(id2.compareTo(id3) >= 0) ||
|
|
236 |
(id3.compareTo(id4) >= 0) ||
|
|
237 |
(id4.compareTo(id5) >= 0))
|
|
238 |
throw new RuntimeException("compareTo failure");
|
|
239 |
|
|
240 |
if ((id5.compareTo(id4) <= 0) ||
|
|
241 |
(id4.compareTo(id3) <= 0) ||
|
|
242 |
(id3.compareTo(id2) <= 0) ||
|
|
243 |
(id2.compareTo(id) <= 0))
|
|
244 |
throw new RuntimeException("compareTo failure");
|
|
245 |
|
|
246 |
if (id.compareTo(id) != 0)
|
|
247 |
throw new RuntimeException("compareTo failure");
|
|
248 |
|
|
249 |
}
|
|
250 |
|
|
251 |
}
|