Created
July 24, 2014 15:33
-
-
Save RobertFischer/2fa36220d801fccb7041 to your computer and use it in GitHub Desktop.
Example of a base class for models.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* The base class for models. | |
* | |
* @param <T> The type of the model under test. | |
*/ | |
public abstract class ModelTestBase<T extends Serializable> { | |
/** | |
* Responsible for generating a test instance. | |
* | |
* @return A test instance; never {@code null} | |
*/ | |
public abstract T newTestInstance(); | |
/** | |
* Responsible for generating a second test instance which is equivalent to the given instance, | |
* but is not the same instance. | |
* | |
* @param base The base unit; never {@code null}. | |
* @return An instance, {@code it}, which is not the same as {@code base}, but where {@code base.equals(it)} will return {@code true} | |
*/ | |
public T newEquivalentTestInstance(T base) { | |
// Don't worry about optimizing this: it's test code. | |
try { | |
final ByteArrayOutputStream baos = new ByteArrayOutputStream(); | |
final ObjectOutputStream oos = new ObjectOutputStream(baos); | |
oos.writeObject(base); | |
final ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); | |
final ObjectInputStream ois = new ObjectInputStream(bais); | |
return getClassUnderTest().cast(ois.readObject()); | |
} catch(Exception e) { | |
throw new RuntimeException("Error cloning test instance", e); | |
} | |
} | |
public Class<T> getClassUnderTest() { | |
return (Class<T>)newTestInstance().getClass(); | |
} | |
/** | |
* Basic sanity check to make sure that we aren't generating null test instances. | |
*/ | |
@Test | |
public void testNewTestInstanceIsNotNull() { | |
assertNotNull(newTestInstance()); | |
} | |
/** | |
* Basic sanity check to make sure that we aren't generating null values for the class under test. | |
*/ | |
@Test | |
public void testClassUnderTestIsNotNull() { | |
assertNotNull(getClassUnderTest()); | |
} | |
@Test | |
public void testNewEquivalentTestInstanceDoesNotReturnSame() { | |
T testInstance = newTestInstance(); | |
assertEquals(testInstance, newEquivalentTestInstance(testInstance)); | |
assertNotSame(testInstance, newEquivalentTestInstance(testInstance)); | |
// Don't be cute. | |
assertNotSame(newEquivalentTestInstance(testInstance), newEquivalentTestInstance(testInstance)); | |
} | |
@Test | |
public void testNewTestInstanceIsNotEqualToAnotherNewTestInstance() { | |
T left = newTestInstance(); | |
T right = newTestInstance(); | |
assertNotSame(left, right); | |
assertNotEquals(left, right); | |
} | |
@Test | |
public void testHasSerializableUUIDField() throws Exception { | |
Field field = getClassUnderTest().getField("UUID"); | |
assertNotNull(field); | |
assertTrue(Modifier.isFinal(field.getModifiers())); | |
assertTrue(Modifier.isStatic(field.getModifiers())); | |
// Add any other checks here (not 0?) | |
} | |
@Test | |
public void testHashCode() { | |
T it = newTestInstance(); | |
T equals = newEquivalentTestInstance(left); | |
T notEquals = newTestInstance(); | |
assertEquals(it.hashCode(), equals.hashCode()); | |
assertNotEquals(it.hashCode(), notEquals.hashCode()); | |
} | |
@Test | |
public void testEquals() { | |
T it = newTestInstance(); | |
T equals = newEquivalentTestInstance(left); | |
T notEquals = newTestInstance(); | |
assertEquals(it, equals); | |
assertNotEquals(it, notEquals); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment