Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.
Also see the following revisions: ------------------------------------------------------------------------ r740150 | mbenson | 2009-02-02 15:24:00 -0800 (Mon, 02 Feb 2009) | 1 line make all [collections] maps implement IterableMap ------------------------------------------------------------------------ git-svn-id: https://svn.apache.org/repos/asf/commons/proper/collections/trunk@815131 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
78bb9be977
commit
47abb18690
|
@ -23,8 +23,9 @@ import java.util.Map;
|
|||
import junit.framework.Test;
|
||||
import junit.framework.TestSuite;
|
||||
|
||||
import org.apache.commons.collections.IterableMap;
|
||||
import org.apache.commons.collections.Predicate;
|
||||
import org.apache.commons.collections.PredicateUtils;
|
||||
import org.apache.commons.collections.functors.TruePredicate;
|
||||
|
||||
/**
|
||||
* Extension of {@link AbstractTestMap} for exercising the
|
||||
|
@ -35,66 +36,68 @@ import org.apache.commons.collections.PredicateUtils;
|
|||
*
|
||||
* @author Phil Steitz
|
||||
*/
|
||||
public class TestPredicatedMap extends AbstractTestMap{
|
||||
|
||||
protected static final Predicate truePredicate = PredicateUtils.truePredicate();
|
||||
protected static final Predicate testPredicate = new Predicate() {
|
||||
public class TestPredicatedMap<K, V> extends AbstractTestIterableMap<K, V> {
|
||||
|
||||
protected static final Predicate<Object> truePredicate = TruePredicate.<Object>truePredicate();
|
||||
|
||||
protected static final Predicate<Object> testPredicate = new Predicate<Object>() {
|
||||
public boolean evaluate(Object o) {
|
||||
return (o instanceof String);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
public TestPredicatedMap(String testName) {
|
||||
super(testName);
|
||||
}
|
||||
|
||||
|
||||
public static Test suite() {
|
||||
return new TestSuite(TestPredicatedMap.class);
|
||||
}
|
||||
|
||||
|
||||
public static void main(String args[]) {
|
||||
String[] testCaseName = { TestPredicatedMap.class.getName()};
|
||||
junit.textui.TestRunner.main(testCaseName);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
protected Map decorateMap(Map map, Predicate keyPredicate,
|
||||
Predicate valuePredicate) {
|
||||
protected IterableMap<K, V> decorateMap(Map<K, V> map, Predicate<? super K> keyPredicate,
|
||||
Predicate<? super V> valuePredicate) {
|
||||
return PredicatedMap.decorate(map, keyPredicate, valuePredicate);
|
||||
}
|
||||
|
||||
public Map makeEmptyMap() {
|
||||
return decorateMap(new HashMap(), truePredicate, truePredicate);
|
||||
|
||||
public IterableMap<K, V> makeObject() {
|
||||
return decorateMap(new HashMap<K, V>(), truePredicate, truePredicate);
|
||||
}
|
||||
|
||||
public Map makeTestMap() {
|
||||
return decorateMap(new HashMap(), testPredicate, testPredicate);
|
||||
|
||||
public IterableMap<K, V> makeTestMap() {
|
||||
return decorateMap(new HashMap<K, V>(), testPredicate, testPredicate);
|
||||
}
|
||||
|
||||
//-----------------------------------------------------------------------
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testEntrySet() {
|
||||
Map map = makeTestMap();
|
||||
Map<K, V> map = makeTestMap();
|
||||
assertTrue("returned entryset should not be null",
|
||||
map.entrySet() != null);
|
||||
map = decorateMap(new HashMap(), null, null);
|
||||
map.put("oneKey", "oneValue");
|
||||
map = decorateMap(new HashMap<K, V>(), null, null);
|
||||
map.put((K) "oneKey", (V) "oneValue");
|
||||
assertTrue("returned entryset should contain one entry",
|
||||
map.entrySet().size() == 1);
|
||||
map.entrySet().size() == 1);
|
||||
map = decorateMap(map, null, null);
|
||||
}
|
||||
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public void testPut() {
|
||||
Map map = makeTestMap();
|
||||
Map<K, V> map = makeTestMap();
|
||||
try {
|
||||
map.put("Hi", new Integer(3));
|
||||
map.put((K) "Hi", (V) new Integer(3));
|
||||
fail("Illegal value should raise IllegalArgument");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
try {
|
||||
map.put(new Integer(3), "Hi");
|
||||
map.put((K) new Integer(3), (V) "Hi");
|
||||
fail("Illegal key should raise IllegalArgument");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
|
@ -103,11 +106,11 @@ public class TestPredicatedMap extends AbstractTestMap{
|
|||
assertTrue(!map.containsKey(new Integer(3)));
|
||||
assertTrue(!map.containsValue(new Integer(3)));
|
||||
|
||||
Map map2 = new HashMap();
|
||||
map2.put("A", "a");
|
||||
map2.put("B", "b");
|
||||
map2.put("C", "c");
|
||||
map2.put("c", new Integer(3));
|
||||
Map<K, V> map2 = new HashMap<K, V>();
|
||||
map2.put((K) "A", (V) "a");
|
||||
map2.put((K) "B", (V) "b");
|
||||
map2.put((K) "C", (V) "c");
|
||||
map2.put((K) "c", (V) new Integer(3));
|
||||
|
||||
try {
|
||||
map.putAll(map2);
|
||||
|
@ -116,21 +119,21 @@ public class TestPredicatedMap extends AbstractTestMap{
|
|||
// expected
|
||||
}
|
||||
|
||||
map.put("E", "e");
|
||||
Iterator iterator = map.entrySet().iterator();
|
||||
map.put((K) "E", (V) "e");
|
||||
Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
|
||||
try {
|
||||
Map.Entry entry = (Map.Entry)iterator.next();
|
||||
entry.setValue(new Integer(3));
|
||||
Map.Entry<K, V> entry = iterator.next();
|
||||
entry.setValue((V) new Integer(3));
|
||||
fail("Illegal value should raise IllegalArgument");
|
||||
} catch (IllegalArgumentException e) {
|
||||
// expected
|
||||
}
|
||||
|
||||
map.put("F", "f");
|
||||
|
||||
map.put((K) "F", (V) "f");
|
||||
iterator = map.entrySet().iterator();
|
||||
Map.Entry entry = (Map.Entry)iterator.next();
|
||||
entry.setValue("x");
|
||||
|
||||
Map.Entry<K, V> entry = iterator.next();
|
||||
entry.setValue((V) "x");
|
||||
|
||||
}
|
||||
|
||||
public String getCompatibilityVersion() {
|
||||
|
@ -147,4 +150,4 @@ public class TestPredicatedMap extends AbstractTestMap{
|
|||
// (java.io.Serializable) map,
|
||||
// "D:/dev/collections/data/test/PredicatedMap.fullCollection.version3.1.obj");
|
||||
// }
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue