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 if (isPutAddSupported()) { 862 if (isAllowNullKey()) { 863 map.put(null, values[0]); 864 } else { 865 try { 866 map.put(null, values[0]); 867 fail("put(null, value) should throw NPE/IAE"); 868 } catch (NullPointerException ex) { 869 } catch (IllegalArgumentException ex) {} 870 } 871 } 872 } 873 874 877 public void testMapPutNullValue() { 878 resetFull(); 879 Object [] keys = getSampleKeys(); 880 881 if (isPutAddSupported()) { 882 if (isAllowNullValue()) { 883 map.put(keys[0], null); 884 } else { 885 try { 886 map.put(keys[0], null); 887 fail("put(key, null) should throw NPE/IAE"); 888 } catch (NullPointerException ex) { 889 } catch (IllegalArgumentException ex) {} 890 } 891 } 892 } 893 894 897 public void testMapPutAll() { 898 if (!isPutAddSupported()) { 899 if (!isPutChangeSupported()) { 900 Map temp = makeFullMap(); 901 resetEmpty(); 902 try { 903 map.putAll(temp); 904 fail("Expected UnsupportedOperationException on putAll"); 905 } catch (UnsupportedOperationException ex) {} 906 } 907 return; 908 } 909 910 resetEmpty(); 911 912 Map m2 = makeFullMap(); 913 914 map.putAll(m2); 915 confirmed.putAll(m2); 916 verify(); 917 918 resetEmpty(); 919 920 m2 = makeConfirmedMap(); 921 Object [] keys = getSampleKeys(); 922 Object [] values = getSampleValues(); 923 for(int i = 0; i < keys.length; i++) { 924 m2.put(keys[i], values[i]); 925 } 926 927 map.putAll(m2); 928 confirmed.putAll(m2); 929 verify(); 930 } 931 932 935 public void testMapRemove() { 936 if (!isRemoveSupported()) { 937 try { 938 resetFull(); 939 map.remove(map.keySet().iterator().next()); 940 fail("Expected UnsupportedOperationException on remove"); 941 } catch (UnsupportedOperationException ex) {} 942 return; 943 } 944 945 resetEmpty(); 946 947 Object [] keys = getSampleKeys(); 948 Object [] values = getSampleValues(); 949 for(int i = 0; i < keys.length; i++) { 950 Object o = map.remove(keys[i]); 951 assertTrue("First map.remove should return null", o == null); 952 } 953 verify(); 954 955 resetFull(); 956 957 for(int i = 0; i < keys.length; i++) { 958 Object o = map.remove(keys[i]); 959 confirmed.remove(keys[i]); 960 verify(); 961 962 assertEquals("map.remove with valid key should return value", 963 values[i], o); 964 } 965 966 Object [] other = getOtherKeys(); 967 968 resetFull(); 969 int size = map.size(); 970 for (int i = 0; i < other.length; i++) { 971 Object o = map.remove(other[i]); 972 assertEquals("map.remove for nonexistent key should return null", 973 o, null); 974 assertEquals("map.remove for nonexistent key should not " + 975 "shrink map", size, map.size()); 976 } 977 verify(); 978 } 979 980 985 public void testValuesClearChangesMap() { 986 if (!isRemoveSupported()) return; 987 988 resetFull(); 990 Collection values = map.values(); 991 assertTrue(map.size() > 0); 992 assertTrue(values.size() > 0); 993 values.clear(); 994 assertTrue(map.size() == 0); 995 assertTrue(values.size() == 0); 996 997 resetFull(); 999 values = map.values(); 1000 assertTrue(map.size() > 0); 1001 assertTrue(values.size() > 0); 1002 map.clear(); 1003 assertTrue(map.size() == 0); 1004 assertTrue(values.size() == 0); 1005 } 1006 1007 1011 public void testKeySetClearChangesMap() { 1012 if (!isRemoveSupported()) return; 1013 1014 resetFull(); 1016 Set keySet = map.keySet(); 1017 assertTrue(map.size() > 0); 1018 assertTrue(keySet.size() > 0); 1019 keySet.clear(); 1020 assertTrue(map.size() == 0); 1021 assertTrue(keySet.size() == 0); 1022 1023 resetFull(); 1025 keySet = map.keySet(); 1026 assertTrue(map.size() > 0); 1027 assertTrue(keySet.size() > 0); 1028 map.clear(); 1029 assertTrue(map.size() == 0); 1030 assertTrue(keySet.size() == 0); 1031 } 1032 1033 1037 public void testEntrySetClearChangesMap() { 1038 if (!isRemoveSupported()) return; 1039 1040 resetFull(); 1042 Set entrySet = map.entrySet(); 1043 assertTrue(map.size() > 0); 1044 assertTrue(entrySet.size() > 0); 1045 entrySet.clear(); 1046 assertTrue(map.size() == 0); 1047 assertTrue(entrySet.size() == 0); 1048 1049 resetFull(); 1051 entrySet = map.entrySet(); 1052 assertTrue(map.size() > 0); 1053 assertTrue(entrySet.size() > 0); 1054 map.clear(); 1055 assertTrue(map.size() == 0); 1056 assertTrue(entrySet.size() == 0); 1057 } 1058 1059 public void testEntrySetContains1() { 1061 resetFull(); 1062 Set entrySet = map.entrySet(); 1063 Map.Entry entry = (Map.Entry ) entrySet.iterator().next(); 1064 assertEquals(true, entrySet.contains(entry)); 1065 } 1066 public void testEntrySetContains2() { 1067 resetFull(); 1068 Set entrySet = map.entrySet(); 1069 Map.Entry entry = (Map.Entry ) entrySet.iterator().next(); 1070 Map.Entry test = cloneMapEntry(entry); 1071 assertEquals(true, entrySet.contains(test)); 1072 } 1073 public void testEntrySetContains3() { 1074 resetFull(); 1075 Set entrySet = map.entrySet(); 1076 Map.Entry entry = (Map.Entry ) entrySet.iterator().next(); 1077 HashMap temp = new HashMap (); 1078 temp.put(entry.getKey(), "A VERY DIFFERENT VALUE"); 1079 Map.Entry test = (Map.Entry ) temp.entrySet().iterator().next(); 1080 assertEquals(false, entrySet.contains(test)); 1081 } 1082 1083 public void testEntrySetRemove1() { 1084 if (!isRemoveSupported()) return; 1085 resetFull(); 1086 int size = map.size(); 1087 Set entrySet = map.entrySet(); 1088 Map.Entry entry = (Map.Entry ) entrySet.iterator().next(); 1089 Object key = entry.getKey(); 1090 1091 assertEquals(true, entrySet.remove(entry)); 1092 assertEquals(false, map.containsKey(key)); 1093 assertEquals(size - 1, map.size()); 1094 } 1095 public void testEntrySetRemove2() { 1096 if (!isRemoveSupported()) return; 1097 resetFull(); 1098 int size = map.size(); 1099 Set entrySet = map.entrySet(); 1100 Map.Entry entry = (Map.Entry ) entrySet.iterator().next(); 1101 Object key = entry.getKey(); 1102 Map.Entry test = cloneMapEntry(entry); 1103 1104 assertEquals(true, entrySet.remove(test)); 1105 assertEquals(false, map.containsKey(key)); 1106 assertEquals(size - 1, map.size()); 1107 } 1108 public void testEntrySetRemove3() { 1109 if (!isRemoveSupported()) return; 1110 resetFull(); 1111 int size = map.size(); 1112 Set entrySet = map.entrySet(); 1113 Map.Entry entry = (Map.Entry ) entrySet.iterator().next(); 1114 Object key = entry.getKey(); 1115 HashMap temp = new HashMap (); 1116 temp.put(entry.getKey(), "A VERY DIFFERENT VALUE"); 1117 Map.Entry test = (Map.Entry ) temp.entrySet().iterator().next(); 1118 1119 assertEquals(false, entrySet.remove(test)); 1120 assertEquals(true, map.containsKey(key)); 1121 assertEquals(size, map.size()); 1122 } 1123 1124 1140 public void testValuesRemoveChangesMap() { 1141 resetFull(); 1142 Object [] sampleValues = getSampleValues(); 1143 Collection values = map.values(); 1144 for (int i = 0; i < sampleValues.length; i++) { 1145 if (map.containsValue(sampleValues[i])) { 1146 int j = 0; while (values.contains(sampleValues[i]) && j < 10000) { 1148 try { 1149 values.remove(sampleValues[i]); 1150 } catch (UnsupportedOperationException e) { 1151 return; 1153 } 1154 j++; 1155 } 1156 assertTrue("values().remove(obj) is broken", j < 10000); 1157 assertTrue( 1158 "Value should have been removed from the underlying map.", 1159 !map.containsValue(sampleValues[i])); 1160 } 1161 } 1162 } 1163 1164 1169 public void testKeySetRemoveChangesMap() { 1170 resetFull(); 1171 Object [] sampleKeys = getSampleKeys(); 1172 Set keys = map.keySet(); 1173 for (int i = 0; i < sampleKeys.length; i++) { 1174 try { 1175 keys.remove(sampleKeys[i]); 1176 } catch (UnsupportedOperationException e) { 1177 return; 1179 } 1180 assertTrue( 1181 "Key should have been removed from the underlying map.", 1182 !map.containsKey(sampleKeys[i])); 1183 } 1184 } 1185 1186 1191 1192 1200 private Map.Entry [] makeEntryArray(Object [] keys, Object [] values) { 1201 Map.Entry [] result = new Map.Entry [keys.length]; 1202 for (int i = 0; i < keys.length; i++) { 1203 Map map = makeConfirmedMap(); 1204 map.put(keys[i], values[i]); 1205 result[i] = (Map.Entry ) map.entrySet().iterator().next(); 1206 } 1207 return result; 1208 } 1209 1210 1211 1219 public BulkTest bulkTestMapEntrySet() { 1220 return new TestMapEntrySet(); 1221 } 1222 1223 public class TestMapEntrySet extends AbstractTestSet { 1224 public TestMapEntrySet() { 1225 super("MapEntrySet"); 1226 } 1227 1228 public Object [] getFullElements() { 1230 Object [] k = getSampleKeys(); 1231 Object [] v = getSampleValues(); 1232 return makeEntryArray(k, v); 1233 } 1234 1235 public Object [] getOtherElements() { 1237 Object [] k = getOtherKeys(); 1238 Object [] v = getOtherValues(); 1239 return makeEntryArray(k, v); 1240 } 1241 1242 public Set makeEmptySet() { 1243 return makeEmptyMap().entrySet(); 1244 } 1245 1246 public Set makeFullSet() { 1247 return makeFullMap().entrySet(); 1248 } 1249 1250 public boolean isAddSupported() { 1251 return false; 1253 } 1254 public boolean isRemoveSupported() { 1255 return AbstractTestMap.this.isRemoveSupported(); 1257 } 1258 public boolean isGetStructuralModify() { 1259 return AbstractTestMap.this.isGetStructuralModify(); 1260 } 1261 public boolean isTestSerialization() { 1262 return false; 1263 } 1264 1265 public void resetFull() { 1266 AbstractTestMap.this.resetFull(); 1267 collection = map.entrySet(); 1268 TestMapEntrySet.this.confirmed = AbstractTestMap.this.confirmed.entrySet(); 1269 } 1270 1271 public void resetEmpty() { 1272 AbstractTestMap.this.resetEmpty(); 1273 collection = map.entrySet(); 1274 TestMapEntrySet.this.confirmed = AbstractTestMap.this.confirmed.entrySet(); 1275 } 1276 1277 public void testMapEntrySetIteratorEntry() { 1278 resetFull(); 1279 Iterator it = collection.iterator(); 1280 int count = 0; 1281 while (it.hasNext()) { 1282 Map.Entry entry = (Map.Entry ) it.next(); 1283 assertEquals(true, AbstractTestMap.this.map.containsKey(entry.getKey())); 1284 assertEquals(true, AbstractTestMap.this.map.containsValue(entry.getValue())); 1285 if (isGetStructuralModify() == false) { 1286 assertEquals(AbstractTestMap.this.map.get(entry.getKey()), entry.getValue()); 1287 } 1288 count++; 1289 } 1290 assertEquals(collection.size(), count); 1291 } 1292 1293 public void testMapEntrySetIteratorEntrySetValue() { 1294 Object key1 = getSampleKeys()[0]; 1295 Object key2 = (getSampleKeys().length ==1 ? getSampleKeys()[0] : getSampleKeys()[1]); 1296 Object newValue1 = getNewSampleValues()[0]; 1297 Object newValue2 = (getNewSampleValues().length ==1 ? getNewSampleValues()[0] : getNewSampleValues()[1]); 1298 1299 resetFull(); 1300 Iterator it = TestMapEntrySet.this.collection.iterator(); 1303 Map.Entry entry1 = getEntry(it, key1); 1304 it = TestMapEntrySet.this.collection.iterator(); 1305 Map.Entry entry2 = getEntry(it, key2); 1306 Iterator itConfirmed = TestMapEntrySet.this.confirmed.iterator(); 1307 Map.Entry entryConfirmed1 = getEntry(itConfirmed, key1); 1308 itConfirmed = TestMapEntrySet.this.confirmed.iterator(); 1309 Map.Entry entryConfirmed2 = getEntry(itConfirmed, key2); 1310 verify(); 1311 1312 if (isSetValueSupported() == false) { 1313 try { 1314 entry1.setValue(newValue1); 1315 } catch (UnsupportedOperationException ex) { 1316 } 1317 return; 1318 } 1319 1320 entry1.setValue(newValue1); 1321 entryConfirmed1.setValue(newValue1); 1322 assertEquals(newValue1, entry1.getValue()); 1323 assertEquals(true, AbstractTestMap.this.map.containsKey(entry1.getKey())); 1324 assertEquals(true, AbstractTestMap.this.map.containsValue(newValue1)); 1325 assertEquals(newValue1, AbstractTestMap.this.map.get(entry1.getKey())); 1326 verify(); 1327 1328 entry1.setValue(newValue1); 1329 entryConfirmed1.setValue(newValue1); 1330 assertEquals(newValue1, entry1.getValue()); 1331 assertEquals(true, AbstractTestMap.this.map.containsKey(entry1.getKey())); 1332 assertEquals(true, AbstractTestMap.this.map.containsValue(newValue1)); 1333 assertEquals(newValue1, AbstractTestMap.this.map.get(entry1.getKey())); 1334 verify(); 1335 1336 entry2.setValue(newValue2); 1337 entryConfirmed2.setValue(newValue2); 1338 assertEquals(newValue2, entry2.getValue()); 1339 assertEquals(true, AbstractTestMap.this.map.containsKey(entry2.getKey())); 1340 assertEquals(true, AbstractTestMap.this.map.containsValue(newValue2)); 1341 assertEquals(newValue2, AbstractTestMap.this.map.get(entry2.getKey())); 1342 verify(); 1343 } 1344 1345 public Map.Entry getEntry(Iterator itConfirmed, Object key) { 1346 Map.Entry entry = null; 1347 while (itConfirmed.hasNext()) { 1348 Map.Entry temp = (Map.Entry ) itConfirmed.next(); 1349 if (temp.getKey() == null) { 1350 if (key == null) { 1351 entry = temp; 1352 break; 1353 } 1354 } else if (temp.getKey().equals(key)) { 1355 entry = temp; 1356 break; 1357 } 1358 } 1359 assertNotNull("No matching entry in map for key '" + key + "'", entry); 1360 return entry; 1361 } 1362 1363 public void testMapEntrySetRemoveNonMapEntry() { 1364 if (isRemoveSupported() == false) return; 1365 resetFull(); 1366 assertEquals(false, getSet().remove(null)); 1367 assertEquals(false, getSet().remove(new Object ())); 1368 } 1369 1370 public void verify() { 1371 super.verify(); 1372 AbstractTestMap.this.verify(); 1373 } 1374 } 1375 1376 1377 1385 public BulkTest bulkTestMapKeySet() { 1386 return new TestMapKeySet(); 1387 } 1388 1389 public class TestMapKeySet extends AbstractTestSet { 1390 public TestMapKeySet() { 1391 super(""); 1392 } 1393 public Object [] getFullElements() { 1394 return getSampleKeys(); 1395 } 1396 1397 public Object [] getOtherElements() { 1398 return getOtherKeys(); 1399 } 1400 1401 public Set makeEmptySet() { 1402 return makeEmptyMap().keySet(); 1403 } 1404 1405 public Set makeFullSet() { 1406 return makeFullMap().keySet(); 1407 } 1408 1409 public boolean isNullSupported() { 1410 return AbstractTestMap.this.isAllowNullKey(); 1411 } 1412 public boolean isAddSupported() { 1413 return false; 1414 } 1415 public boolean isRemoveSupported() { 1416 return AbstractTestMap.this.isRemoveSupported(); 1417 } 1418 public boolean isTestSerialization() { 1419 return false; 1420 } 1421 1422 public void resetEmpty() { 1423 AbstractTestMap.this.resetEmpty(); 1424 collection = map.keySet(); 1425 TestMapKeySet.this.confirmed = AbstractTestMap.this.confirmed.keySet(); 1426 } 1427 1428 public void resetFull() { 1429 AbstractTestMap.this.resetFull(); 1430 collection = map.keySet(); 1431 TestMapKeySet.this.confirmed = AbstractTestMap.this.confirmed.keySet(); 1432 } 1433 1434 public void verify() { 1435 super.verify(); 1436 AbstractTestMap.this.verify(); 1437 } 1438 } 1439 1440 1441 1450 public BulkTest bulkTestMapValues() { 1451 return new TestMapValues(); 1452 } 1453 1454 public class TestMapValues extends AbstractTestCollection { 1455 public TestMapValues() { 1456 super(""); 1457 } 1458 1459 public Object [] getFullElements() { 1460 return getSampleValues(); 1461 } 1462 1463 public Object [] getOtherElements() { 1464 return getOtherValues(); 1465 } 1466 1467 public Collection makeCollection() { 1468 return makeEmptyMap().values(); 1469 } 1470 1471 public Collection makeFullCollection() { 1472 return makeFullMap().values(); 1473 } 1474 1475 public boolean isNullSupported() { 1476 return AbstractTestMap.this.isAllowNullKey(); 1477 } 1478 public boolean isAddSupported() { 1479 return false; 1480 } 1481 public boolean isRemoveSupported() { 1482 return AbstractTestMap.this.isRemoveSupported(); 1483 } 1484 public boolean isTestSerialization() { 1485 return false; 1486 } 1487 1488 public boolean areEqualElementsDistinguishable() { 1489 return true; 1492 } 1493 1494 public Collection makeConfirmedCollection() { 1495 return null; 1497 } 1498 1499 public Collection makeConfirmedFullCollection() { 1500 return null; 1502 } 1503 1504 public void resetFull() { 1505 AbstractTestMap.this.resetFull(); 1506 collection = map.values(); 1507 TestMapValues.this.confirmed = AbstractTestMap.this.confirmed.values(); 1508 } 1509 1510 public void resetEmpty() { 1511 AbstractTestMap.this.resetEmpty(); 1512 collection = map.values(); 1513 TestMapValues.this.confirmed = AbstractTestMap.this.confirmed.values(); 1514 } 1515 1516 public void verify() { 1517 super.verify(); 1518 AbstractTestMap.this.verify(); 1519 } 1520 1521 } 1525 1526 1527 1531 public void resetEmpty() { 1532 this.map = makeEmptyMap(); 1533 views(); 1534 this.confirmed = makeConfirmedMap(); 1535 } 1536 1537 1541 public void resetFull() { 1542 this.map = makeFullMap(); 1543 views(); 1544 this.confirmed = makeConfirmedMap(); 1545 Object [] k = getSampleKeys(); 1546 Object [] v = getSampleValues(); 1547 for (int i = 0; i < k.length; i++) { 1548 confirmed.put(k[i], v[i]); 1549 } 1550 } 1551 1552 1553 1556 private void views() { 1557 this.keySet = map.keySet(); 1558 this.values = map.values(); 1559 this.entrySet = map.entrySet(); 1560 } 1561 1562 1563 1573 public void verify() { 1574 verifyMap(); 1575 verifyEntrySet(); 1576 verifyKeySet(); 1577 verifyValues(); 1578 } 1579 1580 public void verifyMap() { 1581 int size = confirmed.size(); 1582 boolean empty = confirmed.isEmpty(); 1583 assertEquals("Map should be same size as HashMap", 1584 size, map.size()); 1585 assertEquals("Map should be empty if HashMap is", 1586 empty, map.isEmpty()); 1587 assertEquals("hashCodes should be the same", 1588 confirmed.hashCode(), map.hashCode()); 1589 assertTrue("Map should still equal HashMap", map.equals(confirmed)); 1594 } 1599 1600 public void verifyEntrySet() { 1601 int size = confirmed.size(); 1602 boolean empty = confirmed.isEmpty(); 1603 assertEquals("entrySet should be same size as HashMap's" + 1604 "\nTest: " + entrySet + "\nReal: " + confirmed.entrySet(), 1605 size, entrySet.size()); 1606 assertEquals("entrySet should be empty if HashMap is" + 1607 "\nTest: " + entrySet + "\nReal: " + confirmed.entrySet(), 1608 empty, entrySet.isEmpty()); 1609 assertTrue("entrySet should contain all HashMap's elements" + 1610 "\nTest: " + entrySet + "\nReal: " + confirmed.entrySet(), 1611 entrySet.containsAll(confirmed.entrySet())); 1612 assertEquals("entrySet hashCodes should be the same" + 1613 "\nTest: " + entrySet + "\nReal: " + confirmed.entrySet(), 1614 confirmed.entrySet().hashCode(), entrySet.hashCode()); 1615 assertEquals("Map's entry set should still equal HashMap's", 1616 confirmed.entrySet(), entrySet); 1617 } 1618 1619 public void verifyKeySet() { 1620 int size = confirmed.size(); 1621 boolean empty = confirmed.isEmpty(); 1622 assertEquals("keySet should be same size as HashMap's" + 1623 "\nTest: " + keySet + "\nReal: " + confirmed.keySet(), 1624 size, keySet.size()); 1625 assertEquals("keySet should be empty if HashMap is" + 1626 "\nTest: " + keySet + "\nReal: " + confirmed.keySet(), 1627 empty, keySet.isEmpty()); 1628 assertTrue("keySet should contain all HashMap's elements" + 1629 "\nTest: " + keySet + "\nReal: " + confirmed.keySet(), 1630 keySet.containsAll(confirmed.keySet())); 1631 assertEquals("keySet hashCodes should be the same" + 1632 "\nTest: " + keySet + "\nReal: " + confirmed.keySet(), 1633 confirmed.keySet().hashCode(), keySet.hashCode()); 1634 assertEquals("Map's key set should still equal HashMap's", 1635 confirmed.keySet(), keySet); 1636 } 1637 1638 public void verifyValues() { 1639 List known = new ArrayList (confirmed.values()); 1640 List test = new ArrayList (values); 1641 1642 int size = confirmed.size(); 1643 boolean empty = confirmed.isEmpty(); 1644 assertEquals("values should be same size as HashMap's" + 1645 "\nTest: " + test + "\nReal: " + known, 1646 size, values.size()); 1647 assertEquals("values should be empty if HashMap is" + 1648 "\nTest: " + test + "\nReal: " + known, 1649 empty, values.isEmpty()); 1650 assertTrue("values should contain all HashMap's elements" + 1651 "\nTest: " + test + "\nReal: " + known, 1652 test.containsAll(known)); 1653 assertTrue("values should contain all HashMap's elements" + 1654 "\nTest: " + test + "\nReal: " + known, 1655 known.containsAll(test)); 1656 for (Iterator it = known.iterator(); it.hasNext();) { 1658 boolean removed = test.remove(it.next()); 1659 assertTrue("Map's values should still equal HashMap's", removed); 1660 } 1661 assertTrue("Map's values should still equal HashMap's", test.isEmpty()); 1662 } 1663 1664 1665 1668 public void tearDown() throws Exception { 1669 map = null; 1670 keySet = null; 1671 entrySet = null; 1672 values = null; 1673 confirmed = null; 1674 } 1675 1676} 1677 | Popular Tags |