1 16 package org.apache.commons.collections.map; 17 18 import java.util.Comparator ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.SortedMap ; 23 import java.util.TreeMap ; 24 25 import junit.framework.Test; 26 import junit.framework.TestSuite; 27 28 import org.apache.commons.collections.Predicate; 29 import org.apache.commons.collections.PredicateUtils; 30 31 40 public class TestPredicatedSortedMap extends AbstractTestSortedMap{ 41 42 protected static final Predicate truePredicate = PredicateUtils.truePredicate(); 43 protected static final Predicate testPredicate = new Predicate() { 44 public boolean evaluate(Object o) { 45 return (o instanceof String ); 46 } 47 }; 48 49 public TestPredicatedSortedMap(String testName) { 50 super(testName); 51 } 52 53 public static Test suite() { 54 return new TestSuite(TestPredicatedSortedMap.class); 55 } 56 57 public static void main(String args[]) { 58 String [] testCaseName = { TestPredicatedSortedMap.class.getName()}; 59 junit.textui.TestRunner.main(testCaseName); 60 } 61 62 protected SortedMap decorateMap(SortedMap map, Predicate keyPredicate, 64 Predicate valuePredicate) { 65 return PredicatedSortedMap.decorate(map, keyPredicate, valuePredicate); 66 } 67 68 public Map makeEmptyMap() { 69 return decorateMap(new TreeMap (), truePredicate, truePredicate); 70 } 71 72 public Map makeTestMap() { 73 return decorateMap(new TreeMap (), testPredicate, testPredicate); 74 } 75 76 public SortedMap makeTestSortedMap() { 77 return decorateMap(new TreeMap (), testPredicate, testPredicate); 78 } 79 80 public boolean isSubMapViewsSerializable() { 81 return false; 83 } 84 85 public boolean isAllowNullKey() { 86 return false; 87 } 88 89 public void testEntrySet() { 92 SortedMap map = makeTestSortedMap(); 93 assertTrue("returned entryset should not be null", 94 map.entrySet() != null); 95 map = decorateMap(new TreeMap (), null, null); 96 map.put("oneKey", "oneValue"); 97 assertTrue("returned entryset should contain one entry", 98 map.entrySet().size() == 1); 99 map = decorateMap(map, null, null); 100 } 101 102 public void testPut() { 103 Map map = makeTestMap(); 104 try { 105 map.put("Hi", new Integer (3)); 106 fail("Illegal value should raise IllegalArgument"); 107 } catch (IllegalArgumentException e) { 108 } 110 111 try { 112 map.put(new Integer (3), "Hi"); 113 fail("Illegal key should raise IllegalArgument"); 114 } catch (IllegalArgumentException e) { 115 } 117 118 assertTrue(!map.containsKey(new Integer (3))); 119 assertTrue(!map.containsValue(new Integer (3))); 120 121 Map map2 = new HashMap (); 122 map2.put("A", "a"); 123 map2.put("B", "b"); 124 map2.put("C", "c"); 125 map2.put("c", new Integer (3)); 126 127 try { 128 map.putAll(map2); 129 fail("Illegal value should raise IllegalArgument"); 130 } catch (IllegalArgumentException e) { 131 } 133 134 map.put("E", "e"); 135 Iterator iterator = map.entrySet().iterator(); 136 try { 137 Map.Entry entry = (Map.Entry )iterator.next(); 138 entry.setValue(new Integer (3)); 139 fail("Illegal value should raise IllegalArgument"); 140 } catch (IllegalArgumentException e) { 141 } 143 144 map.put("F", "f"); 145 iterator = map.entrySet().iterator(); 146 Map.Entry entry = (Map.Entry )iterator.next(); 147 entry.setValue("x"); 148 149 } 150 151 public void testSortOrder() { 153 SortedMap map = makeTestSortedMap(); 154 map.put("A", "a"); 155 map.put("B", "b"); 156 try { 157 map.put(null, "c"); 158 fail("Null key should raise IllegalArgument"); 159 } catch (IllegalArgumentException e) { 160 } 162 map.put("C", "c"); 163 try { 164 map.put("D", null); 165 fail("Null value should raise IllegalArgument"); 166 } catch (IllegalArgumentException e) { 167 } 169 assertEquals("First key should be A", map.firstKey(), "A"); 170 assertEquals("Last key should be C", map.lastKey(), "C"); 171 assertEquals("First key in tail map should be B", 172 map.tailMap("B").firstKey(), "B"); 173 assertEquals("Last key in head map should be B", 174 map.headMap("C").lastKey(), "B"); 175 assertEquals("Last key in submap should be B", 176 map.subMap("A","C").lastKey(), "B"); 177 178 Comparator c = map.comparator(); 179 assertTrue("natural order, so comparator should be null", 180 c == null); 181 } 182 183 public String getCompatibilityVersion() { 184 return "3.1"; 185 } 186 187 } | Popular Tags |