net.sf.molae.pipe.test
Class CollectionTest<B extends Collection<E>,E>

java.lang.Object
  extended by org.junit.Assert
      extended by net.sf.molae.pipe.test.ObjectTest<B>
          extended by net.sf.molae.pipe.test.CollectionTest<B,E>
Direct Known Subclasses:
ListTest, SetTest

public class CollectionTest<B extends Collection<E>,E>
extends ObjectTest<B>

Test methods to check the contract of collection implementing classes.


Constructor Summary
protected CollectionTest(B testObject)
          Creates a new test with the specified candidate.
 
Method Summary
protected  void assertAddProhibited()
          This method asserts that that the specifed collection does not implement add or addAll.
 void assertConsistency()
          Checks consistency of all reading collection methods.
 void assertEqualContent(String message, Collection<?> col)
          Checks if the specified collection has the same elements as the tested collection.
static void assertEqualSequence(String message, Iterator expected, Iterator actual)
          Compares the specified iterators.
 void assertImmutability()
          Calls all modifying methods of the underlying collection and checks if they throw an UnsupportedOperationException.
protected  void assertValidArray(String message, Object[] a)
          Checks if the specified array is a valid result of the toArray() or toArray(Object[]) operation, respectively.
protected  void checkAddAll(String message, List<E> l)
          This method is used to check the result of an addAll() call on an empty collection.
protected  void checkConsistency()
          This method is called at the start of the test cycle and after each modification of the test object.
static
<E> CollectionTest<Collection<E>,E>
getInstance(Collection<E> testObject)
          Creates a new instance of this class with the specified testObject as test object.
protected  E getTestValue()
          Provides a value of the element type.
protected  void performBulkCycle()
          Performs a test cycle of methods that take collections as argument (Collection.removeAll(Collection), Collection.retainAll(Collection) and Collection.addAll(Collection)).
 void performCycle()
          Performs a test cycle that uses modifying methods but is expected to leave the test object in an equal state as at the start - if everything works fine.
 void performIntegerTest()
          Deprecated. Use setIntegerTestValues(net.sf.molae.pipe.test.CollectionTest) and performValuesTest() instead.
 void performValuesTest()
          Fills the specified collection with five elements and compares it to a ArrayList with the same elements.
static void setIntegerTestValues(CollectionTest<?,? super Integer> test)
           
 void setTestValues(E elem1, E elem2, E elem3, E elem4, E elem5)
          Defines the test data to be used in performValuesTest().
 
Methods inherited from class net.sf.molae.pipe.test.ObjectTest
assertFalse, assertTestValuesSet, getTestObject, setTestValuesSet, toString
 
Methods inherited from class org.junit.Assert
assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertArrayEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertFalse, assertNotNull, assertNotNull, assertNotSame, assertNotSame, assertNull, assertNull, assertSame, assertSame, assertThat, assertThat, assertTrue, assertTrue, fail, fail
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Constructor Detail

CollectionTest

protected CollectionTest(B testObject)
Creates a new test with the specified candidate.

Parameters:
testObject - the candidate of the test
Throws:
NullPointerException - if the specified test objects is null.
Method Detail

assertEqualSequence

public static final void assertEqualSequence(String message,
                                             Iterator expected,
                                             Iterator actual)
Compares the specified iterators. The iterators are considered equal, if they have the same number of elements and all corresponding elements are equal.

Parameters:
message - the message to be displayed if the assertion fails
expected - expected value
actual - actual value
Throws:
AssertionFailedError - if the specified iterators are not equal in the defined way.

getInstance

public static final <E> CollectionTest<Collection<E>,E> getInstance(Collection<E> testObject)
Creates a new instance of this class with the specified testObject as test object.

Parameters:
testObject - the candidate of the test
Returns:
an instance of this class with the specified testObject as test object.
Throws:
NullPointerException - if the specified test objects is null.

assertEqualContent

public final void assertEqualContent(String message,
                                     Collection<?> col)
Checks if the specified collection has the same elements as the tested collection. First both collections are copied to lists hoping that the resulting lists are equal. If not the cardinalities of each contained object are compared.

Parameters:
message - the message to be displayed if the assertion fails
col - the collection to be compared
Throws:
AssertionFailedError - if the specified collection and the tested collection do not contain the same elements with the same cardinality.

checkAddAll

protected void checkAddAll(String message,
                           List<E> l)

This method is used to check the result of an addAll() call on an empty collection.

Must be overwritten for subclasses that have a more specific contract concerning addAll.

This implementation checks that both collections contain the other one.

Parameters:
message - the message to be displayed if the assertion fails
l - the list to compare
Throws:
AssertionFailedError - if the specified list is not equal in the described sense.

assertValidArray

protected void assertValidArray(String message,
                                Object[] a)

Checks if the specified array is a valid result of the toArray() or toArray(Object[]) operation, respectively.

Must be overwritten for subclasses that have a more specific contract concerning lists.

This implementatin calls assertEqualContent(String,Collection) with a list version of the array.

Parameters:
message - the message to be displayed if the assertion fails
a - the result of a toArray() operation.
Throws:
AssertionFailedError - if the specified array is not a valid result.

getTestValue

protected E getTestValue()
Provides a value of the element type.

Returns:
a value of the element type.

assertConsistency

public void assertConsistency()
Checks consistency of all reading collection methods.

Overrides:
assertConsistency in class ObjectTest<B extends Collection<E>>
Throws:
AssertionFailedError - if the collection contract is broken by the test object.

assertImmutability

public void assertImmutability()
Calls all modifying methods of the underlying collection and checks if they throw an UnsupportedOperationException.

Throws:
AssertionFailedError - if any of the called operations did not throw an UnsupportedOperationException

assertAddProhibited

protected void assertAddProhibited()
This method asserts that that the specifed collection does not implement add or addAll. Used by MapTest.

Throws:
AssertionFailedError - if the specified collection implements an adding method.

checkConsistency

protected void checkConsistency()
This method is called at the start of the test cycle and after each modification of the test object. This implementation calls assertConsistency().

Throws:
AssertionFailedError - if the test object is not consistent.

performBulkCycle

protected void performBulkCycle()
Performs a test cycle of methods that take collections as argument (Collection.removeAll(Collection), Collection.retainAll(Collection) and Collection.addAll(Collection)).

Throws:
AssertionFailedError - if the set contract is broken by the specified set.
UnsupportedOperationException - if any of the called methods is not implemented.

performCycle

public void performCycle()
Performs a test cycle that uses modifying methods but is expected to leave the test object in an equal state as at the start - if everything works fine. This implementation calls performBulkCycle().

Throws:
AssertionFailedError - if the test object does not pass the test.
UnsupportedOperationException - if any of the called methods is not implemented.

setTestValues

public void setTestValues(E elem1,
                          E elem2,
                          E elem3,
                          E elem4,
                          E elem5)
Defines the test data to be used in performValuesTest().


setIntegerTestValues

public static void setIntegerTestValues(CollectionTest<?,? super Integer> test)

performValuesTest

public void performValuesTest()
Fills the specified collection with five elements and compares it to a ArrayList with the same elements.

Throws:
AssertionFailedError - if the test object does not pass the test.
IllegalStateException - if test values have not been set.

performIntegerTest

public void performIntegerTest()
Deprecated. Use setIntegerTestValues(net.sf.molae.pipe.test.CollectionTest) and performValuesTest() instead.

Method to test collection implementations that support integers.