| Method from org.apache.commons.collections.TestMap Detail: |
protected void addSampleMappings(Map m) {
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
for(int i = 0; i < keys.length; i++) {
try {
m.put(keys[i], values[i]);
} catch (NullPointerException exception) {
assertTrue("NullPointerException only allowed to be thrown " +
"if either the key or value is null.",
keys[i] == null || values[i] == null);
assertTrue("NullPointerException on null key, but " +
"useNullKey is not overridden to return false.",
keys[i] == null || !useNullKey());
assertTrue("NullPointerException on null value, but " +
"useNullValue is not overridden to return false.",
values[i] == null || !useNullValue());
assertTrue("Unknown reason for NullPointer.", false);
}
}
assertEquals("size must reflect number of mappings added.",
keys.length, m.size());
}
|
public BulkTest bulkTestMapEntrySet() {
return new TestMapEntrySet();
}
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() {
return new TestMapKeySet();
}
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() {
return new TestMapValues();
}
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() {
Object[] result = new Object[] {
(useNullValue()) ? null : "newnonnullvalue",
"newvalue",
(useDuplicateValues()) ? "newvalue" : "newvalue2",
"newblahv", "newfoov", "newbarv", "newbazv", "newtmpv", "newgoshv",
"newgollyv", "newgeev", "newhellov", "newgoodbyev", "newwe'llv",
"newseev", "newyouv", "newallv", "newagainv",
};
return result;
}
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() {
return TestCollection.getOtherNonNullStringElements();
}
|
protected Object[] getOtherValues() {
return TestCollection.getOtherNonNullStringElements();
}
|
protected Object[] getSampleKeys() {
Object[] result = new Object[] {
"blah", "foo", "bar", "baz", "tmp", "gosh", "golly", "gee",
"hello", "goodbye", "we'll", "see", "you", "all", "again",
"key",
"key2",
(useNullKey()) ? null : "nonnullkey"
};
return result;
}
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() {
Object[] result = new Object[] {
"blahv", "foov", "barv", "bazv", "tmpv", "goshv", "gollyv", "geev",
"hellov", "goodbyev", "we'llv", "seev", "youv", "allv", "againv",
(useNullValue()) ? null : "nonnullvalue",
"value",
(useDuplicateValues()) ? "value" : "value2",
};
return result;
}
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() {
return true;
}
Override if your map does not allow add/remove modifications. The
default implementation returns true. |
protected boolean isChangeable() {
return true;
}
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() {
Map m = makeEmptyMap();
addSampleMappings(m);
return m;
}
|
public Object makeObject() {
return makeEmptyMap();
}
|
protected void resetEmpty() {
this.map = makeEmptyMap();
views();
this.confirmed = new HashMap();
}
|
protected void resetFull() {
this.map = makeFullMap();
views();
this.confirmed = new HashMap();
Object[] k = getSampleKeys();
Object[] v = getSampleValues();
for (int i = 0; i < k.length; i++) {
confirmed.put(k[i], v[i]);
}
}
|
protected void tearDown() {
map = null;
keySet = null;
entrySet = null;
values = null;
confirmed = null;
}
Erases any leftover instance variables by setting them to null. |
public void testEmptyMapCompatibility() throws ClassNotFoundException, IOException {
/**
* Create canonical objects with this code
Map map = makeEmptyMap();
if (!(map instanceof Serializable)) return;
writeExternalFormToDisk((Serializable) map, getCanonicalEmptyCollectionName(map));
*/
// test to make sure the canonical form has been preserved
if (!(makeEmptyMap() instanceof Serializable)) return;
Map map = (Map) readExternalFormFromDisk(getCanonicalEmptyCollectionName(makeEmptyMap()));
assertTrue("Map is empty",map.isEmpty() == true);
}
Compare the current serialized form of the Map
against the canonical version in CVS. |
public void testFullMapCompatibility() throws ClassNotFoundException, IOException {
/**
* Create canonical objects with this code
Map map = makeFullMap();
if (!(map instanceof Serializable)) return;
writeExternalFormToDisk((Serializable) map, getCanonicalFullCollectionName(map));
*/
// test to make sure the canonical form has been preserved
if (!(makeFullMap() instanceof Serializable)) return;
Map map = (Map) readExternalFormFromDisk(getCanonicalFullCollectionName(makeFullMap()));
assertEquals("Map is the right size",map.size(), getSampleKeys().length);
}
Compare the current serialized form of the Map
against the canonical version in CVS. |
public void testMakeMap() {
Map em = makeEmptyMap();
assertTrue("failure in test: makeEmptyMap must return a non-null map.",
em != null);
Map em2 = makeEmptyMap();
assertTrue("failure in test: makeEmptyMap must return a non-null map.",
em != null);
assertTrue("failure in test: makeEmptyMap must return a new map " +
"with each invocation.", em != em2);
Map fm = makeFullMap();
assertTrue("failure in test: makeFullMap must return a non-null map.",
fm != null);
Map fm2 = makeFullMap();
assertTrue("failure in test: makeFullMap must return a non-null map.",
fm != null);
assertTrue("failure in test: makeFullMap must return a new map " +
"with each invocation.", fm != fm2);
}
Test to ensure that makeEmptyMap and makeFull returns a new non-null
map with each invocation. |
public void testMapClear() {
if (!isAddRemoveModifiable()) return;
resetEmpty();
map.clear();
confirmed.clear();
verify();
resetFull();
map.clear();
confirmed.clear();
verify();
}
|
public void testMapContainsKey() {
Object[] keys = getSampleKeys();
resetEmpty();
for(int i = 0; i < keys.length; i++) {
assertTrue("Map must not contain key when map is empty",
!map.containsKey(keys[i]));
}
verify();
resetFull();
for(int i = 0; i < keys.length; i++) {
assertTrue("Map must contain key for a mapping in the map. " +
"Missing: " + keys[i], map.containsKey(keys[i]));
}
verify();
}
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() {
Object[] values = getSampleValues();
resetEmpty();
for(int i = 0; i < values.length; i++) {
assertTrue("Empty map must not contain value",
!map.containsValue(values[i]));
}
verify();
resetFull();
for(int i = 0; i < values.length; i++) {
assertTrue("Map must contain value for a mapping in the map.",
map.containsValue(values[i]));
}
verify();
}
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() {
resetEmpty();
assertTrue("Empty maps unequal.", map.equals(confirmed));
verify();
resetFull();
assertTrue("Full maps unequal.", map.equals(confirmed));
verify();
resetFull();
// modify the HashMap created from the full map and make sure this
// change results in map.equals() to return false.
Iterator iter = confirmed.keySet().iterator();
iter.next();
iter.remove();
assertTrue("Different maps equal.", !map.equals(confirmed));
resetFull();
assertTrue("equals(null) returned true.", !map.equals(null));
assertTrue("equals(new Object()) returned true.",
!map.equals(new Object()));
verify();
}
|
public void testMapGet() {
resetEmpty();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
for (int i = 0; i < keys.length; i++) {
assertTrue("Empty map.get() should return null.",
map.get(keys[i]) == null);
}
verify();
resetFull();
for (int i = 0; i < keys.length; i++) {
assertEquals("Full map.get() should return value from mapping.",
values[i], map.get(keys[i]));
}
}
|
public void testMapHashCode() {
resetEmpty();
assertTrue("Empty maps have different hashCodes.",
map.hashCode() == confirmed.hashCode());
resetFull();
assertTrue("Equal maps have different hashCodes.",
map.hashCode() == confirmed.hashCode());
}
|
public void testMapIsEmpty() {
resetEmpty();
assertEquals("Map.isEmpty() should return true with an empty map",
true, map.isEmpty());
verify();
resetFull();
assertEquals("Map.isEmpty() should return false with a non-empty map",
false, map.isEmpty());
verify();
}
|
public void testMapPut() {
if (!isAddRemoveModifiable()) return;
resetEmpty();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
Object[] newValues = getNewSampleValues();
for(int i = 0; i < keys.length; i++) {
Object o = map.put(keys[i], values[i]);
confirmed.put(keys[i], values[i]);
verify();
assertTrue("First map.put should return null", o == null);
assertTrue("Map should contain key after put",
map.containsKey(keys[i]));
assertTrue("Map should contain value after put",
map.containsValue(values[i]));
}
for(int i = 0; i < keys.length; i++) {
Object o = map.put(keys[i], newValues[i]);
confirmed.put(keys[i], newValues[i]);
verify();
assertEquals("Second map.put should return previous value",
values[i], o);
assertTrue("Map should still contain key after put",
map.containsKey(keys[i]));
assertTrue("Map should contain new value after put",
map.containsValue(newValues[i]));
// if duplicates are allowed, we're not guarunteed that the value
// no longer exists, so don't try checking that.
if(!useDuplicateValues()) {
assertTrue("Map should not contain old value after second put",
!map.containsValue(values[i]));
}
}
}
Tests Map.put(Object, Object) |
public void testMapPutAll() {
if (!isAddRemoveModifiable()) return;
resetEmpty();
Map m2 = makeFullMap();
map.putAll(m2);
confirmed.putAll(m2);
verify();
resetEmpty();
m2 = new HashMap();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
for(int i = 0; i < keys.length; i++) {
m2.put(keys[i], values[i]);
}
map.putAll(m2);
confirmed.putAll(m2);
verify();
}
Tests Map.putAll(Collection) |
public void testMapRemove() {
if (!isAddRemoveModifiable()) return;
resetEmpty();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
for(int i = 0; i < keys.length; i++) {
Object o = map.remove(keys[i]);
assertTrue("First map.remove should return null", o == null);
}
verify();
resetFull();
for(int i = 0; i < keys.length; i++) {
Object o = map.remove(keys[i]);
confirmed.remove(keys[i]);
verify();
assertEquals("map.remove with valid key should return value",
values[i], o);
}
Object[] other = getOtherKeys();
resetFull();
int size = map.size();
for (int i = 0; i < other.length; i++) {
Object o = map.remove(other[i]);
assertEquals("map.remove for nonexistent key should return null",
o, null);
assertEquals("map.remove for nonexistent key should not " +
"shrink map", size, map.size());
}
verify();
}
|
public void testMapSize() {
resetEmpty();
assertEquals("Map.size() should be 0 with an empty map",
0, map.size());
verify();
resetFull();
assertEquals("Map.size() should equal the number of entries " +
"in the map", getSampleKeys().length, map.size());
verify();
}
|
public void testMapToString() {
resetEmpty();
assertTrue("Empty map toString() should not return null",
map.toString() != null);
verify();
resetFull();
assertTrue("Empty map toString() should not return null",
map.toString() != null);
verify();
}
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() {
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
Object[] newValues = getNewSampleValues();
assertTrue("failure in test: Must have keys returned from " +
"getSampleKeys.", keys != null);
assertTrue("failure in test: Must have values returned from " +
"getSampleValues.", values != null);
// verify keys and values have equivalent lengths (in case getSampleX are
// overridden)
assertEquals("failure in test: not the same number of sample " +
"keys and values.", keys.length, values.length);
assertEquals("failure in test: not the same number of values and new values.",
values.length, newValues.length);
// verify there aren't duplicate keys, and check values
for(int i = 0; i < keys.length - 1; i++) {
for(int j = i + 1; j < keys.length; j++) {
assertTrue("failure in test: duplicate null keys.",
(keys[i] != null || keys[j] != null));
assertTrue("failure in test: duplicate non-null key.",
(keys[i] == null || keys[j] == null ||
(!keys[i].equals(keys[j]) &&
!keys[j].equals(keys[i]))));
}
assertTrue("failure in test: found null key, but useNullKey " +
"is false.", keys[i] != null || useNullKey());
assertTrue("failure in test: found null value, but useNullValue " +
"is false.", values[i] != null || useNullValue());
assertTrue("failure in test: found null new value, but useNullValue " +
"is false.", newValues[i] != null || useNullValue());
assertTrue("failure in test: values should not be the same as new value",
values[i] != newValues[i] &&
(values[i] == null || !values[i].equals(newValues[i])));
}
}
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() {
return true;
}
Override if your map does not allow duplicate values. The default
implementation returns true. |
protected boolean useNullKey() {
return true;
}
Override if your map does not allow a null key. The
default implementation returns true |
protected boolean useNullValue() {
return true;
}
Override if your map does not allow null values. The
default implementation returns true. |
protected void verify() {
verifyMap();
verifyEntrySet();
verifyKeySet();
verifyValues();
}
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() {
int size = confirmed.size();
boolean empty = confirmed.isEmpty();
assertEquals("entrySet should be same size as HashMap's",
size, entrySet.size());
assertEquals("entrySet should be empty if HashMap is",
empty, entrySet.isEmpty());
assertTrue("entrySet should contain all HashMap's elements",
entrySet.containsAll(confirmed.entrySet()));
assertEquals("entrySet hashCodes should be the same",
confirmed.entrySet().hashCode(), entrySet.hashCode());
assertEquals("Map's entry set should still equal HashMap's",
confirmed.entrySet(), entrySet);
}
|
protected void verifyKeySet() {
int size = confirmed.size();
boolean empty = confirmed.isEmpty();
assertEquals("keySet should be same size as HashMap's",
size, keySet.size());
assertEquals("keySet should be empty if HashMap is",
empty, keySet.isEmpty());
assertTrue("keySet should contain all HashMap's elements",
keySet.containsAll(confirmed.keySet()));
assertEquals("keySet hashCodes should be the same",
confirmed.keySet().hashCode(), keySet.hashCode());
assertEquals("Map's key set should still equal HashMap's",
confirmed.keySet(), keySet);
}
|
protected void verifyMap() {
int size = confirmed.size();
boolean empty = confirmed.isEmpty();
assertEquals("Map should be same size as HashMap",
size, map.size());
assertEquals("Map should be empty if HashMap is",
empty, map.isEmpty());
assertEquals("hashCodes should be the same",
confirmed.hashCode(), map.hashCode());
// this fails for LRUMap because confirmed.equals() somehow modifies
// map, causing concurrent modification exceptions.
//assertEquals("Map should still equal HashMap", confirmed, map);
// this works though and performs the same verification:
assertTrue("Map should still equal HashMap", map.equals(confirmed));
// TODO: this should really be rexamined to figure out why LRU map
// behaves like it does (the equals shouldn't modify since all accesses
// by the confirmed collection should be through an iterator, thus not
// causing LRUMap to change).
}
|
protected void verifyValues() {
Bag bag1 = new HashBag(confirmed.values());
Bag bag2 = new HashBag(values);
int size = confirmed.size();
boolean empty = confirmed.isEmpty();
assertEquals("values should be same size as HashMap's",
size, values.size());
assertEquals("values should be empty if HashMap is",
empty, values.isEmpty());
assertTrue("values should contain all HashMap's elements",
values.containsAll(confirmed.values()));
assertEquals("Map's values should still equal HashMap's",
bag1, bag2);
}
|