1 16 package org.apache.commons.collections.list; 17 18 import java.io.IOException ; 19 import java.io.Serializable ; 20 import java.lang.reflect.InvocationTargetException ; 21 import java.lang.reflect.Method ; 22 import java.util.AbstractCollection ; 23 import java.util.ArrayList ; 24 import java.util.Arrays ; 25 import java.util.Collection ; 26 import java.util.Collections ; 27 import java.util.ConcurrentModificationException ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.ListIterator ; 31 import java.util.NoSuchElementException ; 32 33 import org.apache.commons.collections.BulkTest; 34 import org.apache.commons.collections.collection.AbstractTestCollection; 35 import org.apache.commons.collections.iterators.AbstractTestListIterator; 36 37 55 public abstract class AbstractTestList extends AbstractTestCollection { 56 57 62 public AbstractTestList(String testName) { 63 super(testName); 64 } 65 66 74 public boolean isSetSupported() { 75 return true; 76 } 77 78 83 public void verify() { 84 super.verify(); 85 86 List list1 = getList(); 87 List list2 = getConfirmedList(); 88 89 assertEquals("List should equal confirmed", list1, list2); 90 assertEquals("Confirmed should equal list", list2, list1); 91 92 assertEquals("Hash codes should be equal", list1.hashCode(), list2.hashCode()); 93 94 int i = 0; 95 Iterator iterator1 = list1.iterator(); 96 Iterator iterator2 = list2.iterator(); 97 Object [] array = list1.toArray(); 98 while (iterator2.hasNext()) { 99 assertTrue("List iterator should have next", iterator1.hasNext()); 100 Object o1 = iterator1.next(); 101 Object o2 = iterator2.next(); 102 assertEquals("Iterator elements should be equal", o1, o2); 103 o2 = list1.get(i); 104 assertEquals("get should return correct element", o1, o2); 105 o2 = array[i]; 106 assertEquals("toArray should have correct element", o1, o2); 107 i++; 108 } 109 } 110 111 115 public boolean isEqualsCheckable() { 116 return true; 117 } 118 119 122 public Collection makeConfirmedCollection() { 123 ArrayList list = new ArrayList (); 124 return list; 125 } 126 127 130 public Collection makeConfirmedFullCollection() { 131 ArrayList list = new ArrayList (); 132 list.addAll(Arrays.asList(getFullElements())); 133 return list; 134 } 135 136 141 public abstract List makeEmptyList(); 142 143 148 public List makeFullList() { 149 List list = makeEmptyList(); 151 list.addAll(Arrays.asList(getFullElements())); 152 return list; 153 } 154 155 160 public final Collection makeCollection() { 161 return makeEmptyList(); 162 } 163 164 169 public final Collection makeFullCollection() { 170 return makeFullList(); 171 } 172 173 179 public List getList() { 180 return (List ) collection; 181 } 182 183 188 public List getConfirmedList() { 189 return (List ) confirmed; 190 } 191 192 197 public void testListAddByIndexBoundsChecking() { 198 if (!isAddSupported()) { 199 return; 200 } 201 202 List list; 203 Object element = getOtherElements()[0]; 204 205 try { 206 list = makeEmptyList(); 207 list.add(Integer.MIN_VALUE, element); 208 fail("List.add should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); 209 } catch (IndexOutOfBoundsException e) { 210 } 212 213 try { 214 list = makeEmptyList(); 215 list.add(-1, element); 216 fail("List.add should throw IndexOutOfBoundsException [-1]"); 217 } catch (IndexOutOfBoundsException e) { 218 } 220 221 try { 222 list = makeEmptyList(); 223 list.add(1, element); 224 fail("List.add should throw IndexOutOfBoundsException [1]"); 225 } catch (IndexOutOfBoundsException e) { 226 } 228 229 try { 230 list = makeEmptyList(); 231 list.add(Integer.MAX_VALUE, element); 232 fail("List.add should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); 233 } catch (IndexOutOfBoundsException e) { 234 } 236 } 237 238 242 public void testListAddByIndexBoundsChecking2() { 243 if (!isAddSupported()) { 244 return; 245 } 246 247 List list; 248 Object element = getOtherElements()[0]; 249 250 try { 251 list = makeFullList(); 252 list.add(Integer.MIN_VALUE, element); 253 fail("List.add should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); 254 } catch (IndexOutOfBoundsException e) { 255 } 257 258 try { 259 list = makeFullList(); 260 list.add(-1, element); 261 fail("List.add should throw IndexOutOfBoundsException [-1]"); 262 } catch (IndexOutOfBoundsException e) { 263 } 265 266 try { 267 list = makeFullList(); 268 list.add(list.size() + 1, element); 269 fail("List.add should throw IndexOutOfBoundsException [size + 1]"); 270 } catch (IndexOutOfBoundsException e) { 271 } 273 274 try { 275 list = makeFullList(); 276 list.add(Integer.MAX_VALUE, element); 277 fail("List.add should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); 278 } catch (IndexOutOfBoundsException e) { 279 } 281 } 282 283 286 public void testListAddByIndex() { 287 if (!isAddSupported()) { 288 return; 289 } 290 291 Object element = getOtherElements()[0]; 292 int max = getFullElements().length; 293 294 for (int i = 0; i <= max; i++) { 295 resetFull(); 296 ((List ) collection).add(i, element); 297 ((List ) confirmed).add(i, element); 298 verify(); 299 } 300 } 301 302 305 public void testListEquals() { 306 resetEmpty(); 307 List list = getList(); 308 assertEquals("Empty lists should be equal", true, list.equals(confirmed)); 309 verify(); 310 assertEquals("Empty list should equal self", true, list.equals(list)); 311 verify(); 312 313 List list2 = Arrays.asList(getFullElements()); 314 assertEquals("Empty list shouldn't equal full", false, list.equals(list2)); 315 verify(); 316 317 list2 = Arrays.asList(getOtherElements()); 318 assertEquals("Empty list shouldn't equal other", false, list.equals(list2)); 319 verify(); 320 321 resetFull(); 322 list = getList(); 323 assertEquals("Full lists should be equal", true, list.equals(confirmed)); 324 verify(); 325 assertEquals("Full list should equal self", true, list.equals(list)); 326 verify(); 327 328 list2 = makeEmptyList(); 329 assertEquals("Full list shouldn't equal empty", false, list.equals(list2)); 330 verify(); 331 332 list2 = Arrays.asList(getOtherElements()); 333 assertEquals("Full list shouldn't equal other", false, list.equals(list2)); 334 verify(); 335 336 list2 = Arrays.asList(getFullElements()); 337 if (list2.size() < 2 && isAddSupported()) { 338 list.addAll(Arrays.asList(getOtherElements())); 340 confirmed.addAll(Arrays.asList(getOtherElements())); 341 list2 = new ArrayList (list2); 342 list2.addAll(Arrays.asList(getOtherElements())); 343 } 344 if (list2.size() > 1) { 345 Collections.reverse(list2); 346 assertEquals( 347 "Full list shouldn't equal full list with same elements but different order", 348 false, list.equals(list2)); 349 verify(); 350 } 351 352 resetFull(); 353 list = getList(); 354 assertEquals("List shouldn't equal String", false, list.equals("")); 355 verify(); 356 357 final List listForC = Arrays.asList(getFullElements()); 358 Collection c = new AbstractCollection () { 359 public int size() { 360 return listForC.size(); 361 } 362 363 public Iterator iterator() { 364 return listForC.iterator(); 365 } 366 }; 367 368 assertEquals("List shouldn't equal nonlist with same elements in same order", false, list.equals(c)); 369 verify(); 370 } 371 372 375 public void testListHashCode() { 376 resetEmpty(); 377 int hash1 = collection.hashCode(); 378 int hash2 = confirmed.hashCode(); 379 assertEquals("Empty lists should have equal hashCodes", hash1, hash2); 380 verify(); 381 382 resetFull(); 383 hash1 = collection.hashCode(); 384 hash2 = confirmed.hashCode(); 385 assertEquals("Full lists should have equal hashCodes", hash1, hash2); 386 verify(); 387 } 388 389 392 public void testListGetByIndex() { 393 resetFull(); 394 List list = getList(); 395 Object [] elements = getFullElements(); 396 for (int i = 0; i < elements.length; i++) { 397 assertEquals("List should contain correct elements", elements[i], list.get(i)); 398 verify(); 399 } 400 } 401 402 406 public void testListGetByIndexBoundsChecking() { 407 List list = makeEmptyList(); 408 409 try { 410 list.get(Integer.MIN_VALUE); 411 fail("List.get should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); 412 } catch (IndexOutOfBoundsException e) { 413 } 415 416 try { 417 list.get(-1); 418 fail("List.get should throw IndexOutOfBoundsException [-1]"); 419 } catch (IndexOutOfBoundsException e) { 420 } 422 423 try { 424 list.get(0); 425 fail("List.get should throw IndexOutOfBoundsException [0]"); 426 } catch (IndexOutOfBoundsException e) { 427 } 429 430 try { 431 list.get(1); 432 fail("List.get should throw IndexOutOfBoundsException [1]"); 433 } catch (IndexOutOfBoundsException e) { 434 } 436 437 try { 438 list.get(Integer.MAX_VALUE); 439 fail("List.get should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); 440 } catch (IndexOutOfBoundsException e) { 441 } 443 } 444 445 449 public void testListGetByIndexBoundsChecking2() { 450 List list = makeFullList(); 451 452 try { 453 list.get(Integer.MIN_VALUE); 454 fail("List.get should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); 455 } catch (IndexOutOfBoundsException e) { 456 } 458 459 try { 460 list.get(-1); 461 fail("List.get should throw IndexOutOfBoundsException [-1]"); 462 } catch (IndexOutOfBoundsException e) { 463 } 465 466 try { 467 list.get(getFullElements().length); 468 fail("List.get should throw IndexOutOfBoundsException [size]"); 469 } catch (IndexOutOfBoundsException e) { 470 } 472 473 try { 474 list.get(Integer.MAX_VALUE); 475 fail("List.get should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); 476 } catch (IndexOutOfBoundsException e) { 477 } 479 } 480 481 484 public void testListIndexOf() { 485 resetFull(); 486 List list1 = getList(); 487 List list2 = getConfirmedList(); 488 489 Iterator iterator = list2.iterator(); 490 while (iterator.hasNext()) { 491 Object element = iterator.next(); 492 assertEquals("indexOf should return correct result", 493 list1.indexOf(element), list2.indexOf(element)); 494 verify(); 495 } 496 497 Object [] other = getOtherElements(); 498 for (int i = 0; i < other.length; i++) { 499 assertEquals("indexOf should return -1 for nonexistent element", 500 list1.indexOf(other[i]), -1); 501 verify(); 502 } 503 } 504 505 508 public void testListLastIndexOf() { 509 resetFull(); 510 List list1 = getList(); 511 List list2 = getConfirmedList(); 512 513 Iterator iterator = list2.iterator(); 514 while (iterator.hasNext()) { 515 Object element = iterator.next(); 516 assertEquals("lastIndexOf should return correct result", 517 list1.lastIndexOf(element), list2.lastIndexOf(element)); 518 verify(); 519 } 520 521 Object [] other = getOtherElements(); 522 for (int i = 0; i < other.length; i++) { 523 assertEquals("lastIndexOf should return -1 for nonexistent " + 524 "element", list1.lastIndexOf(other[i]), -1); 525 verify(); 526 } 527 } 528 529 533 public void testListSetByIndexBoundsChecking() { 534 if (!isSetSupported()) { 535 return; 536 } 537 538 List list = makeEmptyList(); 539 Object element = getOtherElements()[0]; 540 541 try { 542 list.set(Integer.MIN_VALUE, element); 543 fail("List.set should throw IndexOutOfBoundsException [Integer.MIN_VALUE]"); 544 } catch (IndexOutOfBoundsException e) { 545 } 547 548 try { 549 list.set(-1, element); 550 fail("List.set should throw IndexOutOfBoundsException [-1]"); 551 } catch (IndexOutOfBoundsException e) { 552 } 554 555 try { 556 list.set(0, element); 557 fail("List.set should throw IndexOutOfBoundsException [0]"); 558 } catch (IndexOutOfBoundsException e) { 559 } 561 562 try { 563 list.set(1, element); 564 fail("List.set should throw IndexOutOfBoundsException [1]"); 565 } catch (IndexOutOfBoundsException e) { 566 } 568 569 try { 570 list.set(Integer.MAX_VALUE, element); 571 fail("List.set should throw IndexOutOfBoundsException [Integer.MAX_VALUE]"); 572 } catch (IndexOutOfBoundsException e) { 573 } 575 } 576 577 578 582 public void testListSetByIndexBoundsChecking2() { 583 if (!isSetSupported()) return; 584 585 List list = makeFullList(); 586 Object element = getOtherElements()[0]; 587 588 try { 589 list.set(Integer.MIN_VALUE, element); 590 fail("List.set should throw IndexOutOfBoundsException " + 591 "[Integer.MIN_VALUE]"); 592 } catch(IndexOutOfBoundsException e) { 593 } 595 596 try { 597 list.set(-1, element); 598 fail("List.set should throw IndexOutOfBoundsException [-1]"); 599 } catch(IndexOutOfBoundsException e) { 600 } 602 603 try { 604 list.set(getFullElements().length, element); 605 fail("List.set should throw IndexOutOfBoundsException [size]"); 606 } catch(IndexOutOfBoundsException e) { 607 } 609 610 try { 611 list.set(Integer.MAX_VALUE, element); 612 fail("List.set should throw IndexOutOfBoundsException " + 613 "[Integer.MAX_VALUE]"); 614 } catch(IndexOutOfBoundsException e) { 615 } 617 } 618 619 620 623 public void testListSetByIndex() { 624 if (!isSetSupported()) return; 625 626 resetFull(); 627 Object [] elements = getFullElements(); 628 Object [] other = getOtherElements(); 629 630 for (int i = 0; i < elements.length; i++) { 631 Object n = other[i % other.length]; 632 Object v = ((List )collection).set(i, n); 633 assertEquals("Set should return correct element", elements[i], v); 634 ((List )confirmed).set(i, n); 635 verify(); 636 } 637 } 638 639 640 644 public void testUnsupportedSet() { 645 if (isSetSupported()) return; 646 647 resetFull(); 648 try { 649 ((List ) collection).set(0, new Object ()); 650 fail("Emtpy collection should not support set."); 651 } catch (UnsupportedOperationException e) { 652 } 654 verify(); 657 } 658 659 660 664 public void testListRemoveByIndexBoundsChecking() { 665 if (!isRemoveSupported()) return; 666 667 List list = makeEmptyList(); 668 669 try { 670 list.remove(Integer.MIN_VALUE); 671 fail("List.remove should throw IndexOutOfBoundsException " + 672 "[Integer.MIN_VALUE]"); 673 } catch(IndexOutOfBoundsException e) { 674 } 676 677 try { 678 list.remove(-1); 679 fail("List.remove should throw IndexOutOfBoundsException [-1]"); 680 } catch(IndexOutOfBoundsException e) { 681 } 683 684 try { 685 list.remove(0); 686 fail("List.remove should throw IndexOutOfBoundsException [0]"); 687 } catch(IndexOutOfBoundsException e) { 688 } 690 691 try { 692 list.remove(1); 693 fail("List.remove should throw IndexOutOfBoundsException [1]"); 694 } catch(IndexOutOfBoundsException e) { 695 } 697 698 try { 699 list.remove(Integer.MAX_VALUE); 700 fail("List.remove should throw IndexOutOfBoundsException " + 701 "[Integer.MAX_VALUE]"); 702 } catch(IndexOutOfBoundsException e) { 703 } 705 } 706 707 708 712 public void testListRemoveByIndexBoundsChecking2() { 713 if (!isRemoveSupported()) return; 714 715 List list = makeFullList(); 716 717 try { 718 list.remove(Integer.MIN_VALUE); 719 fail("List.remove should throw IndexOutOfBoundsException " + 720 "[Integer.MIN_VALUE]"); 721 } catch(IndexOutOfBoundsException e) { 722 } 724 725 try { 726 list.remove(-1); 727 fail("List.remove should throw IndexOutOfBoundsException [-1]"); 728 } catch(IndexOutOfBoundsException e) { 729 } 731 732 try { 733 list.remove(getFullElements().length); 734 fail("List.remove should throw IndexOutOfBoundsException [size]"); 735 } catch(IndexOutOfBoundsException e) { 736 } 738 739 try { 740 list.remove(Integer.MAX_VALUE); 741 fail("List.remove should throw IndexOutOfBoundsException " + 742 "[Integer.MAX_VALUE]"); 743 } catch(IndexOutOfBoundsException e) { 744 } 746 } 747 748 749 752 public void testListRemoveByIndex() { 753 if (!isRemoveSupported()) return; 754 755 int max = getFullElements().length; 756 for (int i = 0; i < max; i++) { 757 resetFull(); 758 Object o1 = ((List )collection).remove(i); 759 Object o2 = ((List )confirmed).remove(i); 760 assertEquals("remove should return correct element", o1, o2); 761 verify(); 762 } 763 } 764 765 766 769 public void testListListIterator() { 770 resetFull(); 771 forwardTest(getList().listIterator(), 0); 772 backwardTest(getList().listIterator(), 0); 773 } 774 775 776 779 public void testListListIteratorByIndex() { 780 resetFull(); 781 try { 782 getList().listIterator(-1); 783 } catch (IndexOutOfBoundsException ex) {} 784 resetFull(); 785 try { 786 getList().listIterator(getList().size() + 1); 787 } catch (IndexOutOfBoundsException ex) {} 788 resetFull(); 789 for (int i = 0; i <= confirmed.size(); i++) { 790 forwardTest(getList().listIterator(i), i); 791 backwardTest(getList().listIterator(i), i); 792 } 793 resetFull(); 794 for (int i = 0; i <= confirmed.size(); i++) { 795 backwardTest(getList().listIterator(i), i); 796 } 797 } 798 799 802 public void testListListIteratorPreviousRemove() { 803 if (isRemoveSupported() == false) return; 804 resetFull(); 805 ListIterator it = getList().listIterator(); 806 Object zero = it.next(); 807 Object one = it.next(); 808 Object two = it.next(); 809 Object two2 = it.previous(); 810 Object one2 = it.previous(); 811 assertSame(one, one2); 812 assertSame(two, two2); 813 assertSame(zero, getList().get(0)); 814 assertSame(one, getList().get(1)); 815 assertSame(two, getList().get(2)); 816 it.remove(); 817 assertSame(zero, getList().get(0)); 818 assertSame(two, getList().get(1)); 819 } 820 821 827 private void forwardTest(ListIterator iter, int i) { 828 List list = getList(); 829 int max = getFullElements().length; 830 831 while (i < max) { 832 assertTrue("Iterator should have next", iter.hasNext()); 833 assertEquals("Iterator.nextIndex should work", 834 iter.nextIndex(), i); 835 assertEquals("Iterator.previousIndex should work", 836 iter.previousIndex(), i - 1); 837 Object o = iter.next(); 838 assertEquals("Iterator returned correct element", list.get(i), o); 839 i++; 840 } 841 842 assertTrue("Iterator shouldn't have next", !iter.hasNext()); 843 assertEquals("nextIndex should be size", iter.nextIndex(), max); 844 assertEquals("previousIndex should be size - 1", 845 iter.previousIndex(), max - 1); 846 847 try { 848 iter.next(); 849 fail("Exhausted iterator should raise NoSuchElement"); 850 } catch (NoSuchElementException e) { 851 } 853 } 854 855 856 862 private void backwardTest(ListIterator iter, int i) { 863 List list = getList(); 864 865 while (i > 0) { 866 assertTrue("Iterator should have previous, i:" + i, iter.hasPrevious()); 867 assertEquals("Iterator.nextIndex should work, i:" + i, iter.nextIndex(), i); 868 assertEquals("Iterator.previousIndex should work, i:" + i, iter.previousIndex(), i - 1); 869 Object o = iter.previous(); 870 assertEquals("Iterator returned correct element", list.get(i - 1), o); 871 i--; 872 } 873 874 assertTrue("Iterator shouldn't have previous", !iter.hasPrevious()); 875 int nextIndex = iter.nextIndex(); 876 assertEquals("nextIndex should be 0, actual value: " + nextIndex, nextIndex, 0); 877 int prevIndex = iter.previousIndex(); 878 assertEquals("previousIndex should be -1, actual value: " + prevIndex, prevIndex, -1); 879 880 try { 881 iter.previous(); 882 fail("Exhausted iterator should raise NoSuchElement"); 883 } catch (NoSuchElementException e) { 884 } 886 887 } 888 889 890 894 public void testListIteratorAdd() { 895 if (!isAddSupported()) return; 896 897 resetEmpty(); 898 List list1 = getList(); 899 List list2 = getConfirmedList(); 900 901 Object [] elements = getFullElements(); 902 ListIterator iter1 = list1.listIterator(); 903 ListIterator iter2 = list2.listIterator(); 904 905 for (int i = 0; i < elements.length; i++) { 906 iter1.add(elements[i]); 907 iter2.add(elements[i]); 908 verify(); 909 } 910 911 resetFull(); 912 iter1 = getList().listIterator(); 913 iter2 = getConfirmedList().listIterator(); 914 for (int i = 0; i < elements.length; i++) { 915 iter1.next(); 916 iter2.next(); 917 iter1.add(elements[i]); 918 iter2.add(elements[i]); 919 verify(); 920 } 921 } 922 923 924 928 public void testListIteratorSet() { 929 if (!isSetSupported()) return; 930 931 Object [] elements = getFullElements(); 932 933 resetFull(); 934 ListIterator iter1 = getList().listIterator(); 935 ListIterator iter2 = getConfirmedList().listIterator(); 936 for (int i = 0; i < elements.length; i++) { 937 iter1.next(); 938 iter2.next(); 939 iter1.set(elements[i]); 940 iter2.set(elements[i]); 941 verify(); 942 } 943 } 944 945 946 public void testEmptyListSerialization() 947 throws IOException , ClassNotFoundException { 948 List list = makeEmptyList(); 949 if (!(list instanceof Serializable && isTestSerialization())) return; 950 951 byte[] objekt = writeExternalFormToBytes((Serializable ) list); 952 List list2 = (List ) readExternalFormFromBytes(objekt); 953 954 assertTrue("Both lists are empty",list.size() == 0); 955 assertTrue("Both lists are empty",list2.size() == 0); 956 } 957 958 public void testFullListSerialization() 959 throws IOException , ClassNotFoundException { 960 List list = makeFullList(); 961 int size = getFullElements().length; 962 if (!(list instanceof Serializable && isTestSerialization())) return; 963 964 byte[] objekt = writeExternalFormToBytes((Serializable ) list); 965 List list2 = (List ) readExternalFormFromBytes(objekt); 966 967 assertEquals("Both lists are same size",list.size(), size); 968 assertEquals("Both lists are same size",list2.size(), size); 969 } 970 971 975 public void testEmptyListCompatibility() throws IOException , ClassNotFoundException { 976 983 984 List list = makeEmptyList(); 986 if(list instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) { 987 List list2 = (List ) readExternalFormFromDisk(getCanonicalEmptyCollectionName(list)); 988 assertTrue("List is empty",list2.size() == 0); 989 assertEquals(list, list2); 990 } 991 } 992 993 997 public void testFullListCompatibility() throws IOException , ClassNotFoundException { 998 1005 1006 List list = makeFullList(); 1008 if(list instanceof Serializable && !skipSerializedCanonicalTests() && isTestSerialization()) { 1009 List list2 = (List ) readExternalFormFromDisk(getCanonicalFullCollectionName(list)); 1010 if (list2.size() == 4) { 1011 return; 1013 } 1014 assertEquals("List is the right size",list.size(), list2.size()); 1015 assertEquals(list, list2); 1016 } 1017 } 1018 1019 1030 public BulkTest bulkTestSubList() { 1031 if (getFullElements().length - 6 < 10) return null; 1032 return new BulkTestSubList(this); 1033 } 1034 1035 1036 public static class BulkTestSubList extends AbstractTestList { 1037 1038 private AbstractTestList outer; 1039 1040 1041 BulkTestSubList(AbstractTestList outer) { 1042 super(""); 1043 this.outer = outer; 1044 } 1045 1046 1047 public Object [] getFullElements() { 1048 List l = Arrays.asList(outer.getFullElements()); 1049 return l.subList(3, l.size() - 3).toArray(); 1050 } 1051 1052 1053 public Object [] getOtherElements() { 1054 return outer.getOtherElements(); 1055 } 1056 1057 1058 public boolean isAddSupported() { 1059 return outer.isAddSupported(); 1060 } 1061 1062 public boolean isSetSupported() { 1063 return outer.isSetSupported(); 1064 } 1065 1066 public boolean isRemoveSupported() { 1067 return outer.isRemoveSupported(); 1068 } 1069 1070 1071 public List makeEmptyList() { 1072 return outer.makeFullList().subList(4, 4); 1073 } 1074 1075 1076 public List makeFullList() { 1077 int size = getFullElements().length; 1078 return outer.makeFullList().subList(3, size - 3); 1079 } 1080 1081 1082 public void resetEmpty() { 1083 outer.resetFull(); 1084 this.collection = outer.getList().subList(4, 4); 1085 this.confirmed = outer.getConfirmedList().subList(4, 4); 1086 } 1087 1088 public void resetFull() { 1089 outer.resetFull(); 1090 int size = outer.confirmed.size(); 1091 this.collection = outer.getList().subList(3, size - 3); 1092 this.confirmed = outer.getConfirmedList().subList(3, size - 3); 1093 } 1094 1095 1096 public void verify() { 1097 super.verify(); 1098 outer.verify(); 1099 } 1100 1101 public boolean isTestSerialization() { 1102 return false; 1103 } 1104 } 1105 1106 1107 1111 public void testListSubListFailFastOnAdd() { 1112 if (!isFailFastSupported()) return; 1113 if (!isAddSupported()) return; 1114 1115 resetFull(); 1116 int size = collection.size(); 1117 List sub = getList().subList(1, size); 1118 getList().add(getOtherElements()[0]); 1119 failFastAll(sub); 1120 1121 resetFull(); 1122 sub = getList().subList(1, size); 1123 getList().add(0, getOtherElements()[0]); 1124 failFastAll(sub); 1125 1126 resetFull(); 1127 sub = getList().subList(1, size); 1128 getList().addAll(Arrays.asList(getOtherElements())); 1129 failFastAll(sub); 1130 1131 resetFull(); 1132 sub = getList().subList(1, size); 1133 getList().addAll(0, Arrays.asList(getOtherElements())); 1134 failFastAll(sub); 1135 1136 } 1137 1138 1139 1143 public void testListSubListFailFastOnRemove() { 1144 if (!isFailFastSupported()) return; 1145 if (!isRemoveSupported()) return; 1146 1147 resetFull(); 1148 int size = collection.size(); 1149 List sub = getList().subList(1, size); 1150 getList().remove(0); 1151 failFastAll(sub); 1152 1153 resetFull(); 1154 sub = getList().subList(1, size); 1155 getList().remove(getFullElements()[2]); 1156 failFastAll(sub); 1157 1158 resetFull(); 1159 sub = getList().subList(1, size); 1160 getList().removeAll(Arrays.asList(getFullElements())); 1161 failFastAll(sub); 1162 1163 resetFull(); 1164 sub = getList().subList(1, size); 1165 getList().retainAll(Arrays.asList(getOtherElements())); 1166 failFastAll(sub); 1167 1168 resetFull(); 1169 sub = getList().subList(1, size); 1170 getList().clear(); 1171 failFastAll(sub); 1172 } 1173 1174 1175 1179 protected void failFastAll(List list) { 1180 Method [] methods = List .class.getMethods(); 1181 for (int i = 0; i < methods.length; i++) { 1182 failFastMethod(list, methods[i]); 1183 } 1184 } 1185 1186 1187 1199 protected void failFastMethod(List list, Method m) { 1200 if (m.getName().equals("equals")) return; 1201 1202 Object element = getOtherElements()[0]; 1203 Collection c = Collections.singleton(element); 1204 1205 Class [] types = m.getParameterTypes(); 1206 Object [] params = new Object [types.length]; 1207 for (int i = 0; i < params.length; i++) { 1208 if (types[i] == Integer.TYPE) params[i] = new Integer (0); 1209 else if (types[i] == Collection .class) params[i] = c; 1210 else if (types[i] == Object .class) params[i] = element; 1211 else if (types[i] == Object [].class) params[i] = new Object [0]; 1212 } 1213 1214 try { 1215 m.invoke(list, params); 1216 fail(m.getName() + " should raise ConcurrentModification"); 1217 } catch (IllegalAccessException e) { 1218 } catch (InvocationTargetException e) { 1220 Throwable t = e.getTargetException(); 1221 if (t instanceof ConcurrentModificationException ) { 1222 return; 1224 } else { 1225 fail(m.getName() + " raised unexpected " + e); 1226 } 1227 } 1228 } 1229 1230 public BulkTest bulkTestListIterator() { 1232 return new TestListIterator(); 1233 } 1234 1235 public class TestListIterator extends AbstractTestListIterator { 1236 public TestListIterator() { 1237 super("TestListIterator"); 1238 } 1239 1240 public Object addSetValue() { 1241 return AbstractTestList.this.getOtherElements()[0]; 1242 } 1243 1244 public boolean supportsRemove() { 1245 return AbstractTestList.this.isRemoveSupported(); 1246 } 1247 1248 public boolean supportsAdd() { 1249 return AbstractTestList.this.isAddSupported(); 1250 } 1251 1252 public boolean supportsSet() { 1253 return AbstractTestList.this.isSetSupported(); 1254 } 1255 1256 public ListIterator makeEmptyListIterator() { 1257 resetEmpty(); 1258 return ((List ) AbstractTestList.this.collection).listIterator(); 1259 } 1260 1261 public ListIterator makeFullListIterator() { 1262 resetFull(); 1263 return ((List ) AbstractTestList.this.collection).listIterator(); 1264 } 1265 } 1266 1267} 1268 | Popular Tags |