1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 * License for the specific language governing permissions and limitations under 15 * the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.util; 19 20import java.util.UUID; 21 22import org.apache.harmony.testframework.serialization.SerializationTest; 23 24import junit.framework.TestCase; 25 26public class UUIDTest extends TestCase { 27 28 /** 29 * @see UUID#UUID(long, long) 30 */ 31 public void test_ConstructorJJ() { 32 UUID uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L); 33 assertEquals(2, uuid.variant()); 34 assertEquals(1, uuid.version()); 35 assertEquals(0x1d07decf81d4faeL, uuid.timestamp()); 36 assertEquals(130742845922168750L, uuid.timestamp()); 37 assertEquals(0x2765, uuid.clockSequence()); 38 assertEquals(0xA0C91E6BF6L, uuid.node()); 39 } 40 41 /** 42 * @see UUID#getLeastSignificantBits() 43 */ 44 public void test_getLeastSignificantBits() { 45 UUID uuid = new UUID(0, 0); 46 assertEquals(0, uuid.getLeastSignificantBits()); 47 uuid = new UUID(0, Long.MIN_VALUE); 48 assertEquals(Long.MIN_VALUE, uuid.getLeastSignificantBits()); 49 uuid = new UUID(0, Long.MAX_VALUE); 50 assertEquals(Long.MAX_VALUE, uuid.getLeastSignificantBits()); 51 } 52 53 /** 54 * @see UUID#getMostSignificantBits() 55 */ 56 public void test_getMostSignificantBits() { 57 UUID uuid = new UUID(0, 0); 58 assertEquals(0, uuid.getMostSignificantBits()); 59 uuid = new UUID(Long.MIN_VALUE, 0); 60 assertEquals(Long.MIN_VALUE, uuid.getMostSignificantBits()); 61 uuid = new UUID(Long.MAX_VALUE, 0); 62 assertEquals(Long.MAX_VALUE, uuid.getMostSignificantBits()); 63 } 64 65 /** 66 * @see UUID#version() 67 */ 68 public void test_version() { 69 UUID uuid = new UUID(0, 0); 70 assertEquals(0, uuid.version()); 71 uuid = new UUID(0x0000000000001000L, 0); 72 assertEquals(1, uuid.version()); 73 uuid = new UUID(0x0000000000002000L, 0); 74 assertEquals(2, uuid.version()); 75 uuid = new UUID(0x0000000000003000L, 0); 76 assertEquals(3, uuid.version()); 77 uuid = new UUID(0x0000000000004000L, 0); 78 assertEquals(4, uuid.version()); 79 uuid = new UUID(0x0000000000005000L, 0); 80 assertEquals(5, uuid.version()); 81 } 82 83 /** 84 * @see UUID#variant() 85 */ 86 public void test_variant() { 87 UUID uuid = new UUID(0, 0x0000000000000000L); 88 assertEquals(0, uuid.variant()); 89 uuid = new UUID(0, 0x7000000000000000L); 90 assertEquals(0, uuid.variant()); 91 uuid = new UUID(0, 0x3000000000000000L); 92 assertEquals(0, uuid.variant()); 93 uuid = new UUID(0, 0x1000000000000000L); 94 assertEquals(0, uuid.variant()); 95 96 uuid = new UUID(0, 0x8000000000000000L); 97 assertEquals(2, uuid.variant()); 98 uuid = new UUID(0, 0xB000000000000000L); 99 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 uuid = new UUID(0x0000000000002000L, 0x8000000000000000L); 133 try { 134 uuid.timestamp(); 135 fail("No UnsupportedOperationException"); 136 } catch (UnsupportedOperationException e) {} 137 } 138 139 /** 140 * @see UUID#clockSequence() 141 */ 142 public void test_clockSequence() { 143 UUID uuid = new UUID(0x0000000000001000L, 0x8000000000000000L); 144 assertEquals(0x0, uuid.clockSequence()); 145 146 uuid = new UUID(0x0000000000001000L, 0x8FFF000000000000L); 147 assertEquals(0x0FFF, uuid.clockSequence()); 148 149 uuid = new UUID(0x0000000000001000L, 0xBFFF000000000000L); 150 assertEquals(0x3FFF, uuid.clockSequence()); 151 152 uuid = new UUID(0x0000000000000000L, 0x8000000000000000L); 153 try { 154 uuid.clockSequence(); 155 fail("No UnsupportedOperationException"); 156 } catch (UnsupportedOperationException e) {} 157 158 uuid = new UUID(0x0000000000002000L, 0x8000000000000000L); 159 try { 160 uuid.clockSequence(); 161 fail("No UnsupportedOperationException"); 162 } catch (UnsupportedOperationException e) {} 163 } 164 165 /** 166 * @see UUID#node() 167 */ 168 public void test_node() { 169 UUID uuid = new UUID(0x0000000000001000L, 0x8000000000000000L); 170 assertEquals(0x0, uuid.node()); 171 172 uuid = new UUID(0x0000000000001000L, 0x8000FFFFFFFFFFFFL); 173 assertEquals(0xFFFFFFFFFFFFL, uuid.node()); 174 175 uuid = new UUID(0x0000000000000000L, 0x8000000000000000L); 176 try { 177 uuid.node(); 178 fail("No UnsupportedOperationException"); 179 } catch (UnsupportedOperationException e) {} 180 181 uuid = new UUID(0x0000000000002000L, 0x8000000000000000L); 182 try { 183 uuid.node(); 184 fail("No UnsupportedOperationException"); 185 } catch (UnsupportedOperationException e) {} 186 } 187 188 /** 189 * @see UUID#compareTo(UUID) 190 */ 191 public void test_compareTo() { 192 UUID uuid1 = new UUID(0, 0); 193 assertEquals(0, uuid1.compareTo(uuid1)); 194 UUID uuid2 = new UUID(1, 0); 195 assertEquals(-1, uuid1.compareTo(uuid2)); 196 assertEquals(1, uuid2.compareTo(uuid1)); 197 198 uuid2 = new UUID(0, 1); 199 assertEquals(-1, uuid1.compareTo(uuid2)); 200 assertEquals(1, uuid2.compareTo(uuid1)); 201 } 202 203 /** 204 * @see UUID#hashCode() 205 */ 206 public void test_hashCode() { 207 UUID uuid = new UUID(0, 0); 208 assertEquals(0, uuid.hashCode()); 209 uuid = new UUID(123, 123); 210 UUID uuidClone = new UUID(123, 123); 211 assertEquals(uuid.hashCode(), uuidClone.hashCode()); 212 } 213 214 /** 215 * @see UUID#equals(Object) 216 */ 217 public void test_equalsObject() { 218 UUID uuid1 = new UUID(0, 0); 219 assertEquals(uuid1, uuid1); 220 assertFalse(uuid1.equals(null)); 221 assertFalse(uuid1.equals("NOT A UUID")); 222 UUID uuid2 = new UUID(0, 0); 223 assertEquals(uuid1, uuid2); 224 assertEquals(uuid2, uuid1); 225 226 uuid1 = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L); 227 uuid2 = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L); 228 assertEquals(uuid1, uuid2); 229 assertEquals(uuid2, uuid1); 230 231 uuid2 = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf7L); 232 assertFalse(uuid1.equals(uuid2)); 233 assertFalse(uuid2.equals(uuid1)); 234 } 235 236 /** 237 * @see UUID#toString() 238 */ 239 public void test_toString() { 240 UUID uuid = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L); 241 String actual = uuid.toString(); 242 assertEquals("f81d4fae-7dec-11d0-a765-00a0c91e6bf6", actual); 243 244 uuid = new UUID(0x0000000000001000L, 0x8000000000000000L); 245 actual = uuid.toString(); 246 assertEquals("00000000-0000-1000-8000-000000000000", actual); 247 } 248 249 /** 250 * @tests serialization/deserialization. 251 */ 252 public void testSerializationSelf() throws Exception { 253 SerializationTest.verifySelf(new UUID(0xf81d4fae7dec11d0L, 254 0xa76500a0c91e6bf6L)); 255 } 256 257 /** 258 * @tests serialization/deserialization compatibility with RI. 259 */ 260 public void testSerializationCompatibility() throws Exception { 261 SerializationTest.verifyGolden(this, new UUID(0xf81d4fae7dec11d0L, 262 0xa76500a0c91e6bf6L)); 263 } 264 265 /** 266 * @see UUID#randomUUID() 267 */ 268 public void test_randomUUID() { 269 UUID uuid = UUID.randomUUID(); 270 assertEquals(2, uuid.variant()); 271 assertEquals(4, uuid.version()); 272 } 273 274 /** 275 * @see UUID#nameUUIDFromBytes(byte[]) 276 */ 277 public void test_nameUUIDFromBytes() throws Exception { 278 byte[] name = { (byte) 0x6b, (byte) 0xa7, (byte) 0xb8, (byte) 0x11, 279 (byte) 0x9d, (byte) 0xad, (byte) 0x11, (byte) 0xd1, 280 (byte) 0x80, (byte) 0xb4, (byte) 0x00, (byte) 0xc0, 281 (byte) 0x4f, (byte) 0xd4, (byte) 0x30, (byte) 0xc8 }; 282 283 UUID uuid = UUID.nameUUIDFromBytes(name); 284 285 assertEquals(2, uuid.variant()); 286 assertEquals(3, uuid.version()); 287 288 assertEquals(0xaff565bc2f771745L, uuid.getLeastSignificantBits()); 289 assertEquals(0x14cdb9b4de013faaL, uuid.getMostSignificantBits()); 290 291 uuid = UUID.nameUUIDFromBytes(new byte[0]); 292 assertEquals(2, uuid.variant()); 293 assertEquals(3, uuid.version()); 294 295 assertEquals(0xa9800998ecf8427eL, uuid.getLeastSignificantBits()); 296 assertEquals(0xd41d8cd98f003204L, uuid.getMostSignificantBits()); 297 298 try { 299 UUID.nameUUIDFromBytes(null); 300 fail("No NPE"); 301 } catch (NullPointerException e) {} 302 } 303 304 /** 305 * @see UUID#fromString(String) 306 */ 307 public void test_fromString() { 308 UUID actual = UUID.fromString("f81d4fae-7dec-11d0-a765-00a0c91e6bf6"); 309 UUID expected = new UUID(0xf81d4fae7dec11d0L, 0xa76500a0c91e6bf6L); 310 assertEquals(expected, actual); 311 312 assertEquals(2, actual.variant()); 313 assertEquals(1, actual.version()); 314 assertEquals(130742845922168750L, actual.timestamp()); 315 assertEquals(10085, actual.clockSequence()); 316 assertEquals(690568981494L, actual.node()); 317 318 actual = UUID.fromString("00000000-0000-1000-8000-000000000000"); 319 expected = new UUID(0x0000000000001000L, 0x8000000000000000L); 320 assertEquals(expected, actual); 321 322 assertEquals(2, actual.variant()); 323 assertEquals(1, actual.version()); 324 assertEquals(0L, actual.timestamp()); 325 assertEquals(0, actual.clockSequence()); 326 assertEquals(0L, actual.node()); 327 328 try { 329 UUID.fromString(null); 330 fail("No NPE"); 331 } catch (NullPointerException e) {} 332 333 try { 334 UUID.fromString(""); 335 fail("No IAE"); 336 } catch (IllegalArgumentException e) {} 337 338 try { 339 UUID.fromString("f81d4fae_7dec-11d0-a765-00a0c91e6bf6"); 340 fail("No IAE"); 341 } catch (IllegalArgumentException e) {} 342 343 try { 344 UUID.fromString("f81d4fae-7dec_11d0-a765-00a0c91e6bf6"); 345 fail("No IAE"); 346 } catch (IllegalArgumentException e) {} 347 348 try { 349 UUID.fromString("f81d4fae-7dec-11d0_a765-00a0c91e6bf6"); 350 fail("No IAE"); 351 } catch (IllegalArgumentException e) {} 352 353 try { 354 UUID.fromString("f81d4fae-7dec-11d0-a765_00a0c91e6bf6"); 355 fail("No IAE"); 356 } catch (IllegalArgumentException e) {} 357 } 358 359 /** 360 * @tests java.util.UUID#fromString(String) 361 */ 362 public void test_fromString_LString_Exception() { 363 364 UUID uuid = UUID.fromString("0-0-0-0-0"); 365 366 try { 367 uuid = UUID.fromString("0-0-0-0-"); 368 fail("should throw IllegalArgumentException"); 369 } catch (IllegalArgumentException e) { 370 // expected 371 } 372 373 try { 374 uuid = UUID.fromString("-0-0-0-0-0"); 375 fail("should throw IllegalArgumentException"); 376 } catch (IllegalArgumentException e) { 377 // expected 378 } 379 380 try { 381 uuid = UUID.fromString("-0-0-0-0"); 382 fail("should throw IllegalArgumentException"); 383 } catch (IllegalArgumentException e) { 384 // expected 385 } 386 387 try { 388 uuid = UUID.fromString("-0-0-0-"); 389 fail("should throw IllegalArgumentException"); 390 } catch (IllegalArgumentException e) { 391 // expected 392 } 393 394 try { 395 uuid = UUID.fromString("0--0-0-0"); 396 fail("should throw IllegalArgumentException"); 397 } catch (IllegalArgumentException e) { 398 // expected 399 } 400 401 try { 402 uuid = UUID.fromString("0-0-0-0-"); 403 fail("should throw IllegalArgumentException"); 404 } catch (IllegalArgumentException e) { 405 // expected 406 } 407 408 try { 409 uuid = UUID.fromString("-1-0-0-0-0"); 410 fail("should throw IllegalArgumentException"); 411 } catch (IllegalArgumentException e) { 412 // expected 413 } 414 415 uuid = UUID.fromString("123456789-0-0-0-0"); 416 assertEquals(0x2345678900000000L, uuid.getMostSignificantBits()); 417 assertEquals(0x0L, uuid.getLeastSignificantBits()); 418 419 uuid = UUID.fromString("111123456789-0-0-0-0"); 420 assertEquals(0x2345678900000000L, uuid.getMostSignificantBits()); 421 assertEquals(0x0L, uuid.getLeastSignificantBits()); 422 423 uuid = UUID.fromString("7fffffffffffffff-0-0-0-0"); 424 assertEquals(0xffffffff00000000L, uuid.getMostSignificantBits()); 425 assertEquals(0x0L, uuid.getLeastSignificantBits()); 426 427 try { 428 uuid = UUID.fromString("8000000000000000-0-0-0-0"); 429 fail("should throw NumberFormatException"); 430 } catch (NumberFormatException e) { 431 // expected 432 } 433 434 uuid = UUID 435 .fromString("7fffffffffffffff-7fffffffffffffff-7fffffffffffffff-0-0"); 436 assertEquals(0xffffffffffffffffL, uuid.getMostSignificantBits()); 437 assertEquals(0x0L, uuid.getLeastSignificantBits()); 438 439 uuid = UUID.fromString("0-0-0-7fffffffffffffff-7fffffffffffffff"); 440 assertEquals(0x0L, uuid.getMostSignificantBits()); 441 assertEquals(0xffffffffffffffffL, uuid.getLeastSignificantBits()); 442 443 try { 444 uuid = UUID.fromString("0-0-0-8000000000000000-0"); 445 fail("should throw NumberFormatException"); 446 } catch (NumberFormatException e) { 447 // expected 448 } 449 450 try { 451 uuid = UUID.fromString("0-0-0-0-8000000000000000"); 452 fail("should throw NumberFormatException"); 453 } catch (NumberFormatException e) { 454 // expected 455 } 456 } 457} 458