Save This Page
Home » commons-collections-3.2.1-src » org.apache.commons » collections » [javadoc | source]
org.apache.commons.collections
abstract public class: TestCollection [javadoc | source]
java.lang.Object
   junit.framework.TestCase
      org.apache.commons.collections.BulkTest
         org.apache.commons.collections.TestObject
            org.apache.commons.collections.TestCollection

All Implemented Interfaces:
    Cloneable

Direct Known Subclasses:
    TestMapValues, TestFastArrayList, TestBoundedFifoBuffer, TestFastArrayList1, TestUnboundedFifoBuffer, TestArrayStack, TestList, TestMapEntrySet, TestCursorableLinkedList, TestBoundedFifoBuffer2, TestSet, TestBinaryHeap, TestMapKeySet, TestArrayList, BulkTestSubList

Tests base java.util.Collection methods and contracts.

You should create a concrete subclass of this class to test any custom Collection implementation. At minimum, you'll have to implement the #makeCollection() method. You might want to override some of the additional protected methods as well:

Element Population Methods

Override these if your collection restricts what kind of elements are allowed (for instance, if null is not permitted):

Supported Operation Methods

Override these if your collection doesn't support certain operations:

Fixture Methods

Fixtures are used to verify that the the operation results in correct state for the collection. Basically, the operation is performed against your collection implementation, and an identical operation is performed against a confirmed collection implementation. A confirmed collection implementation is something like java.util.ArrayList, which is known to conform exactly to its collection interface's contract. After the operation takes place on both your collection implementation and the confirmed collection implementation, the two collections are compared to see if their state is identical. The comparison is usually much more involved than a simple equals test. This verification is used to ensure proper modifications are made along with ensuring that the collection does not change when read-only modifications are made.

The #collection field holds an instance of your collection implementation; the #confirmed field holds an instance of the confirmed collection implementation. The #resetEmpty() and #resetFull() methods set these fields to empty or full collections, so that tests can proceed from a known state.

After a modification operation to both #collection and #confirmed , the #verify() method is invoked to compare the results. You may want to override #verify() to perform additional verifications. For instance, when testing the collection views of a map, TestMap would override #verify() to make sure the map is changed after the collection view is changed. If you're extending this class directly, you will have to provide implementations for the following:

Those methods should provide a confirmed collection implementation that's compatible with your collection implementation.

If you're extending TestList , TestSet , or TestBag , you probably don't have to worry about the above methods, because those three classes already override the methods to provide standard JDK confirmed collections.

Other notes

If your Collection fails one of these tests by design, you may still use this base set of cases. Simply override the test case (method) your Collection fails. For instance, the #testIteratorFailFast() method is provided since most collections have fail-fast iterators; however, that's not strictly required by the collection contract, so you may want to override that method to do nothing.

Field Summary
protected  Collection collection    A collection instance that will be used for testing. 
protected  Collection confirmed    Confirmed collection. This is an instance of a collection that is confirmed to conform exactly to the java.util.Collection contract. Modification operations are tested by performing a mod on your collection, performing the exact same mod on an equivalent confirmed collection, and then calling verify() to make sure your collection still matches the confirmed collection. 
Fields inherited from org.apache.commons.collections.TestObject:
COLLECTIONS_MAJOR_VERSION
Fields inherited from org.apache.commons.collections.BulkTest:
verboseName
Constructor:
 public TestCollection(String testName) 
