Added a bunch of generic tests to the TestMap.java class. Also added

cooresponding changes to other tests.  These tests expose a few deviations from
the Map contract in BeanMap.  These should either be fixed, or BeanMap should be
documented to describe how it differs from the Map contract.


git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/collections/trunk@130552 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Michael Smith 2002-02-22 02:18:50 +00:00
parent a87ee3a6f5
commit 95ccabfc7c
12 changed files with 1051 additions and 110 deletions

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestBeanMap.java,v 1.1 2002/02/20 23:33:23 morgand Exp $
* $Revision: 1.1 $
* $Date: 2002/02/20 23:33:23 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestBeanMap.java,v 1.2 2002/02/22 02:18:50 mas Exp $
* $Revision: 1.2 $
* $Date: 2002/02/22 02:18:50 $
*
* ====================================================================
*
@ -79,8 +79,186 @@ public class TestBeanMap extends TestMap {
return new TestSuite(TestBeanMap.class);
}
public Map makeMap() {
return new BeanMap();
/*
note to self. The getter and setter methods were generated by copying the
field declarations and using the following regular expression search and
replace:
From:
private \(.*\) some\(.*\);
To:
public \1 getSome\2Value() {
return some\2;
}
public void setSome\2Value(\1 value) {
some\2 = value;
}
Also note: The sample keys and mappings were generated manually.
*/
public static class BeanWithProperties {
private int someInt;
private long someLong;
private double someDouble;
private float someFloat;
private short someShort;
private byte someByte;
private char someChar;
private Integer someInteger;
private String someString;
private Object someObject;
public int getSomeIntValue() {
return someInt;
}
public void setSomeIntValue(int value) {
someInt = value;
}
public long getSomeLongValue() {
return someLong;
}
public void setSomeLongValue(long value) {
someLong = value;
}
public double getSomeDoubleValue() {
return someDouble;
}
public void setSomeDoubleValue(double value) {
someDouble = value;
}
public float getSomeFloatValue() {
return someFloat;
}
public void setSomeFloatValue(float value) {
someFloat = value;
}
public short getSomeShortValue() {
return someShort;
}
public void setSomeShortValue(short value) {
someShort = value;
}
public byte getSomeByteValue() {
return someByte;
}
public void setSomeByteValue(byte value) {
someByte = value;
}
public char getSomeCharValue() {
return someChar;
}
public void setSomeCharValue(char value) {
someChar = value;
}
public String getSomeStringValue() {
return someString;
}
public void setSomeStringValue(String value) {
someString = value;
}
public Integer getSomeIntegerValue() {
return someInteger;
}
public void setSomeIntegerValue(Integer value) {
someInteger = value;
}
public Object getSomeObjectValue() {
return someObject;
}
public void setSomeObjectValue(Object value) {
someObject = value;
}
}
/*
note to self. The Sample keys were generated by copying the field
declarations and using the following regular expression search and replace:
From:
private \(.*\) some\(.*\);
To:
"some\2Value",
Then, I manually added the "class" key, which is a property that exists for
all beans (and all objects for that matter.
*/
public Object[] getSampleKeys() {
Object[] keys = new Object[] {
"someIntValue",
"someLongValue",
"someDoubleValue",
"someFloatValue",
"someShortValue",
"someByteValue",
"someCharValue",
"someIntegerValue",
"someStringValue",
"someObjectValue",
"class",
};
return keys;
}
/*
note to self: the sample values were created manually
*/
public Object[] getSampleValues() {
Object[] values = new Object[] {
new Integer(1234),
new Long(1298341928234L),
new Double(123423.34),
new Float(1213332.12f),
new Short((short)134),
new Byte((byte)10),
new Character('a'),
new Integer(1432),
"SomeStringValue",
new Object(),
BeanWithProperties.class,
};
return values;
}
public Object[] getNewSampleValues() {
Object[] values = new Object[] {
new Integer(223),
new Long(23341928234L),
new Double(23423.34),
new Float(213332.12f),
new Short((short)234),
new Byte((byte)20),
new Character('b'),
new Integer(232),
"SomeNewStringValue",
new Object(),
null,
};
return values;
}
public boolean isAddRemoveModifiable() {
return false;
}
public Map makeFullMap() {
return new BeanMap(new BeanWithProperties());
}
public Map makeEmptyMap() {
return new BeanMap();
}
}

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestDoubleOrderedMap.java,v 1.2 2002/02/20 23:48:13 morgand Exp $
* $Revision: 1.2 $
* $Date: 2002/02/20 23:48:13 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestDoubleOrderedMap.java,v 1.3 2002/02/22 02:18:50 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/02/22 02:18:50 $
*
* ====================================================================
*
@ -98,6 +98,32 @@ public class TestDoubleOrderedMap extends TestMap {
return new TestSuite(TestDoubleOrderedMap.class);
}
/**
* The default comparator in double ordered map does not allow null keys.
**/
public boolean useNullKey() {
return false;
}
/**
* The default comparator in double ordered map does not allow null keys,
* and values are keys in this map.
**/
public boolean useNullValue() {
return false;
}
/**
* Double ordered map does not support duplicate values
**/
public boolean useDuplicateValues() {
return false;
}
public Map makeEmptyMap() {
return new DoubleOrderedMap();
}
public Map makeMap() {
return new DoubleOrderedMap();
}

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestFastHashMap1.java,v 1.1 2001/04/21 12:22:30 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2001/04/21 12:22:30 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestFastHashMap1.java,v 1.2 2002/02/22 02:18:50 mas Exp $
* $Revision: 1.2 $
* $Date: 2002/02/22 02:18:50 $
*
* ====================================================================
*
@ -71,7 +71,7 @@ import java.util.Map;
* Test FastHashMap in <strong>fast</strong> mode.
*
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @version $Id: TestFastHashMap1.java,v 1.1 2001/04/21 12:22:30 craigmcc Exp $
* @version $Id: TestFastHashMap1.java,v 1.2 2002/02/22 02:18:50 mas Exp $
*/
public class TestFastHashMap1 extends TestFastHashMap
{
@ -97,6 +97,36 @@ public class TestFastHashMap1 extends TestFastHashMap
return (fhm);
}
/**
* When the fast hash map is in fast mode, the underlying hash map is
* cloned on modification (i.e. on a put). Because of that, any
* previously existing entry set will be representing the old (pre-clone)
* map and will not reflect changes made to the map after the clone. So,
* we must override this test.
**/
public void testEntrySetChangesWithMapPut() {
}
/**
* When the fast hash map is in fast mode, the underlying hash map is
* cloned on modification (i.e. on a remove). Because of that, any
* previously existing entry set will be representing the old (pre-clone)
* map and will not reflect changes made to the map after the clone. So,
* we must override this test.
**/
public void testEntrySetChangesWithMapRemove() {
}
/**
* When the fast hash map is in fast mode, the underlying hash map is
* cloned on modification (i.e. on a put). Because of that, any
* previously existing entry set will be representing the old (pre-clone)
* map, so changes to the set will not be seen in the map. So, we must
* override this test.
**/
public void testEntrySetRemoveCausesMapModification() {
}
public void setUp()
{
map = (HashMap) makeMap();

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestFastTreeMap.java,v 1.3 2001/04/21 12:22:30 craigmcc Exp $
* $Revision: 1.3 $
* $Date: 2001/04/21 12:22:30 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestFastTreeMap.java,v 1.4 2002/02/22 02:18:50 mas Exp $
* $Revision: 1.4 $
* $Date: 2002/02/22 02:18:50 $
*
* ====================================================================
*
@ -69,7 +69,7 @@ import java.util.TreeMap;
/**
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @version $Id: TestFastTreeMap.java,v 1.3 2001/04/21 12:22:30 craigmcc Exp $
* @version $Id: TestFastTreeMap.java,v 1.4 2002/02/22 02:18:50 mas Exp $
*/
public class TestFastTreeMap extends TestTreeMap
{
@ -94,6 +94,13 @@ public class TestFastTreeMap extends TestTreeMap
ftm.setFast(false);
return (ftm);
}
/**
* The comparator for the fast tree map does not support null keys.
**/
public boolean useNullKey() {
return false;
}
public void setUp()
{

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestFastTreeMap1.java,v 1.1 2001/04/21 12:22:30 craigmcc Exp $
* $Revision: 1.1 $
* $Date: 2001/04/21 12:22:30 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestFastTreeMap1.java,v 1.2 2002/02/22 02:18:50 mas Exp $
* $Revision: 1.2 $
* $Date: 2002/02/22 02:18:50 $
*
* ====================================================================
*
@ -71,7 +71,7 @@ import java.util.TreeMap;
* Test FastTreeMap in <strong>fast</strong> mode.
*
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @version $Id: TestFastTreeMap1.java,v 1.1 2001/04/21 12:22:30 craigmcc Exp $
* @version $Id: TestFastTreeMap1.java,v 1.2 2002/02/22 02:18:50 mas Exp $
*/
public class TestFastTreeMap1 extends TestFastTreeMap
{
@ -102,4 +102,33 @@ public class TestFastTreeMap1 extends TestFastTreeMap
map = (TreeMap) makeMap();
}
/**
* When the fast tree map is in fast mode, the underlying tree map is
* cloned on modification (i.e. on a put). Because of that, any
* previously existing entry set will be representing the old (pre-clone)
* map and will not reflect changes made to the map after the clone. So,
* we must override this test.
**/
public void testMapEntrySetChangesWithMapPut() {
}
/**
* When the fast tree map is in fast mode, the underlying tree map is
* cloned on modification (i.e. on a put). Because of that, any
* previously existing entry set will be representing the old (pre-clone)
* map and will not reflect changes made to the map after the clone. So,
* we must override this test.
**/
public void testMapEntrySetChangesWithMapRemove() {
}
/**
* When the fast tree map is in fast mode, the underlying tree map is
* cloned on modification (i.e. on a put). Because of that, any
* previously existing entry set will be representing the old (pre-clone)
* map, so changes to the set will not be seen in the map. So, we must
* override this test.
**/
public void testMapEntrySetRemoveCausesMapModification() {
}
}

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestHashMap.java,v 1.2 2001/07/14 23:33:27 craigmcc Exp $
* $Revision: 1.2 $
* $Date: 2001/07/14 23:33:27 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestHashMap.java,v 1.3 2002/02/22 02:18:50 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/02/22 02:18:50 $
*
* ====================================================================
*
@ -69,7 +69,7 @@ import java.util.Map;
/**
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @version $Id: TestHashMap.java,v 1.2 2001/07/14 23:33:27 craigmcc Exp $
* @version $Id: TestHashMap.java,v 1.3 2002/02/22 02:18:50 mas Exp $
*/
public class TestHashMap extends TestMap
{
@ -89,7 +89,7 @@ public class TestHashMap extends TestMap
junit.textui.TestRunner.main(testCaseName);
}
public Map makeMap() {
public Map makeEmptyMap() {
HashMap hm = new HashMap();
return (hm);
}
@ -98,7 +98,7 @@ public class TestHashMap extends TestMap
public void setUp()
{
map = (HashMap) makeMap();
map = (HashMap) makeEmptyMap();
}
public void testNewMap()

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestLRUMap.java,v 1.14 2002/02/20 22:38:46 morgand Exp $
* $Revision: 1.14 $
* $Date: 2002/02/20 22:38:46 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestLRUMap.java,v 1.15 2002/02/22 02:18:50 mas Exp $
* $Revision: 1.15 $
* $Date: 2002/02/22 02:18:50 $
*
* ====================================================================
*
@ -77,7 +77,7 @@ import java.util.HashMap;
*
* @author <a href="mailto:jstrachan@apache.org">James Strachan</a>
* @author <a href="mailto:morgand@apache.org">Morgan Delagrange</a>
* @version $Id: TestLRUMap.java,v 1.14 2002/02/20 22:38:46 morgand Exp $
* @version $Id: TestLRUMap.java,v 1.15 2002/02/22 02:18:50 mas Exp $
*/
public class TestLRUMap extends TestSequencedHashMap
{
@ -104,9 +104,21 @@ public class TestLRUMap extends TestSequencedHashMap
map2.put(new Integer(1),"foo");
map2.put(new Integer(2),"foo");
map2.put(new Integer(3),"foo");
map2.put(new Integer(4),"foo");
map2.put(new Integer(4),"foo"); // removes 1 since max size exceeded
map2.removeLRU(); // should be Integer(2)
assertTrue("last value should exist",map2.get(new Integer(4)).equals("foo"));
assertTrue("Second to last value should exist",map2.get(new Integer(3)).equals("foo"));
assertTrue("First value inserted should not exist", map2.get(new Integer(1)) == null);
}
public void testMultiplePuts() {
LRUMap map2 = new LRUMap(2);
map2.put(new Integer(1),"foo");
map2.put(new Integer(2),"bar");
map2.put(new Integer(3),"foo");
map2.put(new Integer(4),"bar");
assertTrue("last value should exist",map2.get(new Integer(4)).equals("bar"));
assertTrue("LRU should not exist", map2.get(new Integer(1)) == null);
}

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestMap.java,v 1.7 2002/02/21 20:14:37 morgand Exp $
* $Revision: 1.7 $
* $Date: 2002/02/21 20:14:37 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestMap.java,v 1.8 2002/02/22 02:18:50 mas Exp $
* $Revision: 1.8 $
* $Date: 2002/02/22 02:18:50 $
*
* ====================================================================
*
@ -64,20 +64,28 @@ package org.apache.commons.collections;
import junit.framework.*;
import java.util.Collection;
import java.util.Map;
import java.util.Collection;
import java.util.Set;
import java.util.Iterator;
import java.util.HashSet;
import java.util.NoSuchElementException;
/**
* Tests base {@link java.util.Map} methods and contracts.
* <p>
* To use, simply extend this class, and implement
* the {@link #makeMap} method.
* If your class implements the full Map interface, including optional
* operations, simply extend this class, and implement the {@link
* #makeEmptyMap()} method.
* <p>
* If your {@link Map} fails one of these tests by design,
* you may still use this base set of cases. Simply override the
* test case (method) your {@link Map} fails.
* If your {@link Map} fails one of these tests by design, you may still use
* this base set of cases. Simply override the test case (method) your {@link
* 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
* {@link useDuplicateValues()} and have it return <code>false</code>
*
* @author Michael Smith
* @author Rodney Waldhoff
* @version $Id: TestMap.java,v 1.7 2002/02/21 20:14:37 morgand Exp $
* @version $Id: TestMap.java,v 1.8 2002/02/22 02:18:50 mas Exp $
*/
public abstract class TestMap extends TestObject {
public TestMap(String testName) {
@ -85,53 +93,697 @@ public abstract class TestMap extends TestObject {
}
/**
* Return a new, empty {@link Map} to used for testing.
*/
public abstract Map makeMap();
public Object makeObject() {
return makeMap();
* Override if your map does not allow a <code>null</code> key. The
* default implementation returns <code>true</code>
**/
public boolean useNullKey() {
return true;
}
/**
* Try to put the given pair into the given Collection.
*
* Fails any Throwable except UnsupportedOperationException,
* ClassCastException, or IllegalArgumentException
* or NullPointerException is thrown.
/**
* Override if your map does not allow <code>null</code> values. The
* default implementation returns <code>true</code>.
**/
public boolean useNullValue() {
return true;
}
/**
* Override if your map does not allow duplicate values. The default
* implementation returns <code>true</code>.
**/
public boolean useDuplicateValues() {
return true;
}
/**
* Override if your map allows its mappings to be changed to new values.
* The default implementation returns <code>true</code>.
**/
public boolean isChangeable() {
return true;
}
/**
* Override if your map does not allow add/remove modifications. The
* default implementation returns <code>true</code>.
**/
public boolean isAddRemoveModifiable() {
return true;
}
/**
* Returns the set of keys in the mappings used to test the map. This
* method must return an array with the same length as {@link
* #getSampleValues()} and all array elements must be different. The
* default implementation constructs a set of String keys, and includes a
* single null key if {@link #useNullKey()} returns <code>true</code>.
**/
public 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 values in the mappings used to test the map. This
* method must return an array with the same length as {@link
* #getSampleKeys()}. The default implementation contructs a set of
* String values and includes a single null value if {@link
* #useNullValue()} returns <code>true</code>, and includes two values
* that are the same if {@link #useDuplicateValues()} returns
* <code>true</code>.
**/
public 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 a the set of values that can be used to replace the values
* returned from {@link #getSampleValues()}. This method must return an
* array with the same length as {@link #getSampleValues()}. The values
* returned from this method should not be the same as those returned from
* {@link #getSampleValues()}. The default implementation constructs a
* set of String values and includes a single null value if {@link
* #useNullValue()} returns <code>true</code>, and includes two values
* that are the same if {@link #useDuplicateValues()} returns
* <code>true</code>.
**/
public 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;
}
/**
* Helper method to add all the mappings described by {@link
* #getSampleKeys()} and {@link #getSampleValues()}.
**/
public 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());
}
/**
* Return a new, empty {@link Map} to be used for testing.
*/
protected Object tryToPut(Map map, Object key, Object val) {
public abstract Map makeEmptyMap();
/**
* Return a new, populated map. The mappings in the map should match the
* keys and values returned from {@linke #getSampleKeys()} and {@link
* #getSampleValues()}. The default implementation uses makeEmptyMap()
* and calls {@link #addSampleMappings()} to add all the mappings to the
* map.
**/
public Map makeFullMap() {
Map m = makeEmptyMap();
addSampleMappings(m);
return m;
}
public Object makeObject() {
return makeEmptyMap();
}
/**
* 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.
**/
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])));
}
}
// tests begin here. Each test adds a little bit of tested functionality.
// Many methods assume previous methods passed. That is, they do not
// exhaustively recheck things that have already been checked in a previous
// test methods.
/**
* Test to ensure that makeEmptyMap and makeFull returns a new non-null
* map with each invocation.
**/
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);
}
/**
* Tests Map.isEmpty()
**/
public void testIsEmpty() {
Map em = makeEmptyMap();
assertEquals("Map.isEmpty() should return true with an empty map",
true, em.isEmpty());
Map fm = makeFullMap();
assertEquals("Map.isEmpty() should return false with a non-empty map",
false, fm.isEmpty());
}
/**
* Tests Map.size()
**/
public void testSize() {
Map em = makeEmptyMap();
assertEquals("Map.size() should be 0 with an empty map",
0, em.size());
Map fm = makeFullMap();
assertEquals("Map.size() should equal the number of entries in the map",
getSampleKeys().length, fm.size());
}
/**
* Tests {@link Map#clear()}. If the map {@link #isAddRemoveModifiable()
* can add and remove elements}, then {@link Map#size()} and {@link
* 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. This method checks that the both maps
* returned by makeEmptyMap and makeFullMap have correct behavior.
**/
public void testClear() {
Map em = makeEmptyMap();
try {
return map.put(key,val);
} catch(UnsupportedOperationException e) {
return null;
} catch(ClassCastException e) {
return null;
} catch(IllegalArgumentException e) {
return null;
} catch(NullPointerException e) {
return null;
} catch(Throwable t) {
t.printStackTrace();
fail("Map.put should only throw UnsupportedOperationException, ClassCastException, IllegalArgumentException or NullPointerException. Found " + t.toString());
return null; // never get here, since fail throws exception
em.clear();
assertTrue("Map must throw UnsupportedOperationException if the " +
"map does not support removing elements",
isAddRemoveModifiable());
assertEquals("size() must return zero after clear.",
0, em.size());
assertEquals("isEmpty() must return true after clear.",
true, em.isEmpty());
} catch (UnsupportedOperationException exception) {
assertTrue("Map must not throw UnsupportedOperationException if the " +
"map supports removing elements", !isAddRemoveModifiable());
}
Map fm = makeFullMap();
try {
fm.clear();
assertTrue("Map must throw UnsupportedOperationException if the " +
"map does not support removing elements",
isAddRemoveModifiable());
assertEquals("size() must return zero after clear.",
0, fm.size());
assertEquals("isEmpty() must return true after clear.",
true, fm.isEmpty());
} catch (UnsupportedOperationException exception) {
assertTrue("Map must not throw UnsupportedOperationException if the " +
"map supports removing elements", !isAddRemoveModifiable());
}
}
/*
/**
* Tests:
* <ul>
* <li> Map.entrySet().isEmpty()
* <li> Map.entrySet().size()
* </ul>
**/
public void testEntrySetIsEmpty() {
Map em = makeEmptyMap();
Set es = em.entrySet();
assertEquals("entrySet() must return an empty set when map is empty.",
em.isEmpty(), es.isEmpty());
assertEquals("entrySet() must return a set with the same size as " +
"the map.", em.size(), es.size());
public void testMapContainsKey() {
// XXX finish me
Map fm = makeEmptyMap();
Set fs = fm.entrySet();
assertEquals("entrySet() must return a non-empty set when map is not empty.",
fm.isEmpty(), fs.isEmpty());
assertEquals("entrySet() must return a set with the same size as " +
"the map.", fm.size(), fs.size());
}
public void testMapContainsValue() {
// XXX finish me
/**
* Tests Map.containsKey(Object) by verifying it returns false for all
* sample keys on a map created using makeEmptyMap() and returns true for
* all sample keys returned on a map created using makeFullMap()
**/
public void testContainsKey() {
Object[] keys = getSampleKeys();
Map em = makeEmptyMap();
for(int i = 0; i < keys.length; i++) {
assertTrue("Map must not contain key when map is empty",
!em.containsKey(keys[i]));
}
Map fm = makeFullMap();
for(int i = 0; i < keys.length; i++) {
assertTrue("Map must contain key for a mapping in the map.",
fm.containsKey(keys[i]));
}
}
public void testMapEntrySet() {
// XXX finish me
/**
* Tests Map.containsValue(Object) by verifying it returns false for all
* sample alues on a map created using makeEmptyMap() and returns true for
* all sample values returned on a map created using makeFullMap.
**/
public void testContainsValue() {
Object[] values = getSampleValues();
Map em = makeEmptyMap();
for(int i = 0; i < values.length; i++) {
assertTrue("Empty map must not contain value",
!em.containsValue(values[i]));
}
Map fm = makeFullMap();
for(int i = 0; i < values.length; i++) {
assertTrue("Map must contain value for a mapping in the map.",
fm.containsValue(values[i]));
}
}
/**
* Test to ensure that Map.entrySet() returns a non-null set.
**/
public void testEntrySet() {
Map em = makeEmptyMap();
Set es = em.entrySet();
assertTrue("entrySet() must return a non-null set.", es != null);
Map fm = makeEmptyMap();
Set fs = fm.entrySet();
assertTrue("entrySet() must return a non-null set.", fs != null);
}
/**
* Tests:
* <ul>
* <li> Map.entrySet().contains(Object)
* <li> Map.entrySet().containsAll(Collection)
* </ul>
*
* Note: This test relies on a working commons.collections.DefaultMapEntry class.
**/
public void testEntrySetContainsProperMappings() {
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
Map.Entry[] entries = new Map.Entry[keys.length];
HashSet mappings = new HashSet();
for(int i = 0; i < keys.length; i++) {
entries[i] = new DefaultMapEntry(keys[i], values[i]);
mappings.add(entries[i]);
}
// test an empty map
Map em = makeEmptyMap();
Set es = em.entrySet();
for(int i = 0; i < keys.length; i++) {
assertEquals("entrySet().contains(Object) must return false when map " +
"is empty", false, es.contains(entries[i]));
}
assertEquals("entrySet().containsAll(Collection) must return false when the " +
"map is empty", false, es.containsAll(mappings));
Map fm = makeFullMap();
Set fs = fm.entrySet();
for(int i = 0; i < keys.length; i++) {
assertEquals("entrySet().contains(Object) must return true when map " +
"contains the mapping", true, fs.contains(entries[i]));
}
assertEquals("entrySet().containsAll(Collection) must return true when the " +
"map contains the mapping", true, fs.containsAll(mappings));
try {
es.containsAll((Collection)null);
fail("entrySet().containsAll(null) should " +
"throw a NullPointerException");
} catch (NullPointerException exception) {
// expected
}
try {
fs.containsAll((Collection)null);
fail("entrySet().containsAll(null) should " +
"throw a NullPointerException");
} catch (NullPointerException exception) {
// expected
}
}
// TODO: test entrySet().clear()
// TODO: test entrySet().add() throws OperationNotSupported
// TODO: test entrySet().addAll() throws OperationNotSupported
// TODO: test entrySet().contains(Object)
// TODO: test entrySet().containsAll(Collection)
// TODO: test entrySet().equals(Object)
// TODO: test entrySet().hashCode()
// TODO: test entrySet().toArray()
// TODO: test entrySet().toArray(Object[] a)
// TODO: test entrySet().remove(Object)
// TODO: test entrySet().removeAll(Collection)
// TODO: test entrySet().retainAll(Collection)
/**
* Tests:
* <ul>
* <li> Map.entrySet().iterator()
* <li> Map.entrySet().iterator().hasNext()
* <li> Map.entrySet().iterator().next()
* </ul>
**/
public void testEntrySetIterator() {
Map em = makeEmptyMap();
Set es = em.entrySet();
Iterator eiter = es.iterator();
assertEquals("entrySet().iterator().hasNext() must return false " +
"when then the map is empty.",
false, eiter.hasNext());
// note: we make a new map to test for this because some impls in the
// past have required a call to hasMoreElements before a call to next
// for it to work properly. By using a new map, we make sure this test
// will catch those broken impls.
em = makeEmptyMap();
es = em.entrySet();
eiter = es.iterator();
try {
eiter.next();
fail("entrySet().iterator().next() must throw a NoSuchElementException " +
"when the map is empty");
} catch (NoSuchElementException exception) {
// expected
}
Map fm = makeFullMap();
Set fs = fm.entrySet();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
boolean[] found = new boolean[keys.length];
Iterator iter = fs.iterator();
assertTrue("entrySet().iterator() must return a non-null " +
"iterator.", iter != null);
while(iter.hasNext()) {
Object obj = iter.next();
assertTrue("Null is not allowed to be returned from the " +
"entrySet().iterator()'s next().", obj != null);
assertTrue("Objects returned from entrySet().iterator() must be " +
"instances of Map.Entry.", obj instanceof Map.Entry);
Map.Entry entry = (Map.Entry)obj;
Object key = entry.getKey();
Object value = entry.getValue();
assertTrue("the key for an entry returned from the entry " +
"set's iterator can only be null if useNullKey " +
"is true.",
key != null || (key == null && useNullKey()));
assertTrue("the value for an entry returned from the entry " +
"set's iterator can only be null if useNullValue " +
"is true.",
value != null || (value == null && useNullValue()));
for(int i = 0; i < keys.length; i++) {
if((key == null && keys[i] == null) ||
(key != null && key.equals(keys[i]))) {
assertTrue("entrySet().iterator() must not return " +
"multiple entries with the same key.",
!found[i]);
found[i] = true;
assertTrue
("value of entry returned from iterator " +
"must be the value for the added mapping.",
(value == null && values[i] == null) ||
(value != null && value.equals(values[i])));
}
}
}
for(int i = 0; i < found.length; i++) {
assertTrue("must find all added elements through entrySet's " +
"iterator().", found[i]);
}
}
/**
* Tests Map.entrySet().iterator().remove()
**/
public void testEntrySetIteratorRemove() {
Map m = makeFullMap();
Set s = m.entrySet();
Iterator iter = s.iterator();
try {
iter.remove();
fail("Entry set iterator must not allow a call to remove " +
"before any calls to next");
} catch (IllegalStateException exception) {
// expected exception provided add/remove modifiable
assertTrue("iterator should throw UnsupportedOperationException " +
"if remove is not allowed from the entrySet().iterator()",
isAddRemoveModifiable());
} catch (UnsupportedOperationException exception) {
assertTrue("iterator should not throw UnsupportedOperationException " +
"if the map supports adding and removing elements",
!isAddRemoveModifiable());
}
while(iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
assertTrue("Entry key from entry set iterator must exist in map",
m.containsKey(entry.getKey()));
try {
iter.remove();
// note: we do not check that the mapping was actually removed
// from the map because some classes do not have their
// entrySet().iterator() backed by the map. That test occurs
// below in testEntrySetIteratorRemoveCausesMapModification
} catch (UnsupportedOperationException exception) {
assertTrue("iterator should not throw UnsupportedOperationException " +
"if the map supports adding and removing elements",
!isAddRemoveModifiable());
}
try {
iter.remove();
fail("Entry set iterator must not allow two calls to " +
"remove without a call to next.");
} catch (IllegalStateException exception) {
// expected exception provided add/remove modifiable
assertTrue("iterator should throw UnsupportedOperationException " +
"if remove is not allowed from the entrySet().iterator()",
isAddRemoveModifiable());
} catch (UnsupportedOperationException exception) {
assertTrue("iterator should not throw UnsupportedOperationException " +
"if the map supports adding and removing elements",
!isAddRemoveModifiable());
}
}
}
/**
* Tests whether the map's entrySet() is backed by the map by making sure
* a put in the map is reflected in the entrySet. This test does nothing
* if add/remove modifications are not supported.
**/
public void testEntrySetChangesWithMapPut() {
if(!isAddRemoveModifiable()) return;
Map m = makeEmptyMap();
// test insert reflected in entry set
Set s = m.entrySet();
addSampleMappings(m);
assertEquals("entrySet() must only be empty if map is empty.",
m.isEmpty(), s.isEmpty());
assertEquals("entrySet() must adjust size when map changes.",
m.size(), s.size());
// TODO: test set and map reflect the same contents
}
/**
* Tests whether the map's entrySet() is backed by the map by making sure
* a remove from the map is reflected in the entrySet. This test does nothing
* if add/remove modifications are not supported.
**/
public void testEntrySetChangesWithMapRemove() {
if(!isAddRemoveModifiable()) return;
Map m = makeFullMap();
Set s = m.entrySet();
Object[] keys = getSampleKeys();
Object[] values = getSampleValues();
for(int i = 0; i < keys.length; i++) {
m.remove(keys[i]);
assertEquals("entrySet() must only be empty if map is empty.",
m.isEmpty(), s.isEmpty());
assertEquals("entrySet() must adjust size when map changes.",
m.size(), s.size());
//TODO: test set and map reflect the same contents
}
}
// TODO: test entrySet() changes after Map.remove
// TODO: test entrySet() changes after Map.clear
// TODO: test entrySet() changes after Map.putAll
/**
* Tests whether the map's entrySet() is backed by the map by making sure
* a remove from the entrySet's iterator is reflected in the map. This
* test does nothing if add/remove modifications are not supported.
**/
public void testEntrySetIteratorRemoveCausesMapModification() {
if(!isAddRemoveModifiable()) return;
Map m = makeFullMap();
Set s = m.entrySet();
Iterator iter = s.iterator();
while(iter.hasNext()) {
Map.Entry entry = (Map.Entry)iter.next();
try {
iter.remove();
assertTrue("Entry key from entry set iterator must " +
"no longer exist in map",
!m.containsKey(entry.getKey()));
} catch (UnsupportedOperationException exception) {
// isAddRemoveModifiable is true -- we've checked that above
fail("iterator should not throw UnsupportedOperationException " +
"if the map supports adding and removing elements");
}
}
}
// TODO: test map changes after entrySet().remove
// TODO: test map changes after entrySet().removeAll
// TODO: test map changes after entrySet().retainAll
public void testMapEquals() {
// XXX finish me
}
@ -144,16 +796,10 @@ public abstract class TestMap extends TestObject {
// XXX finish me
}
public void testMapIsEmpty() {
// XXX finish me
}
public void testMapKeySet() {
// XXX finish me
}
*/
//-------TEST AGAINST OPTIONAL OPERATIONS, ENABLE IN TEST SUBCLASSES
public void testMapSupportsNullValues() {
@ -162,7 +808,7 @@ public abstract class TestMap extends TestObject {
return;
}
Map map = makeMap();
Map map = makeEmptyMap();
map.put(new Integer(1),"foo");
assertTrue("no null values in Map",map.containsValue(null) == false);
@ -179,7 +825,7 @@ public abstract class TestMap extends TestObject {
return;
}
Map map = makeMap();
Map map = makeEmptyMap();
map.put(new Integer(4),"foo");
map.put(new Integer(4),"bar");
map.put(new Integer(4),"foo");
@ -195,7 +841,7 @@ public abstract class TestMap extends TestObject {
return;
}
Map map = makeMap();
Map map = makeEmptyMap();
map.put(new Integer(1),"foo");
map.put(new Integer(2),"foo");
map.put(new Integer(3),"foo");
@ -211,7 +857,7 @@ public abstract class TestMap extends TestObject {
return;
}
Map map = makeMap();
Map map = makeEmptyMap();
map.put("1","1");
map.put("2","2");
map.put("3","3");
@ -235,7 +881,7 @@ public abstract class TestMap extends TestObject {
return;
}
Map map = makeMap();
Map map = makeEmptyMap();
map.put("1","1");
map.put("2","2");
map.put("3","3");
@ -278,16 +924,10 @@ public void testMapClear() {
// XXX finish me
}
public void testMapSize() {
// XXX finish me
}
public void testMapValues() {
// XXX finish me
}
*/
/**
* Marker interface, indicating that a TestMap subclass
* can test put(Object,Object) operations.

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestMultiHashMap.java,v 1.1 2001/09/18 10:41:39 jstrachan Exp $
* $Revision: 1.1 $
* $Date: 2001/09/18 10:41:39 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestMultiHashMap.java,v 1.2 2002/02/22 02:18:50 mas Exp $
* $Revision: 1.2 $
* $Date: 2002/02/22 02:18:50 $
*
* ====================================================================
*
@ -90,7 +90,7 @@ public class TestMultiHashMap extends TestMap
junit.textui.TestRunner.main(testCaseName);
}
public Map makeMap() {
public Map makeEmptyMap() {
return new MultiHashMap();
}
@ -223,4 +223,18 @@ public class TestMultiHashMap extends TestMap
return len;
}
public void testEntrySetIterator() {
}
public void testEntrySetContainsProperMappings() {
}
public void testEntrySetIteratorHasProperMappings() {
// override and ignore test -- it will fail when verifying the iterator for
// the set contains the right value -- we're not returning the value, we're
// returning a collection.
// TODO: re-implement this test to ensure the values of the iterator match
// the proper collection rather than the value the superclass is checking
// for.
return;
}
}

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestObject.java,v 1.2 2002/02/20 21:50:16 morgand Exp $
* $Revision: 1.2 $
* $Date: 2002/02/20 21:50:16 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/Attic/TestObject.java,v 1.3 2002/02/22 02:18:50 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/02/22 02:18:50 $
*
* ====================================================================
*
@ -83,14 +83,14 @@ import java.util.NoSuchElementException;
* Tests base {@link java.util.Object} methods and contracts.
* <p>
* To use, simply extend this class, and implement
* the {@link #makeObject} method.
* the {@link #makeObject()} method.
* <p>
* If your {@link Object} fails one of these tests by design,
* you may still use this base set of cases. Simply override the
* test case (method) your {@link Object} fails.
*
* @author Rodney Waldhoff
* @version $Id: TestObject.java,v 1.2 2002/02/20 21:50:16 morgand Exp $
* @version $Id: TestObject.java,v 1.3 2002/02/22 02:18:50 mas Exp $
*/
public abstract class TestObject extends TestCase {
public TestObject(String testName) {
@ -120,6 +120,7 @@ public abstract class TestObject extends TestCase {
Object obj2 = makeObject();
if(obj1.equals(obj2)) {
assertEquals("[2] When two objects are equal, their hashCodes should be also.",obj1.hashCode(),obj2.hashCode());
assertTrue("When obj1.equals(obj2) is true, then obj2.equals(obj1) should also be true", obj2.equals(obj1));
}
}

View File

@ -98,10 +98,10 @@ implements TestMap.SupportsPut, TestMap.EntrySetSupportsRemove
super.setUp();
// use makeMap and cast the result to a SeqHashMap
// so that subclasses of SeqHashMap can share these tests
labRat = (SequencedHashMap) makeMap();
labRat = (SequencedHashMap) makeEmptyMap();
}
public Map makeMap() {
public Map makeEmptyMap() {
return new SequencedHashMap();
}
@ -112,7 +112,7 @@ implements TestMap.SupportsPut, TestMap.EntrySetSupportsRemove
protected Object[] getValues() {
return new Object[] { "bar", "frob", new Object() };
}
public void testSequenceMap() throws Throwable {
Object[] keys = getKeys();
int expectedSize = keys.length;
@ -180,4 +180,4 @@ implements TestMap.SupportsPut, TestMap.EntrySetSupportsRemove
protected void tearDown() {
labRat = null;
}
}
}

View File

@ -1,7 +1,7 @@
/*
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestTreeMap.java,v 1.2 2001/07/14 23:33:27 craigmcc Exp $
* $Revision: 1.2 $
* $Date: 2001/07/14 23:33:27 $
* $Header: /home/jerenkrantz/tmp/commons/commons-convert/cvs/home/cvs/jakarta-commons//collections/src/test/org/apache/commons/collections/TestTreeMap.java,v 1.3 2002/02/22 02:18:50 mas Exp $
* $Revision: 1.3 $
* $Date: 2002/02/22 02:18:50 $
*
* ====================================================================
*
@ -69,7 +69,7 @@ import java.util.Map;
/**
* @author <a href="mailto:jvanzyl@apache.org">Jason van Zyl</a>
* @version $Id: TestTreeMap.java,v 1.2 2001/07/14 23:33:27 craigmcc Exp $
* @version $Id: TestTreeMap.java,v 1.3 2002/02/22 02:18:50 mas Exp $
*/
public class TestTreeMap extends TestMap
{
@ -89,16 +89,20 @@ public class TestTreeMap extends TestMap
junit.textui.TestRunner.main(testCaseName);
}
public Map makeMap() {
public Map makeEmptyMap() {
TreeMap tm = new TreeMap();
return (tm);
}
public boolean useNullKey() {
return false;
}
protected TreeMap map = null;
public void setUp()
{
map = (TreeMap) makeMap();
map = (TreeMap) makeEmptyMap();
}
public void testNewMap()