1 /*
2 * Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
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 *
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.
22 */
23
24 /* @test
25 * @bug 4173528 5068772 8148936
26 * @summary Unit tests for java.util.UUID
27 * @key randomness
28 * @run main/othervm -XX:+CompactStrings UUIDTest
29 * @run main/othervm -XX:-CompactStrings UUIDTest
30 */
31
32 import java.util.*;
33
34 public class UUIDTest {
35
36 static Random generator = new Random();
37
38 public static void main(String[] args) throws Exception {
39 containsTest();
40 randomUUIDTest();
41 nameUUIDFromBytesTest();
42 stringTest();
43 versionTest();
44 variantTest();
45 timestampTest();
90 throw new Exception("byte UUID collision very unlikely");
91 list.add(u1);
92 }
93 }
94
95 private static void stringTest() throws Exception {
96 for (int i=0; i<100; i++) {
97 UUID u1 = UUID.randomUUID();
98 UUID u2 = UUID.fromString(u1.toString());
99 if (!u1.equals(u2))
100 throw new Exception("UUID -> string -> UUID failed");
101 }
102
103 testFromStringError("-0");
104 testFromStringError("x");
105 testFromStringError("----");
106 testFromStringError("-0-0-0-0");
107 testFromStringError("0-0-0-0-");
108 testFromStringError("0-0-0-0-0-");
109 testFromStringError("0-0-0-0-x");
110 }
111
112 private static void testFromStringError(String str) {
113 try {
114 UUID test = UUID.fromString(str);
115 throw new RuntimeException("Should have thrown IAE");
116 } catch (IllegalArgumentException iae) {
117 // pass
118 }
119 }
120
121 private static void versionTest() throws Exception {
122 UUID test = UUID.randomUUID();
123 if (test.version() != 4)
124 throw new Exception("randomUUID not type 4");
125 Random byteSource = new Random();
126 byte[] someBytes = new byte[12];
127 byteSource.nextBytes(someBytes);
128 test = UUID.nameUUIDFromBytes(someBytes);
129 if (test.version() != 3)
|
1 /*
2 * Copyright (c) 2003, 2019, Oracle and/or its affiliates. All rights reserved.
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 *
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.
22 */
23
24 /* @test
25 * @bug 4173528 5068772 8148936 8216407
26 * @summary Unit tests for java.util.UUID
27 * @key randomness
28 * @run main/othervm -XX:+CompactStrings UUIDTest
29 * @run main/othervm -XX:-CompactStrings UUIDTest
30 */
31
32 import java.util.*;
33
34 public class UUIDTest {
35
36 static Random generator = new Random();
37
38 public static void main(String[] args) throws Exception {
39 containsTest();
40 randomUUIDTest();
41 nameUUIDFromBytesTest();
42 stringTest();
43 versionTest();
44 variantTest();
45 timestampTest();
90 throw new Exception("byte UUID collision very unlikely");
91 list.add(u1);
92 }
93 }
94
95 private static void stringTest() throws Exception {
96 for (int i=0; i<100; i++) {
97 UUID u1 = UUID.randomUUID();
98 UUID u2 = UUID.fromString(u1.toString());
99 if (!u1.equals(u2))
100 throw new Exception("UUID -> string -> UUID failed");
101 }
102
103 testFromStringError("-0");
104 testFromStringError("x");
105 testFromStringError("----");
106 testFromStringError("-0-0-0-0");
107 testFromStringError("0-0-0-0-");
108 testFromStringError("0-0-0-0-0-");
109 testFromStringError("0-0-0-0-x");
110 testFromStringError("123456789-0-0-0-0");
111 testFromStringError("0-1abcd-0-0-0");
112 testFromStringError("0-0-1ef01-0-0");
113 testFromStringError("0-0-0-12345-0");
114 testFromStringError("0-0-0-0-f1234567890ab");
115 }
116
117 private static void testFromStringError(String str) {
118 try {
119 UUID test = UUID.fromString(str);
120 throw new RuntimeException("Should have thrown IAE");
121 } catch (IllegalArgumentException iae) {
122 // pass
123 }
124 }
125
126 private static void versionTest() throws Exception {
127 UUID test = UUID.randomUUID();
128 if (test.version() != 4)
129 throw new Exception("randomUUID not type 4");
130 Random byteSource = new Random();
131 byte[] someBytes = new byte[12];
132 byteSource.nextBytes(someBytes);
133 test = UUID.nameUUIDFromBytes(someBytes);
134 if (test.version() != 3)
|