| 1 16 package org.apache.commons.collections.map; 17 18 import java.io.Serializable ; 19 import java.util.ArrayList ; 20 import java.util.Collection ; 21 import java.util.HashMap ; 22 import java.util.Iterator ; 23 import java.util.List ; 24 import java.util.Map ; 25 import java.util.Set ; 26 27 import org.apache.commons.collections.AbstractTestObject; 28 import org.apache.commons.collections.BulkTest; 29 import org.apache.commons.collections.collection.AbstractTestCollection; 30 import org.apache.commons.collections.set.AbstractTestSet; 31 32 121 public abstract class AbstractTestMap extends AbstractTestObject { 122 123 127 private static final boolean JDK12; 128 static { 129 String str = System.getProperty("java.version"); 130 JDK12 = str.startsWith("1.2"); 131 } 132 133 141 142 143 protected Map map; 144 145 146 protected Set entrySet; 147 148 149 protected Set keySet; 150 151 152 protected Collection values; 153 154 155 protected Map confirmed; 156 157 162 public AbstractTestMap(String testName) { 163 super(testName); 164 } 165 166 175 public boolean isPutAddSupported() { 176 return true; 177 } 178 179 188 public boolean isPutChangeSupported() { 189 return true; 190 } 191 192 201 public boolean isSetValueSupported() { 202 return isPutChangeSupported(); 203 } 204 205 213 public boolean isRemoveSupported() { 214 return true; 215 } 216 217 225 public boolean isGetStructuralModify() { 226 return false; 227 } 228 229 236 public boolean isSubMapViewsSerializable() { 237 return true; 238 } 239 240 248 public boolean isAllowNullKey() { 249 return true; 250 } 251 252 260 public boolean isAllowNullValue() { 261 return true; 262 } 263 264 272 public boolean isAllowDuplicateValues() { 273 return true; 274 } 275 276 283 public Object [] getSampleKeys() { 284 Object [] result = new Object [] { 285 "blah", "foo", "bar", "baz", "tmp", "gosh", "golly", "gee", 286 "hello", "goodbye", "we'll", "see", "you", "all", "again", 287 "key", 288 "key2", 289 (isAllowNullKey() && !JDK12) ? null : "nonnullkey" 290 }; 291 return result; 292 } 293 294 295 public Object [] getOtherKeys() { 296 return getOtherNonNullStringElements(); 297 } 298 299 public Object [] getOtherValues() { 300 return getOtherNonNullStringElements(); 301 } 302 303 311 public Object [] getOtherNonNullStringElements() { 312 return new Object [] { 313 "For","then","despite","space","I","would","be","brought", 314 "From","limits","far","remote","where","thou","dost","stay" 315 }; 316 } 317 318 327 public Object [] getSampleValues() { 328 Object [] result = new Object [] { 329 "blahv", "foov", "barv", "bazv", "tmpv", "goshv", "gollyv", "geev", 330 "hellov", "goodbyev", "we'llv", "seev", "youv", "allv", "againv", 331 (isAllowNullValue() && !JDK12) ? null : "nonnullvalue", 332 "value", 333 (isAllowDuplicateValues()) ? "value" : "value2", 334 }; 335 return result; 336 } 337 338 349 public Object [] getNewSampleValues() { 350 Object [] result = new Object [] { 351 (isAllowNullValue() && !JDK12 && isAllowDuplicateValues()) ? null : "newnonnullvalue", 352 "newvalue", 353 (isAllowDuplicateValues()) ? "newvalue" : "newvalue2", 354 "newblahv", "newfoov", "newbarv", "newbazv", "newtmpv", "newgoshv", 355 "newgollyv", "newgeev", "newhellov", "newgoodbyev", "newwe'llv", 356 "newseev", "newyouv", "newallv", "newagainv", 357 }; 358 return result; 359 } 360 361 365 public void addSampleMappings(Map m) { 366 367 Object [] keys = getSampleKeys(); 368 Object [] values = getSampleValues(); 369 370 for(int i = 0; i < keys.length; i++) { 371 try { 372 m.put(keys[i], values[i]); 373 } catch (NullPointerException exception) { 374 assertTrue("NullPointerException only allowed to be thrown " + 375 "if either the key or value is null.", 376 keys[i] == null || values[i] == null); 377 378 assertTrue("NullPointerException on null key, but " + 379 "isAllowNullKey is not overridden to return false.", 380 keys[i] == null || !isAllowNullKey()); 381 382 assertTrue("NullPointerException on null value, but " + 383 "isAllowNullValue is not overridden to return false.", 384 values[i] == null || !isAllowNullValue()); 385 386 assertTrue("Unknown reason for NullPointer.", false); 387 } 388 } 389 assertEquals("size must reflect number of mappings added.", 390 keys.length, m.size()); 391 } 392 393 399 public abstract Map makeEmptyMap(); 400 401 410 public Map makeFullMap() { 411 Map m = makeEmptyMap(); 412 addSampleMappings(m); 413 return m; 414 } 415 416 421 public Object makeObject() { 422 return makeEmptyMap(); 423 } 424 425 430 public Map makeConfirmedMap() { 431 return new HashMap (); 432 } 433 434 437 public Map.Entry cloneMapEntry(Map.Entry entry) { 438 HashMap map = new HashMap (); 439 map.put(entry.getKey(), entry.getValue()); 440 return (Map.Entry ) map.entrySet().iterator().next(); 441 } 442 443 446 public String getCompatibilityVersion() { 447 return super.getCompatibilityVersion(); 448 } 449 460 public void testSampleMappings() { 461 Object [] keys = getSampleKeys(); 462 Object [] values = getSampleValues(); 463 Object [] newValues = getNewSampleValues(); 464 465 assertTrue("failure in test: Must have keys returned from " + 466 "getSampleKeys.", keys != null); 467 468 assertTrue("failure in test: Must have values returned from " + 469 "getSampleValues.", values != null); 470 471 assertEquals("failure in test: not the same number of sample " + 474 "keys and values.", keys.length, values.length); 475 476 assertEquals("failure in test: not the same number of values and new values.", 477 values.length, newValues.length); 478 479 for(int i = 0; i < keys.length - 1; i++) { 481 for(int j = i + 1; j < keys.length; j++) { 482 assertTrue("failure in test: duplicate null keys.", 483 (keys[i] != null || keys[j] != null)); 484 assertTrue("failure in test: duplicate non-null key.", 485 (keys[i] == null || keys[j] == null || 486 (!keys[i].equals(keys[j]) && 487 !keys[j].equals(keys[i])))); 488 } 489 assertTrue("failure in test: found null key, but isNullKeySupported " + 490 "is false.", keys[i] != null || isAllowNullKey()); 491 assertTrue("failure in test: found null value, but isNullValueSupported " + 492 "is false.", values[i] != null || isAllowNullValue()); 493 assertTrue("failure in test: found null new value, but isNullValueSupported " + 494 "is false.", newValues[i] != null || isAllowNullValue()); 495 assertTrue("failure in test: values should not be the same as new value", 496 values[i] != newValues[i] && 497 (values[i] == null || !values[i].equals(newValues[i]))); 498 } 499 } 500 501 506 510 public void testMakeMap() { 511 Map em = makeEmptyMap(); 512 assertTrue("failure in test: makeEmptyMap must return a non-null map.", 513 em != null); 514 515 Map em2 = makeEmptyMap(); 516 assertTrue("failure in test: makeEmptyMap must return a non-null map.", 517 em != null); 518 519 assertTrue("failure in test: makeEmptyMap must return a new map " + 520 "with each invocation.", em != em2); 521 522 Map fm = makeFullMap(); 523 assertTrue("failure in test: makeFullMap must return a non-null map.", 524 fm != null); 525 526 Map fm2 = makeFullMap(); 527 assertTrue("failure in test: makeFullMap must return a non-null map.", 528 fm != null); 529 530 assertTrue("failure in test: makeFullMap must return a new map " + 531 "with each invocation.", fm != fm2); 532 } 533 534 537 public void testMapIsEmpty() { 538 resetEmpty(); 539 assertEquals("Map.isEmpty() should return true with an empty map", 540 true, map.isEmpty()); 541 verify(); 542 543 resetFull(); 544 assertEquals("Map.isEmpty() should return false with a non-empty map", 545 false, map.isEmpty()); 546 verify(); 547 } 548 549 552 public void testMapSize() { 553 resetEmpty(); 554 assertEquals("Map.size() should be 0 with an empty map", 555 0, map.size()); 556 verify(); 557 558 resetFull(); 559 assertEquals("Map.size() should equal the number of entries " + 560 "in the map", getSampleKeys().length, map.size()); 561 verify(); 562 } 563 564 572 public void testMapClear() { 573 if (!isRemoveSupported()) { 574 try { 575 resetFull(); 576 map.clear(); 577 fail("Expected UnsupportedOperationException on clear"); 578 } catch (UnsupportedOperationException ex) {} 579 return; 580 } 581 582 resetEmpty(); 583 map.clear(); 584 confirmed.clear(); 585 verify(); 586 587 resetFull(); 588 map.clear(); 589 confirmed.clear(); 590 verify(); 591 } 592 593 594 599 public void testMapContainsKey() { 600 Object [] keys = getSampleKeys(); 601 602 resetEmpty(); 603 for(int i = 0; i < keys.length; i++) { 604 assertTrue("Map must not contain key when map is empty", 605 !map.containsKey(keys[i])); 606 } 607 verify(); 608 609 resetFull(); 610 for(int i = 0; i < keys.length; i++) { 611 assertTrue("Map must contain key for a mapping in the map. " + 612 "Missing: " + keys[i], map.containsKey(keys[i])); 613 } 614 verify(); 615 } 616 617 622 public void testMapContainsValue() { 623 Object [] values = getSampleValues(); 624 625 resetEmpty(); 626 for(int i = 0; i < values.length; i++) { 627 assertTrue("Empty map must not contain value", 628 !map.containsValue(values[i])); 629 } 630 verify(); 631 632 resetFull(); 633 for(int i = 0; i < values.length; i++) { 634 assertTrue("Map must contain value for a mapping in the map.", 635 map.containsValue(values[i])); 636 } 637 verify(); 638 } 639 640 641 644 public void testMapEquals() { 645 resetEmpty(); 646 assertTrue("Empty maps unequal.", map.equals(confirmed)); 647 verify(); 648 649 resetFull(); 650 assertTrue("Full maps unequal.", map.equals(confirmed)); 651 verify(); 652 653 resetFull(); 654 Iterator iter = confirmed.keySet().iterator(); 657 iter.next(); 658 iter.remove(); 659 assertTrue("Different maps equal.", !map.equals(confirmed)); 660 661 resetFull(); 662 assertTrue("equals(null) returned true.", !map.equals(null)); 663 assertTrue("equals(new Object()) returned true.", 664 !map.equals(new Object ())); 665 verify(); 666 } 667 668 669 672 public void testMapGet() { 673 resetEmpty(); 674 675 Object [] keys = getSampleKeys(); 676 Object [] values = getSampleValues(); 677 678 for (int i = 0; i < keys.length; i++) { 679 assertTrue("Empty map.get() should return null.", 680 map.get(keys[i]) == null); 681 } 682 verify(); 683 684 resetFull(); 685 for (int i = 0; i < keys.length; i++) { 686 assertEquals("Full map.get() should return value from mapping.", 687 values[i], map.get(keys[i])); 688 } 689 } 690 691 694 public void testMapHashCode() { 695 resetEmpty(); 696 assertTrue("Empty maps have different hashCodes.", 697 map.hashCode() == confirmed.hashCode()); 698 699 resetFull(); 700 assertTrue("Equal maps have different hashCodes.", 701 map.hashCode() == confirmed.hashCode()); 702 } 703 704 713 public void testMapToString() { 714 resetEmpty(); 715 assertTrue("Empty map toString() should not return null", 716 map.toString() != null); 717 verify(); 718 719 resetFull(); 720 assertTrue("Empty map toString() should not return null", 721 map.toString() != null); 722 verify(); 723 } 724 725 726 730 public void testEmptyMapCompatibility() throws Exception { 731 738 739 Map map = makeEmptyMap(); 741 if (map instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) { 742 Map map2 = (Map ) readExternalFormFromDisk(getCanonicalEmptyCollectionName(map)); 743 assertEquals("Map is empty", 0, map2.size()); 744 } 745 } 746 747 751 public void testFullMapCompatibility() throws Exception { 752 759 760 Map map = makeFullMap(); 762 if (map instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) { 763 Map map2 = (Map ) readExternalFormFromDisk(getCanonicalFullCollectionName(map)); 764 assertEquals("Map is the right size", getSampleKeys().length, map2.size()); 765 } 766 } 767 768 771 public void testMapPut() { 772 resetEmpty(); 773 Object [] keys = getSampleKeys(); 774 Object [] values = getSampleValues(); 775 Object [] newValues = getNewSampleValues(); 776 777 if (isPutAddSupported()) { 778 for (int i = 0; i < keys.length; i++) { 779 Object o = map.put(keys[i], values[i]); 780 confirmed.put(keys[i], values[i]); 781 verify(); 782 assertTrue("First map.put should return null", o == null); 783 assertTrue("Map should contain key after put", 784 map.containsKey(keys[i])); 785 assertTrue("Map should contain value after put", 786 map.containsValue(values[i])); 787 } 788 if (isPutChangeSupported()) { 789 for (int i = 0; i < keys.length; i++) { 790 Object o = map.put(keys[i], newValues[i]); 791 confirmed.put(keys[i], newValues[i]); 792 verify(); 793 assertEquals("Map.put should return previous value when changed", 794 values[i], o); 795 assertTrue("Map should still contain key after put when changed", 796 map.containsKey(keys[i])); 797 assertTrue("Map should contain new value after put when changed", 798 map.containsValue(newValues[i])); 799 800 if (!isAllowDuplicateValues()) { 803 assertTrue("Map should not contain old value after put when changed", 804 !map.containsValue(values[i])); 805 } 806 } 807 } else { 808 try { 809 map.put(keys[0], newValues[0]); 811 fail("Expected IllegalArgumentException or UnsupportedOperationException on put (change)"); 812 } catch (IllegalArgumentException ex) { 813 } catch (UnsupportedOperationException ex) {} 814 } 815 816 } else if (isPutChangeSupported()) { 817 resetEmpty(); 818 try { 819 map.put(keys[0], values[0]); 820 fail("Expected UnsupportedOperationException or IllegalArgumentException on put (add) when fixed size"); 821 } catch (IllegalArgumentException ex) { 822 } catch (UnsupportedOperationException ex) { 823 } 824 825 resetFull(); 826 int i = 0; 827 for (Iterator it = map.keySet().iterator(); it.hasNext() && i < newValues.length; i++) { 828 Object key = it.next(); 829 Object o = map.put(key, newValues[i]); 830 Object value = confirmed.put(key, newValues[i]); 831 verify(); 832 assertEquals("Map.put should return previous value when changed", 833 value, o); 834 assertTrue("Map should still contain key after put when changed", 835 map.containsKey(key)); 836 assertTrue("Map should contain new value after put when changed", 837 map.containsValue(newValues[i])); 838 839 if (!isAllowDuplicateValues()) { 842 assertTrue("Map should not contain old value after put when changed", 843 !map.containsValue(values[i])); 844 } 845 } 846 } else { 847 try { 848 map.put(keys[0], values[0]); 849 fail("Expected UnsupportedOperationException on put (add)"); 850 } catch (UnsupportedOperationException ex) {} 851 } 852 } 853 854 857 public void testMapPutNullKey() { 858 resetFull(); 859 Object [] values = getSampleValues(); 860 861 |