1 16 package org.apache.commons.collections; 17 18 import java.util.ArrayList ; 19 import java.util.Arrays ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.Map ; 24 25 import junit.framework.Test; 26 import junit.framework.TestSuite; 27 28 import org.apache.commons.collections.map.AbstractTestMap; 29 30 37 public class TestMultiHashMap extends AbstractTestMap { 38 39 public TestMultiHashMap(String testName) { 40 super(testName); 41 } 42 43 public static Test suite() { 44 return new TestSuite(TestMultiHashMap.class); 45 } 46 47 public static void main(String args[]) { 48 String [] testCaseName = { TestMultiHashMap.class.getName()}; 49 junit.textui.TestRunner.main(testCaseName); 50 } 51 52 public String getCompatibilityVersion() { 54 return "2"; 55 } 56 57 public Map makeEmptyMap() { 58 return new MultiHashMap(); 59 } 60 61 public void testPutNGet() { 65 MultiHashMap map = new MultiHashMap(); 66 loadMap(map); 67 checkMap(map); 68 69 assertTrue(map.get(new Integer (99)) == null); 70 71 map.clear(); 72 assertTrue(map.size() == 0); 73 } 74 75 public void testContainsValue() { 76 MultiHashMap map = new MultiHashMap(); 77 loadMap(map); 78 79 assertTrue(map.containsValue("uno")); 80 assertTrue(map.containsValue("quatro")); 81 assertTrue(map.containsValue("two")); 82 83 assertTrue(!map.containsValue("uggaBugga")); 84 85 map.clear(); 86 } 87 88 public void testValues() { 89 MultiHashMap map = new MultiHashMap(); 90 loadMap(map); 91 92 Collection vals = map.values(); 93 assertTrue(vals.size() == getFullSize()); 94 95 map.clear(); 96 } 97 98 static private class MapPair { 99 MapPair(int key, String val) { 100 mKey = new Integer (key); 101 mValue = val; 102 } 103 104 Integer mKey = null; 105 String mValue = null; 106 } 107 108 static private MapPair[][] sMapPairs = 109 { 110 {new MapPair(0,"zero")}, 111 {new MapPair(1,"one"), new MapPair(1,"ONE"), new MapPair(1,"uno")}, 112 {new MapPair(2,"two"), new MapPair(2,"two") }, 113 {new MapPair(3,"three"), new MapPair(3,"THREE"), new MapPair(3,"tres")}, 114 {new MapPair(4,"four"), new MapPair(4,"quatro")} 115 }; 116 117 private void loadMap(MultiHashMap map) { 118 122 int numRows = sMapPairs.length; 123 int maxCols = 0; 124 for (int ii = 0; ii < sMapPairs.length; ii++) { 125 if (sMapPairs[ii].length > maxCols) { 126 maxCols = sMapPairs[ii].length; 127 } 128 } 129 for (int ii = 0; ii < maxCols; ii++) { 130 for (int jj = 0; jj < numRows; jj++) { 131 if (ii < sMapPairs[jj].length) { 132 map.put(sMapPairs[jj][ii].mKey, sMapPairs[jj][ii].mValue); 133 } 135 } 136 } 137 assertTrue(map.size() == sMapPairs.length); 138 } 139 140 private void checkMap(MultiHashMap map) { 141 for (int ii = 0; ii < sMapPairs.length; ii++) { 142 checkKeyList(map, ii); 143 } 144 } 145 146 private void checkKeyList(MultiHashMap map, int index) { 147 assertTrue(index < sMapPairs.length); 148 Integer key = sMapPairs[index][0].mKey; 149 150 Object obj = map.get(key); 151 153 assertTrue(obj != null); 154 assertTrue(obj instanceof Collection ); 155 Collection keyList = (Collection ) obj; 156 157 assertTrue(keyList.size() == sMapPairs[index].length); 158 Iterator iter = keyList.iterator(); 159 while (iter.hasNext()) { 160 Object oval = iter.next(); 161 assertTrue(oval != null); 162 assertTrue(oval instanceof String ); 163 String val = (String ) oval; 164 boolean foundIt = false; 165 for (int ii = 0; ii < sMapPairs[index].length; ii++) { 166 if (val.equals(sMapPairs[index][ii].mValue)) { 167 foundIt = true; 168 } 169 } 170 assertTrue(foundIt); 171 } 172 } 173 174 public int getFullSize() { 175 int len = 0; 176 for (int ii = 0; ii < sMapPairs.length; ii++) { 177 len += sMapPairs[ii].length; 178 } 179 return len; 180 } 181 182 183 public void testEntrySetIterator() { 184 } 185 public void testEntrySetContainsProperMappings() { 186 } 187 public void testEntrySetIteratorHasProperMappings() { 188 return; 195 } 196 197 200 203 public void testMapGet() { 204 } 205 206 public void testMapPut() { 207 } 208 209 public void testMapPutAll() { 210 } 211 212 public void testMapRemove() { 213 } 214 215 public void testMapEquals() { 216 MultiHashMap one = new MultiHashMap(); 217 Integer value = new Integer (1); 218 one.put("One", value); 219 one.remove("One", value); 220 221 MultiHashMap two = new MultiHashMap(); 222 assertEquals(two, one); 223 } 224 225 public void testMapHashCode() { 226 } 227 228 public void verifyMap() { 232 } 235 236 public void verifyEntrySet() { 237 } 240 241 244 public void verifyValues() { 245 values = map.values(); 248 super.verifyValues(); 249 } 250 251 public void testGetCollection() { 253 MultiHashMap map = new MultiHashMap(); 254 map.put("A", "AA"); 255 assertSame(map.get("A"), map.getCollection("A")); 256 } 257 258 public void testTotalSize() { 259 MultiHashMap map = new MultiHashMap(); 260 assertEquals(0, map.totalSize()); 261 map.put("A", "AA"); 262 assertEquals(1, map.totalSize()); 263 map.put("B", "BA"); 264 assertEquals(2, map.totalSize()); 265 map.put("B", "BB"); 266 assertEquals(3, map.totalSize()); 267 map.put("B", "BC"); 268 assertEquals(4, map.totalSize()); 269 map.remove("A"); 270 assertEquals(3, map.totalSize()); 271 map.remove("B", "BC"); 272 assertEquals(2, map.totalSize()); 273 } 274 275 public void testSize_Key() { 276 MultiHashMap map = new MultiHashMap(); 277 assertEquals(0, map.size("A")); 278 assertEquals(0, map.size("B")); 279 map.put("A", "AA"); 280 assertEquals(1, map.size("A")); 281 assertEquals(0, map.size("B")); 282 map.put("B", "BA"); 283 assertEquals(1, map.size("A")); 284 assertEquals(1, map.size("B")); 285 map.put("B", "BB"); 286 assertEquals(1, map.size("A")); 287 assertEquals(2, map.size("B")); 288 map.put("B", "BC"); 289 assertEquals(1, map.size("A")); 290 assertEquals(3, map.size("B")); 291 map.remove("A"); 292 assertEquals(0, map.size("A")); 293 assertEquals(3, map.size("B")); 294 map.remove("B", "BC"); 295 assertEquals(0, map.size("A")); 296 assertEquals(2, map.size("B")); 297 } 298 299 public void testIterator_Key() { 300 MultiHashMap map = new MultiHashMap(); 301 assertEquals(false, map.iterator("A").hasNext()); 302 map.put("A", "AA"); 303 Iterator it = map.iterator("A"); 304 assertEquals(true, it.hasNext()); 305 it.next(); 306 assertEquals(false, it.hasNext()); 307 } 308 309 public void testContainsValue_Key() { 310 MultiHashMap map = new MultiHashMap(); 311 assertEquals(false, map.containsValue("A", "AA")); 312 assertEquals(false, map.containsValue("B", "BB")); 313 map.put("A", "AA"); 314 assertEquals(true, map.containsValue("A", "AA")); 315 assertEquals(false, map.containsValue("A", "AB")); 316 } 317 318 public void testPutAll_KeyCollection() { 319 MultiHashMap map = new MultiHashMap(); 320 Collection coll = Arrays.asList(new Object [] {"X", "Y", "Z"}); 321 322 assertEquals(true, map.putAll("A", coll)); 323 assertEquals(3, map.size("A")); 324 assertEquals(true, map.containsValue("A", "X")); 325 assertEquals(true, map.containsValue("A", "Y")); 326 assertEquals(true, map.containsValue("A", "Z")); 327 328 assertEquals(false, map.putAll("A", null)); 329 assertEquals(3, map.size("A")); 330 assertEquals(true, map.containsValue("A", "X")); 331 assertEquals(true, map.containsValue("A", "Y")); 332 assertEquals(true, map.containsValue("A", "Z")); 333 334 assertEquals(false, map.putAll("A", new ArrayList ())); 335 assertEquals(3, map.size("A")); 336 assertEquals(true, map.containsValue("A", "X")); 337 assertEquals(true, map.containsValue("A", "Y")); 338 assertEquals(true, map.containsValue("A", "Z")); 339 340 coll = Arrays.asList(new Object [] {"M"}); 341 assertEquals(true, map.putAll("A", coll)); 342 assertEquals(4, map.size("A")); 343 assertEquals(true, map.containsValue("A", "X")); 344 assertEquals(true, map.containsValue("A", "Y")); 345 assertEquals(true, map.containsValue("A", "Z")); 346 assertEquals(true, map.containsValue("A", "M")); 347 } 348 349 public void testClone() { 350 MultiHashMap map = new MultiHashMap(); 351 map.put("A", "1"); 352 map.put("A", "2"); 353 Collection coll = (Collection ) map.get("A"); 354 assertEquals(1, map.size()); 355 assertEquals(2, coll.size()); 356 357 MultiHashMap cloned = (MultiHashMap) map.clone(); 358 Collection clonedColl = (Collection ) cloned.get("A"); 359 assertNotSame(map, cloned); 360 assertNotSame(coll, clonedColl); 361 assertEquals(1, map.size()); 362 assertEquals(2, coll.size()); 363 assertEquals(1, cloned.size()); 364 assertEquals(2, clonedColl.size()); 365 map.put("A", "3"); 366 assertEquals(1, map.size()); 367 assertEquals(3, coll.size()); 368 assertEquals(1, cloned.size()); 369 assertEquals(2, clonedColl.size()); 370 } 371 372 public void testConstructorCopy1() { 373 MultiHashMap map = new MultiHashMap(); 374 map.put("A", "1"); 375 map.put("A", "2"); 376 Collection coll = (Collection ) map.get("A"); 377 assertEquals(1, map.size()); 378 assertEquals(2, coll.size()); 379 380 MultiHashMap newMap = new MultiHashMap(map); 381 Collection newColl = (Collection ) newMap.get("A"); 382 assertNotSame(map, newMap); 383 assertNotSame(coll, newColl); 384 assertEquals(1, map.size()); 385 assertEquals(2, coll.size()); 386 assertEquals(1, newMap.size()); 387 assertEquals(2, newColl.size()); 388 389 map.put("A", "3"); 390 assertEquals(1, map.size()); 391 assertEquals(3, coll.size()); 392 assertEquals(1, newMap.size()); 393 assertEquals(2, newColl.size()); 394 } 395 396 public void testConstructorCopy2() { 397 Map map = new HashMap (); 398 map.put("A", "1"); 399 map.put("B", "2"); 400 assertEquals(2, map.size()); 401 402 MultiHashMap newMap = new MultiHashMap(map); 403 Collection newColl = (Collection ) newMap.get("A"); 404 assertNotSame(map, newMap); 405 assertEquals(2, map.size()); 406 assertEquals(2, newMap.size()); 407 assertEquals(1, newColl.size()); 408 409 map.put("A", "3"); 410 assertEquals(2, map.size()); 411 assertEquals(2, newMap.size()); 412 assertEquals(1, newColl.size()); 413 414 map.put("C", "4"); 415 assertEquals(3, map.size()); 416 assertEquals(2, newMap.size()); 417 assertEquals(1, newColl.size()); 418 } 419 420 } 421 | Popular Tags |