public void testDefaultUUIDConstructor() { // this test technically relies on the ToString() and toByteArray() // methods of the UUID class working properly. // If it fails, that is fine... the test only needs to indicate // proper working behavior or that it needs to be fixed. UUID uuid = new UUID(); assertEquals("Default constructor did not create expected null UUID", NULL_UUID_STRING, uuid.ToString()); assertEquals("Expected array did not equal actual array", NULL_UUID_BYTE_ARRAY, uuid.ToByteArray()); }
public void testByteArrayUUIDConstructor() { // passing null UUID uuid; try { uuid = new UUID((byte[])null); fail("Expected exception not caught"); } catch (NullReferenceException) { // this is the success case so do nothing } catch (Exception ex) { fail("Caught unexpected exception: " + ex); } // passing array that is too small try { uuid = new UUID(new byte[UUID_BYTE_ARRAY_LENGTH - 1]); fail("Expected exception not caught"); } catch (IndexOutOfRangeException) { // this is the success case so do nothing } catch (Exception ex) { fail("Caught unexpected exception: " + ex); } // test that creating a uuid from an zero'd array // gives us a null UUID (definition of a null UUID) uuid = new UUID(new byte[UUID_BYTE_ARRAY_LENGTH]); assertEquals("constructor did not create expected null UUID", NULL_UUID_STRING, uuid.ToString()); assertEquals("Expected array did not equal actual array", NULL_UUID_BYTE_ARRAY, uuid.ToByteArray()); // test creating an array from a good byte array uuid = new UUID(VALID_UUID_BYTE_ARRAY); assertEquals("constructor did not create expected UUID", MIXED_CASE_VALID_UUID_STRING.ToLower(), uuid.ToString().ToLower()); // test creating an array from a good byte array with extra data on end uuid = new UUID(VALID_UUID_BYTE_ARRAY_WITH_EXTRA_END); assertEquals("constructor did not create expected UUID", MIXED_CASE_VALID_UUID_STRING.ToLower(), uuid.ToString().ToLower()); }
public void testByteArrayFromOffsetUUIDConstructor() { UUID uuid; // constant for use in this test int EXTRA_DATA_LENGTH = 9; // passing null and 0 try { uuid = new UUID((byte[])null, 0); fail("Expected exception not caught"); } catch (NullReferenceException) { // this is the success case so do nothing } catch (Exception ex) { fail("Caught unexpected exception: " + ex); } // passing an array that is too small try { uuid = new UUID(new byte[UUID_BYTE_ARRAY_LENGTH - 1], 0); fail("Expected exception not caught"); } catch (IndexOutOfRangeException) { // this is the success case so do nothing } catch (Exception ex) { fail("Caught unexpected exception: " + ex); } // passing an index that is negative try { uuid = new UUID(new byte[UUID_BYTE_ARRAY_LENGTH], -1); fail("Expected exception not caught"); } catch (IndexOutOfRangeException) { // this is the success case so do nothing } catch (Exception ex) { fail("Caught unexpected exception: " + ex); } // passing an index that is too big try { uuid = new UUID( new byte[UUID_BYTE_ARRAY_LENGTH], UUID_BYTE_ARRAY_LENGTH); fail("Expected exception not caught"); } catch (IndexOutOfRangeException) { // this is the success case so do nothing } catch (Exception ex) { fail("Caught unexpected exception: " + ex); } // passing an index that is in the array, // but without enough bytes to read UUID_BYTE_ARRAY_LENGTH try { uuid = new UUID(new byte[UUID_BYTE_ARRAY_LENGTH], 1); fail("Expected exception not caught"); } catch (IndexOutOfRangeException) { // this is the success case so do nothing } catch (Exception ex) { fail("Caught unexpected exception: " + ex); } // test that creating a uuid from an zero'd array // gives us a null UUID (definition of a null UUID) uuid = new UUID(new byte[UUID_BYTE_ARRAY_LENGTH], 0); assertEquals("constructor did not create expected null UUID", NULL_UUID_STRING, uuid.ToString()); assertEquals("Expected array did not equal actual array", NULL_UUID_BYTE_ARRAY, uuid.ToByteArray()); // test that creating a uuid from an zero'd array with extra stuff // on the front gives us a null UUID (definition of a null UUID) byte[] null_uuid_array = new byte[UUID_BYTE_ARRAY_LENGTH + EXTRA_DATA_LENGTH]; fill(null_uuid_array, 0, EXTRA_DATA_LENGTH, (byte)'x'); uuid = new UUID(null_uuid_array, EXTRA_DATA_LENGTH); assertEquals("constructor did not create expected null UUID", NULL_UUID_STRING, uuid.ToString()); assertEquals("Expected array did not equal actual array", NULL_UUID_BYTE_ARRAY, uuid.ToByteArray()); // test creating an array from a good byte array uuid = new UUID(VALID_UUID_BYTE_ARRAY, 0); assertEquals("constructor did not create expected null UUID", MIXED_CASE_VALID_UUID_STRING.ToLower(), uuid.ToString().ToLower()); // test creating an array from a good byte array with extra data on end uuid = new UUID(VALID_UUID_BYTE_ARRAY_WITH_EXTRA_END, 0); assertEquals("constructor did not create expected null UUID", MIXED_CASE_VALID_UUID_STRING.ToLower(), uuid.ToString().ToLower()); // test creating uuid from a byte array with extra junk on start uuid = new UUID(VALID_UUID_BYTE_ARRAY_WITH_EXTRA_START, 10); assertEquals("constructor did not create expected null UUID", MIXED_CASE_VALID_UUID_STRING.ToLower(), uuid.ToString().ToLower()); // test creating an uuid from a byte array with extra junk on both ends uuid = new UUID(VALID_UUID_BYTE_ARRAY_WITH_EXTRA_BOTH, 10); assertEquals("constructor did not create expected null UUID", MIXED_CASE_VALID_UUID_STRING.ToLower(), uuid.ToString().ToLower()); }
private void goodStringUUIDConstructorHelper(String uuidString) { UUID temp_uuid = null; try { temp_uuid = new UUID(uuidString); } catch (Exception ex) { fail("Caught unexpected exception: " + ex); } assertEquals("uuid strings were not equal", uuidString.ToLower(), temp_uuid.ToString().ToLower()); }
private void assertUUIDsMatchHelper(UUID expected, UUID actual) { // technically, ToString will always return lowercase uuid strings, // but just to be paranoid, we will always do ToLower in this test assertEquals("UUID strings did not match", expected.ToString().ToLower(), actual.ToString().ToLower()); assertEquals("UUID Equals did not match", expected, actual); }
public void testToString() { // test making a couple UUIDs and then check that the ToString // gives back the same value in string form that was used to create it // test the null uuid UUID uuid = new UUID(); assertEquals("null uuid string and ToString did not match", NULL_UUID_STRING.ToLower(), uuid.ToString().ToLower()); // test a non-null uuid uuid = new UUID(VALID_UUID_BYTE_ARRAY); assertEquals("uuid string and ToString results did not match", MIXED_CASE_VALID_UUID_STRING.ToLower(), uuid.ToString().ToLower()); // The current UUID implementation returns strings all lowercase. // Although relying on this behavior in code is not recommended, // here is a unit test which will break if this assumption // becomes bad. This will act as an early warning to anyone // who relies on this particular behavior. uuid = new UUID(VALID_UUID_BYTE_ARRAY); assertFalse("mixed case uuid string and ToString " + "matched (expected ToString to be all lower case)", MIXED_CASE_VALID_UUID_STRING.Equals(uuid.ToString())); assertEquals("mixed case string ToLower and " + "ToString results did not match (expected ToString to " + "be all lower case)", MIXED_CASE_VALID_UUID_STRING.ToLower(), uuid.ToString()); }