Skip to content

Commit

Permalink
#87 Add some tests and code simplicity (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
magdel authored Feb 23, 2024
1 parent 5b05b64 commit 967baf4
Show file tree
Hide file tree
Showing 6 changed files with 188 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/com/fasterxml/uuid/EthernetAddress.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public class EthernetAddress
{
private static final long serialVersionUID = 1L;

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

/**
* We may need a random number generator, for creating dummy ethernet
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/fasterxml/uuid/Generators.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ public static RandomBasedGenerator randomBasedGenerator(Random rnd) {
* Factory method for constructing UUID generator that uses specified
* random number generator for constructing UUIDs according to standard
* method number 5, but without using a namespace.
* Digester to use will be SHA-1 as recommened by UUID spec.
* Digester to use will be SHA-1 as recommended by UUID spec.
*/
public static NameBasedGenerator nameBasedGenerator() {
return nameBasedGenerator(null);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/com/fasterxml/uuid/UUIDClock.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@
*/
public class UUIDClock
{
private final static UUIDClock DEFAULT = new UUIDClock();
private static final UUIDClock DEFAULT = new UUIDClock();

/**
* @since 4.3
*/
public final static UUIDClock systemTimeClock() {
public static UUIDClock systemTimeClock() {
return DEFAULT;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/com/fasterxml/uuid/UUIDTimer.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ protected final void getAndSetTimestamp(byte[] uuidBytes)
/**********************************************************************
*/

private final static int MAX_WAIT_COUNT = 50;
private static final int MAX_WAIT_COUNT = 50;

/**
* Simple utility method to use to wait for couple of milliseconds,
Expand Down
3 changes: 3 additions & 0 deletions src/main/java/com/fasterxml/uuid/impl/LazyRandom.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ public final class LazyRandom
private static volatile SecureRandom shared;

public static SecureRandom sharedSecureRandom() {
if (shared != null) {
return shared;
}
synchronized (lock) {
SecureRandom result = shared;
if (result == null) {
Expand Down
180 changes: 180 additions & 0 deletions src/test/java/com/fasterxml/uuid/UUIDGeneratorTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,58 @@ public void testGenerateTimeBasedEpochUUID() throws Exception
checkUUIDArrayForCorrectCreationTimeEpoch(uuid_array, start_time, end_time);
}

/**
* Test of generateTimeBasedEpochUUID() method with UUIDClock instance,
* of class com.fasterxml.uuid.UUIDGenerator.
*/
public void testGenerateTimeBasedEpochUUIDWithUUIDClock() throws Exception
{
// this test will attempt to check for reasonable behavior of the
// generateTimeBasedUUID method

Random entropy = new Random(0x666);

// we need a instance to use
TimeBasedEpochGenerator uuid_gen = Generators.timeBasedEpochGenerator(null, UUIDClock.systemTimeClock());
assertEquals(uuid_gen.getType(), UUIDType.TIME_BASED_EPOCH);

// first check that given a number of calls to generateTimeBasedEpochUUID,
// all returned UUIDs order after the last returned UUID
// we'll check this by generating the UUIDs into one array and sorting
// then in another and checking the order of the two match
// change the number in the array statement if you want more or less
// UUIDs to be generated and tested
UUID uuid_array[] = new UUID[SIZE_OF_TEST_ARRAY];

// before we generate all the uuids, lets get the start time
long start_time = System.currentTimeMillis();
Thread.sleep(2); // Clean start time

// now create the array of uuids
for (int i = 0; i < uuid_array.length; i++) {
uuid_array[i] = uuid_gen.generate();
}

// now capture the end time
long end_time = System.currentTimeMillis();
Thread.sleep(2); // Clean end time

// check that none of the UUIDs are null
checkUUIDArrayForNonNullUUIDs(uuid_array);

// check that all the uuids were correct variant and version (type-1)
checkUUIDArrayForCorrectVariantAndVersion(uuid_array, UUIDType.TIME_BASED_EPOCH);

// check that all the uuids were generated with correct order
checkUUIDArrayForCorrectOrdering(uuid_array);

// check that all uuids were unique
checkUUIDArrayForUniqueness(uuid_array);

// check that all uuids have timestamps between the start and end time
checkUUIDArrayForCorrectCreationTimeEpoch(uuid_array, start_time, end_time);
}

/**
* Test of generateTimeBasedEpochRandomUUID() method,
* of class com.fasterxml.uuid.UUIDGenerator.
Expand Down Expand Up @@ -356,6 +408,58 @@ public void testGenerateTimeBasedEpochRandomUUID() throws Exception
checkUUIDArrayForCorrectCreationTimeEpoch(uuid_array, start_time, end_time);
}

/**
* Test of generateTimeBasedEpochRandomUUID() method with UUIDClock instance,
* of class com.fasterxml.uuid.UUIDGenerator.
*/
public void testGenerateTimeBasedEpochRandomUUIDWithUUIDClock() throws Exception
{
// this test will attempt to check for reasonable behavior of the
// generateTimeBasedRandomUUID method

Random entropy = new Random(0x666);

// we need a instance to use
TimeBasedEpochRandomGenerator uuid_gen = Generators.timeBasedEpochRandomGenerator(null, UUIDClock.systemTimeClock());

assertEquals(uuid_gen.getType(), UUIDType.TIME_BASED_EPOCH);
// first check that given a number of calls to generateTimeBasedEpochUUID,
// all returned UUIDs order after the last returned UUID
// we'll check this by generating the UUIDs into one array and sorting
// then in another and checking the order of the two match
// change the number in the array statement if you want more or less
// UUIDs to be generated and tested
UUID uuid_array[] = new UUID[SIZE_OF_TEST_ARRAY];

// before we generate all the uuids, lets get the start time
long start_time = System.currentTimeMillis();
Thread.sleep(2); // Clean start time

// now create the array of uuids
for (int i = 0; i < uuid_array.length; i++) {
uuid_array[i] = uuid_gen.generate();
}

// now capture the end time
long end_time = System.currentTimeMillis();
Thread.sleep(2); // Clean end time

// check that none of the UUIDs are null
checkUUIDArrayForNonNullUUIDs(uuid_array);

// check that all the uuids were correct variant and version (type-1)
checkUUIDArrayForCorrectVariantAndVersion(uuid_array, UUIDType.TIME_BASED_EPOCH);

// check that all uuids were unique
// NOTE: technically, this test 'could' fail, but statistically
// speaking it should be extremely unlikely unless the implementation
// of (Secure)Random is bad
checkUUIDArrayForUniqueness(uuid_array);

// check that all uuids have timestamps between the start and end time
checkUUIDArrayForCorrectCreationTimeEpoch(uuid_array, start_time, end_time);
}

// [#70]: allow use of custom UUIDClock
public void testGenerateTimeBasedEpochUUIDWithFixedClock() throws Exception
{
Expand Down Expand Up @@ -550,6 +654,82 @@ public void testGenerateNameBasedUUIDNameSpaceNameAndMessageDigest()
Arrays.equals(uuid_array, uuid_array2));
}

/**
* Test of generateNameBasedUUID()
* method, of class com.fasterxml.uuid.UUIDGenerator.
*/
public void testGenerateNameBasedUUIDWithDefaults()
{
// this test will attempt to check for reasonable behavior of the
// generateNameBasedUUID method

NameBasedGenerator uuid_gen = Generators.nameBasedGenerator();
assertEquals(uuid_gen.getType(), UUIDType.NAME_BASED_SHA1);
UUID uuid_array[] = new UUID[SIZE_OF_TEST_ARRAY];

// now create the array of uuids
for (int i = 0; i < uuid_array.length; i++)
{
uuid_array[i] = uuid_gen.generate("test name"+i);
}

// check that none of the UUIDs are null
checkUUIDArrayForNonNullUUIDs(uuid_array);

// check that all the uuids were correct variant and version
checkUUIDArrayForCorrectVariantAndVersion(uuid_array, UUIDType.NAME_BASED_SHA1);

// check that all uuids were unique
checkUUIDArrayForUniqueness(uuid_array);

// now create the array of uuids
for (int i = 0; i < uuid_array.length; i++)
{
uuid_array[i] = uuid_gen.generate("test name" + i);
}

// check that none of the UUIDs are null
checkUUIDArrayForNonNullUUIDs(uuid_array);

// check that all the uuids were correct variant and version
checkUUIDArrayForCorrectVariantAndVersion(uuid_array, UUIDType.NAME_BASED_SHA1);

// check that all uuids were unique
checkUUIDArrayForUniqueness(uuid_array);

// now, lets make sure generating two sets of name based uuid with the
// same args always gives the same result
uuid_array = new UUID[SIZE_OF_TEST_ARRAY];

// now create the array of uuids
for (int i = 0; i < uuid_array.length; i++) {
uuid_array[i] = uuid_gen.generate("test name" + i);
}

UUID uuid_array2[] = new UUID[SIZE_OF_TEST_ARRAY];

// now create the array of uuids
for (int i = 0; i < uuid_array2.length; i++) {
uuid_array2[i] = uuid_gen.generate("test name" + i);
}

// check that none of the UUIDs are null
checkUUIDArrayForNonNullUUIDs(uuid_array);
checkUUIDArrayForNonNullUUIDs(uuid_array2);

// check that all the uuids were correct variant and version
checkUUIDArrayForCorrectVariantAndVersion(uuid_array, UUIDType.NAME_BASED_SHA1);
checkUUIDArrayForCorrectVariantAndVersion(uuid_array2, UUIDType.NAME_BASED_SHA1);

// check that all uuids were unique
checkUUIDArrayForUniqueness(uuid_array);
checkUUIDArrayForUniqueness(uuid_array2);

// check that both arrays are equal to one another
assertTrue("expected both arrays to be equal, they were not!",
Arrays.equals(uuid_array, uuid_array2));
}

/**
* Test of generateTimeBasedReorderedUUID() method,
* of class com.fasterxml.uuid.UUIDGenerator.
Expand Down

0 comments on commit 967baf4

Please sign in to comment.