// constant for use in this test
final int EXTRA_DATA_LENGTH = 9;
// construct a UUIDTimer
SecureRandom secure_random = new SecureRandom();
UUIDTimer uuid_timer = new UUIDTimer(secure_random, null);
// test an array thats too small
try
{
byte[] test_array = new byte[UUID_TIMER_ARRAY_LENGTH - 1];
uuid_timer.getAndSetTimestamp(test_array);
// if we get here, we didn't catch the expected exception
fail("Expected exception not caught");
}
catch (ArrayIndexOutOfBoundsException ex)
{
// caught the expected exception, this is good, just go on
}
catch (Exception ex)
{
fail("Unexpected exception caught");
}
// construct a valid array exactly big enough and see that it works
byte[] test_array = new byte[UUID_TIMER_ARRAY_LENGTH];
uuid_timer.getAndSetTimestamp(test_array);
// check that it's not all null
assertArrayNotEqual(test_array,
new byte[UUID_TIMER_ARRAY_LENGTH],
UUID_TIMER_ARRAY_LENGTH);
// construct a valid array bigger then we need
// and make sure getAndSetTimeStamp only touches the begining part
test_array = new byte[UUID_TIMER_ARRAY_LENGTH + EXTRA_DATA_LENGTH];
Arrays.fill(test_array, (byte)'x');
uuid_timer.getAndSetTimestamp(test_array);
for (int i = 0; i < EXTRA_DATA_LENGTH; ++i)
{
assertEquals("test_array element was corrupted",
(byte)'x',
test_array[i + UUID_TIMER_ARRAY_LENGTH]);
}
// check that the timer portion is not all null
assertArrayNotEqual(test_array,
new byte[UUID_TIMER_ARRAY_LENGTH],
UUID_TIMER_ARRAY_LENGTH);
// now make a bunch of timer elements and validate that they are
// are well behaved timer elements
byte[][] array_of_uuid_timer_byte_arrays =
new byte[SIZE_OF_TEST_ARRAY][UUID_TIMER_ARRAY_LENGTH];
// before generating all the uuid timer arrays, get the start time
long start_time = System.currentTimeMillis();
// now create the array of uuid timer output arrays
for (int i = 0; i < array_of_uuid_timer_byte_arrays.length; i++)
{
uuid_timer.getAndSetTimestamp(array_of_uuid_timer_byte_arrays[i]);
}
// now capture the end time
long end_time = System.currentTimeMillis();