CollectionsTest.java revision cc05ad238516f1303687aba4a978e24e57c0c07a
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.util; 19 20import dalvik.annotation.TestTargetNew; 21import dalvik.annotation.TestTargets; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetClass; 24 25import java.util.ArrayList; 26import java.util.Collection; 27import java.util.Collections; 28import java.util.Comparator; 29import java.util.Enumeration; 30import java.util.HashMap; 31import java.util.HashSet; 32import java.util.Iterator; 33import java.util.LinkedList; 34import java.util.List; 35import java.util.ListIterator; 36import java.util.Map; 37import java.util.NoSuchElementException; 38import java.util.Random; 39import java.util.RandomAccess; 40import java.util.Set; 41import java.util.SortedSet; 42import java.util.TreeMap; 43import java.util.TreeSet; 44 45import org.apache.harmony.luni.internal.nls.Messages; 46 47import tests.support.Support_CollectionTest; 48import tests.support.Support_ListTest; 49import tests.support.Support_SetTest; 50import tests.support.Support_UnmodifiableCollectionTest; 51import tests.support.Support_UnmodifiableMapTest; 52 53@TestTargetClass(Collections.class) 54public class CollectionsTest extends junit.framework.TestCase { 55 56 LinkedList ll; 57 58 LinkedList myll; 59 60 LinkedList reversedLinkedList; 61 62 LinkedList myReversedLinkedList; 63 64 Set s; 65 66 Set mys; 67 68 HashMap hm; 69 70 static Object[] objArray; 71 72 static Object[] myobjArray; 73 { 74 objArray = new Object[1000]; 75 myobjArray = new Object[1000]; 76 for (int i = 0; i < objArray.length; i++) { 77 objArray[i] = new Integer(i); 78 myobjArray[i] = new MyInt(i); 79 } 80 } 81 82 public static class ReversedMyIntComparator implements Comparator { 83 public int compare(Object o1, Object o2) { 84 return -((MyInt) o1).compareTo((MyInt) o2); 85 } 86 87 public int equals(Object o1, Object o2) { 88 return ((MyInt) o1).compareTo((MyInt) o2); 89 } 90 } 91 92 public static class SynchCollectionChecker implements Runnable { 93 Collection col; 94 95 int colSize; 96 97 int totalToRun; 98 99 boolean offset; 100 101 volatile int numberOfChecks = 0; 102 103 boolean result = true; 104 105 ArrayList normalCountingList; 106 107 ArrayList offsetCountingList; 108 109 public void run() { 110 // ensure the list either contains the numbers from 0 to size-1 or 111 // the numbers from size to 2*size -1 112 while (numberOfChecks < totalToRun) { 113 synchronized (col) { 114 if (!(col.isEmpty() || col.containsAll(normalCountingList) || col 115 .containsAll(offsetCountingList))) 116 result = false; 117 col.clear(); 118 } 119 if (offset) 120 col.addAll(offsetCountingList); 121 else 122 col.addAll(normalCountingList); 123 numberOfChecks++; 124 } 125 } 126 127 public SynchCollectionChecker(Collection c, boolean offset, 128 int totalChecks) { 129 // The collection to test, whether to offset the filler values by 130 // size or not, and the min number of iterations to run 131 totalToRun = totalChecks; 132 col = c; 133 colSize = c.size(); 134 normalCountingList = new ArrayList(colSize); 135 offsetCountingList = new ArrayList(colSize); 136 for (int counter = 0; counter < colSize; counter++) 137 normalCountingList.add(new Integer(counter)); 138 for (int counter = 0; counter < colSize; counter++) 139 offsetCountingList.add(new Integer(counter + colSize)); 140 col.clear(); 141 if (offset) 142 col.addAll(offsetCountingList); 143 else 144 col.addAll(normalCountingList); 145 } 146 147 public boolean offset() { 148 // answer true iff the list is filled with a counting sequence 149 // starting at the value size to 2*size - 1 150 // else the list with be filled starting at 0 to size - 1 151 return offset; 152 } 153 154 public boolean getResult() { 155 // answer true iff no corruption has been found in the collection 156 return result; 157 } 158 159 public int getNumberOfChecks() { 160 // answer the number of checks that have been performed on the list 161 return numberOfChecks; 162 } 163 } 164 165 public static class SynchMapChecker implements Runnable { 166 Map map; 167 168 int mapSize; 169 170 int totalToRun; 171 172 boolean offset; 173 174 volatile int numberOfChecks = 0; 175 176 boolean result = true; 177 178 Map normalCountingMap; 179 180 Map offsetCountingMap; 181 182 public void run() { 183 Object firstNormalValue = normalCountingMap.get(new Integer(0)); 184 Object lastNormalValue = normalCountingMap.get(new Integer( 185 mapSize - 1)); 186 Object firstOffsetValue = offsetCountingMap 187 .get(new Integer(mapSize)); 188 Object lastOffsetValue = offsetCountingMap.get(new Integer( 189 2 * mapSize - 1)); 190 // ensure the list either contains the numbers from 0 to size-1 or 191 // the numbers from size to 2*size -1 192 while (numberOfChecks < totalToRun) { 193 synchronized (map) { 194 if (!(map.isEmpty() 195 || (map.containsValue(firstNormalValue) && map 196 .containsValue(lastNormalValue)) || (map 197 .containsValue(firstOffsetValue) && map 198 .containsValue(lastOffsetValue)))) 199 result = false; 200 map.clear(); 201 } 202 if (offset) 203 map.putAll(offsetCountingMap); 204 else 205 map.putAll(normalCountingMap); 206 numberOfChecks++; 207 } 208 } 209 210 public SynchMapChecker(Map m, boolean offset, int totalChecks) { 211 // The collection to test, whether to offset the filler values by 212 // size or not, and the min number of iterations to run 213 Integer myInt; 214 totalToRun = totalChecks; 215 map = m; 216 mapSize = m.size(); 217 normalCountingMap = new HashMap(mapSize); 218 offsetCountingMap = new HashMap(mapSize); 219 for (int counter = 0; counter < mapSize; counter++) { 220 myInt = new Integer(counter); 221 normalCountingMap.put(myInt, myInt); 222 } 223 for (int counter = 0; counter < mapSize; counter++) { 224 myInt = new Integer(counter + mapSize); 225 offsetCountingMap.put(myInt, myInt); 226 } 227 map.clear(); 228 if (offset) 229 map.putAll(offsetCountingMap); 230 else 231 map.putAll(normalCountingMap); 232 } 233 234 public boolean offset() { 235 // answer true iff the list is filled with a counting sequence 236 // starting at the value size to 2*size - 1 237 // else the list with be filled starting at 0 to size - 1 238 return offset; 239 } 240 241 public boolean getResult() { 242 // answer true iff no corruption has been found in the collection 243 return result; 244 } 245 246 public int getNumberOfChecks() { 247 // answer the number of checks that have been performed on the list 248 return numberOfChecks; 249 } 250 } 251 252 public static class CollectionTest extends junit.framework.TestCase { 253 254 Collection col; // must contain the Integers 0 to 99 255 256 public CollectionTest(String p1) { 257 super(p1); 258 } 259 260 public CollectionTest(String p1, Collection c) { 261 super(p1); 262 col = c; 263 } 264 265 } 266 267 static class MyInt { 268 int data; 269 270 public MyInt(int value) { 271 data = value; 272 } 273 274 public int compareTo(MyInt object) { 275 return data > object.data ? 1 : (data < object.data ? -1 : 0); 276 } 277 } 278 279 /** 280 * @tests java.util.Collections#binarySearch(java.util.List, 281 * java.lang.Object) 282 */ 283 @TestTargetNew( 284 level = TestLevel.PARTIAL_COMPLETE, 285 notes = "Doesn't verify ClassCastException.", 286 method = "binarySearch", 287 args = {java.util.List.class, java.lang.Object.class} 288 ) 289 public void test_binarySearchLjava_util_ListLjava_lang_Object() { 290 // Test for method int 291 // java.util.Collections.binarySearch(java.util.List, java.lang.Object) 292 // assumes ll is sorted and has no duplicate keys 293 final int llSize = ll.size(); 294 // Ensure a NPE is thrown if the list is NULL 295 try { 296 Collections.binarySearch(null, new Object()); 297 fail("Expected NullPointerException for null list parameter"); 298 } catch (NullPointerException e) { 299 } 300 for (int counter = 0; counter < llSize; counter++) { 301 assertTrue("Returned incorrect binary search item position", ll 302 .get(Collections.binarySearch(ll, ll.get(counter))) == ll 303 .get(counter)); 304 } 305 } 306 307 /** 308 * @tests java.util.Collections#binarySearch(java.util.List, 309 * java.lang.Object, java.util.Comparator) 310 */ 311 @TestTargetNew( 312 level = TestLevel.PARTIAL_COMPLETE, 313 notes = "Doesn't verify ClassCastException.", 314 method = "binarySearch", 315 args = {java.util.List.class, java.lang.Object.class, java.util.Comparator.class} 316 ) 317 public void test_binarySearchLjava_util_ListLjava_lang_ObjectLjava_util_Comparator() { 318 // Test for method int 319 // java.util.Collections.binarySearch(java.util.List, java.lang.Object, 320 // java.util.Comparator) 321 // assumes reversedLinkedList is sorted in reversed order and has no 322 // duplicate keys 323 final int rSize = myReversedLinkedList.size(); 324 ReversedMyIntComparator comp = new ReversedMyIntComparator(); 325 // Ensure a NPE is thrown if the list is NULL 326 try { 327 Collections.binarySearch(null, new Object(), comp); 328 fail("Expected NullPointerException for null list parameter"); 329 } catch (NullPointerException e) { 330 } 331 for (int counter = 0; counter < rSize; counter++) { 332 assertTrue( 333 "Returned incorrect binary search item position using custom comparator", 334 myReversedLinkedList.get(Collections.binarySearch( 335 myReversedLinkedList, myReversedLinkedList 336 .get(counter), comp)) == myReversedLinkedList 337 .get(counter)); 338 } 339 } 340 341 class Mock_ArrayList extends ArrayList { 342 @Override 343 public 344 Object set (int index, Object o){ 345 throw new UnsupportedOperationException(); 346 } 347 } 348 349 /** 350 * @tests java.util.Collections#copy(java.util.List, java.util.List) 351 */ 352 @TestTargetNew( 353 level = TestLevel.COMPLETE, 354 notes = "", 355 method = "copy", 356 args = {java.util.List.class, java.util.List.class} 357 ) 358 public void test_copyLjava_util_ListLjava_util_List() { 359 // Test for method void java.util.Collections.copy(java.util.List, 360 // java.util.List) 361 // Ensure a NPE is thrown if the list is NULL 362 try { 363 Collections.copy(null, ll); 364 fail("Expected NullPointerException for null list first parameter"); 365 } catch (NullPointerException e) { 366 } 367 try { 368 Collections.copy(ll, null); 369 fail("Expected NullPointerException for null list second parameter"); 370 } catch (NullPointerException e) { 371 } 372 final int llSize = ll.size(); 373 ll.set(25, null); 374 ArrayList al = new ArrayList(); 375 Integer extraElement = new Integer(1); 376 Integer extraElement2 = new Integer(2); 377 al.addAll(myReversedLinkedList); 378 al.add(extraElement); 379 al.add(extraElement2); 380 Collections.copy(al, ll); 381 for (int counter = 0; counter < llSize; counter++) { 382 assertTrue("Elements do not match after copying collection", al 383 .get(counter) == ll.get(counter)); 384 } 385 assertTrue("Elements after copied elements affected by copy", 386 extraElement == al.get(llSize) 387 && extraElement2 == al.get(llSize + 1)); 388 389 ArrayList ar1 = new ArrayList(); 390 ArrayList ar2 = new ArrayList(); 391 392 int i; 393 394 for(i = 0; i < 5; i ++) { 395 ar2.add(new Integer(i)); 396 } 397 398 for(i = 0; i < 10; i ++) { 399 ar1.add(new Integer(i)); 400 } 401 402 try { 403 Collections.copy(ar2, ar1); 404 fail("IndexOutOfBoundsException expected"); 405 } catch (IndexOutOfBoundsException e) { 406 //expected 407 } 408 409 Mock_ArrayList mal1 = new Mock_ArrayList(); 410 Mock_ArrayList mal2 = new Mock_ArrayList(); 411 412 for(i = 0; i < 10; i ++) { 413 mal1.add(new Integer(i)); 414 mal2.add(new Integer(10 - i)); 415 } 416 417 try { 418 Collections.copy(mal1, mal2); 419 fail("UnsupportedOperationException expected"); 420 } catch (UnsupportedOperationException e) { 421 //expected 422 } 423 } 424 425 /** 426 * @tests java.util.Collections#copy(java.util.List, java.util.List) 427 */ 428 @TestTargetNew( 429 level = TestLevel.PARTIAL_COMPLETE, 430 notes = "Verifies IndexOutOfBoundsException. Doesn't verify UnsupportedOperationException.", 431 method = "copy", 432 args = {java.util.List.class, java.util.List.class} 433 ) 434 public void test_copy_check_index() { 435 ArrayList a1 = new ArrayList(); 436 a1.add("one"); 437 a1.add("two"); 438 439 ArrayList a2 = new ArrayList(); 440 a2.add("aa"); 441 442 try { 443 Collections.copy(a2, a1); 444 fail("Expected IndexOutOfBoundsException"); 445 } catch (IndexOutOfBoundsException e) { 446 } 447 448 assertEquals("aa", a2.get(0)); 449 } 450 451 /** 452 * @tests java.util.Collections#enumeration(java.util.Collection) 453 */ 454 @TestTargetNew( 455 level = TestLevel.COMPLETE, 456 notes = "", 457 method = "enumeration", 458 args = {java.util.Collection.class} 459 ) 460 public void test_enumerationLjava_util_Collection() { 461 // Test for method java.util.Enumeration 462 // java.util.Collections.enumeration(java.util.Collection) 463 TreeSet ts = new TreeSet(); 464 ts.addAll(s); 465 Enumeration e = Collections.enumeration(ts); 466 int count = 0; 467 while (e.hasMoreElements()) 468 assertTrue("Returned incorrect enumeration", 469 e.nextElement() == objArray[count++]); 470 assertTrue("Enumeration missing elements: " + count, 471 count == objArray.length); 472 } 473 474 /** 475 * @tests java.util.Collections#fill(java.util.List, java.lang.Object) 476 */ 477 @TestTargetNew( 478 level = TestLevel.COMPLETE, 479 notes = "", 480 method = "fill", 481 args = {java.util.List.class, java.lang.Object.class} 482 ) 483 public void test_fillLjava_util_ListLjava_lang_Object() { 484 // Test for method void java.util.Collections.fill(java.util.List, 485 // java.lang.Object) 486 try { 487 Collections.fill(null, new Object()); 488 fail("Expected NullPointerException for null list parameter"); 489 } catch (NullPointerException e) { 490 } 491 final int size = ll.size(); 492 Collections.fill(ll, "k"); 493 assertTrue("Fill modified list size", size == ll.size()); 494 Iterator i = ll.iterator(); 495 while (i.hasNext()) 496 assertEquals("Failed to fill elements", "k", i.next()); 497 498 Collections.fill(ll, null); 499 assertTrue("Fill with nulls modified list size", size == ll.size()); 500 i = ll.iterator(); 501 while (i.hasNext()) 502 assertNull("Failed to fill with nulls", i.next()); 503 504 Mock_ArrayList mal = new Mock_ArrayList(); 505 506 mal.add("one"); 507 mal.add("two"); 508 509 try { 510 Collections.fill(mal, "value"); 511 fail("UnsupportedOperationException ecpected"); 512 } catch (UnsupportedOperationException e) { 513 //expected 514 } 515 } 516 517 /** 518 * @tests java.util.Collections#max(java.util.Collection) 519 */ 520 @TestTargetNew( 521 level = TestLevel.COMPLETE, 522 notes = "", 523 method = "max", 524 args = {java.util.Collection.class} 525 ) 526 public void test_maxLjava_util_Collection() { 527 // Test for method java.lang.Object 528 // java.util.Collections.max(java.util.Collection) 529 // assumes s, objArray are sorted 530 assertTrue("Returned incorrect max element", 531 Collections.max(s) == objArray[objArray.length - 1]); 532 533 ArrayList al = new ArrayList(); 534 535 try { 536 Collections.max(al); 537 fail("NoSuchElementException expected"); 538 } catch (NoSuchElementException e) { 539 //expected 540 } 541 542 al.add("String"); 543 al.add(new Integer(1)); 544 al.add(new Double(3.14)); 545 546 try { 547 Collections.max(al); 548 fail("ClassCastException expected"); 549 } catch (ClassCastException e) { 550 //expected 551 } 552 } 553 554 /** 555 * @tests java.util.Collections#max(java.util.Collection, 556 * java.util.Comparator) 557 */ 558 @TestTargetNew( 559 level = TestLevel.COMPLETE, 560 notes = "", 561 method = "max", 562 args = {java.util.Collection.class, java.util.Comparator.class} 563 ) 564 public void test_maxLjava_util_CollectionLjava_util_Comparator() { 565 // Test for method java.lang.Object 566 // java.util.Collections.max(java.util.Collection, java.util.Comparator) 567 // assumes s, objArray are sorted 568 569 // With this custom (backwards) comparator the 'max' element should be 570 // the smallest in the list 571 ReversedMyIntComparator rmic = new ReversedMyIntComparator(); 572 assertTrue( 573 "Returned incorrect max element using custom comparator", 574 Collections.max(mys, rmic) == myobjArray[0]); 575 576 ArrayList al = new ArrayList(); 577 578 try { 579 Collections.max(al, rmic); 580 fail("NoSuchElementException expected"); 581 } catch (NoSuchElementException e) { 582 //expected 583 } 584 585 al.add("String"); 586 al.add(new Integer(1)); 587 al.add(new Double(3.14)); 588 589 try { 590 Collections.max(al, rmic); 591 fail("ClassCastException expected"); 592 } catch (ClassCastException e) { 593 //expected 594 } 595 } 596 597 /** 598 * @tests java.util.Collections#min(java.util.Collection) 599 */ 600 @TestTargetNew( 601 level = TestLevel.COMPLETE, 602 notes = "", 603 method = "min", 604 args = {java.util.Collection.class} 605 ) 606 public void test_minLjava_util_Collection() { 607 // Test for method java.lang.Object 608 // java.util.Collections.min(java.util.Collection) 609 // assumes s, objArray are sorted 610 assertTrue("Returned incorrect min element", 611 Collections.min(s) == objArray[0]); 612 ArrayList al = new ArrayList(); 613 614 try { 615 Collections.min(al); 616 fail("NoSuchElementException expected"); 617 } catch (NoSuchElementException e) { 618 //expected 619 } 620 621 al.add("String"); 622 al.add(new Integer(1)); 623 al.add(new Double(3.14)); 624 625 try { 626 Collections.min(al); 627 fail("ClassCastException expected"); 628 } catch (ClassCastException e) { 629 //expected 630 } 631 } 632 633 /** 634 * @tests java.util.Collections#min(java.util.Collection, 635 * java.util.Comparator) 636 */ 637 @TestTargetNew( 638 level = TestLevel.COMPLETE, 639 notes = "", 640 method = "min", 641 args = {java.util.Collection.class, java.util.Comparator.class} 642 ) 643 public void test_minLjava_util_CollectionLjava_util_Comparator() { 644 // Test for method java.lang.Object 645 // java.util.Collections.min(java.util.Collection, java.util.Comparator) 646 // assumes s, objArray are sorted 647 648 // With this custom (backwards) comparator the 'min' element should be 649 // the largest in the list 650 ReversedMyIntComparator rmic = new ReversedMyIntComparator(); 651 652 assertTrue( 653 "Returned incorrect min element using custom comparator", 654 Collections.min(mys, rmic) == myobjArray[objArray.length - 1]); 655 656 ArrayList al = new ArrayList(); 657 658 try { 659 Collections.min(al, rmic); 660 fail("NoSuchElementException expected"); 661 } catch (NoSuchElementException e) { 662 //expected 663 } 664 665 al.add("String"); 666 al.add(new Integer(1)); 667 al.add(new Double(3.14)); 668 669 try { 670 Collections.min(al, rmic); 671 fail("ClassCastException expected"); 672 } catch (ClassCastException e) { 673 //expected 674 } 675 } 676 677 /** 678 * @tests java.util.Collections#nCopies(int, java.lang.Object) 679 */ 680 @TestTargetNew( 681 level = TestLevel.COMPLETE, 682 notes = "", 683 method = "nCopies", 684 args = {int.class, java.lang.Object.class} 685 ) 686 public void test_nCopiesILjava_lang_Object() { 687 // Test for method java.util.List java.util.Collections.nCopies(int, 688 // java.lang.Object) 689 Object o = new Object(); 690 List l = Collections.nCopies(100, o); 691 Iterator i = l.iterator(); 692 Object first = i.next(); 693 assertTrue("Returned list consists of copies not refs", first == o); 694 assertEquals("Returned list of incorrect size", 100, l.size()); 695 assertTrue("Contains", l.contains(o)); 696 assertTrue("Contains null", !l.contains(null)); 697 assertTrue("null nCopies contains", !Collections.nCopies(2, null) 698 .contains(o)); 699 assertTrue("null nCopies contains null", Collections.nCopies(2, null) 700 .contains(null)); 701 l = Collections.nCopies(20, null); 702 i = l.iterator(); 703 for (int counter = 0; i.hasNext(); counter++) { 704 assertTrue("List is too large", counter < 20); 705 assertNull("Element should be null: " + counter, i.next()); 706 } 707 try { 708 l.add(o); 709 fail("Returned list is not immutable"); 710 } catch (UnsupportedOperationException e) { 711 // Correct 712 return; 713 } 714 try { 715 Collections.nCopies(-2, new HashSet()); 716 fail("nCopies with negative arg didn't throw IAE"); 717 } catch (IllegalArgumentException e) { 718 // Expected 719 } 720 } 721 722 /** 723 * @tests java.util.Collections#reverse(java.util.List) 724 */ 725 @TestTargetNew( 726 level = TestLevel.COMPLETE, 727 notes = "", 728 method = "reverse", 729 args = {java.util.List.class} 730 ) 731 public void test_reverseLjava_util_List() { 732 // Test for method void java.util.Collections.reverse(java.util.List) 733 try { 734 Collections.reverse(null); 735 fail("Expected NullPointerException for null list parameter"); 736 } catch (NullPointerException e) { 737 } 738 Collections.reverse(ll); 739 Iterator i = ll.iterator(); 740 int count = objArray.length - 1; 741 while (i.hasNext()) { 742 assertTrue("Failed to reverse collection", 743 i.next() == objArray[count]); 744 --count; 745 } 746 ArrayList myList = new ArrayList(); 747 myList.add(null); 748 myList.add(new Integer(20)); 749 Collections.reverse(myList); 750 assertTrue("Did not reverse correctly--first element is: " 751 + myList.get(0), myList.get(0).equals(new Integer(20))); 752 assertNull("Did not reverse correctly--second element is: " 753 + myList.get(1), myList.get(1)); 754 755 Mock_ArrayList mal = new Mock_ArrayList(); 756 757 mal.add("First"); 758 mal.add("Second"); 759 760 try { 761 Collections.reverse(mal); 762 fail("UnsupportedOperationException expected"); 763 } catch (UnsupportedOperationException e) { 764 //expected 765 } 766 } 767 768 /** 769 * @tests java.util.Collections#reverseOrder() 770 */ 771 @TestTargetNew( 772 level = TestLevel.COMPLETE, 773 notes = "", 774 method = "reverseOrder", 775 args = {} 776 ) 777 public void test_reverseOrder() { 778 // Test for method java.util.Comparator 779 // java.util.Collections.reverseOrder() 780 // assumes no duplicates in ll 781 Comparator comp = Collections.reverseOrder(); 782 LinkedList list2 = new LinkedList(ll); 783 Collections.sort(list2, comp); 784 final int llSize = ll.size(); 785 for (int counter = 0; counter < llSize; counter++) 786 assertTrue("New comparator does not reverse sorting order", ll 787 .get(counter) == list2.get(llSize - counter - 1)); 788 } 789 790 /** 791 * @tests java.util.Collections#shuffle(java.util.List) 792 */ 793 @TestTargetNew( 794 level = TestLevel.COMPLETE, 795 notes = "", 796 method = "shuffle", 797 args = {java.util.List.class} 798 ) 799 public void test_shuffleLjava_util_List() { 800 // Test for method void java.util.Collections.shuffle(java.util.List) 801 // Assumes ll is sorted and has no duplicate keys and is large ( > 20 802 // elements) 803 804 // test shuffling a Sequential Access List 805 try { 806 Collections.shuffle(null); 807 fail("Expected NullPointerException for null list parameter"); 808 } catch (NullPointerException e) { 809 } 810 ArrayList al = new ArrayList(); 811 al.addAll(ll); 812 testShuffle(al, "Sequential Access", false); 813 814 // test shuffling a Random Access List 815 LinkedList ll2 = new LinkedList(); 816 ll2.addAll(ll); 817 testShuffle(ll2, "Random Access", false); 818 819 Mock_ArrayList mal = new Mock_ArrayList(); 820 821 mal.add("First"); 822 mal.add("Second"); 823 824 try { 825 Collections.shuffle(mal); 826 fail("UnsupportedOperationException expected"); 827 } catch (UnsupportedOperationException e) { 828 //expected 829 } 830 } 831 832 private void testShuffle(List list, String type, boolean random) { 833 boolean sorted = true; 834 boolean allMatch = true; 835 int index = 0; 836 final int size = list.size(); 837 838 if (random) 839 Collections.shuffle(list); 840 else 841 Collections.shuffle(list, new Random(200)); 842 843 for (int counter = 0; counter < size - 1; counter++) { 844 if (((Integer) list.get(counter)).compareTo((Integer)list.get(counter + 1)) > 0) { 845 sorted = false; 846 } 847 } 848 assertTrue("Shuffling sorted " + type 849 + " list resulted in sorted list (should be unlikely)", !sorted); 850 for (int counter = 0; counter < 20; counter++) { 851 index = 30031 * counter % (size + 1); // 30031 is a large prime 852 if (list.get(index) != ll.get(index)) 853 allMatch = false; 854 } 855 assertTrue("Too many element positions match in shuffled " + type 856 + " list", !allMatch); 857 } 858 859 /** 860 * @tests java.util.Collections#shuffle(java.util.List, java.util.Random) 861 */ 862 @TestTargetNew( 863 level = TestLevel.COMPLETE, 864 notes = "", 865 method = "shuffle", 866 args = {java.util.List.class, java.util.Random.class} 867 ) 868 public void test_shuffleLjava_util_ListLjava_util_Random() { 869 // Test for method void java.util.Collections.shuffle(java.util.List, 870 // java.util.Random) 871 // Assumes ll is sorted and has no duplicate keys and is large ( > 20 872 // elements) 873 874 // test shuffling a Sequential Access List 875 try { 876 Collections.shuffle(null, new Random(200)); 877 fail("Expected NullPointerException for null list parameter"); 878 } catch (NullPointerException e) { 879 } 880 ArrayList al = new ArrayList(); 881 al.addAll(ll); 882 testShuffle(al, "Sequential Access", true); 883 884 // test shuffling a Random Access List 885 LinkedList ll2 = new LinkedList(); 886 ll2.addAll(ll); 887 testShuffle(ll2, "Random Access", true); 888 889 890 Mock_ArrayList mal = new Mock_ArrayList(); 891 892 mal.add("First"); 893 mal.add("Second"); 894 895 try { 896 Collections.shuffle(mal, new Random(200)); 897 fail("UnsupportedOperationException expected"); 898 } catch (UnsupportedOperationException e) { 899 //expected 900 } 901} 902 903 /** 904 * @tests java.util.Collections#singleton(java.lang.Object) 905 */ 906 @TestTargetNew( 907 level = TestLevel.COMPLETE, 908 notes = "", 909 method = "singleton", 910 args = {java.lang.Object.class} 911 ) 912 public void test_singletonLjava_lang_Object() { 913 // Test for method java.util.Set 914 // java.util.Collections.singleton(java.lang.Object) 915 Object o = new Object(); 916 Set single = Collections.singleton(o); 917 assertEquals("Wrong size", 1, single.size()); 918 assertTrue("Contains", single.contains(o)); 919 assertTrue("Contains null", !single.contains(null)); 920 assertTrue("null nCopies contains", !Collections.singleton(null) 921 .contains(o)); 922 assertTrue("null nCopies contains null", Collections.singleton(null) 923 .contains(null)); 924 try { 925 single.add("l"); 926 } catch (UnsupportedOperationException e) { 927 // Correct 928 return; 929 } 930 fail("Allowed modification of singleton"); 931 } 932 933 /** 934 * @tests java.util.Collections#sort(java.util.List) 935 */ 936 @TestTargetNew( 937 level = TestLevel.COMPLETE, 938 notes = "", 939 method = "sort", 940 args = {java.util.List.class} 941 ) 942 public void test_sortLjava_util_List() { 943 // Test for method void java.util.Collections.sort(java.util.List) 944 // assumes no duplicate keys in ll 945 final int llSize = ll.size(); 946 final int rllSize = reversedLinkedList.size(); 947 try { 948 Collections.sort((List)null); 949 fail("Expected NullPointerException for null list parameter"); 950 } catch (NullPointerException e) { 951 } 952 Collections.shuffle(ll); 953 Collections.sort(ll); 954 Collections.sort(reversedLinkedList); 955 for (int counter = 0; counter < llSize - 1; counter++) { 956 assertTrue( 957 "Sorting shuffled list resulted in unsorted list", 958 ((Integer) ll.get(counter)).compareTo((Integer)ll.get(counter + 1)) < 0); 959 } 960 961 for (int counter = 0; counter < rllSize - 1; counter++) { 962 assertTrue("Sorting reversed list resulted in unsorted list", 963 ((Integer) reversedLinkedList.get(counter)) 964 .compareTo((Integer)reversedLinkedList.get(counter + 1)) < 0); 965 } 966 967 ArrayList al = new ArrayList(); 968 969 al.add("String"); 970 al.add(new Integer(1)); 971 al.add(new Double(3.14)); 972 973 try { 974 Collections.sort(al); 975 fail("ClassCastException expected"); 976 } catch (ClassCastException e) { 977 //expected 978 } 979 980 Mock_ArrayList mal = new Mock_ArrayList(); 981 982 mal.add("First"); 983 mal.add("Second"); 984 985 try { 986 Collections.sort(mal); 987 fail("UnsupportedOperationException expected"); 988 } catch (UnsupportedOperationException e) { 989 //expected 990 } 991 } 992 993 /** 994 * @tests java.util.Collections#sort(java.util.List, java.util.Comparator) 995 */ 996 @TestTargetNew( 997 level = TestLevel.COMPLETE, 998 notes = "", 999 method = "sort", 1000 args = {java.util.List.class, java.util.Comparator.class} 1001 ) 1002 public void test_sortLjava_util_ListLjava_util_Comparator() { 1003 // Test for method void java.util.Collections.sort(java.util.List, 1004 // java.util.Comparator) 1005 Comparator comp = new ReversedMyIntComparator(); 1006 try { 1007 Collections.sort(null, comp); 1008 fail("Expected NullPointerException for null list parameter"); 1009 } catch (NullPointerException e) { 1010 } 1011 Collections.shuffle(myll); 1012 Collections.sort(myll, comp); 1013 final int llSize = myll.size(); 1014 1015 for (int counter = 0; counter < llSize - 1; counter++) { 1016 assertTrue( 1017 "Sorting shuffled list with custom comparator resulted in unsorted list", 1018 ((MyInt) myll.get(counter)).compareTo((MyInt) myll 1019 .get(counter + 1)) >= 0); 1020 } 1021 1022 ArrayList al = new ArrayList(); 1023 1024 al.add("String"); 1025 al.add(new Integer(1)); 1026 al.add(new Double(3.14)); 1027 1028 try { 1029 Collections.sort(al, comp); 1030 fail("ClassCastException expected"); 1031 } catch (ClassCastException e) { 1032 //expected 1033 } 1034 1035 Mock_ArrayList mal = new Mock_ArrayList(); 1036 1037 mal.add(new MyInt(1)); 1038 mal.add(new MyInt(2)); 1039 1040 try { 1041 Collections.sort(mal, comp); 1042 fail("UnsupportedOperationException expected"); 1043 } catch (UnsupportedOperationException e) { 1044 //expected 1045 } 1046 } 1047 1048 /** 1049 * @tests java.util.Collections#swap(java.util.List, int, int) 1050 */ 1051 @TestTargetNew( 1052 level = TestLevel.COMPLETE, 1053 notes = "", 1054 method = "swap", 1055 args = {java.util.List.class, int.class, int.class} 1056 ) 1057 public void test_swapLjava_util_ListII() { 1058 // Test for method swap(java.util.List, int, int) 1059 1060 LinkedList smallList = new LinkedList(); 1061 for (int i = 0; i < 10; i++) { 1062 smallList.add(objArray[i]); 1063 } 1064 1065 // test exception cases 1066 try { 1067 Collections.swap(smallList, -1, 6); 1068 fail("Expected IndexOutOfBoundsException for -1"); 1069 } catch (IndexOutOfBoundsException e) { 1070 } 1071 1072 try { 1073 Collections.swap(smallList, 6, -1); 1074 fail("Expected IndexOutOfBoundsException for -1"); 1075 } catch (IndexOutOfBoundsException e) { 1076 } 1077 1078 try { 1079 Collections.swap(smallList, 6, 11); 1080 fail("Expected IndexOutOfBoundsException for 11"); 1081 } catch (IndexOutOfBoundsException e) { 1082 } 1083 1084 try { 1085 Collections.swap(smallList, 11, 6); 1086 fail("Expected IndexOutOfBoundsException for 11"); 1087 } catch (IndexOutOfBoundsException e) { 1088 } 1089 1090 // Ensure a NPE is thrown if the list is NULL 1091 try { 1092 Collections.swap(null, 1, 1); 1093 fail("Expected NullPointerException for null list parameter"); 1094 } catch (NullPointerException e) { 1095 } 1096 1097 // test with valid parameters 1098 Collections.swap(smallList, 4, 7); 1099 assertEquals("Didn't Swap the element at position 4 ", new Integer(7), 1100 smallList.get(4)); 1101 assertEquals("Didn't Swap the element at position 7 ", new Integer(4), 1102 smallList.get(7)); 1103 1104 // make sure other elements didn't get swapped by mistake 1105 for (int i = 0; i < 10; i++) { 1106 if (i != 4 && i != 7) 1107 assertEquals("shouldn't have swapped the element at position " 1108 + i, new Integer(i), smallList.get(i)); 1109 } 1110 } 1111 1112 /** 1113 * @tests java.util.Collections#replaceAll(java.util.List, java.lang.Object, 1114 * java.lang.Object) 1115 */ 1116 @TestTargetNew( 1117 level = TestLevel.COMPLETE, 1118 notes = "", 1119 method = "replaceAll", 1120 args = {java.util.List.class, java.lang.Object.class, java.lang.Object.class} 1121 ) 1122 public void test_replaceAllLjava_util_ListLjava_lang_ObjectLjava_lang_Object() { 1123 // Test for method replaceAll(java.util.List, java.lang.Object, 1124 // java.lang.Object) 1125 1126 String string1 = "A-B-C-D-E-S-JF-SUB-G-H-I-J-SUBL-K-L-LIST-M-N--S-S-O-SUBLIS-P-Q-R-SUBLIST-S-T-U-V-W-X-Y-Z"; 1127 char[] chars = string1.toCharArray(); 1128 List list = new ArrayList(); 1129 for (int i = 0; i < chars.length; i++) { 1130 list.add(new Character(chars[i])); 1131 } 1132 1133 try { 1134 Collections.replaceAll(null, new Object(), new Object()); 1135 fail("Expected NullPointerException for null list parameter"); 1136 } catch (NullPointerException e) { 1137 } 1138 1139 // test replace for an element that is not in the list 1140 boolean result = Collections.replaceAll(list, new Character('1'), 1141 new Character('Z')); 1142 assertFalse("Test1: Collections.replaceAll() returned wrong result", 1143 result); 1144 assertEquals("Test2 : ReplaceAll modified the list incorrectly", 1145 string1, getString(list)); 1146 1147 // test replace for an element that is in the list 1148 result = Collections.replaceAll(list, new Character('S'), 1149 new Character('K')); 1150 assertTrue("Test3: Collections.replaceAll() returned wrong result", 1151 result); 1152 assertEquals("Test4: ReplaceAll modified the list incorrectly", 1153 (string1 = string1.replace('S', 'K')), getString(list)); 1154 1155 // test replace for the last element in the list 1156 result = Collections.replaceAll(list, new Character('Z'), 1157 new Character('N')); 1158 assertTrue("Test5: Collections.replaceAll() returned wrong result", 1159 result); 1160 assertEquals("Test6: ReplaceAll modified the list incorrectly", 1161 (string1 = string1.replace('Z', 'N')), getString(list)); 1162 1163 // test replace for the first element in the list 1164 result = Collections.replaceAll(list, new Character('A'), 1165 new Character('B')); 1166 assertTrue("Test7: Collections.replaceAll() returned wrong result", 1167 result); 1168 assertEquals("Test8: ReplaceAll modified the list incorrectly", 1169 (string1 = string1.replace('A', 'B')), getString(list)); 1170 1171 // test replacing elements with null 1172 LinkedList smallList = new LinkedList(); 1173 for (int i = 0; i < 10; i++) { 1174 smallList.add(objArray[i]); 1175 } 1176 smallList.set(4, new Integer(5)); 1177 result = Collections.replaceAll(smallList, new Integer(5), null); 1178 assertTrue("Test9: Collections.replaceAll() returned wrong result", 1179 result); 1180 for (int i = 0; i < smallList.size(); i++) { 1181 if (i == 4 || i == 5) 1182 assertSame("Test9: ReplaceAll didn't replace element at " + i, 1183 null, smallList.get(i)); 1184 else 1185 assertEquals( 1186 "Test9: ReplaceAll shouldn't have replaced element at " 1187 + i, new Integer(i), smallList.get(i)); 1188 } 1189 1190 // test replacing null elements with another value 1191 result = Collections.replaceAll(smallList, null, new Integer(99)); 1192 assertTrue("Test10: Collections.replaceAll() returned wrong result", 1193 result); 1194 1195 for (int i = 0; i < smallList.size(); i++) { 1196 if (i == 4 || i == 5) 1197 assertEquals("Test10: ReplaceAll didn't replace element at " 1198 + i, new Integer(99), smallList.get(i)); 1199 else 1200 assertEquals( 1201 "Test10: ReplaceAll shouldn't have replaced element at " 1202 + i, new Integer(i), smallList.get(i)); 1203 } 1204 1205 Mock_ArrayList mal = new Mock_ArrayList(); 1206 1207 mal.add("First"); 1208 mal.add("Second"); 1209 1210 try { 1211 Collections.replaceAll(mal, "Second", null); 1212 fail("UnsupportedOperationException expected"); 1213 } catch (UnsupportedOperationException e) { 1214 //expected 1215 } 1216 } 1217 1218 /** 1219 * @tests java.util.Collections#rotate(java.util.List, int) 1220 */ 1221 @TestTargetNew( 1222 level = TestLevel.PARTIAL_COMPLETE, 1223 notes = "Verifies functionality, and UnsupportedOperationException.", 1224 method = "rotate", 1225 args = {java.util.List.class, int.class} 1226 ) 1227 public void test_rotateLjava_util_ListI() { 1228 // Test for method rotate(java.util.List, int) 1229 1230 try { 1231 Collections.rotate(null, 0); 1232 fail("Expected NullPointerException for null list parameter"); 1233 } catch (NullPointerException e) { 1234 } 1235 1236 // Test rotating a Sequential Access List 1237 LinkedList list1 = new LinkedList(); 1238 for (int i = 0; i < 10; i++) { 1239 list1.add(objArray[i]); 1240 } 1241 testRotate(list1, "Sequential Access"); 1242 1243 // Test rotating a Random Access List 1244 ArrayList list2 = new ArrayList(); 1245 for (int i = 0; i < 10; i++) { 1246 list2.add(objArray[i]); 1247 } 1248 testRotate(list2, "Random Access"); 1249 } 1250 1251 private void testRotate(List list, String type) { 1252 // rotate with positive distance 1253 Collections.rotate(list, 7); 1254 assertEquals("Test1: rotate modified the " + type 1255 + " list incorrectly,", "3456789012", getString(list)); 1256 1257 // rotate with negative distance 1258 Collections.rotate(list, -2); 1259 assertEquals("Test2: rotate modified the " + type 1260 + " list incorrectly,", "5678901234", getString(list)); 1261 1262 // rotate sublist with negative distance 1263 List subList = list.subList(1, 5); 1264 Collections.rotate(subList, -1); 1265 assertEquals("Test3: rotate modified the " + type 1266 + " list incorrectly,", "5789601234", getString(list)); 1267 1268 // rotate sublist with positive distance 1269 Collections.rotate(subList, 2); 1270 assertEquals("Test4: rotate modified the " + type 1271 + " list incorrectly,", "5967801234", getString(list)); 1272 1273 // rotate with positive distance that is larger than list size 1274 Collections.rotate(list, 23); 1275 assertEquals("Test5: rotate modified the " + type 1276 + " list incorrectly,", "2345967801", getString(list)); 1277 1278 // rotate with negative distance that is larger than list size 1279 Collections.rotate(list, -23); 1280 assertEquals("Test6: rotate modified the " + type 1281 + " list incorrectly,", "5967801234", getString(list)); 1282 1283 // rotate with 0 and equivalent distances, this should make no 1284 // modifications to the list 1285 Collections.rotate(list, 0); 1286 assertEquals("Test7: rotate modified the " + type 1287 + " list incorrectly,", "5967801234", getString(list)); 1288 1289 Collections.rotate(list, -30); 1290 assertEquals("Test8: rotate modified the " + type 1291 + " list incorrectly,", "5967801234", getString(list)); 1292 1293 Collections.rotate(list, 30); 1294 assertEquals("Test9: rotate modified the " + type 1295 + " list incorrectly,", "5967801234", getString(list)); 1296 } 1297 1298 private String getString(List list) { 1299 StringBuffer buffer = new StringBuffer(); 1300 for (int i = 0; i < list.size(); i++) { 1301 buffer.append(list.get(i)); 1302 } 1303 return buffer.toString(); 1304 } 1305 1306 /** 1307 * @tests java.util.Collections#rotate(java.util.List, int) 1308 */ 1309 @TestTargetNew( 1310 level = TestLevel.COMPLETE, 1311 notes = "", 1312 method = "rotate", 1313 args = {java.util.List.class, int.class} 1314 ) 1315 public void test_rotate2() { 1316 List list = new ArrayList(); 1317 try { 1318 Collections.rotate(list, 5); 1319 } catch (UnsupportedOperationException e) { 1320 fail("Unexpected UnsupportedOperationException for empty list, " 1321 + e); 1322 } 1323 1324 list.add(0, "zero"); 1325 list.add(1, "one"); 1326 list.add(2, "two"); 1327 list.add(3, "three"); 1328 list.add(4, "four"); 1329 1330 Collections.rotate(list, Integer.MIN_VALUE); 1331 assertEquals("Rotated incorrectly at position 0, ", "three", 1332 (String) list.get(0)); 1333 assertEquals("Rotated incorrectly at position 1, ", "four", 1334 (String) list.get(1)); 1335 assertEquals("Rotated incorrectly at position 2, ", "zero", 1336 (String) list.get(2)); 1337 assertEquals("Rotated incorrectly at position 3, ", "one", 1338 (String) list.get(3)); 1339 assertEquals("Rotated incorrectly at position 4, ", "two", 1340 (String) list.get(4)); 1341 } 1342 1343 /** 1344 * @tests java.util.Collections#indexOfSubList(java.util.List, 1345 * java.util.List) 1346 */ 1347 @TestTargetNew( 1348 level = TestLevel.PARTIAL_COMPLETE, 1349 notes = "Verifies boundary conditions, and NullPointerException.", 1350 method = "indexOfSubList", 1351 args = {java.util.List.class, java.util.List.class} 1352 ) 1353 public void test_indexOfSubListLjava_util_ListLjava_util_List() { 1354 // Test for method int indexOfSubList(java.util.List, java.util.List) 1355 List list = new ArrayList(); 1356 try { 1357 Collections.indexOfSubList(null, list); 1358 fail("Expected NullPointerException for null list first parameter"); 1359 } catch (NullPointerException e) { 1360 } 1361 try { 1362 Collections.indexOfSubList(list, null); 1363 fail("Expected NullPointerException for null list second parameter"); 1364 } catch (NullPointerException e) { 1365 } 1366 1367 String string1 = "A-B-C-D-E-S-JF-SUB-G-H-I-J-SUBL-K-L-LIST-M-N--S-S-O-SUBLIS-P-Q-R-SUBLIST-S-T-U-V-W-X-Y-Z"; 1368 1369 testwithCharList(1, string1, "B", true); 1370 testwithCharList(2, string1, "LIST", true); 1371 testwithCharList(3, string1, "SUBLIST", true); 1372 testwithCharList(4, string1, "NONE", true); 1373 testwithCharList(5, string1, "END", true); 1374 1375 // test boundary conditions: 1376 testwithCharList(6, "", "", true); 1377 testwithCharList(7, "LIST", "", true); 1378 testwithCharList(8, "", "SUBLIST", true); 1379 } 1380 1381 /** 1382 * @tests java.util.Collections#indexOfSubList(java.util.List, 1383 * java.util.List) 1384 */ 1385 @TestTargetNew( 1386 level = TestLevel.COMPLETE, 1387 notes = "", 1388 method = "indexOfSubList", 1389 args = {java.util.List.class, java.util.List.class} 1390 ) 1391 public void test_indexOfSubList2() { 1392 ArrayList sub = new ArrayList(); 1393 sub.add(new Integer(1)); 1394 sub.add(new Integer(2)); 1395 sub.add(new Integer(3)); 1396 1397 ArrayList sub2 = new ArrayList(); 1398 sub2.add(new Integer(7)); 1399 sub2.add(new Integer(8)); 1400 1401 ArrayList src = new ArrayList(); 1402 src.addAll(sub); 1403 src.addAll(sub); 1404 src.addAll(sub); 1405 src.add(new Integer(5)); 1406 src.add(new Integer(6)); 1407 1408 // so src becomes a list like this: 1409 // [1, 2, 3, 1, 2, 3, 1, 2, 3, 5, 6] 1410 1411 sub = new ArrayList(src.subList(3, 11)); 1412 // [1, 2, 3, 1, 2, 3, 5, 6] 1413 assertEquals("TestA : Returned wrong indexOfSubList, ", 3, Collections 1414 .indexOfSubList(src, sub)); 1415 1416 sub = new ArrayList(src.subList(6, 11)); 1417 // [1, 2, 3, 5, 6] 1418 assertEquals("TestB : Returned wrong indexOfSubList, ", 6, Collections 1419 .indexOfSubList(src, sub)); 1420 1421 sub = new ArrayList(src.subList(0, 3)); 1422 // [1, 2, 3] 1423 assertEquals("TestCC : Returned wrong indexOfSubList, ", 0, Collections 1424 .indexOfSubList(src, sub)); 1425 1426 sub = new ArrayList(src.subList(9, 11)); 1427 // [5, 6] 1428 assertEquals("TestD : Returned wrong indexOfSubList, ", 9, Collections 1429 .indexOfSubList(src, sub)); 1430 1431 sub = new ArrayList(src.subList(10, 11)); 1432 // [6] 1433 assertEquals("TestE : Returned wrong indexOfSubList, ", 10, Collections 1434 .indexOfSubList(src, sub)); 1435 1436 sub = new ArrayList(src.subList(0, 11)); 1437 // the whole list 1438 assertEquals("TestH : Returned wrong indexIndexOfSubList, ", 0, 1439 Collections.indexOfSubList(src, sub)); 1440 1441 // a non-matching list 1442 assertEquals("TestI : Returned wrong indexOfSubList, ", -1, Collections 1443 .indexOfSubList(src, sub2)); 1444 } 1445 1446 /** 1447 * @param string2 1448 * @param string1 1449 * @param index 1450 */ 1451 private void testwithCharList(int count, String string1, String string2, 1452 boolean first) { 1453 char[] chars = string1.toCharArray(); 1454 List list = new ArrayList(); 1455 for (int i = 0; i < chars.length; i++) { 1456 list.add(new Character(chars[i])); 1457 } 1458 chars = string2.toCharArray(); 1459 List sublist = new ArrayList(); 1460 for (int i = 0; i < chars.length; i++) { 1461 sublist.add(new Character(chars[i])); 1462 } 1463 1464 if (first) 1465 assertEquals("Test " + count + ": Returned wrong index:", string1 1466 .indexOf(string2), Collections 1467 .indexOfSubList(list, sublist)); 1468 else 1469 assertEquals("Test " + count + ": Returned wrong index:", string1 1470 .lastIndexOf(string2), Collections.lastIndexOfSubList(list, 1471 sublist)); 1472 } 1473 1474 /** 1475 * @tests java.util.Collections#lastIndexOfSubList(java.util.List, 1476 * java.util.List) 1477 */ 1478 @TestTargetNew( 1479 level = TestLevel.PARTIAL_COMPLETE, 1480 notes = "Verifies NullPointerException, and boundary conditions.", 1481 method = "lastIndexOfSubList", 1482 args = {java.util.List.class, java.util.List.class} 1483 ) 1484 public void test_lastIndexOfSubListLjava_util_ListLjava_util_List() { 1485 // Test for method int lastIndexOfSubList(java.util.List, 1486 // java.util.List) 1487 String string1 = "A-B-C-D-E-S-JF-SUB-G-H-I-J-SUBL-K-L-LIST-M-N--S-S-O-SUBLIS-P-Q-R-SUBLIST-S-T-U-V-W-X-Y-Z-END"; 1488 1489 List list = new ArrayList(); 1490 try { 1491 Collections.lastIndexOfSubList(null, list); 1492 fail("Expected NullPointerException for null list first parameter"); 1493 } catch (NullPointerException e) { 1494 } 1495 try { 1496 Collections.lastIndexOfSubList(list, null); 1497 fail("Expected NullPointerException for null list second parameter"); 1498 } catch (NullPointerException e) { 1499 } 1500 1501 testwithCharList(1, string1, "B", false); 1502 testwithCharList(2, string1, "LIST", false); 1503 testwithCharList(3, string1, "SUBLIST", false); 1504 testwithCharList(4, string1, "END", false); 1505 testwithCharList(5, string1, "NONE", false); 1506 1507 // test boundary conditions 1508 testwithCharList(6, "", "", false); 1509 testwithCharList(7, "LIST", "", false); 1510 testwithCharList(8, "", "SUBLIST", false); 1511 } 1512 1513 /** 1514 * @tests java.util.Collections#lastIndexOfSubList(java.util.List, 1515 * java.util.List) 1516 */ 1517 @TestTargetNew( 1518 level = TestLevel.COMPLETE, 1519 notes = "", 1520 method = "lastIndexOfSubList", 1521 args = {java.util.List.class, java.util.List.class} 1522 ) 1523 public void test_lastIndexOfSubList2() { 1524 ArrayList sub = new ArrayList(); 1525 sub.add(new Integer(1)); 1526 sub.add(new Integer(2)); 1527 sub.add(new Integer(3)); 1528 1529 ArrayList sub2 = new ArrayList(); 1530 sub2.add(new Integer(7)); 1531 sub2.add(new Integer(8)); 1532 1533 ArrayList src = new ArrayList(); 1534 src.addAll(sub); 1535 src.addAll(sub); 1536 src.addAll(sub); 1537 src.add(new Integer(5)); 1538 src.add(new Integer(6)); 1539 1540 // so src is a list like this: 1541 // [1, 2, 3, 1, 2, 3, 1, 2, 3, 5, 6] 1542 1543 Collections.reverse(src); 1544 // it becomes like this : 1545 // [6, 5, 3, 2, 1, 3, 2, 1, 3, 2, 1] 1546 1547 sub = new ArrayList(src.subList(0, 8)); 1548 // [6, 5, 3, 2, 1, 3, 2, 1] 1549 assertEquals("TestA : Returned wrong lastIndexOfSubList, ", 0, 1550 Collections.lastIndexOfSubList(src, sub)); 1551 1552 sub = new ArrayList(src.subList(0, 5)); 1553 // [6, 5, 3, 2, 1] 1554 assertEquals("TestB : Returned wrong lastIndexOfSubList, ", 0, 1555 Collections.lastIndexOfSubList(src, sub)); 1556 1557 sub = new ArrayList(src.subList(2, 5)); 1558 // [3, 2, 1] 1559 assertEquals("TestC : Returned wrong lastIndexOfSubList, ", 8, 1560 Collections.lastIndexOfSubList(src, sub)); 1561 1562 sub = new ArrayList(src.subList(9, 11)); 1563 // [2, 1] 1564 assertEquals("TestD : Returned wrong lastIndexOfSubList, ", 9, 1565 Collections.lastIndexOfSubList(src, sub)); 1566 1567 sub = new ArrayList(src.subList(10, 11)); 1568 // [1] 1569 assertEquals("TestE : Returned wrong lastIndexOfSubList, ", 10, 1570 Collections.lastIndexOfSubList(src, sub)); 1571 1572 sub = new ArrayList(src.subList(0, 2)); 1573 // [6, 5] 1574 assertEquals("TestF : Returned wrong lastIndexOfSubList, ", 0, 1575 Collections.lastIndexOfSubList(src, sub)); 1576 1577 sub = new ArrayList(src.subList(0, 1)); 1578 // [6] 1579 assertEquals("TestG : Returned wrong lastIndexOfSubList, ", 0, 1580 Collections.lastIndexOfSubList(src, sub)); 1581 1582 sub = new ArrayList(src.subList(0, 11)); 1583 // the whole list 1584 assertEquals("TestH : Returned wrong lastIndexOfSubList, ", 0, 1585 Collections.lastIndexOfSubList(src, sub)); 1586 1587 // a non-matching list 1588 assertEquals("TestI : Returned wrong lastIndexOfSubList, ", -1, 1589 Collections.lastIndexOfSubList(src, sub2)); 1590 } 1591 1592 /** 1593 * @tests java.util.Collections#list(java.util.Enumeration) 1594 */ 1595 @TestTargetNew( 1596 level = TestLevel.COMPLETE, 1597 notes = "", 1598 method = "list", 1599 args = {java.util.Enumeration.class} 1600 ) 1601 public void test_listLjava_util_Enumeration() { 1602 // Test for method java.util.ArrayList list(java.util.Enumeration) 1603 1604 Enumeration e = Collections.enumeration(ll); 1605 ArrayList al = Collections.list(e); 1606 1607 int size = al.size(); 1608 assertEquals("Wrong size", ll.size(), size); 1609 1610 for (int i = 0; i < size; i++) { 1611 assertEquals("wrong element at position " + i + ",", ll.get(i), al 1612 .get(i)); 1613 } 1614 } 1615 1616 /** 1617 * @tests java.util.Collections#synchronizedCollection(java.util.Collection) 1618 */ 1619 @TestTargetNew( 1620 level = TestLevel.COMPLETE, 1621 notes = "", 1622 method = "synchronizedCollection", 1623 args = {java.util.Collection.class} 1624 ) 1625 public void test_synchronizedCollectionLjava_util_Collection() { 1626 // Test for method java.util.Collection 1627 // java.util.Collections.synchronizedCollection(java.util.Collection) 1628 1629 LinkedList smallList = new LinkedList(); 1630 for (int i = 0; i < 50; i++) { 1631 smallList.add(objArray[i]); 1632 } 1633 1634 final int numberOfLoops = 200; 1635 Collection synchCol = Collections.synchronizedCollection(smallList); 1636 // Replacing the previous line with the line below *should* cause the 1637 // test to fail--the collecion below isn't synchronized 1638 // Collection synchCol = smallList; 1639 1640 SynchCollectionChecker normalSynchChecker = new SynchCollectionChecker( 1641 synchCol, false, numberOfLoops); 1642 SynchCollectionChecker offsetSynchChecker = new SynchCollectionChecker( 1643 synchCol, true, numberOfLoops); 1644 Thread normalThread = new Thread(normalSynchChecker); 1645 Thread offsetThread = new Thread(offsetSynchChecker); 1646 normalThread.start(); 1647 offsetThread.start(); 1648 while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops) 1649 || (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) { 1650 try { 1651 Thread.sleep(10); 1652 } catch (InterruptedException e) { 1653 } 1654 } 1655 assertTrue("Returned collection corrupted by multiple thread access", 1656 normalSynchChecker.getResult() 1657 && offsetSynchChecker.getResult()); 1658 try { 1659 normalThread.join(5000); 1660 offsetThread.join(5000); 1661 } catch (InterruptedException e) { 1662 fail("join() interrupted"); 1663 } 1664 1665 synchCol.add(null); 1666 assertTrue("Trying to use nulls in collection failed", synchCol 1667 .contains(null)); 1668 1669 smallList = new LinkedList(); 1670 for (int i = 0; i < 100; i++) { 1671 smallList.add(objArray[i]); 1672 } 1673 new Support_CollectionTest("", Collections 1674 .synchronizedCollection(smallList)).runTest(); 1675 } 1676 1677 /** 1678 * @tests java.util.Collections#synchronizedList(java.util.List) 1679 */ 1680 @TestTargetNew( 1681 level = TestLevel.COMPLETE, 1682 notes = "", 1683 method = "synchronizedList", 1684 args = {java.util.List.class} 1685 ) 1686 public void test_synchronizedListLjava_util_List() { 1687 try { 1688 Collections.synchronizedList(null); 1689 fail("Expected NullPointerException for null list parameter"); 1690 } catch (NullPointerException e) { 1691 } 1692 1693 // test with a Sequential Access List 1694 List smallList = new LinkedList(); 1695 testSynchronizedList(smallList, "Sequential Access"); 1696 1697 smallList = new LinkedList(); 1698 List myList; 1699 for (int i = 0; i < 100; i++) { 1700 smallList.add(objArray[i]); 1701 } 1702 myList = Collections.synchronizedList(smallList); 1703 new Support_ListTest("", myList).runTest(); 1704 1705 // test with a Random Access List 1706 smallList = new ArrayList(); 1707 testSynchronizedList(smallList, "Random Access"); 1708 1709 smallList = new ArrayList(); 1710 for (int i = 0; i < 100; i++) { 1711 smallList.add(objArray[i]); 1712 } 1713 myList = Collections.synchronizedList(smallList); 1714 new Support_ListTest("", myList).runTest(); 1715 } 1716 1717 private void testSynchronizedList(List smallList, String type) { 1718 for (int i = 0; i < 50; i++) { 1719 smallList.add(objArray[i]); 1720 } 1721 final int numberOfLoops = 200; 1722 List synchList = Collections.synchronizedList(smallList); 1723 if (type.equals("Random Access")) 1724 assertTrue( 1725 "Returned synchronized list should implement the Random Access interface", 1726 synchList instanceof RandomAccess); 1727 else 1728 assertTrue( 1729 "Returned synchronized list should not implement the Random Access interface", 1730 !(synchList instanceof RandomAccess)); 1731 1732 // Replacing the previous line with the line below *should* cause the 1733 // test to fail--the list below isn't synchronized 1734 // List synchList = smallList; 1735 SynchCollectionChecker normalSynchChecker = new SynchCollectionChecker( 1736 synchList, false, numberOfLoops); 1737 SynchCollectionChecker offsetSynchChecker = new SynchCollectionChecker( 1738 synchList, true, numberOfLoops); 1739 Thread normalThread = new Thread(normalSynchChecker); 1740 Thread offsetThread = new Thread(offsetSynchChecker); 1741 normalThread.start(); 1742 offsetThread.start(); 1743 while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops) 1744 || (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) { 1745 try { 1746 Thread.sleep(10); 1747 } catch (InterruptedException e) { 1748 } 1749 } 1750 assertTrue( 1751 type 1752 + " list tests: Returned list corrupted by multiple thread access", 1753 normalSynchChecker.getResult() 1754 && offsetSynchChecker.getResult()); 1755 try { 1756 normalThread.join(5000); 1757 offsetThread.join(5000); 1758 } catch (InterruptedException e) { 1759 fail(type + " list tests: join() interrupted"); 1760 } 1761 synchList.set(25, null); 1762 assertNull(type + " list tests: Trying to use nulls in list failed", 1763 synchList.get(25)); 1764 } 1765 1766 /** 1767 * @tests java.util.Collections#synchronizedMap(java.util.Map) 1768 */ 1769 @TestTargetNew( 1770 level = TestLevel.COMPLETE, 1771 notes = "", 1772 method = "synchronizedMap", 1773 args = {java.util.Map.class} 1774 ) 1775 public void test_synchronizedMapLjava_util_Map() { 1776 // Test for method java.util.Map 1777 // java.util.Collections.synchronizedMap(java.util.Map) 1778 HashMap smallMap = new HashMap(); 1779 for (int i = 0; i < 50; i++) { 1780 smallMap.put(objArray[i], objArray[i]); 1781 } 1782 1783 final int numberOfLoops = 200; 1784 Map synchMap = Collections.synchronizedMap(smallMap); 1785 // Replacing the previous line with the line below should cause the test 1786 // to fail--the list below isn't synchronized 1787 // Map synchMap = smallMap; 1788 1789 SynchMapChecker normalSynchChecker = new SynchMapChecker(synchMap, 1790 false, numberOfLoops); 1791 SynchMapChecker offsetSynchChecker = new SynchMapChecker(synchMap, 1792 true, numberOfLoops); 1793 Thread normalThread = new Thread(normalSynchChecker); 1794 Thread offsetThread = new Thread(offsetSynchChecker); 1795 normalThread.start(); 1796 offsetThread.start(); 1797 while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops) 1798 || (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) { 1799 try { 1800 Thread.sleep(10); 1801 } catch (InterruptedException e) { 1802 } 1803 } 1804 assertTrue("Returned map corrupted by multiple thread access", 1805 normalSynchChecker.getResult() 1806 && offsetSynchChecker.getResult()); 1807 try { 1808 normalThread.join(5000); 1809 offsetThread.join(5000); 1810 } catch (InterruptedException e) { 1811 fail("join() interrupted"); 1812 } 1813 1814 // synchronized map does not have to permit null keys or values 1815 synchMap.put(new Long(25), null); 1816 synchMap.put(null, new Long(30)); 1817 assertNull("Trying to use a null value in map failed", synchMap 1818 .get(new Long(25))); 1819 assertTrue("Trying to use a null key in map failed", synchMap.get(null) 1820 .equals(new Long(30))); 1821 1822 smallMap = new HashMap(); 1823 for (int i = 0; i < 100; i++) { 1824 smallMap.put(objArray[i].toString(), objArray[i]); 1825 } 1826 synchMap = Collections.synchronizedMap(smallMap); 1827 new Support_UnmodifiableMapTest("", synchMap).runTest(); 1828 synchMap.keySet().remove(objArray[50].toString()); 1829 assertNull( 1830 "Removing a key from the keySet of the synchronized map did not remove it from the synchronized map: ", 1831 synchMap.get(objArray[50].toString())); 1832 assertNull( 1833 "Removing a key from the keySet of the synchronized map did not remove it from the original map", 1834 smallMap.get(objArray[50].toString())); 1835 } 1836 1837 /** 1838 * @tests java.util.Collections#synchronizedSet(java.util.Set) 1839 */ 1840 @TestTargetNew( 1841 level = TestLevel.COMPLETE, 1842 notes = "", 1843 method = "synchronizedSet", 1844 args = {java.util.Set.class} 1845 ) 1846 public void test_synchronizedSetLjava_util_Set() { 1847 // Test for method java.util.Set 1848 // java.util.Collections.synchronizedSet(java.util.Set) 1849 HashSet smallSet = new HashSet(); 1850 for (int i = 0; i < 50; i++) { 1851 smallSet.add(objArray[i]); 1852 } 1853 1854 final int numberOfLoops = 200; 1855 Set synchSet = Collections.synchronizedSet(smallSet); 1856 // Replacing the previous line with the line below should cause the test 1857 // to fail--the set below isn't synchronized 1858 // Set synchSet = smallSet; 1859 1860 SynchCollectionChecker normalSynchChecker = new SynchCollectionChecker( 1861 synchSet, false, numberOfLoops); 1862 SynchCollectionChecker offsetSynchChecker = new SynchCollectionChecker( 1863 synchSet, true, numberOfLoops); 1864 Thread normalThread = new Thread(normalSynchChecker); 1865 Thread offsetThread = new Thread(offsetSynchChecker); 1866 normalThread.start(); 1867 offsetThread.start(); 1868 while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops) 1869 || (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) { 1870 try { 1871 Thread.sleep(10); 1872 } catch (InterruptedException e) { 1873 } 1874 } 1875 assertTrue("Returned set corrupted by multiple thread access", 1876 normalSynchChecker.getResult() 1877 && offsetSynchChecker.getResult()); 1878 try { 1879 normalThread.join(5000); 1880 offsetThread.join(5000); 1881 } catch (InterruptedException e) { 1882 fail("join() interrupted"); 1883 } 1884 1885 Set mySet = Collections.synchronizedSet(smallSet); 1886 mySet.add(null); 1887 assertTrue("Trying to use nulls in list failed", mySet.contains(null)); 1888 1889 smallSet = new HashSet(); 1890 for (int i = 0; i < 100; i++) { 1891 smallSet.add(objArray[i]); 1892 } 1893 new Support_SetTest("", Collections.synchronizedSet(smallSet)) 1894 .runTest(); 1895 } 1896 1897 /** 1898 * @tests java.util.Collections#synchronizedSortedMap(java.util.SortedMap) 1899 */ 1900 @TestTargetNew( 1901 level = TestLevel.COMPLETE, 1902 notes = "", 1903 method = "synchronizedSortedMap", 1904 args = {java.util.SortedMap.class} 1905 ) 1906 public void test_synchronizedSortedMapLjava_util_SortedMap() { 1907 // Test for method java.util.SortedMap 1908 // java.util.Collections.synchronizedSortedMap(java.util.SortedMap) 1909 TreeMap smallMap = new TreeMap(); 1910 for (int i = 0; i < 50; i++) { 1911 smallMap.put(objArray[i], objArray[i]); 1912 } 1913 1914 final int numberOfLoops = 200; 1915 Map synchMap = Collections.synchronizedMap(smallMap); 1916 // Replacing the previous line with the line below should cause the test 1917 // to fail--the list below isn't synchronized 1918 // Map synchMap = smallMap; 1919 1920 SynchMapChecker normalSynchChecker = new SynchMapChecker(synchMap, 1921 false, numberOfLoops); 1922 SynchMapChecker offsetSynchChecker = new SynchMapChecker(synchMap, 1923 true, numberOfLoops); 1924 Thread normalThread = new Thread(normalSynchChecker); 1925 Thread offsetThread = new Thread(offsetSynchChecker); 1926 normalThread.start(); 1927 offsetThread.start(); 1928 while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops) 1929 || (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) { 1930 try { 1931 Thread.sleep(10); 1932 } catch (InterruptedException e) { 1933 } 1934 } 1935 assertTrue("Returned map corrupted by multiple thread access", 1936 normalSynchChecker.getResult() 1937 && offsetSynchChecker.getResult()); 1938 try { 1939 normalThread.join(5000); 1940 offsetThread.join(5000); 1941 } catch (InterruptedException e) { 1942 fail("join() interrupted"); 1943 } 1944 1945 smallMap = new TreeMap(); 1946 for (int i = 0; i < 100; i++) { 1947 smallMap.put(objArray[i].toString(), objArray[i]); 1948 } 1949 synchMap = Collections.synchronizedSortedMap(smallMap); 1950 new Support_UnmodifiableMapTest("", synchMap).runTest(); 1951 synchMap.keySet().remove(objArray[50].toString()); 1952 assertNull( 1953 "Removing a key from the keySet of the synchronized map did not remove it from the synchronized map", 1954 synchMap.get(objArray[50].toString())); 1955 assertNull( 1956 "Removing a key from the keySet of the synchronized map did not remove it from the original map", 1957 smallMap.get(objArray[50].toString())); 1958 } 1959 1960 /** 1961 * @tests java.util.Collections#synchronizedSortedSet(java.util.SortedSet) 1962 */ 1963 @TestTargetNew( 1964 level = TestLevel.COMPLETE, 1965 notes = "", 1966 method = "synchronizedSortedSet", 1967 args = {java.util.SortedSet.class} 1968 ) 1969 public void test_synchronizedSortedSetLjava_util_SortedSet() { 1970 // Test for method java.util.SortedSet 1971 // java.util.Collections.synchronizedSortedSet(java.util.SortedSet) 1972 TreeSet smallSet = new TreeSet(); 1973 for (int i = 0; i < 50; i++) { 1974 smallSet.add(objArray[i]); 1975 } 1976 1977 final int numberOfLoops = 200; 1978 Set synchSet = Collections.synchronizedSet(smallSet); 1979 // Replacing the previous line with the line below should cause the test 1980 // to fail--the list below isn't synchronized 1981 // Set synchSet = smallSet; 1982 1983 SynchCollectionChecker normalSynchChecker = new SynchCollectionChecker( 1984 synchSet, false, numberOfLoops); 1985 SynchCollectionChecker offsetSynchChecker = new SynchCollectionChecker( 1986 synchSet, true, numberOfLoops); 1987 Thread normalThread = new Thread(normalSynchChecker); 1988 Thread offsetThread = new Thread(offsetSynchChecker); 1989 normalThread.start(); 1990 offsetThread.start(); 1991 while ((normalSynchChecker.getNumberOfChecks() < numberOfLoops) 1992 || (offsetSynchChecker.getNumberOfChecks() < numberOfLoops)) { 1993 try { 1994 Thread.sleep(10); 1995 } catch (InterruptedException e) { 1996 } 1997 } 1998 assertTrue("Returned set corrupted by multiple thread access", 1999 normalSynchChecker.getResult() 2000 && offsetSynchChecker.getResult()); 2001 try { 2002 normalThread.join(5000); 2003 offsetThread.join(5000); 2004 } catch (InterruptedException e) { 2005 fail("join() interrupted"); 2006 } 2007 } 2008 2009 /** 2010 * @tests java.util.Collections#unmodifiableCollection(java.util.Collection) 2011 */ 2012 @TestTargetNew( 2013 level = TestLevel.COMPLETE, 2014 notes = "", 2015 method = "unmodifiableCollection", 2016 args = {java.util.Collection.class} 2017 ) 2018 public void test_unmodifiableCollectionLjava_util_Collection() { 2019 // Test for method java.util.Collection 2020 // java.util.Collections.unmodifiableCollection(java.util.Collection) 2021 boolean exception = false; 2022 Collection c = Collections.unmodifiableCollection(ll); 2023 assertTrue("Returned collection is of incorrect size", c.size() == ll 2024 .size()); 2025 Iterator i = ll.iterator(); 2026 while (i.hasNext()) 2027 assertTrue("Returned list missing elements", c.contains(i.next())); 2028 try { 2029 c.add(new Object()); 2030 } catch (UnsupportedOperationException e) { 2031 exception = true; 2032 // Correct 2033 } 2034 if (!exception) { 2035 fail("Allowed modification of collection"); 2036 } 2037 2038 try { 2039 c.remove(new Object()); 2040 fail("Allowed modification of collection"); 2041 } catch (UnsupportedOperationException e) { 2042 // Correct 2043 } 2044 2045 Collection myCollection = new ArrayList(); 2046 myCollection.add(new Integer(20)); 2047 myCollection.add(null); 2048 c = Collections.unmodifiableCollection(myCollection); 2049 assertTrue("Collection should contain null", c.contains(null)); 2050 assertTrue("Collection should contain Integer(20)", c 2051 .contains(new Integer(20))); 2052 2053 myCollection = new ArrayList(); 2054 for (int counter = 0; counter < 100; counter++) { 2055 myCollection.add(objArray[counter]); 2056 } 2057 new Support_UnmodifiableCollectionTest("", Collections 2058 .unmodifiableCollection(myCollection)).runTest(); 2059 } 2060 2061 /** 2062 * @tests java.util.Collections#unmodifiableList(java.util.List) 2063 */ 2064 @TestTargetNew( 2065 level = TestLevel.COMPLETE, 2066 notes = "", 2067 method = "unmodifiableList", 2068 args = {java.util.List.class} 2069 ) 2070 public void test_unmodifiableListLjava_util_List() { 2071 // Test for method java.util.List 2072 // java.util.Collections.unmodifiableList(java.util.List) 2073 2074 // test with a Sequential Access List 2075 boolean exception = false; 2076 List c = Collections.unmodifiableList(ll); 2077 // Ensure a NPE is thrown if the list is NULL 2078 try { 2079 Collections.unmodifiableList(null); 2080 fail("Expected NullPointerException for null list parameter"); 2081 } catch (NullPointerException e) { 2082 } 2083 2084 assertTrue("Returned list is of incorrect size", c.size() == ll.size()); 2085 assertTrue( 2086 "Returned List should not implement Random Access interface", 2087 !(c instanceof RandomAccess)); 2088 2089 Iterator i = ll.iterator(); 2090 while (i.hasNext()) 2091 assertTrue("Returned list missing elements", c.contains(i.next())); 2092 try { 2093 c.add(new Object()); 2094 } catch (UnsupportedOperationException e) { 2095 exception = true; 2096 // Correct 2097 } 2098 if (!exception) { 2099 fail("Allowed modification of list"); 2100 } 2101 2102 try { 2103 c.remove(new Object()); 2104 fail("Allowed modification of list"); 2105 } catch (UnsupportedOperationException e) { 2106 // Correct 2107 } 2108 2109 // test with a Random Access List 2110 List smallList = new ArrayList(); 2111 smallList.add(null); 2112 smallList.add("yoink"); 2113 c = Collections.unmodifiableList(smallList); 2114 assertNull("First element should be null", c.get(0)); 2115 assertTrue("List should contain null", c.contains(null)); 2116 assertTrue( 2117 "T1. Returned List should implement Random Access interface", 2118 c instanceof RandomAccess); 2119 2120 smallList = new ArrayList(); 2121 for (int counter = 0; counter < 100; counter++) { 2122 smallList.add(objArray[counter]); 2123 } 2124 List myList = Collections.unmodifiableList(smallList); 2125 assertTrue("List should not contain null", !myList.contains(null)); 2126 assertTrue( 2127 "T2. Returned List should implement Random Access interface", 2128 myList instanceof RandomAccess); 2129 2130 assertTrue("get failed on unmodifiable list", myList.get(50).equals( 2131 new Integer(50))); 2132 ListIterator listIterator = myList.listIterator(); 2133 for (int counter = 0; listIterator.hasNext(); counter++) { 2134 assertTrue("List has wrong elements", ((Integer) listIterator 2135 .next()).intValue() == counter); 2136 } 2137 new Support_UnmodifiableCollectionTest("", smallList).runTest(); 2138 } 2139 2140 /** 2141 * @tests java.util.Collections#unmodifiableMap(java.util.Map) 2142 */ 2143 @TestTargetNew( 2144 level = TestLevel.COMPLETE, 2145 notes = "", 2146 method = "unmodifiableMap", 2147 args = {java.util.Map.class} 2148 ) 2149 public void test_unmodifiableMapLjava_util_Map() { 2150 // Test for method java.util.Map 2151 // java.util.Collections.unmodifiableMap(java.util.Map) 2152 boolean exception = false; 2153 Map c = Collections.unmodifiableMap(hm); 2154 assertTrue("Returned map is of incorrect size", c.size() == hm.size()); 2155 Iterator i = hm.keySet().iterator(); 2156 while (i.hasNext()) { 2157 Object x = i.next(); 2158 assertTrue("Returned map missing elements", c.get(x).equals( 2159 hm.get(x))); 2160 } 2161 try { 2162 c.put(new Object(), ""); 2163 } catch (UnsupportedOperationException e) { 2164 exception = true; 2165 // Correct 2166 } 2167 assertTrue("Allowed modification of map", exception); 2168 2169 exception = false; 2170 try { 2171 c.remove(new Object()); 2172 } catch (UnsupportedOperationException e) { 2173 // Correct 2174 exception = true; 2175 } 2176 assertTrue("Allowed modification of map", exception); 2177 2178 exception = false; 2179 Iterator it = c.entrySet().iterator(); 2180 Map.Entry entry = (Map.Entry) it.next(); 2181 try { 2182 entry.setValue("modified"); 2183 } catch (UnsupportedOperationException e) { 2184 // Correct 2185 exception = true; 2186 } 2187 assertTrue("Allowed modification of entry", exception); 2188 2189 exception = false; 2190 Object[] array = c.entrySet().toArray(); 2191 try { 2192 ((Map.Entry) array[0]).setValue("modified"); 2193 } catch (UnsupportedOperationException e) { 2194 // Correct 2195 exception = true; 2196 } 2197 assertTrue("Allowed modification of array entry", exception); 2198 2199 exception = false; 2200 Map.Entry[] array2 = (Map.Entry[]) c.entrySet().toArray( 2201 new Map.Entry[0]); 2202 try { 2203 array2[0].setValue("modified"); 2204 } catch (UnsupportedOperationException e) { 2205 // Correct 2206 exception = true; 2207 } 2208 assertTrue("Allowed modification of array entry2", exception); 2209 2210 HashMap smallMap = new HashMap(); 2211 smallMap.put(null, new Long(30)); 2212 smallMap.put(new Long(25), null); 2213 Map unmodMap = Collections.unmodifiableMap(smallMap); 2214 2215 assertNull("Trying to use a null value in map failed", unmodMap 2216 .get(new Long(25))); 2217 assertTrue("Trying to use a null key in map failed", unmodMap.get(null) 2218 .equals(new Long(30))); 2219 2220 smallMap = new HashMap(); 2221 for (int counter = 0; counter < 100; counter++) { 2222 smallMap.put(objArray[counter].toString(), objArray[counter]); 2223 } 2224 unmodMap = Collections.unmodifiableMap(smallMap); 2225 new Support_UnmodifiableMapTest("", unmodMap).runTest(); 2226 2227 } 2228 2229 /** 2230 * @tests java.util.Collections#unmodifiableSet(java.util.Set) 2231 */ 2232 @TestTargetNew( 2233 level = TestLevel.COMPLETE, 2234 notes = "", 2235 method = "unmodifiableSet", 2236 args = {java.util.Set.class} 2237 ) 2238 public void test_unmodifiableSetLjava_util_Set() { 2239 // Test for method java.util.Set 2240 // java.util.Collections.unmodifiableSet(java.util.Set) 2241 boolean exception = false; 2242 Set c = Collections.unmodifiableSet(s); 2243 assertTrue("Returned set is of incorrect size", c.size() == s.size()); 2244 Iterator i = ll.iterator(); 2245 while (i.hasNext()) 2246 assertTrue("Returned set missing elements", c.contains(i.next())); 2247 try { 2248 c.add(new Object()); 2249 } catch (UnsupportedOperationException e) { 2250 exception = true; 2251 // Correct 2252 } 2253 if (!exception) { 2254 fail("Allowed modification of set"); 2255 } 2256 try { 2257 c.remove(new Object()); 2258 fail("Allowed modification of set"); 2259 } catch (UnsupportedOperationException e) { 2260 // Correct 2261 } 2262 2263 Set mySet = Collections.unmodifiableSet(new HashSet()); 2264 assertTrue("Should not contain null", !mySet.contains(null)); 2265 mySet = Collections.unmodifiableSet(Collections.singleton(null)); 2266 assertTrue("Should contain null", mySet.contains(null)); 2267 2268 mySet = new TreeSet(); 2269 for (int counter = 0; counter < 100; counter++) { 2270 mySet.add(objArray[counter]); 2271 } 2272 new Support_UnmodifiableCollectionTest("", Collections 2273 .unmodifiableSet(mySet)).runTest(); 2274 } 2275 2276 /** 2277 * @tests java.util.Collections#unmodifiableSortedMap(java.util.SortedMap) 2278 */ 2279 @TestTargetNew( 2280 level = TestLevel.COMPLETE, 2281 notes = "", 2282 method = "unmodifiableSortedMap", 2283 args = {java.util.SortedMap.class} 2284 ) 2285 public void test_unmodifiableSortedMapLjava_util_SortedMap() { 2286 // Test for method java.util.SortedMap 2287 // java.util.Collections.unmodifiableSortedMap(java.util.SortedMap) 2288 boolean exception = false; 2289 TreeMap tm = new TreeMap(); 2290 tm.putAll(hm); 2291 Map c = Collections.unmodifiableSortedMap(tm); 2292 assertTrue("Returned map is of incorrect size", c.size() == tm.size()); 2293 Iterator i = hm.keySet().iterator(); 2294 while (i.hasNext()) { 2295 Object x = i.next(); 2296 assertTrue("Returned map missing elements", c.get(x).equals( 2297 tm.get(x))); 2298 } 2299 try { 2300 c.put(new Object(), ""); 2301 } catch (UnsupportedOperationException e) { 2302 exception = true; 2303 // Correct 2304 } 2305 2306 if (!exception) { 2307 fail("Allowed modification of map"); 2308 } 2309 try { 2310 c.remove(new Object()); 2311 } catch (UnsupportedOperationException e) { 2312 // Correct 2313 return; 2314 } 2315 fail("Allowed modification of map"); 2316 } 2317 2318 /** 2319 * @tests java.util.Collections#unmodifiableSortedSet(java.util.SortedSet) 2320 */ 2321 @TestTargetNew( 2322 level = TestLevel.COMPLETE, 2323 notes = "", 2324 method = "unmodifiableSortedSet", 2325 args = {java.util.SortedSet.class} 2326 ) 2327 public void test_unmodifiableSortedSetLjava_util_SortedSet() { 2328 // Test for method java.util.SortedSet 2329 // java.util.Collections.unmodifiableSortedSet(java.util.SortedSet) 2330 boolean exception = false; 2331 SortedSet ss = new TreeSet(); 2332 ss.addAll(s); 2333 SortedSet c = Collections.unmodifiableSortedSet(ss); 2334 assertTrue("Returned set is of incorrect size", c.size() == ss.size()); 2335 Iterator i = ll.iterator(); 2336 while (i.hasNext()) 2337 assertTrue("Returned set missing elements", c.contains(i.next())); 2338 try { 2339 c.add(new Object()); 2340 } catch (UnsupportedOperationException e) { 2341 exception = true; 2342 // Correct 2343 } 2344 if (!exception) { 2345 fail("Allowed modification of set"); 2346 } 2347 try { 2348 c.remove(new Object()); 2349 } catch (UnsupportedOperationException e) { 2350 // Correct 2351 return; 2352 } 2353 fail("Allowed modification of set"); 2354 } 2355 2356 /** 2357 * Test unmodifiable objects toString methods 2358 */ 2359 @TestTargets({ 2360 @TestTargetNew( 2361 level = TestLevel.PARTIAL_COMPLETE, 2362 notes = "", 2363 method = "toString", 2364 args = {} 2365 ), 2366 @TestTargetNew( 2367 level = TestLevel.PARTIAL_COMPLETE, 2368 notes = "", 2369 method = "unmodifiableCollection", 2370 args = {java.util.Collection.class} 2371 ), 2372 @TestTargetNew( 2373 level = TestLevel.PARTIAL_COMPLETE, 2374 notes = "", 2375 method = "unmodifiableMap", 2376 args = {java.util.Map.class} 2377 ) 2378 }) 2379 public void test_unmodifiable_toString_methods() { 2380 // Regression for HARMONY-552 2381 ArrayList al = new ArrayList(); 2382 al.add("a"); 2383 al.add("b"); 2384 Collection uc = Collections.unmodifiableCollection(al); 2385 assertEquals("[a, b]", uc.toString()); 2386 HashMap m = new HashMap(); 2387 m.put("one", "1"); 2388 m.put("two", "2"); 2389 Map um = Collections.unmodifiableMap(m); 2390 assertEquals("{one=1, two=2}", um.toString()); 2391 } 2392 2393 @TestTargetNew( 2394 level = TestLevel.COMPLETE, 2395 notes = "", 2396 method = "singletonList", 2397 args = {java.lang.Object.class} 2398 ) 2399 public void test_singletonListLjava_lang_Object() { 2400 // Test for method java.util.Set 2401 // java.util.Collections.singleton(java.lang.Object) 2402 String str = "Singleton"; 2403 2404 List single = Collections.singletonList(str); 2405 assertEquals(1, single.size()); 2406 assertTrue(single.contains(str)); 2407 assertFalse(single.contains(null)); 2408 assertFalse(Collections.singletonList(null).contains(str)); 2409 assertTrue(Collections.singletonList(null).contains(null)); 2410 2411 try { 2412 single.add("New element"); 2413 fail("UnsupportedOperationException expected"); 2414 } catch (UnsupportedOperationException e) { 2415 //expected 2416 } 2417 } 2418 2419 @TestTargetNew( 2420 level = TestLevel.COMPLETE, 2421 notes = "", 2422 method = "singletonMap", 2423 args = {java.lang.Object.class, java.lang.Object.class} 2424 ) 2425 public void test_singletonMapLjava_lang_Object() { 2426 // Test for method java.util.Set 2427 // java.util.Collections.singleton(java.lang.Object) 2428 Double key = new Double (3.14); 2429 String value = "Fundamental constant"; 2430 2431 Map single = Collections.singletonMap(key, value); 2432 assertEquals(1, single.size()); 2433 assertTrue(single.containsKey(key)); 2434 assertTrue(single.containsValue(value)); 2435 assertFalse(single.containsKey(null)); 2436 assertFalse(single.containsValue(null)); 2437 assertFalse(Collections.singletonMap(null, null).containsKey(key)); 2438 assertFalse(Collections.singletonMap(null, null).containsValue(value)); 2439 assertTrue(Collections.singletonMap(null, null).containsKey(null)); 2440 assertTrue(Collections.singletonMap(null, null).containsValue(null)); 2441 2442 try { 2443 single.clear(); 2444 fail("UnsupportedOperationException expected"); 2445 } catch (UnsupportedOperationException e) { 2446 //expected 2447 } 2448 2449 try { 2450 single.put(new Double(1), "one wrong value"); 2451 fail("UnsupportedOperationException expected"); 2452 } catch (UnsupportedOperationException e) { 2453 //expected 2454 } 2455 } 2456 2457 /** 2458 * Sets up the fixture, for example, open a network connection. This method 2459 * is called before a test is executed. 2460 */ 2461 protected void setUp() { 2462 ll = new LinkedList(); 2463 myll = new LinkedList(); 2464 s = new HashSet(); 2465 mys = new HashSet(); 2466 reversedLinkedList = new LinkedList(); // to be sorted in reverse order 2467 myReversedLinkedList = new LinkedList(); // to be sorted in reverse 2468 // order 2469 hm = new HashMap(); 2470 for (int i = 0; i < objArray.length; i++) { 2471 ll.add(objArray[i]); 2472 myll.add(myobjArray[i]); 2473 s.add(objArray[i]); 2474 mys.add(myobjArray[i]); 2475 reversedLinkedList.add(objArray[objArray.length - i - 1]); 2476 myReversedLinkedList.add(myobjArray[myobjArray.length - i - 1]); 2477 hm.put(objArray[i].toString(), objArray[i]); 2478 } 2479 } 2480 2481 /** 2482 * Tears down the fixture, for example, close a network connection. This 2483 * method is called after a test is executed. 2484 */ 2485 protected void tearDown() { 2486 } 2487 2488 protected void doneSuite() { 2489 objArray = null; 2490 } 2491} 2492