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