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:
Henri Yandell 2009-09-15 05:57:36 +00:00
parent 78bb9be977
commit 47abb18690
1 changed files with 43 additions and 40 deletions

View File

@ -23,8 +23,9 @@ import java.util.Map;
import junit.framework.Test; import junit.framework.Test;
import junit.framework.TestSuite; import junit.framework.TestSuite;
import org.apache.commons.collections.IterableMap;
import org.apache.commons.collections.Predicate; 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 * Extension of {@link AbstractTestMap} for exercising the
@ -35,16 +36,16 @@ import org.apache.commons.collections.PredicateUtils;
* *
* @author Phil Steitz * @author Phil Steitz
*/ */
public class TestPredicatedMap extends AbstractTestMap{ public class TestPredicatedMap<K, V> extends AbstractTestIterableMap<K, V> {
protected static final Predicate truePredicate = PredicateUtils.truePredicate(); protected static final Predicate<Object> truePredicate = TruePredicate.<Object>truePredicate();
protected static final Predicate testPredicate = new Predicate() {
protected static final Predicate<Object> testPredicate = new Predicate<Object>() {
public boolean evaluate(Object o) { public boolean evaluate(Object o) {
return (o instanceof String); return (o instanceof String);
} }
}; };
public TestPredicatedMap(String testName) { public TestPredicatedMap(String testName) {
super(testName); super(testName);
} }
@ -59,42 +60,44 @@ public class TestPredicatedMap extends AbstractTestMap{
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
protected Map decorateMap(Map map, Predicate keyPredicate, protected IterableMap<K, V> decorateMap(Map<K, V> map, Predicate<? super K> keyPredicate,
Predicate valuePredicate) { Predicate<? super V> valuePredicate) {
return PredicatedMap.decorate(map, keyPredicate, valuePredicate); return PredicatedMap.decorate(map, keyPredicate, valuePredicate);
} }
public Map makeEmptyMap() { public IterableMap<K, V> makeObject() {
return decorateMap(new HashMap(), truePredicate, truePredicate); return decorateMap(new HashMap<K, V>(), truePredicate, truePredicate);
} }
public Map makeTestMap() { public IterableMap<K, V> makeTestMap() {
return decorateMap(new HashMap(), testPredicate, testPredicate); return decorateMap(new HashMap<K, V>(), testPredicate, testPredicate);
} }
//----------------------------------------------------------------------- //-----------------------------------------------------------------------
@SuppressWarnings("unchecked")
public void testEntrySet() { public void testEntrySet() {
Map map = makeTestMap(); Map<K, V> map = makeTestMap();
assertTrue("returned entryset should not be null", assertTrue("returned entryset should not be null",
map.entrySet() != null); map.entrySet() != null);
map = decorateMap(new HashMap(), null, null); map = decorateMap(new HashMap<K, V>(), null, null);
map.put("oneKey", "oneValue"); map.put((K) "oneKey", (V) "oneValue");
assertTrue("returned entryset should contain one entry", assertTrue("returned entryset should contain one entry",
map.entrySet().size() == 1); map.entrySet().size() == 1);
map = decorateMap(map, null, null); map = decorateMap(map, null, null);
} }
@SuppressWarnings("unchecked")
public void testPut() { public void testPut() {
Map map = makeTestMap(); Map<K, V> map = makeTestMap();
try { try {
map.put("Hi", new Integer(3)); map.put((K) "Hi", (V) new Integer(3));
fail("Illegal value should raise IllegalArgument"); fail("Illegal value should raise IllegalArgument");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
try { try {
map.put(new Integer(3), "Hi"); map.put((K) new Integer(3), (V) "Hi");
fail("Illegal key should raise IllegalArgument"); fail("Illegal key should raise IllegalArgument");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
@ -103,11 +106,11 @@ public class TestPredicatedMap extends AbstractTestMap{
assertTrue(!map.containsKey(new Integer(3))); assertTrue(!map.containsKey(new Integer(3)));
assertTrue(!map.containsValue(new Integer(3))); assertTrue(!map.containsValue(new Integer(3)));
Map map2 = new HashMap(); Map<K, V> map2 = new HashMap<K, V>();
map2.put("A", "a"); map2.put((K) "A", (V) "a");
map2.put("B", "b"); map2.put((K) "B", (V) "b");
map2.put("C", "c"); map2.put((K) "C", (V) "c");
map2.put("c", new Integer(3)); map2.put((K) "c", (V) new Integer(3));
try { try {
map.putAll(map2); map.putAll(map2);
@ -116,20 +119,20 @@ public class TestPredicatedMap extends AbstractTestMap{
// expected // expected
} }
map.put("E", "e"); map.put((K) "E", (V) "e");
Iterator iterator = map.entrySet().iterator(); Iterator<Map.Entry<K, V>> iterator = map.entrySet().iterator();
try { try {
Map.Entry entry = (Map.Entry)iterator.next(); Map.Entry<K, V> entry = iterator.next();
entry.setValue(new Integer(3)); entry.setValue((V) new Integer(3));
fail("Illegal value should raise IllegalArgument"); fail("Illegal value should raise IllegalArgument");
} catch (IllegalArgumentException e) { } catch (IllegalArgumentException e) {
// expected // expected
} }
map.put("F", "f"); map.put((K) "F", (V) "f");
iterator = map.entrySet().iterator(); iterator = map.entrySet().iterator();
Map.Entry entry = (Map.Entry)iterator.next(); Map.Entry<K, V> entry = iterator.next();
entry.setValue("x"); entry.setValue((V) "x");
} }