Method from org.apache.commons.collections.TestCollection Summary:
areEqualElementsDistinguishable,   getFullElements,   getFullNonNullElements,   getFullNonNullStringElements,   getOtherElements,   getOtherNonNullElements,   getOtherNonNullStringElements,   isAddSupported,   isRemoveSupported,   makeCollection,   makeConfirmedCollection,   makeConfirmedFullCollection,   makeFullCollection,   makeObject,   resetEmpty,   resetFull,   testCollectionAdd,   testCollectionAddAll,   testCollectionClear,   testCollectionContains,   testCollectionContainsAll,   testCollectionIsEmpty,   testCollectionIterator,   testCollectionIteratorFailFast,   testCollectionIteratorRemove,   testCollectionRemove,   testCollectionRemoveAll,   testCollectionRetainAll,   testCollectionSize,   testCollectionToArray,   testCollectionToArray2,   testCollectionToString,   testUnsupportedAdd,   testUnsupportedRemove,   verify
Methods from org.apache.commons.collections.TestObject:
getCanonicalEmptyCollectionName,   getCanonicalFullCollectionName,   getCompatibilityVersion,   makeObject,   readExternalFormFromBytes,   readExternalFormFromDisk,   supportsEmptyCollections,   supportsFullCollections,   testCanonicalEmptyCollectionExists,   testCanonicalFullCollectionExists,   testObjectEqualsSelf,   testObjectHashCodeEqualsContract,   testObjectHashCodeEqualsSelfHashCode,   testSimpleSerialization,   writeExternalFormToBytes,   writeExternalFormToDisk
Methods from org.apache.commons.collections.BulkTest:
clone,   ignoredSimpleTests,   makeSuite,   toString
Methods from java.lang.Object:
equals,   getClass,   hashCode,   notify,   notifyAll,   toString,   wait,   wait,   wait
Method from org.apache.commons.collections.TestCollection Detail:
 protected boolean areEqualElementsDistinguishable() 
    Specifies whether equal elements in the collection are, in fact, distinguishable with information not readily available. That is, if a particular value is to be removed from the collection, then there is one and only one value that can be removed, even if there are other elements which are equal to it.

    In most collection cases, elements are not distinguishable (equal is equal), thus this method defaults to return false. In some cases, however, they are. For example, the collection returned from the map's values() collection view are backed by the map, so while there may be two values that are equal, their associated keys are not. Since the keys are distinguishable, the values are.

    This flag is used to skip some verifications for iterator.remove() where it is impossible to perform an equivalent modification on the confirmed collection because it is not possible to determine which value in the confirmed collection to actually remove. Tests that override the default (i.e. where equal elements are distinguishable), should provide additional tests on iterator.remove() to make sure the proper elements are removed when remove() is called on the iterator.

 protected Object[] getFullElements() 
    Returns an array of objects that are contained in a collection produced by #makeFullCollection() . Every element in the returned array must be an element in a full collection.

    The default implementation returns a heterogenous array of objects with some duplicates and with the null element. Override if you require specific testing elements. Note that if you override #makeFullCollection() , you must override this method to reflect the contents of a full collection.

 public static Object[] getFullNonNullElements() 
    Returns a list of elements suitable for return by getFullElements() . The array returned by this method does not include null, but does include a variety of objects of different types. Override getFullElements to return the results of this method if your collection does not support the null element.
 public static Object[] getFullNonNullStringElements() 
    Returns a list of string elements suitable for return by getFullElements() . Override getFullElements to return the results of this method if your collection does not support heterogenous elements or the null element.
 protected Object[] getOtherElements() 
    Returns an array of elements that are not contained in a full collection. Every element in the returned array must not exist in a collection returned by #makeFullCollection() . The default implementation returns a heterogenous array of elements without null. Note that some of the tests add these elements to an empty or full collection, so if your collection restricts certain kinds of elements, you should override this method.
 public static Object[] getOtherNonNullElements() 
    Returns the default list of objects returned by getOtherElements() . Includes many objects of different types.
 public static Object[] getOtherNonNullStringElements() 
    Returns a list of string elements suitable for return by getOtherElements() . Override getOtherElements to return the results of this method if your collection does not support heterogenous elements or the null element.
 protected boolean isAddSupported() 
    Returns true if the collections produced by #makeCollection() and #makeFullCollection() support the add and addAll operations.

    Default implementation returns true. Override if your collection class does not support add or addAll.

 protected boolean isRemoveSupported() 
    Returns true if the collections produced by #makeCollection() and #makeFullCollection() support the remove, removeAll, retainAll, clear and iterator().remove() methods. Default implementation returns true. Override if your collection class does not support removal operations.
 abstract protected Collection makeCollection()
    Return a new, empty Collection to be used for testing.
 abstract protected Collection makeConfirmedCollection()
 abstract protected Collection makeConfirmedFullCollection()
 protected Collection makeFullCollection() 
    Returns a full collection to be used for testing. The collection returned by this method should contain every element returned by #getFullElements() . The default implementation, in fact, simply invokes addAll on an empty collection with the results of #getFullElements() . Override this default if your collection doesn't support addAll.
 public Object makeObject() 
    Returns an empty collection for Object tests.
 protected  void resetEmpty() 
    Resets the #collection and #confirmed fields to empty collections. Invoke this method before performing a modification test.
 protected  void resetFull() 
    Resets the #collection and #confirmed fields to full collections. Invoke this method before performing a modification test.
 public  void testCollectionAdd() 
 public  void testCollectionAddAll() 
 public  void testCollectionClear() 
 public  void testCollectionContains() 
 public  void testCollectionContainsAll() 
 public  void testCollectionIsEmpty() 
 public  void testCollectionIterator() 
 public  void testCollectionIteratorFailFast() 
    Tests that the collection's iterator is fail-fast.
 public  void testCollectionIteratorRemove() 
 public  void testCollectionRemove() 
 public  void testCollectionRemoveAll() 
 public  void testCollectionRetainAll() 
 public  void testCollectionSize() 
 public  void testCollectionToArray() 
 public  void testCollectionToArray2() 
 public  void testCollectionToString() 
    Tests toString on a collection.
 public  void testUnsupportedAdd() 
    If #isAddSupported() returns false, tests that add operations raise UnsupportedOperationException.
 public  void testUnsupportedRemove() 
    If isRemoveSupported() returns false, tests to see that remove operations raise an UnsupportedOperationException.
 protected  void verify()