junit.com.mcdermottroe.exemplar
Class NormalClassTestCase<T extends Copyable<T> & Comparable<T>>

java.lang.Object
  extended by junit.framework.Assert
      extended by junit.framework.TestCase
          extended by junit.com.mcdermottroe.exemplar.ExemplarTestCase<T>
              extended by junit.com.mcdermottroe.exemplar.NormalClassTestCase<T>
Type Parameters:
T - The type of the class being tested.
All Implemented Interfaces:
junit.framework.Test
Direct Known Subclasses:
ConsoleFormatterTest, ExceptionClassTestCase, ExitCodeTest, LanguageAPIPairTest, LexerTest, OptionTestCase, ParserTest, PEDeclTableTest, PowerSetTest, TaskLogHandlerTest, TaskTest, TimerTest, XMLAttributeContentTypeTest, XMLAttributeDefaultTypeTest, XMLAttributeTest, XMLDocumentTypeTest, XMLElementContentModelTest, XMLExternalIdentifierTest, XMLObjectTestCase, XMLParserGeneratorTestCase

public abstract class NormalClassTestCase<T extends Copyable<T> & Comparable<T>>
extends ExemplarTestCase<T>

Base class for JUnit tests that test classes which produce "normal" classes. That includes all classes that are not abstract, exceptions or utility classes.

Since:
0.1

Field Summary
protected  boolean allowPublicStaticMembers
          Flag to set if we allow 'public static final' members in the tested class.
protected  boolean ignoreHashCodeTests
          Flag to set if we don't want to run any tests on the Object.hashCode() method of the Object.
private  List<T> sampleObjects
          A selection of sample Objects which can be used within the tests.
 
Fields inherited from class junit.com.mcdermottroe.exemplar.ExemplarTestCase
DELIBERATE_PASS, testedClass, TMP
 
Constructor Summary
NormalClassTestCase()
           
 
Method Summary
protected  void addSample(T sample)
          Add a sample to the collection of sample objects.
private  boolean hasEquals()
          Test whether or not the tested class implements an Object.equals(Object) method.
private  boolean hasHashCode()
          Test whether or not the tested class implements a Object.hashCode() method.
private  boolean hasToString()
          Test whether or not the tested class implements a Object.toString() method.
protected  List<T> samples()
          Provide a List of sample objects to operate on.
 void setUp()
          Set up the JUnit test.
 void testAllConstructorsNonPrivate()
          This test ensures that there are no private constructors declared.
 void testAllFieldsPrivateOrProtectedOrPublicStaticFinal()
          All fields must be either 'private', 'protected' or (if allowed by the allowPublicStaticMembers flag) 'public static final'.
 void testCompareToConsistentWithEquals()
          Test to ensure that a.compareTo(b) == 0 implies a.equals(b).
 void testCompareToNullThrowsNPE()
          Test that Comparable.compareTo(Object) throws a NullPointerException when given null as its parameter.
 void testCompareToSignsConsistent()
          Test that: Integer.signum(a.compareTo(b)) == -Integer.signum(b.compareTo(a)) as required by the contract of Comparable.compareTo(Object).
 void testCompareToTransitive()
          Test to ensure that: (a.compareTo(b) op 0 && b.compareTo(c) op 0) implies a.compareTo(c) op 0.
 void testCopyable()
          This ensures that it is possible to call Copyable.getCopy() and not have an exception thrown.
 void testEqualsConsistent()
          Methods implementing Object.equals(Object) are required to be consistent.
 void testEqualsNullFalse()
          Every call to equals where the parameter is null must return false.
 void testEqualsNullPointerException()
          Test equals with an object which is not of the same type.
 void testEqualsReflexive()
          Methods implementing Object.equals(Object) are required to be reflexive.
 void testEqualsSymmetric()
          Methods implementing Object.equals(Object) are required to be symmetric.
 void testEqualsTransitive()
          Methods implementing Object.equals(Object) are required to be transitive.
 void testGetCopyProducesIdenticalObject()
          This ensures that it is possible to call Copyable.getCopy() and get an object which is equal to, but not the same (referentially) to the original object.
 void testHashCodeConsistentOverTime()
          This ensures that the result of Object.hashCode() does not change over time.
 void testHashCodeConsistentWithEquals()
          This ensures that where x.equals(y), x.hashCode() == y.hashCode().
 void testImplementsCopyable()
          Ensure that all classes are Copyable.
 void testSerialization()
          Test that all serializable classes serialize and unserialize without apparent loss of information.
 void testToStringConsistent()
          This ensures that for all non-null Objects x and y, x.toString().equals(y.toString()) iff x.equals(y).
 
Methods inherited from class junit.com.mcdermottroe.exemplar.ExemplarTestCase
getTestedClass, testAllMethodsBeingTested, testCorrectPackage
 
Methods inherited from class junit.framework.TestCase
countTestCases, createResult, getName, run, run, runBare, runTest, setName, tearDown, toString
 
Methods inherited from class junit.framework.Assert
assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertTrue, assertTrue, fail, fail, failNotEquals, failNotSame, failSame, format
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

allowPublicStaticMembers

