001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
013: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
014: * License for the specific language governing permissions and limitations under
015: * the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.util.UUID;
021:
022: import org.apache.harmony.testframework.serialization.SerializationTest;
023:
024: import junit.framework.TestCase;
025:
026: public class UUIDTest extends TestCase {
027:
028: /**
029: * @see UUID#UUID(long, long)
030: */
031: public void test_ConstructurJJ() {
032: UUID uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
033: assertEquals(2, uuid.variant());
034: assertEquals(1, uuid.version());
035: assertEquals(0x1d07decf81d4faeL, uuid.timestamp());
036: assertEquals(130742845922168750L, uuid.timestamp());
037: assertEquals(0x2765, uuid.clockSequence());
038: assertEquals(0xA0C91E6BF6L, uuid.node());
039: }
040:
041: /**
042: * @see UUID#getLeastSignificantBits()
043: */
044: public void test_getLeastSignificantBits() {
045: UUID uuid = new UUID(0, 0);
046: assertEquals(0, uuid.getLeastSignificantBits());
047: uuid = new UUID(0, Long.MIN_VALUE);
048: assertEquals(Long.MIN_VALUE, uuid.getLeastSignificantBits());
049: uuid = new UUID(0, Long.MAX_VALUE);
050: assertEquals(Long.MAX_VALUE, uuid.getLeastSignificantBits());
051: }
052:
053: /**
054: * @see UUID#getMostSignificantBits()
055: */
056: public void test_getMostSignificantBits() {
057: UUID uuid = new UUID(0, 0);
058: assertEquals(0, uuid.getMostSignificantBits());
059: uuid = new UUID(Long.MIN_VALUE, 0);
060: assertEquals(Long.MIN_VALUE, uuid.getMostSignificantBits());
061: uuid = new UUID(Long.MAX_VALUE, 0);
062: assertEquals(Long.MAX_VALUE, uuid.getMostSignificantBits());
063: }
064:
065: /**
066: * @see UUID#version()
067: */
068: public void test_version() {
069: UUID uuid = new UUID(0, 0);
070: assertEquals(0, uuid.version());
071: uuid = new UUID(0x0000000000001000L, 0);
072: assertEquals(1, uuid.version());
073: uuid = new UUID(0x0000000000002000L, 0);
074: assertEquals(2, uuid.version());
075: uuid = new UUID(0x0000000000003000L, 0);
076: assertEquals(3, uuid.version());
077: uuid = new UUID(0x0000000000004000L, 0);
078: assertEquals(4, uuid.version());
079: uuid = new UUID(0x0000000000005000L, 0);
080: assertEquals(5, uuid.version());
081: }
082:
083: /**
084: * @see UUID#variant()
085: */
086: public void test_variant() {
087: UUID uuid = new UUID(0, 0x0000000000000000L);
088: assertEquals(0, uuid.variant());
089: uuid = new UUID(0, 0x7000000000000000L);
090: assertEquals(0, uuid.variant());
091: uuid = new UUID(0, 0x3000000000000000L);
092: assertEquals(0, uuid.variant());
093: uuid = new UUID(0, 0x1000000000000000L);
094: assertEquals(0, uuid.variant());
095:
096: uuid = new UUID(0, 0x8000000000000000L);
097: assertEquals(2, uuid.variant());
098: uuid = new UUID(0, 0xB000000000000000L);
099: assertEquals(2, uuid.variant());
100: uuid = new UUID(0, 0xA000000000000000L);
101: assertEquals(2, uuid.variant());
102: uuid = new UUID(0, 0x9000000000000000L);
103: assertEquals(2, uuid.variant());
104:
105: uuid = new UUID(0, 0xC000000000000000L);
106: assertEquals(6, uuid.variant());
107: uuid = new UUID(0, 0xD000000000000000L);
108: assertEquals(6, uuid.variant());
109:
110: uuid = new UUID(0, 0xE000000000000000L);
111: assertEquals(7, uuid.variant());
112: uuid = new UUID(0, 0xF000000000000000L);
113: assertEquals(7, uuid.variant());
114: }
115:
116: /**
117: * @see UUID#timestamp()
118: */
119: public void test_timestamp() {
120: UUID uuid = new UUID(0x0000000000001000L, 0x8000000000000000L);
121: assertEquals(0x0, uuid.timestamp());
122:
123: uuid = new UUID(0x7777777755551333L, 0x8000000000000000L);
124: assertEquals(0x333555577777777L, uuid.timestamp());
125:
126: uuid = new UUID(0x0000000000000000L, 0x8000000000000000L);
127: try {
128: uuid.timestamp();
129: fail("No UnsupportedOperationException");
130: } catch (UnsupportedOperationException e) {
131: }
132:
133: uuid = new UUID(0x0000000000002000L, 0x8000000000000000L);
134: try {
135: uuid.timestamp();
136: fail("No UnsupportedOperationException");
137: } catch (UnsupportedOperationException e) {
138: }
139: }
140:
141: /**
142: * @see UUID#clockSequence()
143: */
144: public void test_clockSequence() {
145: UUID uuid = new UUID(0x0000000000001000L, 0x8000000000000000L);
146: assertEquals(0x0, uuid.clockSequence());
147:
148: uuid = new UUID(0x0000000000001000L, 0x8FFF000000000000L);
149: assertEquals(0x0FFF, uuid.clockSequence());
150:
151: uuid = new UUID(0x0000000000001000L, 0xBFFF000000000000L);
152: assertEquals(0x3FFF, uuid.clockSequence());
153:
154: uuid = new UUID(0x0000000000000000L, 0x8000000000000000L);
155: try {
156: uuid.clockSequence();
157: fail("No UnsupportedOperationException");
158: } catch (UnsupportedOperationException e) {
159: }
160:
161: uuid = new UUID(0x0000000000002000L, 0x8000000000000000L);
162: try {
163: uuid.clockSequence();
164: fail("No UnsupportedOperationException");
165: } catch (UnsupportedOperationException e) {
166: }
167: }
168:
169: /**
170: * @see UUID#node()
171: */
172: public void test_node() {
173: UUID uuid = new UUID(0x0000000000001000L, 0x8000000000000000L);
174: assertEquals(0x0, uuid.node());
175:
176: uuid = new UUID(0x0000000000001000L, 0x8000FFFFFFFFFFFFL);
177: assertEquals(0xFFFFFFFFFFFFL, uuid.node());
178:
179: uuid = new UUID(0x0000000000000000L, 0x8000000000000000L);
180: try {
181: uuid.node();
182: fail("No UnsupportedOperationException");
183: } catch (UnsupportedOperationException e) {
184: }
185:
186: uuid = new UUID(0x0000000000002000L, 0x8000000000000000L);
187: try {
188: uuid.node();
189: fail("No UnsupportedOperationException");
190: } catch (UnsupportedOperationException e) {
191: }
192: }
193:
194: /**
195: * @see UUID#compareTo(UUID)
196: */
197: public void test_compareTo() {
198: UUID uuid1 = new UUID(0, 0);
199: assertEquals(0, uuid1.compareTo(uuid1));
200: UUID uuid2 = new UUID(1, 0);
201: assertEquals(-1, uuid1.compareTo(uuid2));
202: assertEquals(1, uuid2.compareTo(uuid1));
203:
204: uuid2 = new UUID(0, 1);
205: assertEquals(-1, uuid1.compareTo(uuid2));
206: assertEquals(1, uuid2.compareTo(uuid1));
207: }
208:
209: /**
210: * @see UUID#hashCode()
211: */
212: public void test_hashCode() {
213: UUID uuid = new UUID(0, 0);
214: assertEquals(0, uuid.hashCode());
215: uuid = new UUID(123, 123);
216: UUID uuidClone = new UUID(123, 123);
217: assertEquals(uuid.hashCode(), uuidClone.hashCode());
218: }
219:
220: /**
221: * @see UUID#equals(Object)
222: */
223: public void test_equalsObject() {
224: UUID uuid1 = new UUID(0, 0);
225: assertEquals(uuid1, uuid1);
226: assertFalse(uuid1.equals(null));
227: assertFalse(uuid1.equals("NOT A UUID"));
228: UUID uuid2 = new UUID(0, 0);
229: assertEquals(uuid1, uuid2);
230: assertEquals(uuid2, uuid1);
231:
232: uuid1 = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
233: uuid2 = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
234: assertEquals(uuid1, uuid2);
235: assertEquals(uuid2, uuid1);
236:
237: uuid2 = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf7L);
238: assertFalse(uuid1.equals(uuid2));
239: assertFalse(uuid2.equals(uuid1));
240: }
241:
242: /**
243: * @see UUID#toString()
244: */
245: public void test_toString() {
246: UUID uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L);
247: String actual = uuid.toString();
248: assertEquals("f81d4fae-7dec-11d0-a765-00a0c91e6bf6", actual);
249:
250: uuid = new UUID(0x0000000000001000L, 0x8000000000000000L);
251: actual = uuid.toString();
252: assertEquals("00000000-0000-1000-8000-000000000000", actual);
253: }
254:
255: /**
256: * @tests serialization/deserialization.
257: */
258: public void testSerializationSelf() throws Exception {
259: SerializationTest.verifySelf(new UUID(0xf81d4fae7dec11d0L,
260: 0xa76500a0c91e6bf6L));
261: }
262:
263: /**
264: * @tests serialization/deserialization compatibility with RI.
265: */
266: public void testSerializationCompatibility() throws Exception {
267: SerializationTest.verifyGolden(this , new UUID(
268: 0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L));
269: }
270:
271: /**
272: * @see UUID#randomUUID()
273: */
274: public void test_randomUUID() {
275: UUID uuid = UUID.randomUUID();
276: assertEquals(2, uuid.variant());
277: assertEquals(4, uuid.version());
278: }
279:
280: /**
281: * @see UUID#nameUUIDFromBytes(byte[])
282: */
283: public void test_nameUUIDFromBytes() throws Exception {
284: byte[] name = { (byte) 0x6b, (byte) 0xa7, (byte) 0xb8,
285: (byte) 0x11, (byte) 0x9d, (byte) 0xad, (byte) 0x11,
286: (byte) 0xd1, (byte) 0x80, (byte) 0xb4, (byte) 0x00,
287: (byte) 0xc0, (byte) 0x4f, (byte) 0xd4, (byte) 0x30,
288: (byte) 0xc8 };
289:
290: UUID uuid = UUID.nameUUIDFromBytes(name);
291:
292: assertEquals(2, uuid.variant());
293: assertEquals(3, uuid.version());
294:
295: assertEquals(0xaff565bc2f771745L, uuid
296: .getLeastSignificantBits());
297: assertEquals(0x14cdb9b4de013faaL, uuid.getMostSignificantBits());
298:
299: uuid = UUID.nameUUIDFromBytes(new byte[0]);
300: assertEquals(2, uuid.variant());
301: assertEquals(3, uuid.version());
302:
303: assertEquals(0xa9800998ecf8427eL, uuid
304: .getLeastSignificantBits());
305: assertEquals(0xd41d8cd98f003204L, uuid.getMostSignificantBits());
306:
307: try {
308: UUID.nameUUIDFromBytes(null);
309: fail("No NPE");
310: } catch (NullPointerException e) {
311: }
312: }
313:
314: /**
315: * @see UUID#fromString(String)
316: */
317: public void test_fromString() {
318: UUID actual = UUID
319: .fromString("f81d4fae-7dec-11d0-a765-00a0c91e6bf6");
320: UUID expected = new UUID(0xf81d4fae7dec11d0L,
321: 0xa76500a0c91e6bf6L);
322: assertEquals(expected, actual);
323:
324: assertEquals(2, actual.variant());
325: assertEquals(1, actual.version());
326: assertEquals(130742845922168750L, actual.timestamp());
327: assertEquals(10085, actual.clockSequence());
328: assertEquals(690568981494L, actual.node());
329:
330: actual = UUID
331: .fromString("00000000-0000-1000-8000-000000000000");
332: expected = new UUID(0x0000000000001000L, 0x8000000000000000L);
333: assertEquals(expected, actual);
334:
335: assertEquals(2, actual.variant());
336: assertEquals(1, actual.version());
337: assertEquals(0L, actual.timestamp());
338: assertEquals(0, actual.clockSequence());
339: assertEquals(0L, actual.node());
340:
341: try {
342: UUID.fromString(null);
343: fail("No NPE");
344: } catch (NullPointerException e) {
345: }
346:
347: try {
348: UUID.fromString("");
349: fail("No IAE");
350: } catch (IllegalArgumentException e) {
351: }
352:
353: try {
354: UUID.fromString("f81d4fae_7dec-11d0-a765-00a0c91e6bf6");
355: fail("No IAE");
356: } catch (IllegalArgumentException e) {
357: }
358:
359: try {
360: UUID.fromString("f81d4fae-7dec_11d0-a765-00a0c91e6bf6");
361: fail("No IAE");
362: } catch (IllegalArgumentException e) {
363: }
364:
365: try {
366: UUID.fromString("f81d4fae-7dec-11d0_a765-00a0c91e6bf6");
367: fail("No IAE");
368: } catch (IllegalArgumentException e) {
369: }
370:
371: try {
372: UUID.fromString("f81d4fae-7dec-11d0-a765_00a0c91e6bf6");
373: fail("No IAE");
374: } catch (IllegalArgumentException e) {
375: }
376: }
377:
378: /**
379: * @tests java.util.UUID#fromString(String)
380: */
381: public void test_fromString_LString_Exception() {
382:
383: UUID uuid = UUID.fromString("0-0-0-0-0");
384:
385: try {
386: uuid = UUID.fromString("0-0-0-0-");
387: fail("should throw IllegalArgumentException");
388: } catch (IllegalArgumentException e) {
389: // expected
390: }
391:
392: try {
393: uuid = UUID.fromString("-0-0-0-0-0");
394: fail("should throw IllegalArgumentException");
395: } catch (IllegalArgumentException e) {
396: // expected
397: }
398:
399: try {
400: uuid = UUID.fromString("-0-0-0-0");
401: fail("should throw IllegalArgumentException");
402: } catch (IllegalArgumentException e) {
403: // expected
404: }
405:
406: try {
407: uuid = UUID.fromString("-0-0-0-");
408: fail("should throw IllegalArgumentException");
409: } catch (IllegalArgumentException e) {
410: // expected
411: }
412:
413: try {
414: uuid = UUID.fromString("0--0-0-0");
415: fail("should throw IllegalArgumentException");
416: } catch (IllegalArgumentException e) {
417: // expected
418: }
419:
420: try {
421: uuid = UUID.fromString("0-0-0-0-");
422: fail("should throw IllegalArgumentException");
423: } catch (IllegalArgumentException e) {
424: // expected
425: }
426:
427: try {
428: uuid = UUID.fromString("-1-0-0-0-0");
429: fail("should throw IllegalArgumentException");
430: } catch (IllegalArgumentException e) {
431: // expected
432: }
433:
434: uuid = UUID.fromString("123456789-0-0-0-0");
435: assertEquals(0x2345678900000000L, uuid.getMostSignificantBits());
436: assertEquals(0x0L, uuid.getLeastSignificantBits());
437:
438: uuid = UUID.fromString("111123456789-0-0-0-0");
439: assertEquals(0x2345678900000000L, uuid.getMostSignificantBits());
440: assertEquals(0x0L, uuid.getLeastSignificantBits());
441:
442: uuid = UUID.fromString("7fffffffffffffff-0-0-0-0");
443: assertEquals(0xffffffff00000000L, uuid.getMostSignificantBits());
444: assertEquals(0x0L, uuid.getLeastSignificantBits());
445:
446: try {
447: uuid = UUID.fromString("8000000000000000-0-0-0-0");
448: fail("should throw NumberFormatException");
449: } catch (NumberFormatException e) {
450: // expected
451: }
452:
453: uuid = UUID
454: .fromString("7fffffffffffffff-7fffffffffffffff-7fffffffffffffff-0-0");
455: assertEquals(0xffffffffffffffffL, uuid.getMostSignificantBits());
456: assertEquals(0x0L, uuid.getLeastSignificantBits());
457:
458: uuid = UUID
459: .fromString("0-0-0-7fffffffffffffff-7fffffffffffffff");
460: assertEquals(0x0L, uuid.getMostSignificantBits());
461: assertEquals(0xffffffffffffffffL, uuid
462: .getLeastSignificantBits());
463:
464: try {
465: uuid = UUID.fromString("0-0-0-8000000000000000-0");
466: fail("should throw NumberFormatException");
467: } catch (NumberFormatException e) {
468: // expected
469: }
470:
471: try {
472: uuid = UUID.fromString("0-0-0-0-8000000000000000");
473: fail("should throw NumberFormatException");
474: } catch (NumberFormatException e) {
475: // expected
476: }
477: }
478: }
|