1 16 package org.apache.commons.collections.map; 17 18 import java.util.HashMap ; 19 import java.util.Iterator ; 20 import java.util.Map ; 21 22 import junit.framework.Test; 23 import junit.framework.TestSuite; 24 25 import org.apache.commons.collections.Predicate; 26 import org.apache.commons.collections.PredicateUtils; 27 28 37 public class TestPredicatedMap extends AbstractTestMap{ 38 39 protected static final Predicate truePredicate = PredicateUtils.truePredicate(); 40 protected static final Predicate testPredicate = new Predicate() { 41 public boolean evaluate(Object o) { 42 return (o instanceof String ); 43 } 44 }; 45 46 47 public TestPredicatedMap(String testName) { 48 super(testName); 49 } 50 51 public static Test suite() { 52 return new TestSuite(TestPredicatedMap.class); 53 } 54 55 public static void main(String args[]) { 56 String [] testCaseName = { TestPredicatedMap.class.getName()}; 57 junit.textui.TestRunner.main(testCaseName); 58 } 59 60 protected Map decorateMap(Map map, Predicate keyPredicate, 62 Predicate valuePredicate) { 63 return PredicatedMap.decorate(map, keyPredicate, valuePredicate); 64 } 65 66 public Map makeEmptyMap() { 67 return decorateMap(new HashMap (), truePredicate, truePredicate); 68 } 69 70 public Map makeTestMap() { 71 return decorateMap(new HashMap (), testPredicate, testPredicate); 72 } 73 74 public void testEntrySet() { 76 Map map = makeTestMap(); 77 assertTrue("returned entryset should not be null", 78 map.entrySet() != null); 79 map = decorateMap(new HashMap (), null, null); 80 map.put("oneKey", "oneValue"); 81 assertTrue("returned entryset should contain one entry", 82 map.entrySet().size() == 1); 83 map = decorateMap(map, null, null); 84 } 85 86 public void testPut() { 87 Map map = makeTestMap(); 88 try { 89 map.put("Hi", new Integer (3)); 90 fail("Illegal value should raise IllegalArgument"); 91 } catch (IllegalArgumentException e) { 92 } 94 95 try { 96 map.put(new Integer (3), "Hi"); 97 fail("Illegal key should raise IllegalArgument"); 98 } catch (IllegalArgumentException e) { 99 } 101 102 assertTrue(!map.containsKey(new Integer (3))); 103 assertTrue(!map.containsValue(new Integer (3))); 104 105 Map map2 = new HashMap (); 106 map2.put("A", "a"); 107 map2.put("B", "b"); 108 map2.put("C", "c"); 109 map2.put("c", new Integer (3)); 110 111 try { 112 map.putAll(map2); 113 fail("Illegal value should raise IllegalArgument"); 114 } catch (IllegalArgumentException e) { 115 } 117 118 map.put("E", "e"); 119 Iterator iterator = map.entrySet().iterator(); 120 try { 121 Map.Entry entry = (Map.Entry )iterator.next(); 122 entry.setValue(new Integer (3)); 123 fail("Illegal value should raise IllegalArgument"); 124 } catch (IllegalArgumentException e) { 125 } 127 128 map.put("F", "f"); 129 iterator = map.entrySet().iterator(); 130 Map.Entry entry = (Map.Entry )iterator.next(); 131 entry.setValue("x"); 132 133 } 134 135 public String getCompatibilityVersion() { 136 return "3.1"; 137 } 138 139 } | Popular Tags |