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

All Implemented Interfaces:
    Cloneable

Direct Known Subclasses:
    TestFastHashMap, TestBeanMap, TestReferenceMap, TestSequencedHashMap, TestMultiHashMap, TestFastHashMap1, TestLRUMap, TestFastTreeMap, TestFastTreeMap1, TestStaticBucketMap, TestTreeMap, TestProxyMap, TestDoubleOrderedMap

Tests base java.util.Map methods and contracts.

The forces at work here are similar to those in TestCollection . If your class implements the full Map interface, including optional operations, simply extend this class, and implement the #makeEmptyMap() method.

On the other hand, if your map implemenation is wierd, you may have to override one or more of the other protected methods. They're described below.

Entry Population Methods

Override these methods if your map requires special entries:

Supported Operation Methods

Override these methods if your map doesn't support certain operations:

Fixture Methods

For tests on modification operations (puts and removes), fixtures are used to verify that that operation results in correct state for the map and its collection views. Basically, the modification is performed against your map implementation, and an identical modification is performed against a confirmed map implementation. A confirmed map implementation is something like java.util.HashMap, which is known to conform exactly to the Map contract. After the modification takes place on both your map implementation and the confirmed map implementation, the two maps are compared to see if their state is identical. The comparison also compares the collection views to make sure they're still the same.

The upshot of all that is that any test that modifies the map in any way will verify that all of the map's state is still correct, including the state of its collection views. So for instance if a key is removed by the map's key set's iterator, then the entry set is checked to make sure the key/value pair no longer appears.

The #map field holds an instance of your collection implementation. The #entrySet , #keySet and #values fields hold that map's collection views. And the #confirmed field holds an instance of the confirmed collection implementation. The #resetEmpty() and #resetFull() methods set these fields to empty or full maps, so that tests can proceed from a known state.

After a modification operation to both #map and #confirmed , the #verify() method is invoked to compare the results. The verify() method calls separate methods to verify the map and its three collection views ({@link verifyEntrySet() , verifyKeySet() , and verifyValues() ). You may want to override one of the verification methodsto perform additional verifications. For instance, TestDoubleOrderedMap would want override its #verifyValues() method to verify that the values are unique and in ascending order.

Other Notes

If your Map fails one of these tests by design, you may still use this base set of cases. Simply override the test case (method) your Map fails and/or the methods that define the assumptions used by the test cases. For example, if your map does not allow duplicate values, override #useDuplicateValues() and have it return false

Nested Class Summary:
class  TestMap.TestMapEntrySet   
class  TestMap.TestMapKeySet   
class  TestMap.TestMapValues   
Field Summary
protected  Map map    Map created by reset(). 
protected  Set entrySet    Entry set of map created by reset(). 
protected  Set keySet    Key set of map created by reset(). 
protected  Collection values    Values collection of map created by reset(). 
protected  Map confirmed    HashMap created by reset(). 
Fields inherited from org.apache.commons.collections.TestObject:
COLLECTIONS_MAJOR_VERSION
Fields inherited from org.apache.commons.collections.BulkTest:
verboseName
Constructor:
 public TestMap(String testName) 
