Skip to content

Commit 967baf4

Browse files
authored
#87 Add some tests and code simplicity (#96)
1 parent 5b05b64 commit 967baf4

File tree

6 files changed

+188
-5
lines changed

6 files changed

+188
-5
lines changed

src/main/java/com/fasterxml/uuid/EthernetAddress.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public class EthernetAddress
3333
{
3434
private static final long serialVersionUID = 1L;
3535

36-
private final static char[] HEX_CHARS = "0123456789abcdefABCDEF".toCharArray();
36+
private static final char[] HEX_CHARS = "0123456789abcdefABCDEF".toCharArray();
3737

3838
/**
3939
* We may need a random number generator, for creating dummy ethernet

src/main/java/com/fasterxml/uuid/Generators.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public static RandomBasedGenerator randomBasedGenerator(Random rnd) {
7474
* Factory method for constructing UUID generator that uses specified
7575
* random number generator for constructing UUIDs according to standard
7676
* method number 5, but without using a namespace.
77-
* Digester to use will be SHA-1 as recommened by UUID spec.
77+
* Digester to use will be SHA-1 as recommended by UUID spec.
7878
*/
7979
public static NameBasedGenerator nameBasedGenerator() {
8080
return nameBasedGenerator(null);

src/main/java/com/fasterxml/uuid/UUIDClock.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@
2626
*/
2727
public class UUIDClock
2828
{
29-
private final static UUIDClock DEFAULT = new UUIDClock();
29+
private static final UUIDClock DEFAULT = new UUIDClock();
3030

3131
/**
3232
* @since 4.3
3333
*/
34-
public final static UUIDClock systemTimeClock() {
34+
public static UUIDClock systemTimeClock() {
3535
return DEFAULT;
3636
}
3737

src/main/java/com/fasterxml/uuid/UUIDTimer.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -358,7 +358,7 @@ protected final void getAndSetTimestamp(byte[] uuidBytes)
358358
/**********************************************************************
359359
*/
360360

361-
private final static int MAX_WAIT_COUNT = 50;
361+
private static final int MAX_WAIT_COUNT = 50;
362362

363363
/**
364364
* Simple utility method to use to wait for couple of milliseconds,

src/main/java/com/fasterxml/uuid/impl/LazyRandom.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ public final class LazyRandom
1616
private static volatile SecureRandom shared;
1717

1818
public static SecureRandom sharedSecureRandom() {
19+
if (shared != null) {
20+
return shared;
21+
}
1922
synchronized (lock) {
2023
SecureRandom result = shared;
2124
if (result == null) {

src/test/java/com/fasterxml/uuid/UUIDGeneratorTest.java

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,58 @@ public void testGenerateTimeBasedEpochUUID() throws Exception
304304
checkUUIDArrayForCorrectCreationTimeEpoch(uuid_array, start_time, end_time);
305305
}
306306

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+
307359
/**
308360
* Test of generateTimeBasedEpochRandomUUID() method,
309361
* of class com.fasterxml.uuid.UUIDGenerator.
@@ -356,6 +408,58 @@ public void testGenerateTimeBasedEpochRandomUUID() throws Exception
356408
checkUUIDArrayForCorrectCreationTimeEpoch(uuid_array, start_time, end_time);
357409
}
358410

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+
359463
// [#70]: allow use of custom UUIDClock
360464
public void testGenerateTimeBasedEpochUUIDWithFixedClock() throws Exception
361465
{
@@ -550,6 +654,82 @@ public void testGenerateNameBasedUUIDNameSpaceNameAndMessageDigest()
550654
Arrays.equals(uuid_array, uuid_array2));
551655
}
552656

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+
553733
/**
554734
* Test of generateTimeBasedReorderedUUID() method,
555735
* of class com.fasterxml.uuid.UUIDGenerator.

0 commit comments

Comments
 (0)