net.sf.molae.pipe.test
Class CollectionTest

java.lang.Object
  |
  +--junit.framework.Assert
        |
        +--net.sf.molae.pipe.test.ObjectTest
              |
              +--net.sf.molae.pipe.test.CollectionTest
Direct Known Subclasses:
ListTest, SetTest

public class CollectionTest
extends ObjectTest

Test methods to check the contract of collection implementing classes.


Constructor Summary
protected CollectionTest(Collection 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 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 CollectionTest getInstance(Collection testObject)
          Creates a new instance of this class with the specified testObject as test object.
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()
          Method to test collection implementations that support integers.
 
Methods inherited from class net.sf.molae.pipe.test.ObjectTest
assertFalse, getTestObject, toString
 
Methods inherited from class junit.framework.Assert
assert, assert, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertEquals, assertNotNull, assertNotNull, assertNull, assertNull, assertSame, assertSame, 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(Collection 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 CollectionTest getInstance(Collection 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 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.

assertConsistency

public void assertConsistency()
Checks consistency of all reading collection methods.
Overrides:
assertConsistency in class ObjectTest
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.

performIntegerTest

public void performIntegerTest()
Method to test collection implementations that support integers. Fills the specified collection with three elements and compares it to a ArrayList with the same elements.
Throws:
AssertionFailedError - if the test object does not pass the test.