1 5 package com.tctest; 6 7 import com.tc.exception.TCNonPortableObjectError; 8 import com.tc.object.config.ConfigVisitor; 9 import com.tc.object.config.DSOClientConfigHelper; 10 import com.tc.object.tx.ReadOnlyException; 11 import com.tc.simulator.app.ApplicationConfig; 12 import com.tc.simulator.listener.ListenerProvider; 13 import com.tc.util.Assert; 14 15 import java.lang.reflect.Method ; 16 import java.util.AbstractList ; 17 import java.util.ArrayList ; 18 import java.util.Arrays ; 19 import java.util.HashSet ; 20 import java.util.Iterator ; 21 import java.util.LinkedList ; 22 import java.util.List ; 23 import java.util.ListIterator ; 24 import java.util.Set ; 25 import java.util.Stack ; 26 import java.util.Vector ; 27 28 public class GenericListTestApp extends GenericTestApp { 29 30 public GenericListTestApp(String appId, ApplicationConfig cfg, ListenerProvider listenerProvider) { 31 super(appId, cfg, listenerProvider, List .class); 32 } 33 34 protected Object getTestObject(String testName) { 35 List lists = (List ) sharedMap.get("lists"); 36 return lists.iterator(); 37 } 38 39 protected void setupTestObject(String testName) { 40 List lists = new ArrayList (); 41 lists.add(new LinkedList ()); 42 lists.add(new ArrayList ()); 43 lists.add(new Vector ()); 44 lists.add(new Stack ()); 45 lists.add(new MyArrayList()); 46 lists.add(new MyArrayList5()); 47 lists.add(new MyArrayList6()); 48 lists.add(new MyLinkedList()); 49 lists.add(new MyVector()); 50 lists.add(new MyStack()); 51 lists.add(new MyAbstractListSubclass()); 52 53 sharedMap.put("lists", lists); 54 sharedMap.put("arrayforLinkedList", new Object [2]); 55 sharedMap.put("arrayforArrayList", new Object [2]); 56 sharedMap.put("arrayforVector", new Object [2]); 57 sharedMap.put("arrayforStack", new Object [2]); 58 sharedMap.put("arrayforAbstractListSubclass", new Object [2]); 59 sharedMap.put("arrayforMyArrayList", new Object [2]); 60 sharedMap.put("arrayforMyArrayList5", new Object [2]); 61 sharedMap.put("arrayforMyArrayList6", new Object [2]); 62 sharedMap.put("arrayforMyLinkedList", new Object [2]); 63 sharedMap.put("arrayforMyVector", new Object [2]); 64 sharedMap.put("arrayforMyStack", new Object [2]); 65 sharedMap.put("arrayforMyAbstractListSubclass", new Object [2]); 66 } 67 68 void testBasicAdd(List list, boolean validate) { 69 if (validate) { 70 assertSingleElement(list, "rollin in my 6-4"); 71 } else { 72 synchronized (list) { 73 boolean added = list.add("rollin in my 6-4"); 74 Assert.assertTrue(added); 75 } 76 } 77 } 78 79 void testVectorSetSizeGrow(List list, boolean validate) { 80 if (!(list instanceof Vector )) { return; } 81 82 int size = 5; 83 Vector v = (Vector ) list; 84 85 if (validate) { 86 Assert.assertEquals("start", v.get(0)); 87 for (int i = 1; i < size; i++) { 88 Object val = v.get(i); 89 Assert.assertNull("element " + i + " is " + val, val); 90 } 91 Assert.assertEquals("end", v.get(size)); 92 } else { 93 synchronized (v) { 94 v.add("start"); 95 v.setSize(size); 96 v.add("end"); 97 } 98 } 99 } 100 101 void testVectorSetSizeShrink(List list, boolean validate) { 102 if (!(list instanceof Vector )) { return; } 103 104 Vector v = (Vector ) list; 105 106 if (validate) { 107 Assert.assertEquals("start", v.get(0)); 108 Assert.assertEquals("end", v.get(1)); 109 } else { 110 synchronized (v) { 111 v.add("start"); 112 v.add("ho hum"); 113 v.add("ho hum2"); 114 v.add("ho hum3"); 115 116 v.setSize(1); 117 v.add("end"); 118 } 119 } 120 } 121 122 void testVectorAddElement(List list, boolean validate) { 123 if (!(list instanceof Vector )) { return; } 124 125 Vector vector = (Vector ) list; 126 127 if (validate) { 128 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element" }), vector); 129 } else { 130 synchronized (vector) { 131 vector.addElement("first element"); 132 vector.addElement("second element"); 133 } 134 } 135 } 136 137 void testVectorRetainAll(List list, boolean validate) { 138 if (!(list instanceof Vector )) { return; } 139 140 Vector vector = (Vector ) list; 141 142 if (validate) { 143 assertListsEqual(Arrays.asList(new Object [] { "second element" }), vector); 144 } else { 145 146 synchronized (vector) { 147 vector.addElement("first element"); 148 vector.addElement("second element"); 149 vector.addElement("third element"); 150 151 List retainList = new ArrayList (1); 152 retainList.add("second element"); 153 154 vector.retainAll(retainList); 155 } 156 } 157 } 158 159 void testVectorRemoveAll(List list, boolean validate) { 160 if (!(list instanceof Vector )) { return; } 161 162 163 Vector vector = (Vector ) list; 164 165 if (validate) { 166 assertListsEqual(Arrays.asList(new Object [] { "first element", "third element" }), vector); 167 } else { 168 synchronized (vector) { 169 vector.addElement("first element"); 170 vector.addElement("second element"); 171 vector.addElement("third element"); 172 173 List removeList = new ArrayList (1); 174 removeList.add("second element"); 175 vector.removeAll(removeList); 176 } 177 } 178 } 179 180 void testVectorRemoveAllElements(List list, boolean validate) { 181 if (!(list instanceof Vector )) { return; } 182 183 184 Vector vector = (Vector ) list; 185 186 if (validate) { 187 assertEmptyList(vector); 188 } else { 189 synchronized (vector) { 190 vector.addElement("first element"); 191 vector.addElement("second element"); 192 vector.addElement("third element"); 193 194 vector.removeAllElements(); 195 } 196 } 197 } 198 199 void testVectorSetElementAt(List list, boolean validate) { 200 if (!(list instanceof Vector )) { return; } 201 202 203 Vector vector = (Vector ) list; 204 205 if (validate) { 206 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element", "third element" }), vector); 207 } else { 208 synchronized (vector) { 209 vector.addElement("first element"); 210 vector.addElement(null); 211 vector.addElement("third element"); 212 213 vector.setElementAt("second element", 1); 214 } 215 } 216 } 217 218 void testVectorInsertElementAt(List list, boolean validate) { 219 if (!(list instanceof Vector )) { return; } 220 221 222 Vector vector = (Vector ) list; 223 224 if (validate) { 225 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element", "third element" }), vector); 226 } else { 227 synchronized (vector) { 228 vector.addElement("first element"); 229 vector.addElement("third element"); 230 231 vector.insertElementAt("second element", 1); 232 } 233 } 234 } 235 236 void testLinkedListRemoveFirst(List list, boolean validate) { 237 if (!(list instanceof LinkedList )) { return; } 238 239 240 LinkedList linkedList = (LinkedList ) list; 241 242 if (validate) { 243 assertSingleElement(linkedList, "teck"); 244 } else { 245 synchronized (linkedList) { 246 linkedList.add("timmy"); 247 linkedList.add("teck"); 248 } 249 250 synchronized (linkedList) { 251 linkedList.removeFirst(); 252 } 253 } 254 } 255 256 void testLinkedListRemoveLast(List list, boolean validate) { 257 if (!(list instanceof LinkedList )) { return; } 258 259 260 LinkedList linkedList = (LinkedList ) list; 261 262 if (validate) { 263 assertSingleElement(linkedList, "timmy"); 264 } else { 265 synchronized (linkedList) { 266 linkedList.add("timmy"); 267 linkedList.add("teck"); 268 } 269 270 synchronized (linkedList) { 271 linkedList.removeLast(); 272 } 273 } 274 } 275 276 void testLinkedListAddFirst(List list, boolean validate) { 277 if (!(list instanceof LinkedList )) { return; } 278 279 280 LinkedList linkedList = (LinkedList ) list; 281 282 if (validate) { 283 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element" }), list); 284 } else { 285 synchronized (linkedList) { 286 linkedList.add("second element"); 287 } 288 289 synchronized (linkedList) { 290 linkedList.addFirst("first element"); 291 } 292 } 293 } 294 295 void testLinkedListAddLast(List list, boolean validate) { 296 if (!(list instanceof LinkedList )) { return; } 297 298 299 LinkedList linkedList = (LinkedList ) list; 300 301 if (validate) { 302 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element" }), list); 303 } else { 304 synchronized (linkedList) { 305 linkedList.add("first element"); 306 } 307 308 synchronized (linkedList) { 309 linkedList.addLast("second element"); 310 } 311 } 312 } 313 314 void testBasicAddNull(List list, boolean validate) { 315 316 317 if (validate) { 318 assertListsEqual(Arrays.asList(new Object [] { null, null, "my cat hates you", null }), list); 319 } else { 320 synchronized (list) { 321 boolean added; 322 added = list.add(null); 323 Assert.assertTrue(added); 324 added = list.add(null); 325 Assert.assertTrue(added); 326 added = list.add("my cat hates you"); 327 Assert.assertTrue(added); 328 added = list.add(null); 329 Assert.assertTrue(added); 330 } 331 } 332 } 333 334 void testBasicAddAt(List list, boolean validate) { 335 336 337 if (validate) { 338 assertListsEqual(Arrays.asList(new Object [] { "1", "2", "3", "4" }), list); 339 } else { 340 synchronized (list) { 341 list.add(0, "2"); 342 } 343 synchronized (list) { 344 list.add(0, "1"); 345 } 346 synchronized (list) { 347 list.add(2, "4"); 348 } 349 synchronized (list) { 350 list.add(2, "3"); 351 } 352 } 353 } 354 355 void testAdd(List list, boolean validate) { 356 357 358 if (validate) { 359 assertListsEqual(Arrays.asList(new Object [] { "element" }), list); 360 } else { 361 synchronized (list) { 362 list.add("element"); 363 } 364 } 365 } 366 367 void testAddAll(List list, boolean validate) { 368 369 370 if (validate) { 371 assertListsEqual(Arrays.asList(new Object [] { "patty", "calahan", "was", "here" }), list); 372 } else { 373 List toAdd = new ArrayList (); 374 toAdd.add("patty"); 375 toAdd.add("calahan"); 376 toAdd.add("was"); 377 toAdd.add("here"); 378 379 synchronized (list) { 380 list.addAll(toAdd); 381 } 382 } 383 } 384 385 void testAddAllAt(List list, boolean validate) { 386 387 388 if (validate) { 389 assertListsEqual(Arrays.asList(new Object [] { "uno", "dos", "tres", "catorce?" }), list); 390 } else { 391 synchronized (list) { 392 list.add("uno"); 393 } 394 395 List toAdd = new ArrayList (); 396 toAdd.add("dos"); 397 toAdd.add("tres"); 398 toAdd.add("catorce?"); 399 400 synchronized (list) { 401 list.addAll(1, toAdd); 402 } 403 } 404 } 405 406 void testClear(List list, boolean validate) { 407 408 409 if (validate) { 410 assertEmptyList(list); 411 } else { 412 synchronized (list) { 413 list.add("clear me baby"); 414 list.clear(); 415 } 416 417 synchronized (list) { 418 list.add("clear me baby one more time"); 419 } 420 421 synchronized (list) { 422 list.clear(); 423 } 424 } 425 } 426 427 void testSetElementAt(List list, boolean validate) { 428 429 430 if (validate) { 431 assertSingleElement(list, "new"); 432 } else { 433 synchronized (list) { 434 list.add("orig"); 435 } 436 437 synchronized (list) { 438 list.set(0, "new"); 439 } 440 } 441 } 442 443 void testRemoveAt(List list, boolean validate) { 444 445 446 if (validate) { 447 String item0 = (String ) list.get(0); 448 String item1 = (String ) list.get(1); 449 Assert.assertEquals("value", item0); 450 Assert.assertEquals("different value", item1); 451 } else { 452 synchronized (list) { 453 list.add("value"); 454 list.add("different value"); 455 list.add("value"); 456 } 457 458 synchronized (list) { 459 Object prev = list.remove(2); 460 Assert.assertEquals("value", prev); 461 } 462 } 463 } 464 465 void testRemoveNull1(List list, boolean validate) { 466 467 468 if (validate) { 469 assertListsEqual(Arrays.asList(new Object [] { "first element", null, "third element" }), list); 470 } else { 471 synchronized (list) { 472 list.add("first element"); 473 list.add(null); 474 list.add(null); 475 list.add("third element"); 476 } 477 synchronized (list) { 478 list.remove(null); 479 } 480 } 481 } 482 483 void testRemoveNull2(List list, boolean validate) { 484 485 486 if (validate) { 487 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element" }), list); 488 } else { 489 synchronized (list) { 490 list.add("first element"); 491 list.add(null); 492 list.add("second element"); 493 list.add(null); 494 } 495 synchronized (list) { 496 list.remove(null); 497 list.remove(null); 498 } 499 } 500 } 501 502 void testSubList(List list, boolean validate) { 503 504 505 if (validate) { 506 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element", "third element", 507 "fourth element" }), list); 508 } else { 509 synchronized (list) { 510 list.add("first element"); 511 list.add("third element"); 512 list.add("fourth element"); 513 } 514 List subList = list.subList(1, 2); 515 ListIterator listIterator = subList.listIterator(); 516 synchronized (list) { 517 listIterator.add("second element"); 518 } 519 } 520 } 521 522 void testRemoveAll(List list, boolean validate) { 523 524 525 if (validate) { 526 assertEmptyList(list); 527 } else { 528 synchronized (list) { 529 list.add("first element"); 530 list.add("second element"); 531 } 532 List removeList = new ArrayList (2); 533 removeList.add("first element"); 534 removeList.add("second element"); 535 synchronized (list) { 536 list.removeAll(removeList); 537 } 538 } 539 } 540 541 void testRemoveRange(List list, boolean validate) { 542 if (list instanceof MyArrayList6) { return; } 544 545 if (validate) { 546 assertListsEqual(Arrays.asList(new Object [] { "first element", "fourth element" }), list); 547 } else { 548 synchronized (list) { 549 list.add("first element"); 550 list.add("second element"); 551 list.add("third element"); 552 list.add("fourth element"); 553 } 554 Class listClass = AbstractList .class; 555 Class [] parameterType = new Class [2]; 556 parameterType[0] = Integer.TYPE; 557 parameterType[1] = Integer.TYPE; 558 559 try { 560 synchronized (list) { 561 Method m = listClass.getDeclaredMethod("removeRange", parameterType); 562 m.setAccessible(true); m.invoke(list, new Object [] { new Integer (1), new Integer (3) }); 565 } 566 } catch (Exception e) { 567 } 569 } 570 } 571 572 void testToArray(List list, boolean validate) { 573 574 575 Object [] array = getArray(list); 576 577 if (validate) { 578 assertListsEqual(Arrays.asList(array), list); 579 } else { 580 synchronized (list) { 581 list.add("first element"); 582 list.add("second element"); 583 } 584 synchronized (array) { 585 Object [] returnArray = list.toArray(array); 586 Assert.assertTrue(returnArray == array); 587 } 588 } 589 } 590 591 void testListIteratorSet1(List list, boolean validate) { 593 594 595 if (validate) { 596 assertListsEqual(Arrays.asList(new Object [] { "modified first element", "second element", "third element" }), 597 list); 598 } else { 599 synchronized (list) { 600 list.add("first element"); 601 list.add("second element"); 602 list.add("third element"); 603 } 604 synchronized (list) { 605 ListIterator lIterator = list.listIterator(); 606 lIterator.next(); 607 lIterator.set("modified first element"); 608 } 609 } 610 } 611 612 void testListIteratorSet2(List list, boolean validate) { 613 614 615 if (validate) { 616 assertListsEqual(Arrays.asList(new Object [] { "first element", "modified second element", "third element" }), 617 list); 618 } else { 619 synchronized (list) { 620 list.add("first element"); 621 list.add("second element"); 622 list.add("third element"); 623 } 624 synchronized (list) { 625 ListIterator lIterator = list.listIterator(); 626 lIterator.next(); 627 lIterator.next(); 628 lIterator.set("modified second element"); 629 } 630 } 631 } 632 633 void testListIteratorSetRemove1(List list, boolean validate) { 634 635 636 if (validate) { 637 assertListsEqual(Arrays.asList(new Object [] { "modified first element", "third element" }), list); 638 } else { 639 synchronized (list) { 640 list.add("first element"); 641 list.add("second element"); 642 list.add("third element"); 643 } 644 synchronized (list) { 645 ListIterator lIterator = list.listIterator(); 646 lIterator.next(); 647 lIterator.next(); 648 lIterator.remove(); 649 lIterator.previous(); 650 lIterator.set("modified first element"); 651 } 652 } 653 } 654 655 void testListIteratorSetRemove2(List list, boolean validate) { 656 657 658 if (validate) { 659 assertListsEqual(Arrays.asList(new Object [] { "first element", "modified second element" }), list); 660 } else { 661 synchronized (list) { 662 list.add("first element"); 663 list.add("second element"); 664 list.add("third element"); 665 } 666 synchronized (list) { 667 ListIterator lIterator = list.listIterator(); 668 lIterator.next(); 669 lIterator.next(); 670 lIterator.set("modified second element"); 671 lIterator.next(); 672 lIterator.remove(); 673 } 674 } 675 } 676 677 void testListIteratorDuplicateElementRemove(List list, boolean validate) { 678 679 680 if (validate) { 681 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element" }), list); 682 } else { 683 synchronized (list) { 684 list.add("first element"); 685 list.add("second element"); 686 list.add("first element"); 687 } 688 synchronized (list) { 689 ListIterator lIterator = list.listIterator(); 690 lIterator.next(); 691 lIterator.next(); 692 lIterator.next(); 693 lIterator.remove(); 694 } 695 } 696 } 697 698 void testListIteratorAdd1(List list, boolean validate) { 699 700 701 if (validate) { 702 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element", "third element" }), list); 703 } else { 705 synchronized (list) { 706 list.add("second element"); 707 list.add("third element"); 708 } 709 synchronized (list) { 710 ListIterator lIterator = list.listIterator(); 711 lIterator.add("first element"); 712 } 713 } 714 } 715 716 void testListIteratorAdd2(List list, boolean validate) { 717 718 719 if (validate) { 720 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element", "third element" }), list); 721 } else { 722 synchronized (list) { 723 list.add("first element"); 724 list.add("third element"); 725 } 726 synchronized (list) { 727 ListIterator lIterator = list.listIterator(); 728 lIterator.next(); 729 lIterator.add("second element"); 730 } 731 } 732 } 733 734 void testListIteratorAddSet1(List list, boolean validate) { 735 736 737 if (validate) { 738 assertListsEqual(Arrays.asList(new Object [] { "modified first element", "second element", "third element" }), 739 list); 740 } else { 741 synchronized (list) { 742 list.add("second element"); 743 list.add("third element"); 744 } 745 synchronized (list) { 746 ListIterator lIterator = list.listIterator(); 747 lIterator.add("first element"); 748 lIterator.previous(); 749 lIterator.set("modified first element"); 750 } 751 } 752 } 753 754 void testListIteratorAddSet2(List list, boolean validate) { 755 756 757 if (validate) { 758 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element", "modified third element" }), 759 list); 760 } else { 761 synchronized (list) { 762 list.add("first element"); 763 list.add("third element"); 764 } 765 synchronized (list) { 766 ListIterator lIterator = list.listIterator(); 767 lIterator.next(); 768 lIterator.add("second element"); 769 lIterator.next(); 770 lIterator.set("modified third element"); 771 } 772 } 773 } 774 775 void testListIteratorAddSet3(List list, boolean validate) { 776 777 778 if (validate) { 779 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element", "modified third element", 780 "fourth element" }), list); 781 } else { 782 synchronized (list) { 783 list.add("first element"); 784 list.add("second element"); 785 list.add("fourth element"); 786 } 787 synchronized (list) { 788 ListIterator lIterator = list.listIterator(1); 789 lIterator.next(); 790 lIterator.add("third element"); 791 lIterator.previous(); 792 lIterator.set("modified third element"); 793 } 794 } 795 } 796 797 void testListIteratorAddNull(List list, boolean validate) { 798 799 800 if (validate) { 801 assertListsEqual(Arrays.asList(new Object [] { null, null, "third element" }), list); 802 } else { 803 synchronized (list) { 804 ListIterator lIterator = list.listIterator(); 805 lIterator.add(null); 806 lIterator.add(null); 807 lIterator.add("third element"); 808 } 809 } 810 } 811 812 void testListIteratorAddRemove(List list, boolean validate) { 813 814 815 if (validate) { 816 assertListsEqual(Arrays.asList(new Object [] { "second element", "third element" }), list); 817 } else { 818 synchronized (list) { 819 list.add("second element"); 820 list.add("third element"); 821 } 822 synchronized (list) { 823 ListIterator lIterator = list.listIterator(); 824 lIterator.add("first element"); 825 lIterator.previous(); 826 lIterator.remove(); 827 } 828 } 829 } 830 831 void testListIteratorRemoveNull(List list, boolean validate) { 832 833 834 if (validate) { 835 assertListsEqual(Arrays.asList(new Object [] { "first element", null, "third element" }), list); 836 } else { 837 synchronized (list) { 838 list.add("first element"); 839 list.add(null); 840 list.add(null); 841 list.add("third element"); 842 } 843 synchronized (list) { 844 ListIterator lIterator = list.listIterator(); 845 lIterator.next(); 846 lIterator.next(); 847 lIterator.remove(); 848 } 849 } 850 } 851 852 void testReadOnlyAdd(List list, boolean validate) { 854 855 856 if (list instanceof Vector ) { return; } 857 858 if (validate) { 859 assertEmptyList(list); 860 } else { 861 synchronized (list) { 862 try { 863 list.add("first element"); 864 throw new AssertionError ("Should have thrown a ReadOnlyException"); 865 } catch (ReadOnlyException t) { 866 } 868 } 869 } 870 } 871 872 void testReadOnlySet(List list, boolean validate) { 873 if (list instanceof Vector ) { return; } 874 875 876 if (validate) { 877 assertEmptyList(list); 878 } else { 879 synchronized (list) { 880 try { 881 list.set(0, "first element"); 882 throw new AssertionError ("Should have thrown a ReadOnlyException"); 883 } catch (ReadOnlyException t) { 884 } 886 } 887 } 888 } 889 890 void testSetUpRemove(List list, boolean validate) { 892 if (list instanceof Vector ) { return; } 893 894 895 if (validate) { 896 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element" }), list); 897 } else { 898 synchronized (list) { 899 list.add("first element"); 900 list.add("second element"); 901 } 902 tryReadOnlyRemove(list); 903 } 904 } 905 906 private void tryReadOnlyRemove(List list) { 908 synchronized (list) { 909 try { 910 list.remove("second element"); 911 throw new AssertionError ("Should have thrown a ReadOnlyException"); 912 } catch (ReadOnlyException t) { 913 } 915 } 916 } 917 918 void testSetUpToArray(List list, boolean validate) { 919 if (list instanceof Vector ) { return; } 920 921 922 Object [] array = getArray(list); 923 if (validate) { 924 assertEmptyObjectArray(array); 925 } else { 926 synchronized (list) { 927 list.add("first element"); 928 list.add("second element"); 929 } 930 tryReadOnlyToArray(list); 931 } 932 } 933 934 void tryReadOnlyToArray(List list) { 935 Object [] array = getArray(list); 936 synchronized (array) { 937 try { 938 Object [] returnArray = list.toArray(array); 939 Assert.assertTrue(returnArray == array); 940 throw new AssertionError ("Should have thrown a ReadOnlyException"); 941 } catch (ReadOnlyException t) { 942 } 944 } 945 } 946 947 void testSetUpIteratorRemove(List list, boolean validate) { 949 if (list instanceof Vector ) { return; } 950 951 952 if (validate) { 953 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element" }), list); 954 } else { 955 synchronized (list) { 956 list.add("first element"); 957 list.add("second element"); 958 } 959 tryReadOnlyIteratorRemove(list); 960 } 961 } 962 963 private void tryReadOnlyIteratorRemove(List list) { 965 synchronized (list) { 966 try { 967 Iterator iterator = list.iterator(); 968 iterator.next(); 969 iterator.remove(); 970 throw new AssertionError ("Should have thrown a ReadOnlyException"); 971 } catch (ReadOnlyException t) { 972 } 974 } 975 } 976 977 void testSetUpClear(List list, boolean validate) { 979 if (list instanceof Vector ) { return; } 980 981 982 if (validate) { 983 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element" }), list); 984 } else { 985 synchronized (list) { 986 list.add("first element"); 987 list.add("second element"); 988 } 989 tryReadOnlyClear(list); 990 } 991 } 992 993 private void tryReadOnlyClear(List list) { 995 synchronized (list) { 996 try { 997 list.clear(); 998 throw new AssertionError ("Should have thrown a ReadOnlyException"); 999 } catch (ReadOnlyException t) { 1000 } 1002 } 1003 } 1004 1005 void testSetUpRetainAll(List list, boolean validate) { 1007 if (list instanceof Vector ) { return; } 1008 1009 1010 if (validate) { 1011 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element" }), list); 1012 } else { 1013 synchronized (list) { 1014 list.add("first element"); 1015 list.add("second element"); 1016 } 1017 tryReadOnlyRetainAll(list); 1018 } 1019 } 1020 1021 private void tryReadOnlyRetainAll(List list) { 1023 synchronized (list) { 1024 List toRetain = new ArrayList (); 1025 toRetain.add("first element"); 1026 try { 1027 list.retainAll(toRetain); 1028 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1029 } catch (ReadOnlyException t) { 1030 } 1032 } 1033 } 1034 1035 void testSetUpRemoveAll(List list, boolean validate) { 1037 if (list instanceof Vector ) { return; } 1038 1039 1040 if (validate) { 1041 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element" }), list); 1042 } else { 1043 synchronized (list) { 1044 list.add("first element"); 1045 list.add("second element"); 1046 } 1047 tryReadOnlyRemoveAll(list); 1048 } 1049 } 1050 1051 private void tryReadOnlyRemoveAll(List list) { 1053 synchronized (list) { 1054 List toRemove = new ArrayList (); 1055 toRemove.add("first element"); 1056 try { 1057 list.removeAll(toRemove); 1058 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1059 } catch (ReadOnlyException e) { 1060 } 1062 } 1063 } 1064 1065 void testListIteratorReadOnlyAdd(List list, boolean validate) { 1066 if (list instanceof Vector ) { return; } 1067 1068 1069 if (validate) { 1070 assertEmptyList(list); 1071 } else { 1072 synchronized (list) { 1073 ListIterator lIterator = list.listIterator(); 1074 try { 1075 lIterator.add("first element"); 1076 throw new AssertionError ("Should have thrown a ReadOnlyException"); 1077 } catch (ReadOnlyException e) { 1078 } 1080 } 1081 } 1082 } 1083 1084 void testCollectionsAddAll(List list, boolean validate) { 1085 1086 1087 if (validate) { 1088 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element", "third element" }), list); 1089 } else { 1090 synchronized (list) { 1091 list.addAll(Arrays.asList(new Object [] { "first element", "second element", "third element" })); 1092 } 1093 } 1094 } 1095 1096 void testIteratorRemove(List list, boolean validate) { 1098 1099 1100 if (validate) { 1101 assertListsEqual(Arrays.asList(new Object [] { "second element" }), list); 1102 } else { 1103 synchronized (list) { 1104 list.add("first element"); 1105 list.add("second element"); 1106 } 1107 synchronized (list) { 1108 Iterator iterator = list.iterator(); 1109 Assert.assertEquals(true, iterator.hasNext()); 1110 iterator.next(); 1111 iterator.remove(); 1112 } 1113 } 1114 } 1115 1116 void testIteratorDuplicateElementRemove(List list, boolean validate) { 1117 1118 1119 if (validate) { 1120 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element" }), list); 1121 } else { 1122 synchronized (list) { 1123 list.add("first element"); 1124 list.add("second element"); 1125 list.add("first element"); 1126 } 1127 synchronized (list) { 1128 Iterator iterator = list.iterator(); 1129 Assert.assertEquals(true, iterator.hasNext()); 1130 iterator.next(); 1131 iterator.next(); 1132 iterator.next(); 1133 iterator.remove(); 1134 } 1135 } 1136 } 1137 1138 void testIteratorRemoveNull(List list, boolean validate) { 1139 1140 1141 if (validate) { 1142 assertListsEqual(Arrays.asList(new Object [] { "first element", null, "second element" }), list); 1143 } else { 1144 synchronized (list) { 1145 list.add("first element"); 1146 list.add(null); 1147 list.add(null); 1148 list.add("second element"); 1149 } 1150 synchronized (list) { 1151 Iterator iterator = list.iterator(); 1152 Assert.assertEquals(true, iterator.hasNext()); 1153 iterator.next(); 1154 iterator.next(); 1155 iterator.remove(); 1156 } 1157 } 1158 } 1159 1160 void testStackPush(List list, boolean validate) { 1162 if (!(list instanceof Stack )) { return; } 1163 1164 1165 if (validate) { 1166 assertListsEqual(Arrays.asList(new Object [] { "first element", "second element" }), list); 1167 } else { 1168 synchronized (list) { 1169 Stack s = (Stack ) list; 1170 s.push("first element"); 1171 s.push("second element"); 1172 } 1173 } 1174 } 1175 1176 void testStackPop(List list, boolean validate) { 1177 if (!(list instanceof Stack )) { return; } 1178 1179 1180 if (validate) { 1181 assertListsEqual(Arrays.asList(new Object [] { "first element" }), list); 1182 } else { 1183 Stack s = (Stack ) list; 1184 synchronized (list) { 1185 s.push("first element"); 1186 s.push("second element"); 1187 } 1188 synchronized (list) { 1189 Object o = s.pop(); 1190 Assert.assertEquals("second element", o); 1191 } 1192 } 1193 } 1194 1195 void testAddNonPortableObject(List list, boolean validate) { 1196 if (!validate) { 1197 synchronized (list) { 1198 try { 1199 list.add(new MyArrayList2()); 1200 throw new AssertionError ("Should have thrown a TCNonPortableObjectError."); 1201 } catch (TCNonPortableObjectError e) { 1202 } 1204 } 1205 synchronized (list) { 1206 try { 1207 list.add(new MyArrayList3()); 1208 throw new AssertionError ("Should have thrown a TCNonPortableObjectError."); 1209 } catch (TCNonPortableObjectError e) { 1210 } 1212 } 1213 } 1214 } 1215 1216 private Object getMySubclassArray(List list) { 1217 if (list instanceof MyArrayList6) { return (Object [])sharedMap.get("arrayforMyArrayList6"); } 1218 if (list instanceof MyArrayList5) { return (Object [])sharedMap.get("arrayforMyArrayList5"); } 1219 if (list instanceof MyArrayList) { return (Object [])sharedMap.get("arrayforMyArrayList"); } 1220 if (list instanceof MyLinkedList) { return (Object [])sharedMap.get("arrayforMyLinkedList"); } 1221 if (list instanceof MyVector) { return (Object [])sharedMap.get("arrayforMyVector"); } 1222 if (list instanceof MyStack) { return (Object [])sharedMap.get("arrayforMyStack"); } 1223 if (list instanceof MyAbstractListSubclass) { return (Object [])sharedMap.get("arrayforMyAbstractListSubclass"); } 1224 return null; 1225 } 1226 1227 private Object [] getArray(List list) { 1228 Object o = getMySubclassArray(list); 1229 if (o != null) { return (Object []) o; } 1230 1231 if (list instanceof LinkedList ) { return (Object []) sharedMap.get("arrayforLinkedList"); } 1232 if (list instanceof ArrayList ) { return (Object []) sharedMap.get("arrayforArrayList"); } 1233 if (list instanceof Stack ) { return (Object []) sharedMap.get("arrayforStack"); 1236 } 1237 if (list instanceof Vector ) { return (Object []) sharedMap.get("arrayforVector"); } 1238 if (list instanceof MyAbstractListSubclass) { return (Object []) sharedMap.get("arrayforAbstractListSubclass"); } 1239 return null; 1240 } 1241 1242 private static void assertEmptyObjectArray(Object [] array) { 1243 for (int i = 0; i < array.length; i++) { 1244 Assert.assertNull(array[i]); 1245 } 1246 } 1247 1248 private static void assertEmptyList(List list) { 1249 Assert.assertEquals(0, list.size()); 1250 Assert.assertTrue(list.isEmpty()); 1251 1252 int count = 0; 1253 for (Iterator i = list.iterator(); i.hasNext();) { 1254 count++; 1255 } 1256 Assert.assertEquals(0, count); 1257 } 1258 1259 private static void assertListsEqual(List expect, List actual) { 1260 Assert.assertEquals(expect.size(), actual.size()); 1261 1262 Assert.assertTrue(expect.containsAll(actual)); 1263 Assert.assertTrue(actual.containsAll(expect)); 1264 1265 for (int i = 0, n = expect.size(); i < n; i++) { 1266 Assert.assertEquals(expect.get(i), actual.get(i)); 1267 } 1268 1269 if (expect.isEmpty()) { 1270 Assert.assertTrue(actual.isEmpty()); 1271 } else { 1272 Assert.assertFalse(actual.isEmpty()); 1273 } 1274 1275 for (Iterator iExpect = expect.iterator(), iActual = actual.iterator(); iExpect.hasNext();) { 1276 Assert.assertEquals(iExpect.next(), iActual.next()); 1277 } 1278 1279 } 1280 1281 public static void visitL1DSOConfig(ConfigVisitor visitor, DSOClientConfigHelper config) { 1282 String testClass = GenericListTestApp.class.getName(); 1283 config.getOrCreateSpec(testClass); 1284 String writeAllowedMethodExpression = "* " + testClass + "*.*(..)"; 1285 config.addWriteAutolock(writeAllowedMethodExpression); 1286 String readOnlyMethodExpression = "* " + testClass + "*.*ReadOnly*(..)"; 1287 config.addReadAutolock(readOnlyMethodExpression); 1288 config.addIncludePattern(testClass + "$*"); 1289 } 1290 1291 private static void assertSingleElement(List list, Object obj) { 1292 Assert.assertEquals(1, list.size()); 1293 Assert.assertEquals(obj, list.get(0)); 1294 Assert.assertFalse(list.isEmpty()); 1295 Assert.assertTrue(list.contains(obj)); 1296 1297 int count = 0; 1298 for (Iterator i = list.iterator(); i.hasNext();) { 1299 count++; 1300 Assert.assertEquals(obj, i.next()); 1301 } 1302 Assert.assertEquals(1, count); 1303 1304 } 1305 1306 private static class MyArrayList extends ArrayList { 1307 public MyArrayList() { 1308 super(); 1309 } 1310 } 1311 1312 private static class MyArrayList2 extends ArrayList { 1313 private Vector v; 1314 1315 protected void removeRange(int fromIndex, int toIndex) { 1316 super.removeRange(fromIndex, toIndex); 1317 } 1318 } 1319 1320 private static class MyArrayList3 extends ArrayList { 1321 private Vector v; 1322 1323 public void removeRangeLocal(int fromIndex, int toIndex) { 1324 super.removeRange(fromIndex, toIndex); 1325 } 1326 } 1327 1328 private static class MyArrayList4 extends ArrayList { 1329 } 1331 1332 private static class MyArrayList5 extends MyArrayList4 { 1333 } 1335 1336 private static class MyArrayList6 extends ArrayList { 1337 1338 int i = 3; 1339 1340 MyArrayList6() { 1341 Set s = new HashSet (); 1342 s.add("test"); 1343 new ArrayList (s); 1344 if (size() != 0) { throw new AssertionError (); } 1345 } 1346 1347 MyArrayList6(Set s1) { 1348 super(s1); 1349 Set s = new HashSet (); 1350 s.add("test"); 1351 ArrayList l = new ArrayList (s); 1352 if (size() != 0) { throw new AssertionError (); } 1353 } 1354 } 1355 1356 private static class MyLinkedList extends LinkedList { 1357 public MyLinkedList() { 1358 super(); 1359 } 1360 } 1361 1362 private static class MyVector extends Vector { 1363 public MyVector() { 1364 super(); 1365 } 1366 } 1367 1368 private static class MyStack extends Stack { 1369 public MyStack() { 1370 super(); 1371 } 1372 } 1373 1374 private static class MyAbstractListSubclass extends AbstractList { 1375 1378 private ArrayList data = new ArrayList (); 1379 1380 public void add(int index, Object element) { 1381 data.add(index, element); 1382 } 1383 1384 public Object set(int index, Object element) { 1385 return data.set(index, element); 1386 } 1387 1388 public Object get(int index) { 1389 return data.get(index); 1390 } 1391 1392 public int size() { 1393 return data.size(); 1394 } 1395 1396 public Object remove(int index) { 1397 return data.remove(index); 1398 } 1399 1400 } 1401 1402} 1403 | Popular Tags |