Method from org.apache.commons.collections.TestMap Summary:
addSampleMappings,   bulkTestMapEntrySet,   bulkTestMapKeySet,   bulkTestMapValues,   getNewSampleValues,   getOtherKeys,   getOtherValues,   getSampleKeys,   getSampleValues,   isAddRemoveModifiable,   isChangeable,   makeEmptyMap,   makeFullMap,   makeObject,   resetEmpty,   resetFull,   tearDown,   testEmptyMapCompatibility,   testFullMapCompatibility,   testMakeMap,   testMapClear,   testMapContainsKey,   testMapContainsValue,   testMapEquals,   testMapGet,   testMapHashCode,   testMapIsEmpty,   testMapPut,   testMapPutAll,   testMapRemove,   testMapSize,   testMapToString,   testSampleMappings,   useDuplicateValues,   useNullKey,   useNullValue,   verify,   verifyEntrySet,   verifyKeySet,   verifyMap,   verifyValues
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.TestMap Detail:
 protected  void addSampleMappings(Map m) 
 public BulkTest bulkTestMapEntrySet() 
    Bulk test Map#entrySet() . This method runs through all of the tests in TestSet . After modification operations, #verify() is invoked to ensure that the map and the other collection views are still valid.
 public BulkTest bulkTestMapKeySet() 
    Bulk test Map#keySet() . This method runs through all of the tests in TestSet . After modification operations, #verify() is invoked to ensure that the map and the other collection views are still valid.
 public BulkTest bulkTestMapValues() 
    Bulk test Map#values() . This method runs through all of the tests in TestCollection . After modification operations, #verify() is invoked to ensure that the map and the other collection views are still valid.
 protected Object[] getNewSampleValues() 
    Returns a the set of values that can be used to replace the values returned from #getSampleValues() . This method must return an array with the same length as #getSampleValues() . The values returned from this method should not be the same as those returned from #getSampleValues() . The default implementation constructs a set of String values and includes a single null value if #useNullValue() returns true, and includes two values that are the same if #useDuplicateValues() returns true.
 protected Object[] getOtherKeys() 
 protected Object[] getOtherValues() 
 protected Object[] getSampleKeys() 
    Returns the set of keys in the mappings used to test the map. This method must return an array with the same length as #getSampleValues() and all array elements must be different. The default implementation constructs a set of String keys, and includes a single null key if #useNullKey() returns true.
 protected Object[] getSampleValues() 
    Returns the set of values in the mappings used to test the map. This method must return an array with the same length as #getSampleKeys() . The default implementation contructs a set of String values and includes a single null value if #useNullValue() returns true, and includes two values that are the same if #useDuplicateValues() returns true.
 protected boolean isAddRemoveModifiable() 
    Override if your map does not allow add/remove modifications. The default implementation returns true.
 protected boolean isChangeable() 
    Override if your map allows its mappings to be changed to new values. The default implementation returns true.
 abstract protected Map makeEmptyMap()
    Return a new, empty Map to be used for testing.
 protected Map makeFullMap() 
 public Object makeObject() 
 protected  void resetEmpty() 
 protected  void resetFull() 
 protected  void tearDown() 
    Erases any leftover instance variables by setting them to null.
 public  void testEmptyMapCompatibility() throws ClassNotFoundException, IOException 
    Compare the current serialized form of the Map against the canonical version in CVS.
 public  void testFullMapCompatibility() throws ClassNotFoundException, IOException 
    Compare the current serialized form of the Map against the canonical version in CVS.
 public  void testMakeMap() 
    Test to ensure that makeEmptyMap and makeFull returns a new non-null map with each invocation.
 public  void testMapClear() 
 public  void testMapContainsKey() 
    Tests Map.containsKey(Object) by verifying it returns false for all sample keys on a map created using an empty map and returns true for all sample keys returned on a full map.
 public  void testMapContainsValue() 
    Tests Map.containsValue(Object) by verifying it returns false for all sample values on an empty map and returns true for all sample values on a full map.
 public  void testMapEquals() 
    Tests Map.equals(Object)
 public  void testMapGet() 
    Tests Map.get(Object)
 public  void testMapHashCode() 
    Tests Map.hashCode()
 public  void testMapIsEmpty() 
    Tests Map.isEmpty()
 public  void testMapPut() 
    Tests Map.put(Object, Object)
 public  void testMapPutAll() 
    Tests Map.putAll(Collection)
 public  void testMapRemove() 
    Tests Map.remove(Object)
 public  void testMapSize() 
    Tests Map.size()
 public  void testMapToString() 
    Tests Map.toString(). Since the format of the string returned by the toString() method is not defined in the Map interface, there is no common way to test the results of the toString() method. Thereforce, it is encouraged that Map implementations override this test with one that checks the format matches any format defined in its API. This default implementation just verifies that the toString() method does not return null.
 public  void testSampleMappings() 
    Test to ensure the test setup is working properly. This method checks to ensure that the getSampleKeys and getSampleValues methods are returning results that look appropriate. That is, they both return a non-null array of equal length. The keys array must not have any duplicate values, and may only contain a (single) null key if useNullKey() returns true. The values array must only have a null value if useNullValue() is true and may only have duplicate values if useDuplicateValues() returns true.
 protected boolean useDuplicateValues() 
    Override if your map does not allow duplicate values. The default implementation returns true.
 protected boolean useNullKey() 
    Override if your map does not allow a null key. The default implementation returns true
 protected boolean useNullValue() 
    Override if your map does not allow null values. The default implementation returns true.
 protected  void verify() 
    Verifies that #map is still equal to #confirmed . This method checks that the map is equal to the HashMap, and that the map's collection views are still equal to the HashMap's collection views. An equals test is done on the maps and their collection views; their size and isEmpty results are compared; their hashCodes are compared; and containsAll tests are run on the collection views.
 protected  void verifyEntrySet() 
 protected  void verifyKeySet() 
 protected  void verifyMap() 
 protected  void verifyValues()