1 16 package org.apache.commons.collections.bidimap; 17 18 import java.util.Collection ; 19 import java.util.HashMap ; 20 import java.util.Iterator ; 21 import java.util.Map ; 22 import java.util.Set ; 23 24 import org.apache.commons.collections.BidiMap; 25 import org.apache.commons.collections.BulkTest; 26 import org.apache.commons.collections.MapIterator; 27 import org.apache.commons.collections.iterators.AbstractTestMapIterator; 28 import org.apache.commons.collections.map.AbstractTestMap; 29 30 38 public abstract class AbstractTestBidiMap extends AbstractTestMap { 39 40 private static final Object [][] entriesKV = 42 new Object [][] { 43 new Object [] { "key1", "value1" }, 44 new Object [] { "key2", "value2" }, 45 new Object [] { "key3", "value3" } 46 }; 47 private static final Object [][] entriesVK = 48 new Object [][] { 49 new Object [] { "value1", "key1" }, 50 new Object [] { "value2", "key2" }, 51 new Object [] { "value3", "key3" } 52 }; 53 protected final Object [][] entries; 54 55 public AbstractTestBidiMap(String testName) { 56 super(testName); 57 entries = entriesKV; 58 } 59 60 public AbstractTestBidiMap() { 61 super("Inverse"); 62 entries = entriesVK; 63 } 64 65 71 public abstract BidiMap makeEmptyBidiMap(); 72 73 78 public BidiMap makeFullBidiMap() { 79 final BidiMap map = makeEmptyBidiMap(); 80 for (int i = 0; i < entries.length; i++) { 81 map.put(entries[i][0], entries[i][1]); 82 } 83 return map; 84 } 85 86 89 public final Map makeEmptyMap() { 90 return makeEmptyBidiMap(); 91 } 92 93 96 public boolean isAllowDuplicateValues() { 97 return false; 98 } 99 100 103 public String getCompatibilityVersion() { 104 return "3"; 105 } 106 107 public void testBidiPut() { 110 if (isPutAddSupported() == false || isPutChangeSupported() == false) return; 111 112 BidiMap map = makeEmptyBidiMap(); 113 BidiMap inverse = map.inverseBidiMap(); 114 assertEquals(0, map.size()); 115 assertEquals(map.size(), inverse.size()); 116 117 map.put("A", "B"); 118 assertEquals(1, map.size()); 119 assertEquals(map.size(), inverse.size()); 120 assertEquals("B", map.get("A")); 121 assertEquals("A", inverse.get("B")); 122 123 map.put("A", "C"); 124 assertEquals(1, map.size()); 125 assertEquals(map.size(), inverse.size()); 126 assertEquals("C", map.get("A")); 127 assertEquals("A", inverse.get("C")); 128 129 map.put("B", "C"); 130 assertEquals(1, map.size()); 131 assertEquals(map.size(), inverse.size()); 132 assertEquals("C", map.get("B")); 133 assertEquals("B", inverse.get("C")); 134 135 map.put("E", "F"); 136 assertEquals(2, map.size()); 137 assertEquals(map.size(), inverse.size()); 138 assertEquals("F", map.get("E")); 139 assertEquals("E", inverse.get("F")); 140 } 141 142 147 public void verify() { 148 verifyInverse(); 149 super.verify(); 150 } 151 152 public void verifyInverse() { 153 assertEquals(map.size(), ((BidiMap) map).inverseBidiMap().size()); 154 Map map1 = new HashMap (map); 155 Map map2 = new HashMap (((BidiMap) map).inverseBidiMap()); 156 Set keys1 = map1.keySet(); 157 Set keys2 = map2.keySet(); 158 Collection values1 = map1.values(); 159 Collection values2 = map2.values(); 160 assertEquals(true, keys1.containsAll(values2)); 161 assertEquals(true, values2.containsAll(keys1)); 162 assertEquals(true, values1.containsAll(keys2)); 163 assertEquals(true, keys2.containsAll(values1)); 164 } 165 166 public void testBidiGetKey() { 169 doTestGetKey(makeFullBidiMap(), entries[0][0], entries[0][1]); 170 } 171 172 public void testBidiGetKeyInverse() { 173 doTestGetKey( 174 makeFullBidiMap().inverseBidiMap(), 175 entries[0][1], 176 entries[0][0]); 177 } 178 179 private final void doTestGetKey(BidiMap map, Object key, Object value) { 180 assertEquals("Value not found for key.", value, map.get(key)); 181 assertEquals("Key not found for value.", key, map.getKey(value)); 182 } 183 184 public void testBidiInverse() { 187 final BidiMap map = makeFullBidiMap(); 188 final BidiMap inverseMap = map.inverseBidiMap(); 189 190 assertSame( 191 "Inverse of inverse is not equal to original.", 192 map, 193 inverseMap.inverseBidiMap()); 194 195 assertEquals( 196 "Value not found for key.", 197 entries[0][0], 198 inverseMap.get(entries[0][1])); 199 200 assertEquals( 201 "Key not found for value.", 202 entries[0][1], 203 inverseMap.getKey(entries[0][0])); 204 } 205 206 public void testBidiModifyEntrySet() { 208 if (isSetValueSupported() == false) return; 209 210 modifyEntrySet(makeFullBidiMap()); 211 modifyEntrySet(makeFullBidiMap().inverseBidiMap()); 212 } 213 214 private final void modifyEntrySet(BidiMap map) { 215 final Map.Entry entry = (Map.Entry )map.entrySet().iterator().next(); 217 218 final Object key = entry.getKey(); 220 final Object oldValue = entry.getValue(); 221 222 final Object newValue = "newValue"; 224 entry.setValue(newValue); 225 226 assertEquals( 227 "Modifying entrySet did not affect underlying Map.", 228 newValue, 229 map.get(key)); 230 231 assertNull( 232 "Modifying entrySet did not affect inverse Map.", 233 map.getKey(oldValue)); 234 } 235 236 public void testBidiClear() { 238 if (isRemoveSupported() == false) { 239 try { 240 makeFullBidiMap().clear(); 241 fail(); 242 } catch(UnsupportedOperationException ex) {} 243 return; 244 } 245 246 BidiMap map = makeFullBidiMap(); 247 map.clear(); 248 assertTrue("Map was not cleared.", map.isEmpty()); 249 assertTrue("Inverse map was not cleared.", map.inverseBidiMap().isEmpty()); 250 251 map = makeFullBidiMap().inverseBidiMap(); 253 map.clear(); 254 assertTrue("Map was not cleared.", map.isEmpty()); 255 assertTrue("Inverse map was not cleared.", map.inverseBidiMap().isEmpty()); 256 257 } 258 259 public void testBidiRemove() { 261 if (isRemoveSupported() == false) { 262 try { 263 makeFullBidiMap().remove(entries[0][0]); 264 fail(); 265 } catch(UnsupportedOperationException ex) {} 266 try { 267 makeFullBidiMap().removeValue(entries[0][1]); 268 fail(); 269 } catch(UnsupportedOperationException ex) {} 270 return; 271 } 272 273 remove(makeFullBidiMap(), entries[0][0]); 274 remove(makeFullBidiMap().inverseBidiMap(), entries[0][1]); 275 276 removeValue(makeFullBidiMap(), entries[0][1]); 277 removeValue(makeFullBidiMap().inverseBidiMap(), entries[0][0]); 278 279 assertEquals(null, makeFullBidiMap().removeValue("NotPresent")); 280 } 281 282 private final void remove(BidiMap map, Object key) { 283 final Object value = map.remove(key); 284 assertTrue("Key was not removed.", !map.containsKey(key)); 285 assertNull("Value was not removed.", map.getKey(value)); 286 } 287 288 private final void removeValue(BidiMap map, Object value) { 289 final Object key = map.removeValue(value); 290 assertTrue("Key was not removed.", !map.containsKey(key)); 291 assertNull("Value was not removed.", map.getKey(value)); 292 } 293 294 public void testBidiKeySetValuesOrder() { 296 resetFull(); 297 Iterator keys = map.keySet().iterator(); 298 Iterator values = map.values().iterator(); 299 for (; keys.hasNext() && values.hasNext();) { 300 Object key = keys.next(); 301 Object value = values.next(); 302 assertSame(map.get(key), value); 303 } 304 assertEquals(false, keys.hasNext()); 305 assertEquals(false, values.hasNext()); 306 } 307 308 public void testBidiRemoveByKeySet() { 310 if (isRemoveSupported() == false) return; 311 312 removeByKeySet(makeFullBidiMap(), entries[0][0], entries[0][1]); 313 removeByKeySet(makeFullBidiMap().inverseBidiMap(), entries[0][1], entries[0][0]); 314 } 315 316 private final void removeByKeySet(BidiMap map, Object key, Object value) { 317 map.keySet().remove(key); 318 319 assertTrue("Key was not removed.", !map.containsKey(key)); 320 assertTrue("Value was not removed.", !map.containsValue(value)); 321 322 assertTrue( 323 "Key was not removed from inverse map.", 324 !map.inverseBidiMap().containsValue(key)); 325 assertTrue( 326 "Value was not removed from inverse map.", 327 !map.inverseBidiMap().containsKey(value)); 328 } 329 330 public void testBidiRemoveByEntrySet() { 332 if (isRemoveSupported() == false) return; 333 334 removeByEntrySet(makeFullBidiMap(), entries[0][0], entries[0][1]); 335 removeByEntrySet(makeFullBidiMap().inverseBidiMap(), entries[0][1], entries[0][0]); 336 } 337 338 private final void removeByEntrySet(BidiMap map, Object key, Object value) { 339 Map temp = new HashMap (); 340 temp.put(key, value); 341 map.entrySet().remove(temp.entrySet().iterator().next()); 342 343 assertTrue("Key was not removed.", !map.containsKey(key)); 344 assertTrue("Value was not removed.", !map.containsValue(value)); 345 346 assertTrue( 347 "Key was not removed from inverse map.", 348 !map.inverseBidiMap().containsValue(key)); 349 assertTrue( 350 "Value was not removed from inverse map.", 351 !map.inverseBidiMap().containsKey(value)); 352 } 353 354 public BulkTest bulkTestMapEntrySet() { 356 return new TestBidiMapEntrySet(); 357 } 358 359 public class TestBidiMapEntrySet extends TestMapEntrySet { 360 public TestBidiMapEntrySet() { 361 super(); 362 } 363 public void testMapEntrySetIteratorEntrySetValueCrossCheck() { 364 Object key1 = getSampleKeys()[0]; 365 Object key2 = getSampleKeys()[1]; 366 Object newValue1 = getNewSampleValues()[0]; 367 Object newValue2 = getNewSampleValues()[1]; 368 369 resetFull(); 370 Iterator it = TestBidiMapEntrySet.this.collection.iterator(); 373 Map.Entry entry1 = getEntry(it, key1); 374 it = TestBidiMapEntrySet.this.collection.iterator(); 375 Map.Entry entry2 = getEntry(it, key2); 376 Iterator itConfirmed = TestBidiMapEntrySet.this.confirmed.iterator(); 377 Map.Entry entryConfirmed1 = getEntry(itConfirmed, key1); 378 itConfirmed = TestBidiMapEntrySet.this.confirmed.iterator(); 379 Map.Entry entryConfirmed2 = getEntry(itConfirmed, key2); 380 TestBidiMapEntrySet.this.verify(); 381 382 if (isSetValueSupported() == false) { 383 try { 384 entry1.setValue(newValue1); 385 } catch (UnsupportedOperationException ex) { 386 } 387 return; 388 } 389 390 entry1.setValue(newValue1); 392 entryConfirmed1.setValue(newValue1); 393 entry2.setValue(newValue2); 394 entryConfirmed2.setValue(newValue2); 395 396 try { 399 entry2.setValue(newValue1); } catch (IllegalArgumentException ex) { 401 return; } 403 entryConfirmed2.setValue(newValue1); 404 AbstractTestBidiMap.this.confirmed.remove(key1); 405 assertEquals(newValue1, entry2.getValue()); 406 assertEquals(true, AbstractTestBidiMap.this.map.containsKey(entry2.getKey())); 407 assertEquals(true, AbstractTestBidiMap.this.map.containsValue(newValue1)); 408 assertEquals(newValue1, AbstractTestBidiMap.this.map.get(entry2.getKey())); 409 assertEquals(false, AbstractTestBidiMap.this.map.containsKey(key1)); 410 assertEquals(false, AbstractTestBidiMap.this.map.containsValue(newValue2)); 411 TestBidiMapEntrySet.this.verify(); 412 413 it.next(); if (isRemoveSupported()) { 416 it.remove(); 417 } 418 } 419 } 420 421 public BulkTest bulkTestInverseMap() { 422 return new TestInverseBidiMap(this); 423 } 424 425 public class TestInverseBidiMap extends AbstractTestBidiMap { 426 final AbstractTestBidiMap main; 427 428 public TestInverseBidiMap(AbstractTestBidiMap main) { 429 super(); 430 this.main = main; 431 } 432 public BidiMap makeEmptyBidiMap() { 433 return main.makeEmptyBidiMap().inverseBidiMap(); 434 } 435 public BidiMap makeFullBidiMap() { 436 return main.makeFullBidiMap().inverseBidiMap(); 437 } 438 public Map makeFullMap() { 439 return ((BidiMap) main.makeFullMap()).inverseBidiMap(); 440 } 441 public Object [] getSampleKeys() { 442 return main.getSampleValues(); 443 } 444 public Object [] getSampleValues() { 445 return main.getSampleKeys(); 446 } 447 448 public String getCompatibilityVersion() { 449 return main.getCompatibilityVersion(); 450 } 451 public boolean isAllowNullKey() { 452 return main.isAllowNullKey(); 453 } 454 public boolean isAllowNullValue() { 455 return main.isAllowNullValue(); 456 } 457 public boolean isPutAddSupported() { 458 return main.isPutAddSupported(); 459 } 460 public boolean isPutChangeSupported() { 461 return main.isPutChangeSupported(); 462 } 463 public boolean isSetValueSupported() { 464 return main.isSetValueSupported(); 465 } 466 public boolean isRemoveSupported() { 467 return main.isRemoveSupported(); 468 } 469 470 } 471 472 public BulkTest bulkTestBidiMapIterator() { 474 return new TestBidiMapIterator(); 475 } 476 477 public class TestBidiMapIterator extends AbstractTestMapIterator { 478 public TestBidiMapIterator() { 479 super("TestBidiMapIterator"); 480 } 481 482 public Object [] addSetValues() { 483 return AbstractTestBidiMap.this.getNewSampleValues(); 484 } 485 486 public boolean supportsRemove() { 487 return AbstractTestBidiMap.this.isRemoveSupported(); 488 } 489 490 public boolean supportsSetValue() { 491 return AbstractTestBidiMap.this.isSetValueSupported(); 492 } 493 494 public MapIterator makeEmptyMapIterator() { 495 resetEmpty(); 496 return ((BidiMap) AbstractTestBidiMap.this.map).mapIterator(); 497 } 498 499 public MapIterator makeFullMapIterator() { 500 resetFull(); 501 return ((BidiMap) AbstractTestBidiMap.this.map).mapIterator(); 502 } 503 504 public Map getMap() { 505 return AbstractTestBidiMap.this.map; 507 } 508 509 public Map getConfirmedMap() { 510 return AbstractTestBidiMap.this.confirmed; 512 } 513 514 public void verify() { 515 super.verify(); 516 AbstractTestBidiMap.this.verify(); 517 } 518 } 519 520 public void testBidiMapIteratorSet() { 522 Object newValue1 = getOtherValues()[0]; 523 Object newValue2 = getOtherValues()[1]; 524 525 resetFull(); 526 BidiMap bidi = (BidiMap) map; 527 MapIterator it = bidi.mapIterator(); 528 assertEquals(true, it.hasNext()); 529 Object key1 = it.next(); 530 531 if (isSetValueSupported() == false) { 532 try { 533 it.setValue(newValue1); 534 fail(); 535 } catch (UnsupportedOperationException ex) { 536 } 537 return; 538 } 539 540 it.setValue(newValue1); 541 confirmed.put(key1, newValue1); 542 assertSame(key1, it.getKey()); 543 assertSame(newValue1, it.getValue()); 544 assertEquals(true, bidi.containsKey(key1)); 545 assertEquals(true, bidi.containsValue(newValue1)); 546 assertEquals(newValue1, bidi.get(key1)); 547 verify(); 548 549 it.setValue(newValue1); confirmed.put(key1, newValue1); 551 assertSame(key1, it.getKey()); 552 assertSame(newValue1, it.getValue()); 553 assertEquals(true, bidi.containsKey(key1)); 554 assertEquals(true, bidi.containsValue(newValue1)); 555 assertEquals(newValue1, bidi.get(key1)); 556 verify(); 557 558 Object key2 = it.next(); 559 it.setValue(newValue2); 560 confirmed.put(key2, newValue2); 561 assertSame(key2, it.getKey()); 562 assertSame(newValue2, it.getValue()); 563 assertEquals(true, bidi.containsKey(key2)); 564 assertEquals(true, bidi.containsValue(newValue2)); 565 assertEquals(newValue2, bidi.get(key2)); 566 verify(); 567 568 try { 571 it.setValue(newValue1); fail(); 573 } catch (IllegalArgumentException ex) { 574 return; } 576 confirmed.put(key2, newValue1); 577 AbstractTestBidiMap.this.confirmed.remove(key1); 578 assertEquals(newValue1, it.getValue()); 579 assertEquals(true, bidi.containsKey(it.getKey())); 580 assertEquals(true, bidi.containsValue(newValue1)); 581 assertEquals(newValue1, bidi.get(it.getKey())); 582 assertEquals(false, bidi.containsKey(key1)); 583 assertEquals(false, bidi.containsValue(newValue2)); 584 verify(); 585 586 it.next(); if (isRemoveSupported()) { 589 it.remove(); 590 } 591 } 592 593 } 594 | Popular Tags |