1 16 package org.apache.commons.collections.collection; 17 18 import java.io.ByteArrayInputStream ; 19 import java.io.ByteArrayOutputStream ; 20 import java.io.ObjectInputStream ; 21 import java.io.ObjectOutputStream ; 22 import java.io.Serializable ; 23 import java.lang.reflect.Array ; 24 import java.util.ArrayList ; 25 import java.util.Arrays ; 26 import java.util.Collection ; 27 import java.util.Collections ; 28 import java.util.ConcurrentModificationException ; 29 import java.util.HashMap ; 30 import java.util.HashSet ; 31 import java.util.Iterator ; 32 import java.util.List ; 33 import java.util.Map ; 34 import java.util.NoSuchElementException ; 35 36 import org.apache.commons.collections.AbstractTestObject; 37 38 123 public abstract class AbstractTestCollection extends AbstractTestObject { 124 125 135 136 139 142 public Collection collection; 143 144 152 public Collection confirmed; 153 154 159 public AbstractTestCollection(String testName) { 160 super(testName); 161 } 162 163 186 public boolean areEqualElementsDistinguishable() { 187 return false; 188 } 189 190 198 public boolean isAddSupported() { 199 return true; 200 } 201 202 211 public boolean isRemoveSupported() { 212 return true; 213 } 214 215 219 public boolean isNullSupported() { 220 return true; 221 } 222 223 227 public boolean isFailFastSupported() { 228 return false; 229 } 230 231 235 public boolean isEqualsCheckable() { 236 return false; 237 } 238 239 244 public void verify() { 245 int confirmedSize = confirmed.size(); 246 assertEquals("Collection size should match confirmed collection's", 247 confirmedSize, collection.size()); 248 assertEquals("Collection isEmpty() result should match confirmed " + 249 " collection's", 250 confirmed.isEmpty(), collection.isEmpty()); 251 252 261 Object [] confirmedValues = new Object [confirmedSize]; 263 264 Iterator iter; 265 266 iter = confirmed.iterator(); 267 int pos = 0; 268 while(iter.hasNext()) { 269 confirmedValues[pos++] = iter.next(); 270 } 271 272 boolean[] matched = new boolean[confirmedSize]; 275 276 iter = collection.iterator(); 279 while(iter.hasNext()) { 280 Object o = iter.next(); 281 boolean match = false; 282 for(int i = 0; i < confirmedSize; i++) { 283 if(matched[i]) { 284 continue; 286 } 287 if(o == confirmedValues[i] || 288 (o != null && o.equals(confirmedValues[i]))) { 289 matched[i] = true; 291 match = true; 292 break; 293 } 294 } 295 if(!match) { 297 fail("Collection should not contain a value that the " + 298 "confirmed collection does not have: " + o + 299 "\nTest: " + collection + "\nReal: " + confirmed); 300 } 301 } 302 303 for(int i = 0; i < confirmedSize; i++) { 305 if(!matched[i]) { 306 fail("Collection should contain all values that are in the confirmed collection" + 308 "\nTest: " + collection + "\nReal: " + confirmed); 309 } 310 } 311 } 312 313 319 public void resetEmpty() { 320 this.collection = makeCollection(); 321 this.confirmed = makeConfirmedCollection(); 322 } 323 324 329 public void resetFull() { 330 this.collection = makeFullCollection(); 331 this.confirmed = makeConfirmedFullCollection(); 332 } 333 334 342 public abstract Collection makeConfirmedCollection(); 343 344 352 public abstract Collection makeConfirmedFullCollection(); 353 354 357 public abstract Collection makeCollection(); 358 359 367 public Collection makeFullCollection() { 368 Collection c = makeCollection(); 369 c.addAll(Arrays.asList(getFullElements())); 370 return c; 371 } 372 373 376 public Object makeObject() { 377 return makeCollection(); 378 } 379 380 383 public Map.Entry cloneMapEntry(Map.Entry entry) { 384 HashMap map = new HashMap (); 385 map.put(entry.getKey(), entry.getValue()); 386 return (Map.Entry ) map.entrySet().iterator().next(); 387 } 388 389 400 public Object [] getFullElements() { 401 if (isNullSupported()) { 402 ArrayList list = new ArrayList (); 403 list.addAll(Arrays.asList(getFullNonNullElements())); 404 list.add(4, null); 405 return list.toArray(); 406 } else { 407 return (Object []) getFullNonNullElements().clone(); 408 } 409 } 410 411 420 public Object [] getOtherElements() { 421 return getOtherNonNullElements(); 422 } 423 424 433 public Object [] getFullNonNullElements() { 434 return new Object [] { 435 new String (""), 436 new String ("One"), 437 new Integer (2), 438 "Three", 439 new Integer (4), 440 "One", 441 new Double (5), 442 new Float (6), 443 "Seven", 444 "Eight", 445 new String ("Nine"), 446 new Integer (10), 447 new Short ((short)11), 448 new Long (12), 449 "Thirteen", 450 "14", 451 "15", 452 new Byte ((byte)16) 453 }; 454 } 455 456 461 public Object [] getOtherNonNullElements() { 462 return new Object [] { 463 new Integer (0), 464 new Float (0), 465 new Double (0), 466 "Zero", 467 new Short ((short)0), 468 new Byte ((byte)0), 469 new Long (0), 470 new Character ('\u0000'), 471 "0" 472 }; 473 } 474 475 481 public Object [] getFullNonNullStringElements() { 482 return new Object [] { 483 "If","the","dull","substance","of","my","flesh","were","thought", 484 "Injurious","distance","could","not","stop","my","way", 485 }; 486 } 487 488 494 public Object [] getOtherNonNullStringElements() { 495 return new Object [] { 496 "For","then","despite","space","I","would","be","brought", 497 "From","limits","far","remote","where","thou","dost","stay" 498 }; 499 } 500 501 506 public void testCollectionAdd() { 507 if (!isAddSupported()) return; 508 509 Object [] elements = getFullElements(); 510 for (int i = 0; i < elements.length; i++) { 511 resetEmpty(); 512 boolean r = collection.add(elements[i]); 513 confirmed.add(elements[i]); 514 verify(); 515 assertTrue("Empty collection changed after add", r); 516 assertEquals("Collection size is 1 after first add", 1, collection.size()); 517 } 518 519 resetEmpty(); 520 int size = 0; 521 for (int i = 0; i < elements.length; i++) { 522 boolean r = collection.add(elements[i]); 523 confirmed.add(elements[i]); 524 verify(); 525 if (r) size++; 526 assertEquals("Collection size should grow after add", 527 size, collection.size()); 528 assertTrue("Collection should contain added element", 529 collection.contains(elements[i])); 530 } 531 } 532 533 534 537 public void testCollectionAddAll() { 538 if (!isAddSupported()) return; 539 540 resetEmpty(); 541 Object [] elements = getFullElements(); 542 boolean r = collection.addAll(Arrays.asList(elements)); 543 confirmed.addAll(Arrays.asList(elements)); 544 verify(); 545 assertTrue("Empty collection should change after addAll", r); 546 for (int i = 0; i < elements.length; i++) { 547 assertTrue("Collection should contain added element", 548 collection.contains(elements[i])); 549 } 550 551 resetFull(); 552 int size = collection.size(); 553 elements = getOtherElements(); 554 r = collection.addAll(Arrays.asList(elements)); 555 confirmed.addAll(Arrays.asList(elements)); 556 verify(); 557 assertTrue("Full collection should change after addAll", r); 558 for (int i = 0; i < elements.length; i++) { 559 assertTrue("Full collection should contain added element", 560 collection.contains(elements[i])); 561 } 562 assertEquals("Size should increase after addAll", 563 size + elements.length, collection.size()); 564 565 resetFull(); 566 size = collection.size(); 567 r = collection.addAll(Arrays.asList(getFullElements())); 568 confirmed.addAll(Arrays.asList(getFullElements())); 569 verify(); 570 if (r) { 571 assertTrue("Size should increase if addAll returns true", 572 size < collection.size()); 573 } else { 574 assertEquals("Size should not change if addAll returns false", 575 size, collection.size()); 576 } 577 } 578 579 580 584 public void testUnsupportedAdd() { 585 if (isAddSupported()) return; 586 587 resetEmpty(); 588 try { 589 collection.add(new Object ()); 590 fail("Emtpy collection should not support add."); 591 } catch (UnsupportedOperationException e) { 592 } 594 verify(); 597 598 try { 599 collection.addAll(Arrays.asList(getFullElements())); 600 fail("Emtpy collection should not support addAll."); 601 } catch (UnsupportedOperationException e) { 602 } 604 verify(); 607 608 resetFull(); 609 try { 610 collection.add(new Object ()); 611 fail("Full collection should not support add."); 612 } catch (UnsupportedOperationException e) { 613 } 615 verify(); 618 619 try { 620 collection.addAll(Arrays.asList(getOtherElements())); 621 fail("Full collection should not support addAll."); 622 } catch (UnsupportedOperationException e) { 623 } 625 verify(); 628 } 629 630 631 634 public void testCollectionClear() { 635 if (!isRemoveSupported()) return; 636 637 resetEmpty(); 638 collection.clear(); verify(); 640 641 resetFull(); 642 collection.clear(); 643 confirmed.clear(); 644 verify(); 645 } 646 647 648 651 public void testCollectionContains() { 652 Object [] elements; 653 654 resetEmpty(); 655 elements = getFullElements(); 656 for(int i = 0; i < elements.length; i++) { 657 assertTrue("Empty collection shouldn't contain element[" + i + "]", 658 !collection.contains(elements[i])); 659 } 660 verify(); 662 663 elements = getOtherElements(); 664 for(int i = 0; i < elements.length; i++) { 665 assertTrue("Empty collection shouldn't contain element[" + i + "]", 666 !collection.contains(elements[i])); 667 } 668 verify(); 670 671 resetFull(); 672 elements = getFullElements(); 673 for(int i = 0; i < elements.length; i++) { 674 assertTrue("Full collection should contain element[" + i + "]", 675 collection.contains(elements[i])); 676 } 677 verify(); 679 680 resetFull(); 681 elements = getOtherElements(); 682 for(int i = 0; i < elements.length; i++) { 683 assertTrue("Full collection shouldn't contain element", 684 !collection.contains(elements[i])); 685 } 686 } 687 688 689 692 public void testCollectionContainsAll() { 693 resetEmpty(); 694 Collection col = new HashSet (); 695 assertTrue("Every Collection should contain all elements of an " + 696 "empty Collection.", collection.containsAll(col)); 697 col.addAll(Arrays.asList(getOtherElements())); 698 assertTrue("Empty Collection shouldn't contain all elements of " + 699 "a non-empty Collection.", !collection.containsAll(col)); 700 verify(); 702 703 resetFull(); 704 assertTrue("Full collection shouldn't contain other elements", 705 !collection.containsAll(col)); 706 707 col.clear(); 708 col.addAll(Arrays.asList(getFullElements())); 709 assertTrue("Full collection should containAll full elements", 710 collection.containsAll(col)); 711 verify(); 713 714 int min = (getFullElements().length < 2 ? 0 : 2); 715 int max = (getFullElements().length == 1 ? 1 : 716 (getFullElements().length <= 5 ? getFullElements().length - 1 : 5)); 717 col = Arrays.asList(getFullElements()).subList(min, max); 718 assertTrue("Full collection should containAll partial full " + 719 "elements", collection.containsAll(col)); 720 assertTrue("Full collection should containAll itself", 721 collection.containsAll(collection)); 722 verify(); 724 725 col = new ArrayList (); 726 col.addAll(Arrays.asList(getFullElements())); 727 col.addAll(Arrays.asList(getFullElements())); 728 assertTrue("Full collection should containAll duplicate full " + 729 "elements", collection.containsAll(col)); 730 731 verify(); 733 } 734 735 738 public void testCollectionIsEmpty() { 739 resetEmpty(); 740 assertEquals("New Collection should be empty.", 741 true, collection.isEmpty()); 742 verify(); 744 745 resetFull(); 746 assertEquals("Full collection shouldn't be empty", 747 false, collection.isEmpty()); 748 verify(); 750 } 751 752 753 756 public void testCollectionIterator() { 757 resetEmpty(); 758 Iterator it1 = collection.iterator(); 759 assertEquals("Iterator for empty Collection shouldn't have next.", 760 false, it1.hasNext()); 761 try { 762 it1.next(); 763 fail("Iterator at end of Collection should throw " + 764 "NoSuchElementException when next is called."); 765 } catch(NoSuchElementException e) { 766 } 768 verify(); 770 771 resetFull(); 772 it1 = collection.iterator(); 773 for (int i = 0; i < collection.size(); i++) { 774 assertTrue("Iterator for full collection should haveNext", 775 it1.hasNext()); 776 it1.next(); 777 } 778 assertTrue("Iterator should be finished", !it1.hasNext()); 779 780 ArrayList list = new ArrayList (); 781 it1 = collection.iterator(); 782 for (int i = 0; i < collection.size(); i++) { 783 Object next = it1.next(); 784 assertTrue("Collection should contain element returned by " + 785 "its iterator", collection.contains(next)); 786 list.add(next); 787 } 788 try { 789 it1.next(); 790 fail("iterator.next() should raise NoSuchElementException " + 791 "after it finishes"); 792 } catch (NoSuchElementException e) { 793 } 795 verify(); 797 } 798 799 800 803 public void testCollectionIteratorRemove() { 804 if (!isRemoveSupported()) return; 805 806 resetEmpty(); 807 try { 808 collection.iterator().remove(); 809 fail("New iterator.remove should raise IllegalState"); 810 } catch (IllegalStateException e) { 811 } 813 verify(); 814 815 try { 816 Iterator iter = collection.iterator(); 817 iter.hasNext(); 818 iter.remove(); 819 fail("New iterator.remove should raise IllegalState " + 820 "even after hasNext"); 821 } catch (IllegalStateException e) { 822 } 824 verify(); 825 826 resetFull(); 827 int size = collection.size(); 828 Iterator iter = collection.iterator(); 829 while (iter.hasNext()) { 830 Object o = iter.next(); 831 if (o instanceof Map.Entry ) { 834 o = cloneMapEntry((Map.Entry ) o); 835 } 836 iter.remove(); 837 838 if(!areEqualElementsDistinguishable()) { 847 confirmed.remove(o); 848 verify(); 849 } 850 851 size--; 852 assertEquals("Collection should shrink by one after " + 853 "iterator.remove", size, collection.size()); 854 } 855 assertTrue("Collection should be empty after iterator purge", 856 collection.isEmpty()); 857 858 resetFull(); 859 iter = collection.iterator(); 860 iter.next(); 861 iter.remove(); 862 try { 863 iter.remove(); 864 fail("Second iter.remove should raise IllegalState"); 865 } catch (IllegalStateException e) { 866 } 868 } 869 870 871 874 public void testCollectionRemove() { 875 if (!isRemoveSupported()) return; 876 877 resetEmpty(); 878 Object [] elements = getFullElements(); 879 for (int i = 0; i < elements.length; i++) { 880 assertTrue("Shouldn't remove nonexistent element", 881 !collection.remove(elements[i])); 882 verify(); 883 } 884 885 Object [] other = getOtherElements(); 886 887 resetFull(); 888 for (int i = 0; i < other.length; i++) { 889 assertTrue("Shouldn't remove nonexistent other element", 890 !collection.remove(other[i])); 891 verify(); 892 } 893 894 int size = collection.size(); 895 for (int i = 0; i < elements.length; i++) { 896 resetFull(); 897 assertTrue("Collection should remove extant element: " + elements[i], 898 collection.remove(elements[i])); 899 900 if(!areEqualElementsDistinguishable()) { 909 confirmed.remove(elements[i]); 910 verify(); 911 } 912 913 assertEquals("Collection should shrink after remove", 914 size - 1, collection.size()); 915 } 916 } 917 918 919 922 public void testCollectionRemoveAll() { 923 if (!isRemoveSupported()) return; 924 925 resetEmpty(); 926 assertTrue("Emtpy collection removeAll should return false for " + 927 "empty input", 928 !collection.removeAll(Collections.EMPTY_SET)); 929 verify(); 930 931 assertTrue("Emtpy collection removeAll should return false for " + 932 "nonempty input", 933 !collection.removeAll(new ArrayList (collection))); 934 verify(); 935 936 resetFull(); 937 assertTrue("Full collection removeAll should return false for " + 938 "empty input", 939 !collection.removeAll(Collections.EMPTY_SET)); 940 verify(); 941 942 assertTrue("Full collection removeAll should return false for other elements", 943 !collection.removeAll(Arrays.asList(getOtherElements()))); 944 verify(); 945 946 assertTrue("Full collection removeAll should return true for full elements", 947 collection.removeAll(new HashSet (collection))); 948 confirmed.removeAll(new HashSet (confirmed)); 949 verify(); 950 951 resetFull(); 952 int size = collection.size(); 953 int min = (getFullElements().length < 2 ? 0 : 2); 954 int max = (getFullElements().length == 1 ? 1 : 955 (getFullElements().length <= 5 ? getFullElements().length - 1 : 5)); 956 Collection all = Arrays.asList(getFullElements()).subList(min, max); 957 assertTrue("Full collection removeAll should work", 958 collection.removeAll(all)); 959 confirmed.removeAll(all); 960 verify(); 961 962 assertTrue("Collection should shrink after removeAll", 963 collection.size() < size); 964 Iterator iter = all.iterator(); 965 while (iter.hasNext()) { 966 assertTrue("Collection shouldn't contain removed element", 967 !collection.contains(iter.next())); 968 } 969 } 970 971 972 975 public void testCollectionRetainAll() { 976 if (!isRemoveSupported()) return; 977 978 resetEmpty(); 979 List elements = Arrays.asList(getFullElements()); 980 List other = Arrays.asList(getOtherElements()); 981 982 assertTrue("Empty retainAll() should return false", 983 !collection.retainAll(Collections.EMPTY_SET)); 984 verify(); 985 986 assertTrue("Empty retainAll() should return false", 987 !collection.retainAll(elements)); 988 verify(); 989 990 resetFull(); 991 assertTrue("Collection should change from retainAll empty", 992 collection.retainAll(Collections.EMPTY_SET)); 993 confirmed.retainAll(Collections.EMPTY_SET); 994 verify(); 995 996 resetFull(); 997 assertTrue("Collection changed from retainAll other", 998 collection.retainAll(other)); 999 confirmed.retainAll(other); 1000 verify(); 1001 1002 resetFull(); 1003 int size = collection.size(); 1004 assertTrue("Collection shouldn't change from retainAll elements", 1005 !collection.retainAll(elements)); 1006 verify(); 1007 assertEquals("Collection size shouldn't change", size, 1008 collection.size()); 1009 1010 if (getFullElements().length > 1) { 1011 resetFull(); 1012 size = collection.size(); 1013 int min = (getFullElements().length < 2 ? 0 : 2); 1014 int max = (getFullElements().length <= 5 ? getFullElements().length - 1 : 5); 1015 assertTrue("Collection should changed by partial retainAll", 1016 collection.retainAll(elements.subList(min, max))); 1017 confirmed.retainAll(elements.subList(min, max)); 1018 verify(); 1019 1020 Iterator iter = collection.iterator(); 1021 while (iter.hasNext()) { 1022 assertTrue("Collection only contains retained element", 1023 elements.subList(min, max).contains(iter.next())); 1024 } 1025 } 1026 1027 resetFull(); 1028 HashSet set = new HashSet (elements); 1029 size = collection.size(); 1030 assertTrue("Collection shouldn't change from retainAll without " + 1031 "duplicate elements", !collection.retainAll(set)); 1032 verify(); 1033 assertEquals("Collection size didn't change from nonduplicate " + 1034 "retainAll", size, collection.size()); 1035 } 1036 1037 1038 1041 public void testCollectionSize() { 1042 resetEmpty(); 1043 assertEquals("Size of new Collection is 0.", 0, collection.size()); 1044 1045 resetFull(); 1046 assertTrue("Size of full collection should be greater than zero", 1047 collection.size() > 0); 1048 } 1049 1050 1051 1054 public void testCollectionToArray() { 1055 resetEmpty(); 1056 assertEquals("Empty Collection should return empty array for toArray", 1057 0, collection.toArray().length); 1058 1059 resetFull(); 1060 Object [] array = collection.toArray(); 1061 assertEquals("Full collection toArray should be same size as " + 1062 "collection", array.length, collection.size()); 1063 Object [] confirmedArray = confirmed.toArray(); 1064 assertEquals("length of array from confirmed collection should " + 1065 "match the length of the collection's array", 1066 confirmedArray.length, array.length); 1067 boolean[] matched = new boolean[array.length]; 1068 1069 for (int i = 0; i < array.length; i++) { 1070 assertTrue("Collection should contain element in toArray", 1071 collection.contains(array[i])); 1072 1073 boolean match = false; 1074 for(int j = 0; j < array.length; j++) { 1076 if(matched[j]) continue; 1078 if(array[i] == confirmedArray[j] || 1079 (array[i] != null && array[i].equals(confirmedArray[j]))) { 1080 matched[j] = true; 1081 match = true; 1082 break; 1083 } 1084 } 1085 if(!match) { 1086 fail("element " + i + " in returned array should be found " + 1087 "in the confirmed collection's array"); 1088 } 1089 } 1090 for(int i = 0; i < matched.length; i++) { 1091 assertEquals("Collection should return all its elements in " + 1092 "toArray", true, matched[i]); 1093 } 1094 } 1095 1096 1097 1100 public void testCollectionToArray2() { 1101 resetEmpty(); 1102 Object [] a = new Object [] { new Object (), null, null }; 1103 Object [] array = collection.toArray(a); 1104 assertEquals("Given array shouldn't shrink", array, a); 1105 assertEquals("Last element should be set to null", a[0], null); 1106 verify(); 1107 1108 resetFull(); 1109 try { 1110 array = collection.toArray(new Void [0]); 1111 fail("toArray(new Void[0]) should raise ArrayStore"); 1112 } catch (ArrayStoreException e) { 1113 } 1115 verify(); 1116 1117 try { 1118 array = collection.toArray(null); 1119 fail("toArray(null) should raise NPE"); 1120 } catch (NullPointerException e) { 1121 } 1123 verify(); 1124 1125 array = collection.toArray(new Object [0]); 1126 a = collection.toArray(); 1127 assertEquals("toArrays should be equal", 1128 Arrays.asList(array), Arrays.asList(a)); 1129 1130 HashSet classes = new HashSet (); 1133 for (int i = 0; i < array.length; i++) { 1134 classes.add((array[i] == null) ? null : array[i].getClass()); 1135 } 1136 if (classes.size() > 1) return; 1137 1138 Class cl = (Class )classes.iterator().next(); 1139 if (Map.Entry .class.isAssignableFrom(cl)) { cl = Map.Entry .class; 1141 } 1142 a = (Object [])Array.newInstance(cl, 0); 1143 array = collection.toArray(a); 1144 assertEquals("toArray(Object[]) should return correct array type", 1145 a.getClass(), array.getClass()); 1146 assertEquals("type-specific toArrays should be equal", 1147 Arrays.asList(array), 1148 Arrays.asList(collection.toArray())); 1149 verify(); 1150 } 1151 1152 1153 1156 public void testCollectionToString() { 1157 resetEmpty(); 1158 assertTrue("toString shouldn't return null", 1159 collection.toString() != null); 1160 1161 resetFull(); 1162 assertTrue("toString shouldn't return null", 1163 collection.toString() != null); 1164 } 1165 1166 1167 1171 public void testUnsupportedRemove() { 1172 if (isRemoveSupported()) return; 1173 1174 resetEmpty(); 1175 try { 1176 collection.clear(); 1177 fail("clear should raise UnsupportedOperationException"); 1178 } catch (UnsupportedOperationException e) { 1179 } 1181 verify(); 1182 1183 try { 1184 collection.remove(null); 1185 fail("remove should raise UnsupportedOperationException"); 1186 } catch (UnsupportedOperationException e) { 1187 } 1189 verify(); 1190 1191 try { 1192 collection.removeAll(null); 1193 fail("removeAll should raise UnsupportedOperationException"); 1194 } catch (UnsupportedOperationException e) { 1195 } 1197 verify(); 1198 1199 try { 1200 collection.retainAll(null); 1201 fail("removeAll should raise UnsupportedOperationException"); 1202 } catch (UnsupportedOperationException e) { 1203 } 1205 verify(); 1206 1207 resetFull(); 1208 try { 1209 Iterator iterator = collection.iterator(); 1210 iterator.next(); 1211 iterator.remove(); 1212 fail("iterator.remove should raise UnsupportedOperationException"); 1213 } catch (UnsupportedOperationException e) { 1214 } 1216 verify(); 1217 1218 } 1219 1220 1221 1224 public void testCollectionIteratorFailFast() { 1225 if (!isFailFastSupported()) return; 1226 1227 if (isAddSupported()) { 1228 resetFull(); 1229 try { 1230 Iterator iter = collection.iterator(); 1231 Object o = getOtherElements()[0]; 1232 collection.add(o); 1233 confirmed.add(o); 1234 iter.next(); 1235 fail("next after add should raise ConcurrentModification"); 1236 } catch (ConcurrentModificationException e) { 1237 } 1239 verify(); 1240 1241 resetFull(); 1242 try { 1243 Iterator iter = collection.iterator(); 1244 collection.addAll(Arrays.asList(getOtherElements())); 1245 confirmed.addAll(Arrays.asList(getOtherElements())); 1246 iter.next(); 1247 fail("next after addAll should raise ConcurrentModification"); 1248 } catch (ConcurrentModificationException e) { 1249 } 1251 verify(); 1252 } 1253 1254 if (!isRemoveSupported()) return; 1255 1256 resetFull(); 1257 try { 1258 Iterator iter = collection.iterator(); 1259 collection.clear(); 1260 iter.next(); 1261 fail("next after clear should raise ConcurrentModification"); 1262 } catch (ConcurrentModificationException e) { 1263 } catch (NoSuchElementException e) { 1265 } 1267 1268 resetFull(); 1269 try { 1270 Iterator iter = collection.iterator(); 1271 collection.remove(getFullElements()[0]); 1272 iter.next(); 1273 fail("next after remove should raise ConcurrentModification"); 1274 } catch (ConcurrentModificationException e) { 1275 } 1277 1278 resetFull(); 1279 try { 1280 Iterator iter = collection.iterator(); 1281 List sublist = Arrays.asList(getFullElements()).subList(2,5); 1282 collection.removeAll(sublist); 1283 iter.next(); 1284 fail("next after removeAll should raise ConcurrentModification"); 1285 } catch (ConcurrentModificationException e) { 1286 } 1288 1289 resetFull(); 1290 try { 1291 Iterator iter = collection.iterator(); 1292 List sublist = Arrays.asList(getFullElements()).subList(2,5); 1293 collection.retainAll(sublist); 1294 iter.next(); 1295 fail("next after retainAll should raise ConcurrentModification"); 1296 } catch (ConcurrentModificationException e) { 1297 } 1299 } 1300 1301 public void testSerializeDeserializeThenCompare() throws Exception { 1302 Object obj = makeCollection(); 1303 if (obj instanceof Serializable && isTestSerialization()) { 1304 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 1305 ObjectOutputStream out = new ObjectOutputStream (buffer); 1306 out.writeObject(obj); 1307 out.close(); 1308 1309 ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (buffer.toByteArray())); 1310 Object dest = in.readObject(); 1311 in.close(); 1312 if (isEqualsCheckable()) { 1313 assertEquals("obj != deserialize(serialize(obj)) - EMPTY Collection", obj, dest); 1314 } 1315 } 1316 obj = makeFullCollection(); 1317 if (obj instanceof Serializable && isTestSerialization()) { 1318 ByteArrayOutputStream buffer = new ByteArrayOutputStream (); 1319 ObjectOutputStream out = new ObjectOutputStream (buffer); 1320 out.writeObject(obj); 1321 out.close(); 1322 1323 ObjectInputStream in = new ObjectInputStream (new ByteArrayInputStream (buffer.toByteArray())); 1324 Object dest = in.readObject(); 1325 in.close(); 1326 if (isEqualsCheckable()) { 1327 assertEquals("obj != deserialize(serialize(obj)) - FULL Collection", obj, dest); 1328 } 1329 } 1330 } 1331 1332} 1333 | Popular Tags |