protected boolean allowPublicStaticMembers
Flag to set if we allow 'public static final' members in the tested class. This should only be permitted in generated classes.


ignoreHashCodeTests

protected boolean ignoreHashCodeTests
Flag to set if we don't want to run any tests on the Object.hashCode() method of the Object.


sampleObjects

private List<T extends Copyable<T> & Comparable<T>> sampleObjects
A selection of sample Objects which can be used within the tests. This allows more realistic, but still generalised, testing to be performed and avoids only testing the no-arg constructor. This List should be initialised in the setUp() method.

Constructor Detail

NormalClassTestCase

public NormalClassTestCase()
Method Detail

setUp

public void setUp()
           throws Exception
Set up the JUnit test.

Overrides:
setUp in class ExemplarTestCase<T extends Copyable<T> & Comparable<T>>
Throws:
Exception - if TestCase.setUp() throws one.
See Also:
ExemplarTestCase.testedClass, TestCase.setUp()

testAllConstructorsNonPrivate

public void testAllConstructorsNonPrivate()
This test ensures that there are no private constructors declared. This is to allow for better extensibility. Only utility classes should have provate constructors as they should never be subclassed.


testAllFieldsPrivateOrProtectedOrPublicStaticFinal

public void testAllFieldsPrivateOrProtectedOrPublicStaticFinal()
All fields must be either 'private', 'protected' or (if allowed by the allowPublicStaticMembers flag) 'public static final'.


testHashCodeConsistentOverTime

public void testHashCodeConsistentOverTime()
This ensures that the result of Object.hashCode() does not change over time.


testHashCodeConsistentWithEquals

public void testHashCodeConsistentWithEquals()
This ensures that where x.equals(y), x.hashCode() == y.hashCode().


testEqualsReflexive

public void testEqualsReflexive()
Methods implementing Object.equals(Object) are required to be reflexive. That is, for all non-null reference values x, x.equals(x) must return true.


testEqualsSymmetric

public void testEqualsSymmetric()
Methods implementing Object.equals(Object) are required to be symmetric. That is, for any non-null reference values x and y, x.equals(y) should return true if and only if y.equals(x) returns true.


testEqualsTransitive

public void testEqualsTransitive()
Methods implementing Object.equals(Object) are required to be transitive. That is, for any non-null reference values x, y, and z, if x.equals(y) returns true and y.equals(z) returns true then x.equals(z) must return true.


testEqualsConsistent

public void testEqualsConsistent()
Methods implementing Object.equals(Object) are required to be consistent. That is, for all non-null reference values x and y, multiple invocations of x.equals(y) consistently return true or false, provided no information used in the equals comparisons on the objects is modified.


testEqualsNullFalse

public void testEqualsNullFalse()
Every call to equals where the parameter is null must return false.


testEqualsNullPointerException

public void testEqualsNullPointerException()
Test equals with an object which is not of the same type.


testToStringConsistent

public void testToStringConsistent()
This ensures that for all non-null Objects x and y, x.toString().equals(y.toString()) iff x.equals(y). This is not required by the contract of Object.toString() but is an all-round good idea.


testImplementsCopyable

public void testImplementsCopyable()
Ensure that all classes are Copyable.


testCopyable

public void testCopyable()
This ensures that it is possible to call Copyable.getCopy() and not have an exception thrown.


testGetCopyProducesIdenticalObject

public void testGetCopyProducesIdenticalObject()
This ensures that it is possible to call Copyable.getCopy() and get an object which is equal to, but not the same (referentially) to the original object.


testCompareToSignsConsistent

public void testCompareToSignsConsistent()
Test that: Integer.signum(a.compareTo(b)) == -Integer.signum(b.compareTo(a)) as required by the contract of Comparable.compareTo(Object).


testCompareToTransitive

public void testCompareToTransitive()
Test to ensure that: (a.compareTo(b) op 0 && b.compareTo(c) op 0) implies a.compareTo(c) op 0.


testCompareToNullThrowsNPE

public void testCompareToNullThrowsNPE()
Test that Comparable.compareTo(Object) throws a NullPointerException when given null as its parameter.


testCompareToConsistentWithEquals

public void testCompareToConsistentWithEquals()
Test to ensure that a.compareTo(b) == 0 implies a.equals(b).


testSerialization

public void testSerialization()
Test that all serializable classes serialize and unserialize without apparent loss of information.


addSample

protected void addSample(T sample)
                  throws CopyException
Add a sample to the collection of sample objects.

Parameters:
sample - The sample object to add.
Throws:
CopyException - if copying the sample using Copyable.getCopy() fails.

samples

protected List<T> samples()
Provide a List of sample objects to operate on.

Returns:
A guaranteed non-empty List of sample objects.

hasEquals

private boolean hasEquals()
Test whether or not the tested class implements an Object.equals(Object) method.

Returns:
True if the tested class implements an Object.equals(Object) method, false otherwise.

hasHashCode

private boolean hasHashCode()
Test whether or not the tested class implements a Object.hashCode() method.

Returns:
True if the tested class implements a Object.hashCode() method, false otherwise.

hasToString

private boolean hasToString()
Test whether or not the tested class implements a Object.toString() method.

Returns:
True if the tested class implements a Object.toString() method, false otherwise.