|
17 | 17 |
|
18 | 18 | package com.fasterxml.uuid;
|
19 | 19 |
|
| 20 | +import java.nio.ByteBuffer; |
20 | 21 | import java.security.MessageDigest;
|
21 | 22 | import java.util.*;
|
22 | 23 |
|
|
29 | 30 | import com.fasterxml.uuid.impl.UUIDUtil;
|
30 | 31 | import com.fasterxml.uuid.impl.NameBasedGenerator;
|
31 | 32 | import com.fasterxml.uuid.impl.RandomBasedGenerator;
|
| 33 | +import com.fasterxml.uuid.impl.TimeBasedEpochGenerator; |
32 | 34 | import com.fasterxml.uuid.impl.TimeBasedReorderedGenerator;
|
33 | 35 | import com.fasterxml.uuid.impl.TimeBasedGenerator;
|
34 | 36 |
|
@@ -236,6 +238,60 @@ public void testGenerateTimeBasedUUIDWithEthernetAddress()
|
236 | 238 | // check that all UUIDs have the correct ethernet address in the UUID
|
237 | 239 | checkUUIDArrayForCorrectEthernetAddress(uuid_array, ethernet_address);
|
238 | 240 | }
|
| 241 | + |
| 242 | + public void testV7value() |
| 243 | + { |
| 244 | + // Test vector from spec |
| 245 | + UUID testValue = UUID.fromString("017F22E2-79B0-7CC3-98C4-DC0C0C07398F"); |
| 246 | + checkUUIDArrayForCorrectCreationTimeEpoch(new UUID[] { testValue }, 1645557742000L, 1645557742010L); |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * Test of generateTimeBasedEpochUUID() method, |
| 251 | + * of class com.fasterxml.uuid.UUIDGenerator. |
| 252 | + */ |
| 253 | + public void testGenerateTimeBasedEpochUUID() |
| 254 | + { |
| 255 | + // this test will attempt to check for reasonable behavior of the |
| 256 | + // generateTimeBasedUUID method |
| 257 | + |
| 258 | + // we need a instance to use |
| 259 | + TimeBasedEpochGenerator uuid_gen = Generators.timeBasedEpochGenerator(); |
| 260 | + |
| 261 | + // first check that given a number of calls to generateTimeBasedEpochUUID, |
| 262 | + // all returned UUIDs order after the last returned UUID |
| 263 | + // we'll check this by generating the UUIDs into one array and sorting |
| 264 | + // then in another and checking the order of the two match |
| 265 | + // change the number in the array statement if you want more or less |
| 266 | + // UUIDs to be generated and tested |
| 267 | + UUID uuid_array[] = new UUID[SIZE_OF_TEST_ARRAY]; |
| 268 | + |
| 269 | + // before we generate all the uuids, lets get the start time |
| 270 | + long start_time = System.currentTimeMillis(); |
| 271 | + |
| 272 | + // now create the array of uuids |
| 273 | + for (int i = 0; i < uuid_array.length; i++) { |
| 274 | + uuid_array[i] = uuid_gen.generate(); |
| 275 | + } |
| 276 | + |
| 277 | + // now capture the end time |
| 278 | + long end_time = System.currentTimeMillis(); |
| 279 | + |
| 280 | + // check that none of the UUIDs are null |
| 281 | + checkUUIDArrayForNonNullUUIDs(uuid_array); |
| 282 | + |
| 283 | + // check that all the uuids were correct variant and version (type-1) |
| 284 | + checkUUIDArrayForCorrectVariantAndVersion(uuid_array, UUIDType.TIME_BASED_EPOCH); |
| 285 | + |
| 286 | + // check that all the uuids were generated with correct order |
| 287 | +// checkUUIDArrayForCorrectOrdering(uuid_array); |
| 288 | + |
| 289 | + // check that all uuids were unique |
| 290 | + checkUUIDArrayForUniqueness(uuid_array); |
| 291 | + |
| 292 | + // check that all uuids have timestamps between the start and end time |
| 293 | + checkUUIDArrayForCorrectCreationTimeEpoch(uuid_array, start_time, end_time); |
| 294 | + } |
239 | 295 |
|
240 | 296 | /**
|
241 | 297 | * Test of generateNameBasedUUID(UUID, String)
|
@@ -409,7 +465,7 @@ public void testGenerateTimeBasedReorderedUUID()
|
409 | 465 | // we need a instance to use
|
410 | 466 | TimeBasedReorderedGenerator uuid_gen = Generators.timeBasedReorderedGenerator();
|
411 | 467 |
|
412 |
| - // first check that given a number of calls to generateTimeBasedUUID, |
| 468 | + // first check that given a number of calls to generateTimeBasedReorderedUUID, |
413 | 469 | // all returned UUIDs order after the last returned UUID
|
414 | 470 | // we'll check this by generating the UUIDs into one array and sorting
|
415 | 471 | // then in another and checking the order of the two match
|
@@ -458,7 +514,7 @@ public void testGenerateTimeBasedReorderedUUIDWithEthernetAddress()
|
458 | 514 | // we need a instance to use
|
459 | 515 | TimeBasedReorderedGenerator uuid_gen = Generators.timeBasedReorderedGenerator(ethernet_address);
|
460 | 516 |
|
461 |
| - // check that given a number of calls to generateTimeBasedUUID, |
| 517 | + // check that given a number of calls to generateTimeBasedReorderedUUID, |
462 | 518 | // all returned UUIDs order after the last returned UUID
|
463 | 519 | // we'll check this by generating the UUIDs into one array and sorting
|
464 | 520 | // then in another and checking the order of the two match
|
@@ -701,6 +757,30 @@ private void checkUUIDArrayForCorrectCreationTimeReorder(UUID[] uuidArray,
|
701 | 757 | }
|
702 | 758 | }
|
703 | 759 |
|
| 760 | + // Modified version for Variant 7 (Unix Epoch timestamps) |
| 761 | + private void checkUUIDArrayForCorrectCreationTimeEpoch(UUID[] uuidArray, |
| 762 | + long startTime, long endTime) |
| 763 | + { |
| 764 | + |
| 765 | + // 21-Feb-2020, tatu: Not sure why this would be checked, as timestamps come |
| 766 | + // from |
| 767 | + // System.currenTimeMillis()... |
| 768 | + assertTrue("Start time: " + startTime + " was after the end time: " + endTime, startTime <= endTime); |
| 769 | + |
| 770 | + // let's check that all uuids in the array have a timestamp which lands |
| 771 | + // between the start and end time |
| 772 | + for (int i = 0; i < uuidArray.length; i++) { |
| 773 | + byte[] temp_uuid = UUIDUtil.asByteArray(uuidArray[i]); |
| 774 | + ByteBuffer buff = ByteBuffer.wrap(temp_uuid); |
| 775 | + long uuid_time = buff.getLong() >>> 16; |
| 776 | + // now check that the times are correct |
| 777 | + assertTrue("Start time: " + startTime + " was not before UUID timestamp: " + uuid_time, |
| 778 | + startTime <= uuid_time); |
| 779 | + assertTrue("UUID timestamp: " + uuid_time + " was not before the end time: " + endTime, |
| 780 | + uuid_time <= endTime); |
| 781 | + } |
| 782 | + } |
| 783 | + |
704 | 784 | private void checkUUIDArrayForCorrectEthernetAddress(UUID[] uuidArray,
|
705 | 785 | EthernetAddress ethernetAddress)
|
706 | 786 | {
|
|
0 commit comments