@@ -304,6 +304,58 @@ public void testGenerateTimeBasedEpochUUID() throws Exception
304
304
checkUUIDArrayForCorrectCreationTimeEpoch (uuid_array , start_time , end_time );
305
305
}
306
306
307
+ /**
308
+ * Test of generateTimeBasedEpochUUID() method with UUIDClock instance,
309
+ * of class com.fasterxml.uuid.UUIDGenerator.
310
+ */
311
+ public void testGenerateTimeBasedEpochUUIDWithUUIDClock () throws Exception
312
+ {
313
+ // this test will attempt to check for reasonable behavior of the
314
+ // generateTimeBasedUUID method
315
+
316
+ Random entropy = new Random (0x666 );
317
+
318
+ // we need a instance to use
319
+ TimeBasedEpochGenerator uuid_gen = Generators .timeBasedEpochGenerator (null , UUIDClock .systemTimeClock ());
320
+ assertEquals (uuid_gen .getType (), UUIDType .TIME_BASED_EPOCH );
321
+
322
+ // first check that given a number of calls to generateTimeBasedEpochUUID,
323
+ // all returned UUIDs order after the last returned UUID
324
+ // we'll check this by generating the UUIDs into one array and sorting
325
+ // then in another and checking the order of the two match
326
+ // change the number in the array statement if you want more or less
327
+ // UUIDs to be generated and tested
328
+ UUID uuid_array [] = new UUID [SIZE_OF_TEST_ARRAY ];
329
+
330
+ // before we generate all the uuids, lets get the start time
331
+ long start_time = System .currentTimeMillis ();
332
+ Thread .sleep (2 ); // Clean start time
333
+
334
+ // now create the array of uuids
335
+ for (int i = 0 ; i < uuid_array .length ; i ++) {
336
+ uuid_array [i ] = uuid_gen .generate ();
337
+ }
338
+
339
+ // now capture the end time
340
+ long end_time = System .currentTimeMillis ();
341
+ Thread .sleep (2 ); // Clean end time
342
+
343
+ // check that none of the UUIDs are null
344
+ checkUUIDArrayForNonNullUUIDs (uuid_array );
345
+
346
+ // check that all the uuids were correct variant and version (type-1)
347
+ checkUUIDArrayForCorrectVariantAndVersion (uuid_array , UUIDType .TIME_BASED_EPOCH );
348
+
349
+ // check that all the uuids were generated with correct order
350
+ checkUUIDArrayForCorrectOrdering (uuid_array );
351
+
352
+ // check that all uuids were unique
353
+ checkUUIDArrayForUniqueness (uuid_array );
354
+
355
+ // check that all uuids have timestamps between the start and end time
356
+ checkUUIDArrayForCorrectCreationTimeEpoch (uuid_array , start_time , end_time );
357
+ }
358
+
307
359
/**
308
360
* Test of generateTimeBasedEpochRandomUUID() method,
309
361
* of class com.fasterxml.uuid.UUIDGenerator.
@@ -356,6 +408,58 @@ public void testGenerateTimeBasedEpochRandomUUID() throws Exception
356
408
checkUUIDArrayForCorrectCreationTimeEpoch (uuid_array , start_time , end_time );
357
409
}
358
410
411
+ /**
412
+ * Test of generateTimeBasedEpochRandomUUID() method with UUIDClock instance,
413
+ * of class com.fasterxml.uuid.UUIDGenerator.
414
+ */
415
+ public void testGenerateTimeBasedEpochRandomUUIDWithUUIDClock () throws Exception
416
+ {
417
+ // this test will attempt to check for reasonable behavior of the
418
+ // generateTimeBasedRandomUUID method
419
+
420
+ Random entropy = new Random (0x666 );
421
+
422
+ // we need a instance to use
423
+ TimeBasedEpochRandomGenerator uuid_gen = Generators .timeBasedEpochRandomGenerator (null , UUIDClock .systemTimeClock ());
424
+
425
+ assertEquals (uuid_gen .getType (), UUIDType .TIME_BASED_EPOCH );
426
+ // first check that given a number of calls to generateTimeBasedEpochUUID,
427
+ // all returned UUIDs order after the last returned UUID
428
+ // we'll check this by generating the UUIDs into one array and sorting
429
+ // then in another and checking the order of the two match
430
+ // change the number in the array statement if you want more or less
431
+ // UUIDs to be generated and tested
432
+ UUID uuid_array [] = new UUID [SIZE_OF_TEST_ARRAY ];
433
+
434
+ // before we generate all the uuids, lets get the start time
435
+ long start_time = System .currentTimeMillis ();
436
+ Thread .sleep (2 ); // Clean start time
437
+
438
+ // now create the array of uuids
439
+ for (int i = 0 ; i < uuid_array .length ; i ++) {
440
+ uuid_array [i ] = uuid_gen .generate ();
441
+ }
442
+
443
+ // now capture the end time
444
+ long end_time = System .currentTimeMillis ();
445
+ Thread .sleep (2 ); // Clean end time
446
+
447
+ // check that none of the UUIDs are null
448
+ checkUUIDArrayForNonNullUUIDs (uuid_array );
449
+
450
+ // check that all the uuids were correct variant and version (type-1)
451
+ checkUUIDArrayForCorrectVariantAndVersion (uuid_array , UUIDType .TIME_BASED_EPOCH );
452
+
453
+ // check that all uuids were unique
454
+ // NOTE: technically, this test 'could' fail, but statistically
455
+ // speaking it should be extremely unlikely unless the implementation
456
+ // of (Secure)Random is bad
457
+ checkUUIDArrayForUniqueness (uuid_array );
458
+
459
+ // check that all uuids have timestamps between the start and end time
460
+ checkUUIDArrayForCorrectCreationTimeEpoch (uuid_array , start_time , end_time );
461
+ }
462
+
359
463
// [#70]: allow use of custom UUIDClock
360
464
public void testGenerateTimeBasedEpochUUIDWithFixedClock () throws Exception
361
465
{
@@ -550,6 +654,82 @@ public void testGenerateNameBasedUUIDNameSpaceNameAndMessageDigest()
550
654
Arrays .equals (uuid_array , uuid_array2 ));
551
655
}
552
656
657
+ /**
658
+ * Test of generateNameBasedUUID()
659
+ * method, of class com.fasterxml.uuid.UUIDGenerator.
660
+ */
661
+ public void testGenerateNameBasedUUIDWithDefaults ()
662
+ {
663
+ // this test will attempt to check for reasonable behavior of the
664
+ // generateNameBasedUUID method
665
+
666
+ NameBasedGenerator uuid_gen = Generators .nameBasedGenerator ();
667
+ assertEquals (uuid_gen .getType (), UUIDType .NAME_BASED_SHA1 );
668
+ UUID uuid_array [] = new UUID [SIZE_OF_TEST_ARRAY ];
669
+
670
+ // now create the array of uuids
671
+ for (int i = 0 ; i < uuid_array .length ; i ++)
672
+ {
673
+ uuid_array [i ] = uuid_gen .generate ("test name" +i );
674
+ }
675
+
676
+ // check that none of the UUIDs are null
677
+ checkUUIDArrayForNonNullUUIDs (uuid_array );
678
+
679
+ // check that all the uuids were correct variant and version
680
+ checkUUIDArrayForCorrectVariantAndVersion (uuid_array , UUIDType .NAME_BASED_SHA1 );
681
+
682
+ // check that all uuids were unique
683
+ checkUUIDArrayForUniqueness (uuid_array );
684
+
685
+ // now create the array of uuids
686
+ for (int i = 0 ; i < uuid_array .length ; i ++)
687
+ {
688
+ uuid_array [i ] = uuid_gen .generate ("test name" + i );
689
+ }
690
+
691
+ // check that none of the UUIDs are null
692
+ checkUUIDArrayForNonNullUUIDs (uuid_array );
693
+
694
+ // check that all the uuids were correct variant and version
695
+ checkUUIDArrayForCorrectVariantAndVersion (uuid_array , UUIDType .NAME_BASED_SHA1 );
696
+
697
+ // check that all uuids were unique
698
+ checkUUIDArrayForUniqueness (uuid_array );
699
+
700
+ // now, lets make sure generating two sets of name based uuid with the
701
+ // same args always gives the same result
702
+ uuid_array = new UUID [SIZE_OF_TEST_ARRAY ];
703
+
704
+ // now create the array of uuids
705
+ for (int i = 0 ; i < uuid_array .length ; i ++) {
706
+ uuid_array [i ] = uuid_gen .generate ("test name" + i );
707
+ }
708
+
709
+ UUID uuid_array2 [] = new UUID [SIZE_OF_TEST_ARRAY ];
710
+
711
+ // now create the array of uuids
712
+ for (int i = 0 ; i < uuid_array2 .length ; i ++) {
713
+ uuid_array2 [i ] = uuid_gen .generate ("test name" + i );
714
+ }
715
+
716
+ // check that none of the UUIDs are null
717
+ checkUUIDArrayForNonNullUUIDs (uuid_array );
718
+ checkUUIDArrayForNonNullUUIDs (uuid_array2 );
719
+
720
+ // check that all the uuids were correct variant and version
721
+ checkUUIDArrayForCorrectVariantAndVersion (uuid_array , UUIDType .NAME_BASED_SHA1 );
722
+ checkUUIDArrayForCorrectVariantAndVersion (uuid_array2 , UUIDType .NAME_BASED_SHA1 );
723
+
724
+ // check that all uuids were unique
725
+ checkUUIDArrayForUniqueness (uuid_array );
726
+ checkUUIDArrayForUniqueness (uuid_array2 );
727
+
728
+ // check that both arrays are equal to one another
729
+ assertTrue ("expected both arrays to be equal, they were not!" ,
730
+ Arrays .equals (uuid_array , uuid_array2 ));
731
+ }
732
+
553
733
/**
554
734
* Test of generateTimeBasedReorderedUUID() method,
555
735
* of class com.fasterxml.uuid.UUIDGenerator.
0 commit comments