1 16 package org.apache.commons.collections.map; 17 18 import junit.framework.Test; 19 import junit.framework.TestSuite; 20 import junit.framework.Assert; 21 22 import java.util.Map ; 23 import java.util.HashMap ; 24 import java.util.Collection ; 25 26 35 public class TestCompositeMap extends AbstractTestMap { 36 37 private boolean pass = false; 38 39 public TestCompositeMap(String testName) { 40 super(testName); 41 } 42 43 public static Test suite() { 44 return new TestSuite(TestCompositeMap.class); 45 } 46 47 public void setUp() throws Exception { 48 super.setUp(); 49 this.pass = false; 50 } 51 52 public static void main(String args[]) { 53 String [] testCaseName = {TestCompositeMap.class.getName()}; 54 junit.textui.TestRunner.main(testCaseName); 55 } 56 57 public Map makeEmptyMap() { 58 CompositeMap map = new CompositeMap(); 59 map.addComposited(new HashMap ()); 60 map.setMutator(new CompositeMap.MapMutator() { 61 public void resolveCollision(CompositeMap composite, 62 Map existing, 63 Map added, 64 Collection intersect) { 65 } 67 68 public Object put(CompositeMap map, Map [] composited, Object key, Object value) { 69 return composited[0].put(key, value); 70 } 71 72 public void putAll(CompositeMap map, Map [] composited, Map t) { 73 composited[0].putAll(t); 74 } 75 76 }); 77 return map; 78 } 79 80 private Map buildOne() { 81 HashMap map = new HashMap (); 82 map.put("1", "one"); 83 map.put("2", "two"); 84 return map; 85 } 86 87 public Map buildTwo() { 88 HashMap map = new HashMap (); 89 map.put("3", "three"); 90 map.put("4", "four"); 91 return map; 92 } 93 94 public void testGet() { 95 CompositeMap map = new CompositeMap(buildOne(), buildTwo()); 96 Assert.assertEquals("one", map.get("1")); 97 Assert.assertEquals("four", map.get("4")); 98 } 99 100 public void testAddComposited() { 101 CompositeMap map = new CompositeMap(buildOne(), buildTwo()); 102 HashMap three = new HashMap (); 103 three.put("5", "five"); 104 map.addComposited(three); 105 assertTrue(map.containsKey("5")); 106 try { 107 map.addComposited(three); 108 fail("Expecting IllegalArgumentException."); 109 } catch (IllegalArgumentException ex) { 110 } 112 } 113 114 public void testRemoveComposited() { 115 CompositeMap map = new CompositeMap(buildOne(), buildTwo()); 116 HashMap three = new HashMap (); 117 three.put("5", "five"); 118 map.addComposited(three); 119 assertTrue(map.containsKey("5")); 120 121 map.removeComposited(three); 122 assertFalse(map.containsKey("5")); 123 124 map.removeComposited(buildOne()); 125 assertFalse(map.containsKey("2")); 126 127 } 128 129 public void testRemoveFromUnderlying() { 130 CompositeMap map = new CompositeMap(buildOne(), buildTwo()); 131 HashMap three = new HashMap (); 132 three.put("5", "five"); 133 map.addComposited(three); 134 assertTrue(map.containsKey("5")); 135 136 three.remove("5"); 138 assertFalse(map.containsKey("5")); 139 } 140 141 public void testRemoveFromComposited() { 142 CompositeMap map = new CompositeMap(buildOne(), buildTwo()); 143 HashMap three = new HashMap (); 144 three.put("5", "five"); 145 map.addComposited(three); 146 assertTrue(map.containsKey("5")); 147 148 map.remove("5"); 150 assertFalse(three.containsKey("5")); 151 } 152 153 public void testResolveCollision() { 154 CompositeMap map = new CompositeMap(buildOne(), buildTwo(), 155 new CompositeMap.MapMutator() { 156 public void resolveCollision(CompositeMap composite, 157 Map existing, 158 Map added, 159 Collection intersect) { 160 pass = true; 161 } 162 163 public Object put(CompositeMap map, Map [] composited, Object key, 164 Object value) { 165 throw new UnsupportedOperationException (); 166 } 167 168 public void putAll(CompositeMap map, Map [] composited, Map t) { 169 throw new UnsupportedOperationException (); 170 } 171 }); 172 173 map.addComposited(buildOne()); 174 assertTrue(pass); 175 } 176 177 public void testPut() { 178 CompositeMap map = new CompositeMap(buildOne(), buildTwo(), 179 new CompositeMap.MapMutator() { 180 public void resolveCollision(CompositeMap composite, 181 Map existing, 182 Map added, 183 Collection intersect) { 184 throw new UnsupportedOperationException (); 185 } 186 187 public Object put(CompositeMap map, Map [] composited, Object key, 188 Object value) { 189 pass = true; 190 return "foo"; 191 } 192 193 public void putAll(CompositeMap map, Map [] composited, Map t) { 194 throw new UnsupportedOperationException (); 195 } 196 }); 197 198 map.put("willy", "wonka"); 199 assertTrue(pass); 200 } 201 202 public void testPutAll() { 203 CompositeMap map = new CompositeMap(buildOne(), buildTwo(), 204 new CompositeMap.MapMutator() { 205 public void resolveCollision(CompositeMap composite, 206 Map existing, 207 Map added, 208 Collection intersect) { 209 throw new UnsupportedOperationException (); 210 } 211 212 public Object put(CompositeMap map, Map [] composited, Object key, 213 Object value) { 214 throw new UnsupportedOperationException (); 215 } 216 217 public void putAll(CompositeMap map, Map [] composited, Map t) { 218 pass = true; 219 } 220 }); 221 222 map.putAll(null); 223 assertTrue(pass); 224 } 225 } 226 227 | Popular Tags |