public abstract class AbstractTestMap extends AbstractTestObject
Map
methods and contracts.
The forces at work here are similar to those in AbstractTestCollection
.
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 implementation is weird, 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 MethodsOverride these methods if your map doesn't support certain operations:
isPutAddSupported()
isPutChangeSupported()
isSetValueSupported()
isRemoveSupported()
isGetStructuralModify()
isAllowDuplicateValues()
isAllowNullKey()
isAllowNullValue()
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 (verifyMap()
, 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
isAllowDuplicateValues()
and have it return false
Modifier and Type | Class and Description |
---|---|
class |
AbstractTestMap.TestMapEntrySet |
class |
AbstractTestMap.TestMapKeySet |
class |
AbstractTestMap.TestMapValues |
Modifier and Type | Field and Description |
---|---|
protected java.util.Map |
confirmed
HashMap created by reset().
|
protected java.util.Set |
entrySet
Entry set of map created by reset().
|
protected java.util.Set |
keySet
Key set of map created by reset().
|
protected java.util.Map |
map
Map created by reset().
|
protected java.util.Collection |
values
Values collection of map created by reset().
|
COLLECTIONS_MAJOR_VERSION
Constructor and Description |
---|
AbstractTestMap(java.lang.String testName)
JUnit constructor.
|
Modifier and Type | Method and Description |
---|---|
void |
addSampleMappings(java.util.Map m)
Helper method to add all the mappings described by
getSampleKeys() and getSampleValues() . |
BulkTest |
bulkTestMapEntrySet()
Bulk test
Map.entrySet() . |
BulkTest |
bulkTestMapKeySet()
Bulk test
Map.keySet() . |
BulkTest |
bulkTestMapValues()
Bulk test
Map.values() . |
java.util.Map.Entry |
cloneMapEntry(java.util.Map.Entry entry)
Creates a new Map Entry that is independent of the first and the map.
|
java.lang.String |
getCompatibilityVersion()
Gets the compatability version, needed for package access.
|
java.lang.Object[] |
getNewSampleValues()
Returns a the set of values that can be used to replace the values
returned from
getSampleValues() . |
java.lang.Object[] |
getOtherKeys() |
java.lang.Object[] |
getOtherNonNullStringElements()
Returns a list of string elements suitable for return by
getOtherKeys() or getOtherValues() . |
java.lang.Object[] |
getOtherValues() |
java.lang.Object[] |
getSampleKeys()
Returns the set of keys in the mappings used to test the map.
|
java.lang.Object[] |
getSampleValues()
Returns the set of values in the mappings used to test the map.
|
boolean |
isAllowDuplicateValues()
Returns true if the maps produced by
makeEmptyMap() and makeFullMap()
supports duplicate values. |
boolean |
isAllowNullKey()
Returns true if the maps produced by
makeEmptyMap() and makeFullMap()
supports null keys. |
boolean |
isAllowNullValue()
Returns true if the maps produced by
makeEmptyMap() and makeFullMap()
supports null values. |
boolean |
isGetStructuralModify()
Returns true if the maps produced by
makeEmptyMap() and makeFullMap()
can cause structural modification on a get(). |
boolean |
isPutAddSupported()
Returns true if the maps produced by
makeEmptyMap() and makeFullMap()
support the put and putAll operations
adding new mappings. |
boolean |
isPutChangeSupported()
Returns true if the maps produced by
makeEmptyMap() and makeFullMap()
support the put and putAll operations
changing existing mappings. |
boolean |
isRemoveSupported()
Returns true if the maps produced by
makeEmptyMap() and makeFullMap()
support the remove and clear operations. |
boolean |
isSetValueSupported()
Returns true if the maps produced by
makeEmptyMap() and makeFullMap()
support the setValue operation on entrySet entries. |
boolean |
isSubMapViewsSerializable()
Returns whether the sub map views of SortedMap are serializable.
|
java.util.Map |
makeConfirmedMap()
Override to return a map other than HashMap as the confirmed map.
|
abstract java.util.Map |
makeEmptyMap()
Return a new, empty
Map to be used for testing. |
java.util.Map |
makeFullMap()
Return a new, populated map.
|
java.lang.Object |
makeObject()
Implements the superclass method to return the map to be tested.
|
void |
resetEmpty()
|
void |
resetFull()
|
void |
tearDown()
Erases any leftover instance variables by setting them to null.
|
void |
testEmptyMapCompatibility()
Compare the current serialized form of the Map
against the canonical version in CVS.
|
void |
testEntrySetClearChangesMap()
Tests that the
Map.entrySet() collection is backed by
the underlying map for clear(). |
void |
testEntrySetContains1() |
void |
testEntrySetContains2() |
void |
testEntrySetContains3() |
void |
testEntrySetRemove1() |
void |
testEntrySetRemove2() |
void |
testEntrySetRemove3() |
void |
testFullMapCompatibility()
Compare the current serialized form of the Map
against the canonical version in CVS.
|
void |
testKeySetClearChangesMap()
Tests that the
Map.keySet() collection is backed by
the underlying map for clear(). |
void |
testKeySetRemoveChangesMap()
Tests that the
Map.keySet() set is backed by
the underlying map by removing from the keySet set
and testing if the key was removed from the map. |
void |
testMakeMap()
Test to ensure that makeEmptyMap and makeFull returns a new non-null
map with each invocation.
|
void |
testMapClear()
Tests
Map.clear() . |
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.
|
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.
|
void |
testMapEquals()
Tests Map.equals(Object)
|
void |
testMapGet()
Tests Map.get(Object)
|
void |
testMapHashCode()
Tests Map.hashCode()
|
void |
testMapIsEmpty()
Tests Map.isEmpty()
|
void |
testMapPut()
Tests Map.put(Object, Object)
|
void |
testMapPutAll()
Tests Map.putAll(map)
|
void |
testMapPutNullKey()
Tests Map.put(null, value)
|
void |
testMapPutNullValue()
Tests Map.put(null, value)
|
void |
testMapRemove()
Tests Map.remove(Object)
|
void |
testMapSize()
Tests Map.size()
|
void |
testMapToString()
Tests Map.toString().
|
void |
testSampleMappings()
Test to ensure the test setup is working properly.
|
void |
testValuesClearChangesMap()
Tests that the
Map.values() collection is backed by
the underlying map for clear(). |
void |
testValuesRemoveChangesMap()
Tests that the
Map.values() collection is backed by
the underlying map by removing from the values collection
and testing if the value was removed from the map. |
void |
verify()
|
void |
verifyEntrySet() |
void |
verifyKeySet() |
void |
verifyMap() |
void |
verifyValues() |
getCanonicalEmptyCollectionName, getCanonicalFullCollectionName, isEqualsCheckable, isTestSerialization, readExternalFormFromBytes, readExternalFormFromDisk, skipSerializedCanonicalTests, supportsEmptyCollections, supportsFullCollections, testCanonicalEmptyCollectionExists, testCanonicalFullCollectionExists, testEqualsNull, testObjectEqualsSelf, testObjectHashCodeEqualsContract, testObjectHashCodeEqualsSelfHashCode, testSerializeDeserializeThenCompare, testSimpleSerialization, writeExternalFormToBytes, writeExternalFormToDisk
clone, ignoredTests, makeSuite, toString
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, countTestCases, createResult, fail, fail, failNotEquals, failNotSame, failSame, format, getName, run, run, runBare, runTest, setName, setUp
protected java.util.Map map
protected java.util.Set entrySet
protected java.util.Set keySet
protected java.util.Collection values
protected java.util.Map confirmed
public AbstractTestMap(java.lang.String testName)
testName
- the test namepublic boolean isPutAddSupported()
makeEmptyMap()
and makeFullMap()
support the put
and putAll
operations
adding new mappings.
Default implementation returns true. Override if your collection class does not support put adding.
public boolean isPutChangeSupported()
makeEmptyMap()
and makeFullMap()
support the put
and putAll
operations
changing existing mappings.
Default implementation returns true. Override if your collection class does not support put changing.
public boolean isSetValueSupported()
makeEmptyMap()
and makeFullMap()
support the setValue
operation on entrySet entries.
Default implementation returns isPutChangeSupported(). Override if your collection class does not support setValue but does support put changing.
public boolean isRemoveSupported()
makeEmptyMap()
and makeFullMap()
support the remove
and clear
operations.
Default implementation returns true. Override if your collection class does not support removal operations.
public boolean isGetStructuralModify()
makeEmptyMap()
and makeFullMap()
can cause structural modification on a get(). The example is LRUMap.
Default implementation returns false. Override if your map class structurally modifies on get.
public boolean isSubMapViewsSerializable()
public boolean isAllowNullKey()
makeEmptyMap()
and makeFullMap()
supports null keys.
Default implementation returns true. Override if your collection class does not support null keys.
public boolean isAllowNullValue()
makeEmptyMap()
and makeFullMap()
supports null values.
Default implementation returns true. Override if your collection class does not support null values.
public boolean isAllowDuplicateValues()
makeEmptyMap()
and makeFullMap()
supports duplicate values.
Default implementation returns true. Override if your collection class does not support duplicate values.
public java.lang.Object[] getSampleKeys()
getSampleValues()
and all array elements must be different. The
default implementation constructs a set of String keys, and includes a
single null key if isAllowNullKey()
returns true
.public java.lang.Object[] getOtherKeys()
public java.lang.Object[] getOtherValues()
public java.lang.Object[] getOtherNonNullStringElements()
getOtherKeys()
or getOtherValues()
.
Override getOtherElements to returnthe results of this method if your collection does not support heterogenous elements or the null element.
public java.lang.Object[] getSampleValues()
getSampleKeys()
. The default implementation constructs a set of
String values and includes a single null value if
isAllowNullValue()
returns true
, and includes
two values that are the same if isAllowDuplicateValues()
returns
true
.public java.lang.Object[] getNewSampleValues()
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
isAllowNullValue()
returns true
, and includes two values
that are the same if isAllowDuplicateValues()
returns
true
.public void addSampleMappings(java.util.Map m)
getSampleKeys()
and getSampleValues()
.public abstract java.util.Map makeEmptyMap()
Map
to be used for testing.public java.util.Map makeFullMap()
getSampleKeys()
and
getSampleValues()
. The default implementation uses makeEmptyMap()
and calls addSampleMappings(java.util.Map)
to add all the mappings to the
map.public java.lang.Object makeObject()
makeObject
in class AbstractTestObject
public java.util.Map makeConfirmedMap()
public java.util.Map.Entry cloneMapEntry(java.util.Map.Entry entry)
public java.lang.String getCompatibilityVersion()
getCompatibilityVersion
in class AbstractTestObject
null
if this object shouldn't be
tested for compatibility with previous versions.public void testSampleMappings()
public void testMakeMap()
public void testMapIsEmpty()
public void testMapSize()
public void testMapClear()
Map.clear()
. If the map isRemoveSupported()
can add and remove elements}, then Map.size()
and
Map.isEmpty()
are used to ensure that map has no elements after
a call to clear. If the map does not support adding and removing
elements, this method checks to ensure clear throws an
UnsupportedOperationException.public void testMapContainsKey()
public void testMapContainsValue()
public void testMapEquals()
public void testMapGet()
public void testMapHashCode()
public void testMapToString()
public void testEmptyMapCompatibility() throws java.lang.Exception
java.lang.Exception
public void testFullMapCompatibility() throws java.lang.Exception
java.lang.Exception
public void testMapPut()
public void testMapPutNullKey()
public void testMapPutNullValue()
public void testMapPutAll()
public void testMapRemove()
public void testValuesClearChangesMap()
Map.values()
collection is backed by
the underlying map for clear().public void testKeySetClearChangesMap()
Map.keySet()
collection is backed by
the underlying map for clear().public void testEntrySetClearChangesMap()
Map.entrySet()
collection is backed by
the underlying map for clear().public void testEntrySetContains1()
public void testEntrySetContains2()
public void testEntrySetContains3()
public void testEntrySetRemove1()
public void testEntrySetRemove2()
public void testEntrySetRemove3()
public void testValuesRemoveChangesMap()
Map.values()
collection is backed by
the underlying map by removing from the values collection
and testing if the value was removed from the map.
We should really test the "vice versa" case--that values removed from the map are removed from the values collection--also, but that's a more difficult test to construct (lacking a "removeValue" method.)
See bug 9573.
public void testKeySetRemoveChangesMap()
Map.keySet()
set is backed by
the underlying map by removing from the keySet set
and testing if the key was removed from the map.public BulkTest bulkTestMapEntrySet()
Map.entrySet()
. This method runs through all of
the tests in AbstractTestSet
.
After modification operations, verify()
is invoked to ensure
that the map and the other collection views are still valid.AbstractTestSet
instance for testing the map's entry setpublic BulkTest bulkTestMapKeySet()
Map.keySet()
. This method runs through all of
the tests in AbstractTestSet
.
After modification operations, verify()
is invoked to ensure
that the map and the other collection views are still valid.AbstractTestSet
instance for testing the map's key setpublic BulkTest bulkTestMapValues()
Map.values()
. This method runs through all of
the tests in AbstractTestCollection
.
After modification operations, verify()
is invoked to ensure
that the map and the other collection views are still valid.AbstractTestCollection
instance for testing the map's
values collectionpublic void resetEmpty()
public void resetFull()
public void verify()
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.public void verifyMap()
public void verifyEntrySet()
public void verifyKeySet()
public void verifyValues()
public void tearDown() throws java.lang.Exception
tearDown
in class junit.framework.TestCase
java.lang.Exception
Copyright © 2001-1969 Apache Software Foundation. All Rights Reserved.