You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by ba...@apache.org on 2009/09/15 07:58:11 UTC
svn commit: r815152 -
/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java
Author: bayard
Date: Tue Sep 15 05:58:11 2009
New Revision: 815152
URL: http://svn.apache.org/viewvc?rev=815152&view=rev
Log:
Merging from -r468106:814127 of collections_jdk5_branch - namely where this code was generified; mostly in r738956.
Also see the following revisions:
------------------------------------------------------------------------
r751887 | mbenson | 2009-03-09 15:40:01 -0700 (Mon, 09 Mar 2009) | 1 line
add methods to wrap Maps and SortedMaps not based on Commons Collections classes to their Iterable*Map counterparts
------------------------------------------------------------------------
r471166 | scolebourne | 2006-11-04 03:33:22 -0800 (Sat, 04 Nov 2006) | 1 line
Removed Typed* containers such as TypedList and TypedMap as generics now provides type safety
------------------------------------------------------------------------
Modified:
commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java
Modified: commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java
URL: http://svn.apache.org/viewvc/commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java?rev=815152&r1=815151&r2=815152&view=diff
==============================================================================
--- commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java (original)
+++ commons/proper/collections/trunk/src/test/org/apache/commons/collections/TestMapUtils.java Tue Sep 15 05:58:11 2009
@@ -33,9 +33,9 @@
import org.apache.commons.collections.keyvalue.DefaultKeyValue;
import org.apache.commons.collections.keyvalue.DefaultMapEntry;
+import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.collections.map.LazyMap;
import org.apache.commons.collections.map.PredicatedMap;
-import org.apache.commons.collections.map.TestPredicatedMap;
import org.apache.commons.collections.collection.TestTransformedCollection;
/**
@@ -55,13 +55,12 @@
super(name);
}
-
public static Test suite() {
return BulkTest.makeSuite(TestMapUtils.class);
}
- public Predicate getPredicate() {
- return new Predicate() {
+ public Predicate<Object> getPredicate() {
+ return new Predicate<Object>() {
public boolean evaluate(Object o) {
return o instanceof String;
}
@@ -69,10 +68,9 @@
}
public void testPredicatedMap() {
- Predicate p = getPredicate();
- Map map = MapUtils.predicatedMap(new HashMap(), p, p);
- assertTrue("returned object should be a PredicatedMap",
- map instanceof PredicatedMap);
+ Predicate<Object> p = getPredicate();
+ Map<Object, Object> map = MapUtils.predicatedMap(new HashMap<Object, Object>(), p, p);
+ assertTrue("returned object should be a PredicatedMap", map instanceof PredicatedMap);
try {
map = MapUtils.predicatedMap(null, p, p);
fail("Expecting IllegalArgumentException for null map.");
@@ -81,95 +79,33 @@
}
}
- // Since a typed map is a predicated map, I copied the tests for predicated map
- public void testTypedMapIllegalPut() {
- final Map map = MapUtils.typedMap(new HashMap(), String.class, String.class);
-
- try {
- map.put("Hi", new Integer(3));
- fail("Illegal value should raise IllegalArgument");
- } catch (IllegalArgumentException e) {
- // expected
- }
-
- try {
- map.put(new Integer(3), "Hi");
- fail("Illegal key should raise IllegalArgument");
- } catch (IllegalArgumentException e) {
- // expected
- }
-
- 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));
-
- try {
- map.putAll(map2);
- fail("Illegal value should raise IllegalArgument");
- } catch (IllegalArgumentException e) {
- // expected
- }
-
- map.put("E", "e");
- Iterator iterator = map.entrySet().iterator();
- try {
- Map.Entry entry = (Map.Entry)iterator.next();
- entry.setValue(new Integer(3));
- fail("Illegal value should raise IllegalArgument");
- } catch (IllegalArgumentException e) {
- // expected
- }
-
- }
-
- public BulkTest bulkTestTypedMap() {
- return new TestPredicatedMap("") {
- public boolean isAllowNullKey() {
- return false;
- }
-
- public boolean isAllowNullValue() {
- return false;
- }
-
- public Map makeEmptyMap() {
- return MapUtils.typedMap(new HashMap(), String.class, String.class);
- }
- };
- }
-
public void testLazyMapFactory() {
- Factory factory = FactoryUtils.constantFactory(new Integer(5));
- Map map = MapUtils.lazyMap(new HashMap(), factory);
+ Factory<Integer> factory = FactoryUtils.constantFactory(new Integer(5));
+ Map<Object, Object> map = MapUtils.lazyMap(new HashMap<Object, Object>(), factory);
assertTrue(map instanceof LazyMap);
try {
- map = MapUtils.lazyMap(new HashMap(), (Factory) null);
+ map = MapUtils.lazyMap(new HashMap<Object, Object>(), (Factory<Object>) null);
fail("Expecting IllegalArgumentException for null factory");
} catch (IllegalArgumentException e) {
// expected
}
try {
- map = MapUtils.lazyMap(null, factory);
+ map = MapUtils.lazyMap((Map<Object, Object>) null, factory);
fail("Expecting IllegalArgumentException for null map");
} catch (IllegalArgumentException e) {
// expected
}
- Transformer transformer = TransformerUtils.asTransformer(factory);
- map = MapUtils.lazyMap(new HashMap(), transformer);
+ Transformer<Object, Integer> transformer = TransformerUtils.asTransformer(factory);
+ map = MapUtils.lazyMap(new HashMap<Object, Object>(), transformer);
assertTrue(map instanceof LazyMap);
try {
- map = MapUtils.lazyMap(new HashMap(), (Transformer) null);
+ map = MapUtils.lazyMap(new HashMap<Object, Object>(), (Transformer<Object, Object>) null);
fail("Expecting IllegalArgumentException for null transformer");
} catch (IllegalArgumentException e) {
// expected
}
try {
- map = MapUtils.lazyMap(null, transformer);
+ map = MapUtils.lazyMap((Map<Object, Object>) null, transformer);
fail("Expecting IllegalArgumentException for null map");
} catch (IllegalArgumentException e) {
// expected
@@ -177,7 +113,7 @@
}
public void testLazyMapTransformer() {
- Map map = MapUtils.lazyMap(new HashMap(), new Transformer() {
+ Map<Object, Object> map = MapUtils.lazyMap(new HashMap<Object, Object>(), new Transformer<Object, Object>() {
public Object transform(Object mapKey) {
if (mapKey instanceof String) {
return new Integer((String) mapKey);
@@ -197,20 +133,20 @@
}
public void testInvertMap() {
- final Map in = new HashMap( 5 , 1 );
- in.put( "1" , "A" );
- in.put( "2" , "B" );
- in.put( "3" , "C" );
- in.put( "4" , "D" );
- in.put( "5" , "E" );
+ final Map<String, String> in = new HashMap<String, String>(5, 1);
+ in.put("1", "A");
+ in.put("2", "B");
+ in.put("3", "C");
+ in.put("4", "D");
+ in.put("5", "E");
- final Set inKeySet = new HashSet( in.keySet() );
- final Set inValSet = new HashSet( in.values() );
+ final Set<String> inKeySet = new HashSet<String>(in.keySet());
+ final Set<String> inValSet = new HashSet<String>(in.values());
- final Map out = MapUtils.invertMap(in);
+ final Map<String, String> out = MapUtils.invertMap(in);
- final Set outKeySet = new HashSet( out.keySet() );
- final Set outValSet = new HashSet( out.values() );
+ final Set<String> outKeySet = new HashSet<String>(out.keySet());
+ final Set<String> outValSet = new HashSet<String>(out.values());
assertTrue( inKeySet.equals( outValSet ));
assertTrue( inValSet.equals( outKeySet ));
@@ -232,11 +168,11 @@
fail();
} catch (NullPointerException ex) {}
- Map test = MapUtils.putAll(new HashMap(), new String[0]);
+ Map<String, String> test = MapUtils.putAll(new HashMap<String, String>(), new String[0]);
assertEquals(0, test.size());
// sub array
- test = MapUtils.putAll(new HashMap(), new String[][] {
+ test = MapUtils.putAll(new HashMap<String, String>(), new String[][] {
{"RED", "#FF0000"},
{"GREEN", "#00FF00"},
{"BLUE", "#0000FF"}
@@ -250,7 +186,7 @@
assertEquals(3, test.size());
try {
- MapUtils.putAll(new HashMap(), new String[][] {
+ MapUtils.putAll(new HashMap<String, String>(), new String[][] {
{"RED", "#FF0000"},
null,
{"BLUE", "#0000FF"}
@@ -259,7 +195,7 @@
} catch (IllegalArgumentException ex) {}
try {
- MapUtils.putAll(new HashMap(), new String[][] {
+ MapUtils.putAll(new HashMap<String, String>(), new String[][] {
{"RED", "#FF0000"},
{"GREEN"},
{"BLUE", "#0000FF"}
@@ -268,7 +204,7 @@
} catch (IllegalArgumentException ex) {}
try {
- MapUtils.putAll(new HashMap(), new String[][] {
+ MapUtils.putAll(new HashMap<String, String>(), new String[][] {
{"RED", "#FF0000"},
{},
{"BLUE", "#0000FF"}
@@ -277,7 +213,7 @@
} catch (IllegalArgumentException ex) {}
// flat array
- test = MapUtils.putAll(new HashMap(), new String[] {
+ test = MapUtils.putAll(new HashMap<String, String>(), new String[] {
"RED", "#FF0000",
"GREEN", "#00FF00",
"BLUE", "#0000FF"
@@ -290,7 +226,7 @@
assertEquals("#0000FF", test.get("BLUE"));
assertEquals(3, test.size());
- test = MapUtils.putAll(new HashMap(), new String[] {
+ test = MapUtils.putAll(new HashMap<String, String>(), new String[] {
"RED", "#FF0000",
"GREEN", "#00FF00",
"BLUE", "#0000FF",
@@ -305,10 +241,10 @@
assertEquals(3, test.size());
// map entry
- test = MapUtils.putAll(new HashMap(), new Object[] {
- new DefaultMapEntry("RED", "#FF0000"),
- new DefaultMapEntry("GREEN", "#00FF00"),
- new DefaultMapEntry("BLUE", "#0000FF")
+ test = MapUtils.putAll(new HashMap<String, String>(), new Object[] {
+ new DefaultMapEntry<String, String>("RED", "#FF0000"),
+ new DefaultMapEntry<String, String>("GREEN", "#00FF00"),
+ new DefaultMapEntry<String, String>("BLUE", "#0000FF")
});
assertEquals(true, test.containsKey("RED"));
assertEquals("#FF0000", test.get("RED"));
@@ -319,10 +255,10 @@
assertEquals(3, test.size());
// key value
- test = MapUtils.putAll(new HashMap(), new Object[] {
- new DefaultKeyValue("RED", "#FF0000"),
- new DefaultKeyValue("GREEN", "#00FF00"),
- new DefaultKeyValue("BLUE", "#0000FF")
+ test = MapUtils.putAll(new HashMap<String, String>(), new Object[] {
+ new DefaultKeyValue<String, String>("RED", "#FF0000"),
+ new DefaultKeyValue<String, String>("GREEN", "#00FF00"),
+ new DefaultKeyValue<String, String>("BLUE", "#0000FF")
});
assertEquals(true, test.containsKey("RED"));
assertEquals("#FF0000", test.get("RED"));
@@ -334,17 +270,17 @@
}
public void testConvertResourceBundle() {
- final Map in = new HashMap( 5 , 1 );
- in.put( "1" , "A" );
- in.put( "2" , "B" );
- in.put( "3" , "C" );
- in.put( "4" , "D" );
- in.put( "5" , "E" );
+ final Map<String, String> in = new HashMap<String, String>( 5 , 1 );
+ in.put("1", "A");
+ in.put("2", "B");
+ in.put("3", "C");
+ in.put("4", "D");
+ in.put("5", "E");
ResourceBundle b = new ListResourceBundle() {
public Object[][] getContents() {
final Object[][] contents = new Object[ in.size() ][2];
- final Iterator i = in.keySet().iterator();
+ final Iterator<String> i = in.keySet().iterator();
int n = 0;
while ( i.hasNext() ) {
final Object key = i.next();
@@ -357,20 +293,19 @@
}
};
- final Map out = MapUtils.toMap(b);
+ final Map<String, Object> out = MapUtils.toMap(b);
assertTrue( in.equals(out));
}
public void testDebugAndVerbosePrintCasting() {
- final Map inner = new HashMap(2, 1);
- inner.put( new Integer(2) , "B" );
- inner.put( new Integer(3) , "C" );
-
- final Map outer = new HashMap(2, 1);
- outer.put( new Integer(0) , inner );
- outer.put( new Integer(1) , "A");
-
+ final Map<Integer, String> inner = new HashMap<Integer, String>(2, 1);
+ inner.put(2, "B");
+ inner.put(3, "C");
+
+ final Map<Integer, Object> outer = new HashMap<Integer, Object>(2, 1);
+ outer.put(0, inner);
+ outer.put(1, "A");
final ByteArrayOutputStream out = new ByteArrayOutputStream();
final PrintStream outPrint = new PrintStream(out);
@@ -407,10 +342,10 @@
final String INDENT = " ";
- final Map map = new TreeMap(); // treeMap guarantees order across JDKs for test
- map.put( new Integer(2) , "B" );
- map.put( new Integer(3) , "C" );
- map.put( new Integer(4) , null );
+ final Map<Integer, String> map = new TreeMap<Integer, String>(); // treeMap guarantees order across JDKs for test
+ map.put(2, "B");
+ map.put(3, "C");
+ map.put(4, null);
outPrint.println("{");
outPrint.println(INDENT + "2 = B");
@@ -430,10 +365,10 @@
final String INDENT = " ";
- final Map map = new TreeMap(); // treeMap guarantees order across JDKs for test
- map.put( new Integer(2) , "B" );
- map.put( new Integer(3) , "C" );
- map.put( new Integer(4) , null );
+ final Map<Integer, String> map = new TreeMap<Integer, String>(); // treeMap guarantees order across JDKs for test
+ map.put(2, "B");
+ map.put(3, "C");
+ map.put(4, null);
outPrint.println("{");
outPrint.println(INDENT + "2 = B " + String.class.getName());
@@ -473,7 +408,7 @@
public void testVerbosePrintNullStream() {
try {
- MapUtils.verbosePrint(null, "Map", new HashMap());
+ MapUtils.verbosePrint(null, "Map", new HashMap<Object, Object>());
fail("Should generate NullPointerException");
} catch (NullPointerException expected) {
}
@@ -481,7 +416,7 @@
public void testDebugPrintNullStream() {
try {
- MapUtils.debugPrint(null, "Map", new HashMap());
+ MapUtils.debugPrint(null, "Map", new HashMap<Object, Object>());
fail("Should generate NullPointerException");
} catch (NullPointerException expected) {
}
@@ -493,8 +428,8 @@
final String INDENT = " ";
- final Map map = new HashMap();
- map.put( null , "A" );
+ final Map<Object, String> map = new HashMap<Object, String>();
+ map.put(null, "A");
outPrint.println("{");
outPrint.println(INDENT + "null = A " + String.class.getName());
@@ -512,8 +447,8 @@
final String INDENT = " ";
- final Map map = new HashMap();
- map.put( null , "A" );
+ final Map<Object, String> map = new HashMap<Object, String>();
+ map.put(null, "A");
outPrint.println("{");
outPrint.println(INDENT + "null = A");
@@ -531,8 +466,8 @@
final String INDENT = " ";
- final Map map = new HashMap();
- map.put( null , map );
+ final Map<Object, Map<?, ?>> map = new HashMap<Object, Map<?, ?>>();
+ map.put(null, map);
outPrint.println("{");
outPrint.println(INDENT + "null = (this Map) " + HashMap.class.getName());
@@ -550,8 +485,8 @@
final String INDENT = " ";
- final Map map = new HashMap();
- map.put( null , map );
+ final Map<Object, Map<?, ?>> map = new HashMap<Object, Map<?, ?>>();
+ map.put(null, map);
outPrint.println("{");
outPrint.println(INDENT + "null = (this Map)");
@@ -569,10 +504,10 @@
final String INDENT = " ";
- final Map map = new HashMap();
- final Map map2= new HashMap();
- map.put( null , map2 );
- map2.put( "2", "B" );
+ final Map<Object, Object> map = new HashMap<Object, Object>();
+ final Map<Object, Object> map2= new HashMap<Object, Object>();
+ map.put(null, map2);
+ map2.put("2", "B");
outPrint.println("{");
outPrint.println(INDENT + "null = ");
@@ -593,10 +528,10 @@
final String INDENT = " ";
- final Map map = new HashMap();
- final Map map2= new HashMap();
- map.put( null , map2 );
- map2.put( "2", "B" );
+ final Map<Object, Object> map = new HashMap<Object, Object>();
+ final Map<Object, Object> map2= new HashMap<Object, Object>();
+ map.put(null, map2);
+ map2.put("2", "B");
outPrint.println("{");
outPrint.println(INDENT + "null = ");
@@ -633,14 +568,14 @@
out.reset();
- final Map inner = new TreeMap(); // treeMap guarantees order across JDKs for test
- inner.put( new Integer(2) , "B" );
- inner.put( new Integer(3) , "C" );
-
- final Map outer = new TreeMap();
- outer.put( new Integer(1) , inner );
- outer.put( new Integer(0) , "A");
- outer.put( new Integer(7) , outer);
+ final Map<Integer, String> inner = new TreeMap<Integer, String>(); // treeMap guarantees order across JDKs for test
+ inner.put(2, "B");
+ inner.put(3, "C");
+
+ final Map<Integer, Object> outer = new TreeMap<Integer, Object>();
+ outer.put(1, inner);
+ outer.put(0, "A");
+ outer.put(7, outer);
MapUtils.verbosePrint(outPrint, "Print Map", outer);
assertEquals(EXPECTED_OUT, out.toString());
@@ -668,14 +603,14 @@
out.reset();
- final Map inner = new TreeMap(); // treeMap guarantees order across JDKs for test
- inner.put( new Integer(2) , "B" );
- inner.put( new Integer(3) , "C" );
-
- final Map outer = new TreeMap();
- outer.put( new Integer(1) , inner );
- outer.put( new Integer(0) , "A");
- outer.put( new Integer(7) , outer);
+ final Map<Integer, String> inner = new TreeMap<Integer, String>(); // treeMap guarantees order across JDKs for test
+ inner.put(2, "B");
+ inner.put(3, "C");
+
+ final Map<Integer, Object> outer = new TreeMap<Integer, Object>();
+ outer.put(1, inner);
+ outer.put(0, "A");
+ outer.put(7, outer);
MapUtils.debugPrint(outPrint, "Print Map", outer);
assertEquals(EXPECTED_OUT, out.toString());
@@ -688,21 +623,20 @@
final String LABEL = "Print Map";
final String INDENT = " ";
-
- final Map grandfather = new TreeMap();// treeMap guarantees order across JDKs for test
- final Map father = new TreeMap();
- final Map son = new TreeMap();
-
- grandfather.put( new Integer(0), "A" );
- grandfather.put( new Integer(1), father );
-
- father.put( new Integer(2), "B" );
- father.put( new Integer(3), grandfather);
- father.put( new Integer(4), son);
-
- son.put( new Integer(5), "C");
- son.put( new Integer(6), grandfather);
- son.put( new Integer(7), father);
+ final Map<Integer, Object> grandfather = new TreeMap<Integer, Object>();// treeMap guarantees order across JDKs for test
+ final Map<Integer, Object> father = new TreeMap<Integer, Object>();
+ final Map<Integer, Object> son = new TreeMap<Integer, Object>();
+
+ grandfather.put(0, "A");
+ grandfather.put(1, father);
+
+ father.put(2, "B");
+ father.put(3, grandfather);
+ father.put(4, son);
+
+ son.put(5, "C");
+ son.put(6, grandfather);
+ son.put(7, father);
outPrint.println(LABEL + " = ");
outPrint.println("{");
@@ -735,21 +669,20 @@
final String LABEL = "Print Map";
final String INDENT = " ";
-
- final Map grandfather = new TreeMap();// treeMap guarantees order across JDKs for test
- final Map father = new TreeMap();
- final Map son = new TreeMap();
-
- grandfather.put( new Integer(0), "A" );
- grandfather.put( new Integer(1), father );
-
- father.put( new Integer(2), "B" );
- father.put( new Integer(3), grandfather);
- father.put( new Integer(4), son);
-
- son.put( new Integer(5), "C");
- son.put( new Integer(6), grandfather);
- son.put( new Integer(7), father);
+ final Map<Integer, Object> grandfather = new TreeMap<Integer, Object>();// treeMap guarantees order across JDKs for test
+ final Map<Integer, Object> father = new TreeMap<Integer, Object>();
+ final Map<Integer, Object> son = new TreeMap<Integer, Object>();
+
+ grandfather.put(0, "A");
+ grandfather.put(1, father);
+
+ father.put(2, "B");
+ father.put(3, grandfather);
+ father.put(4, son);
+
+ son.put(5, "C");
+ son.put(6, grandfather);
+ son.put(7, father);
outPrint.println(LABEL + " = ");
outPrint.println("{");
@@ -777,34 +710,34 @@
//-----------------------------------------------------------------------
public void testIsEmptyWithEmptyMap() {
- Map map = new HashMap();
+ Map<Object, Object> map = new HashMap<Object, Object>();
assertEquals(true, MapUtils.isEmpty(map));
}
public void testIsEmptyWithNonEmptyMap() {
- Map map = new HashMap();
+ Map<String, String> map = new HashMap<String, String>();
map.put("item", "value");
assertEquals(false, MapUtils.isEmpty(map));
}
public void testIsEmptyWithNull() {
- Map map = null;
+ Map<Object, Object> map = null;
assertEquals(true, MapUtils.isEmpty(map));
}
public void testIsNotEmptyWithEmptyMap() {
- Map map = new HashMap();
+ Map<Object, Object> map = new HashMap<Object, Object>();
assertEquals(false, MapUtils.isNotEmpty(map));
}
public void testIsNotEmptyWithNonEmptyMap() {
- Map map = new HashMap();
+ Map<String, String> map = new HashMap<String, String>();
map.put("item", "value");
assertEquals(true, MapUtils.isNotEmpty(map));
}
public void testIsNotEmptyWithNull() {
- Map map = null;
+ Map<Object, Object> map = null;
assertEquals(false, MapUtils.isNotEmpty(map));
}
@@ -843,4 +776,38 @@
assertEquals(new Integer((String) list.get(i)), map.get(new Integer((String) list.get(i))));
}
}
+
+ public void testIterableMap() {
+ try {
+ MapUtils.iterableMap(null);
+ fail("Should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ }
+ HashMap<String, String> map = new HashMap<String, String>();
+ map.put("foo", "foov");
+ map.put("bar", "barv");
+ map.put("baz", "bazv");
+ IterableMap<String, String> iMap = MapUtils.iterableMap(map);
+ assertEquals(map, iMap);
+ assertNotSame(map, iMap);
+ HashedMap<String, String> hMap = new HashedMap<String, String>(map);
+ assertSame(hMap, MapUtils.iterableMap(hMap));
+ }
+
+ public void testIterableSortedMap() {
+ try {
+ MapUtils.iterableSortedMap(null);
+ fail("Should throw IllegalArgumentException");
+ } catch (IllegalArgumentException e) {
+ }
+ TreeMap<String, String> map = new TreeMap<String, String>();
+ map.put("foo", "foov");
+ map.put("bar", "barv");
+ map.put("baz", "bazv");
+ IterableSortedMap<String, String> iMap = MapUtils.iterableSortedMap(map);
+ assertEquals(map, iMap);
+ assertNotSame(map, iMap);
+ assertSame(iMap, MapUtils.iterableMap(iMap));
+ }
+
}