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, WITHOUT 13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 14 * License for the specific language governing permissions and limitations under 15 * the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.util; 19 20import dalvik.annotation.TestTargets; 21import dalvik.annotation.TestLevel; 22import dalvik.annotation.TestTargetNew; 23import dalvik.annotation.TestTargetClass; 24 25import junit.framework.TestCase; 26 27import java.io.Serializable; 28import java.util.ArrayList; 29import java.util.Arrays; 30import java.util.Collection; 31import java.util.Collections; 32import java.util.Comparator; 33import java.util.HashMap; 34import java.util.HashSet; 35import java.util.Iterator; 36import java.util.LinkedList; 37import java.util.List; 38import java.util.Map; 39import java.util.RandomAccess; 40import java.util.Set; 41import java.util.SortedMap; 42import java.util.SortedSet; 43import java.util.TreeMap; 44import java.util.TreeSet; 45import java.util.Vector; 46 47import org.apache.harmony.testframework.serialization.SerializationTest; 48import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 49 50import tests.util.SerializationTester; 51 52@TestTargetClass(Collections.class) 53public class CollectionsTest extends TestCase { 54 55 private static final SerializableAssert comparator = new SerializableAssert() { 56 public void assertDeserialized(Serializable reference, Serializable test) { 57 assertSame(reference, test); 58 } 59 }; 60 61 /** 62 * @tests java.util.Collections#binarySearch(java.util.List, 63 * java.lang.Object, java.util.Comparator) 64 */ 65 @TestTargetNew( 66 level = TestLevel.COMPLETE, 67 notes = "", 68 method = "binarySearch", 69 args = {java.util.List.class, java.lang.Object.class, java.util.Comparator.class} 70 ) 71 public void test_binarySearchLjava_util_ListLjava_lang_ObjectLjava_util_Comparator() { 72 // Regression for HARMONY-94 73 LinkedList<Integer> lst = new LinkedList<Integer>(); 74 lst.add(new Integer(30)); 75 Collections.sort(lst, null); 76 int index = Collections.binarySearch(lst, new Integer(2), null); 77 assertEquals(-1, index); 78 79 LinkedList<String> lls = new LinkedList<String>(); 80 lls.add("1"); 81 lls.add("2"); 82 lls.add("3"); 83 lls.add("4"); 84 lls.add(""); 85 LinkedList<String> ll = lls; 86 87 try { 88 Collections.binarySearch(ll, new Integer(10), null); 89 fail("ClassCastException expected"); 90 } catch (ClassCastException e) { 91 //expected 92 } 93 } 94 95 /** 96 * @tests java.util.Collections#binarySearch(java.util.List, 97 * java.lang.Object) 98 */ 99 @TestTargetNew( 100 level = TestLevel.COMPLETE, 101 notes = "", 102 method = "binarySearch", 103 args = {java.util.List.class, java.lang.Object.class} 104 ) 105 @SuppressWarnings("unchecked") 106 public void test_binarySearchLjava_util_ListLjava_lang_Object() { 107 // regression for Harmony-1367 108 List localList = new LinkedList(); 109 assertEquals(-1, Collections.binarySearch(localList, new Object())); 110 localList.add(new Object()); 111 try { 112 Collections.binarySearch(localList, new Integer(1)); 113 fail("Should throw ClassCastException"); 114 } catch (ClassCastException e) { 115 // expected 116 } 117 118 LinkedList<String> lls = new LinkedList<String>(); 119 lls.add("1"); 120 lls.add("2"); 121 lls.add("3"); 122 lls.add("4"); 123 lls.add(""); 124 LinkedList ll = lls; 125 126 try { 127 Collections.binarySearch(ll, new Integer(10)); 128 fail("ClassCastException expected"); 129 } catch (ClassCastException e) { 130 //expected 131 } 132 } 133 134 /** 135 * @tests java.util.Collections#rotate(java.util.List, int) 136 */ 137 @TestTargetNew( 138 level = TestLevel.PARTIAL_COMPLETE, 139 notes = "UnsupportedOperationException is not tested.", 140 method = "rotate", 141 args = {java.util.List.class, int.class} 142 ) 143 public void test_rotateLjava_util_ListI() { 144 // Regression for HARMONY-19 Rotate an *empty* list 145 Collections.rotate(new ArrayList<Object>(), 25); 146 147 // Regression for HARMONY-20 148 List<String> list = new ArrayList<String>(); 149 list.add(0, "zero"); 150 list.add(1, "one"); 151 list.add(2, "two"); 152 list.add(3, "three"); 153 list.add(4, "four"); 154 155 Collections.rotate(list, Integer.MIN_VALUE); 156 assertEquals("Rotated incorrectly at position 0, ", "three", 157 list.get(0)); 158 assertEquals("Rotated incorrectly at position 1, ", "four", 159 list.get(1)); 160 assertEquals("Rotated incorrectly at position 2, ", "zero", 161 list.get(2)); 162 assertEquals("Rotated incorrectly at position 3, ", "one", 163 list.get(3)); 164 assertEquals("Rotated incorrectly at position 4, ", "two", 165 list.get(4)); 166 } 167 168 /** 169 * @tests java.util.Collections#synchronizedCollection(java.util.Collection) 170 */ 171 @TestTargetNew( 172 level = TestLevel.PARTIAL_COMPLETE, 173 notes = "Verifies NullPointerException.", 174 method = "synchronizedCollection", 175 args = {java.util.Collection.class} 176 ) 177 public void test_synchronizedCollectionLjava_util_Collection() { 178 try { 179 // Regression for HARMONY-93 180 Collections.synchronizedCollection(null); 181 fail("Assert 0: synchronizedCollection(null) must throw NPE"); 182 } catch (NullPointerException e) { 183 // expected 184 } 185 } 186 187 /** 188 * @tests java.util.Collections#synchronizedSortedMap(java.util.SortedMap) 189 */ 190 @TestTargetNew( 191 level = TestLevel.PARTIAL_COMPLETE, 192 notes = "Verifies NullPointerException.", 193 method = "synchronizedSortedMap", 194 args = {java.util.SortedMap.class} 195 ) 196 public void test_synchronizedSortedMapLjava_util_SortedMap() { 197 try { 198 // Regression for HARMONY-93 199 Collections.synchronizedSortedMap(null); 200 fail("Assert 0: synchronizedSortedMap(null) must throw NPE"); 201 } catch (NullPointerException e) { 202 // expected 203 } 204 } 205 206 /** 207 * @tests java.util.Collections#synchronizedMap(java.util.Map) 208 */ 209 @TestTargetNew( 210 level = TestLevel.PARTIAL_COMPLETE, 211 notes = "Verifies NullPointerException.", 212 method = "synchronizedMap", 213 args = {java.util.Map.class} 214 ) 215 public void test_synchronizedMapLjava_util_Map() { 216 try { 217 // Regression for HARMONY-93 218 Collections.synchronizedMap(null); 219 fail("Assert 0: synchronizedMap(map) must throw NPE"); 220 } catch (NullPointerException e) { 221 // expected 222 } 223 } 224 225 /** 226 * @tests java.util.Collections#synchronizedSet(java.util.Set) 227 */ 228 @TestTargetNew( 229 level = TestLevel.PARTIAL_COMPLETE, 230 notes = "Verifies NullPointerException.", 231 method = "synchronizedSet", 232 args = {java.util.Set.class} 233 ) 234 public void test_synchronizedSetLjava_util_Set() { 235 try { 236 // Regression for HARMONY-93 237 Collections.synchronizedSet(null); 238 fail("Assert 0: synchronizedSet(set) must throw NPE"); 239 } catch (NullPointerException e) { 240 // expected 241 } 242 } 243 244 /** 245 * @tests java.util.Collections#synchronizedSortedSet(java.util.SortedSet) 246 */ 247 @TestTargetNew( 248 level = TestLevel.PARTIAL_COMPLETE, 249 notes = "Verifies NullPointerException.", 250 method = "synchronizedSortedSet", 251 args = {java.util.SortedSet.class} 252 ) 253 public void test_synchronizedSortedSetLjava_util_SortedSet() { 254 try { 255 // Regression for HARMONY-93 256 Collections.synchronizedSortedSet(null); 257 fail("Assert 0: synchronizedSortedSet(null) must throw NPE"); 258 } catch (NullPointerException e) { 259 // expected 260 } 261 } 262 263 /** 264 * @tests java.util.Collections#unmodifiableCollection(java.util.Collection) 265 */ 266 @TestTargetNew( 267 level = TestLevel.PARTIAL_COMPLETE, 268 notes = "Verifies NullPointerException.", 269 method = "unmodifiableCollection", 270 args = {java.util.Collection.class} 271 ) 272 public void test_unmodifiableCollectionLjava_util_Collection() { 273 try { 274 // Regression for HARMONY-93 275 Collections.unmodifiableCollection(null); 276 fail("Assert 0: unmodifiableCollection(null) must throw NPE"); 277 } catch (NullPointerException e) { 278 // expected 279 } 280 } 281 282 /** 283 * @tests java.util.Collections#unmodifiableMap(java.util.Map) 284 */ 285 @TestTargetNew( 286 level = TestLevel.PARTIAL_COMPLETE, 287 notes = "Verifies NullPointerException.", 288 method = "unmodifiableMap", 289 args = {java.util.Map.class} 290 ) 291 public void test_unmodifiableMapLjava_util_Map() { 292 try { 293 // Regression for HARMONY-93 294 Collections.unmodifiableMap(null); 295 fail("Assert 0: unmodifiableMap(null) must throw NPE"); 296 } catch (NullPointerException e) { 297 // expected 298 } 299 } 300 301 /** 302 * @tests java.util.Collections#unmodifiableSet(java.util.Set) 303 */ 304 @TestTargetNew( 305 level = TestLevel.PARTIAL_COMPLETE, 306 notes = "Verifies NullPointerException.", 307 method = "unmodifiableSet", 308 args = {java.util.Set.class} 309 ) 310 public void test_unmodifiableSetLjava_util_Set() { 311 try { 312 // Regression for HARMONY-93 313 Collections.unmodifiableSet(null); 314 fail("Assert 0: unmodifiableSet(null) must throw NPE"); 315 } catch (NullPointerException e) { 316 // expected 317 } 318 } 319 320 /** 321 * @tests java.util.Collections#unmodifiableSortedMap(java.util.SortedMap) 322 */ 323 @TestTargetNew( 324 level = TestLevel.PARTIAL_COMPLETE, 325 notes = "Verifies NullPointerException.", 326 method = "unmodifiableSortedMap", 327 args = {java.util.SortedMap.class} 328 ) 329 public void test_unmodifiableSortedMapLjava_util_SortedMap() { 330 try { 331 // Regression for HARMONY-93 332 Collections.unmodifiableSortedMap(null); 333 fail("Assert 0: unmodifiableSortedMap(null) must throw NPE"); 334 } catch (NullPointerException e) { 335 // expected 336 } 337 } 338 339 /** 340 * @tests java.util.Collections#unmodifiableSortedSet(java.util.SortedSet) 341 */ 342 @TestTargetNew( 343 level = TestLevel.PARTIAL_COMPLETE, 344 notes = "Verifies NullPointerException.", 345 method = "unmodifiableSortedSet", 346 args = {java.util.SortedSet.class} 347 ) 348 public void test_unmodifiableSortedSetLjava_util_SortedSet() { 349 try { 350 // Regression for HARMONY-93 351 Collections.unmodifiableSortedSet(null); 352 fail("Assert 0: unmodifiableSortedSet(null) must throw NPE"); 353 } catch (NullPointerException e) { 354 // expected 355 } 356 } 357 358 /** 359 * @tests java.util.Collections#frequency(java.util.Collection,Object) 360 */ 361 @TestTargetNew( 362 level = TestLevel.COMPLETE, 363 notes = "", 364 method = "frequency", 365 args = {java.util.Collection.class, java.lang.Object.class} 366 ) 367 public void test_frequencyLjava_util_CollectionLint() { 368 try { 369 Collections.frequency(null, null); 370 fail("Assert 0: frequency(null,<any>) must throw NPE"); 371 } catch (NullPointerException e) {} 372 373 List<String> strings = Arrays.asList(new String[] { "1", "2", "3", "1", "1" }); 374 375 assertEquals("Assert 1: did not find three \"1\" strings", 3, 376 Collections.frequency(strings, "1")); 377 378 assertEquals("Assert 2: did not find one \"2\" strings", 1, Collections 379 .frequency(strings, "2")); 380 381 assertEquals("Assert 3: did not find three \"3\" strings", 1, 382 Collections.frequency(strings, "3")); 383 384 assertEquals("Assert 4: matched on null when there are none", 0, 385 Collections.frequency(strings, null)); 386 387 List<Object> objects = Arrays.asList(new Object[] { new Integer(1), null, null, 388 new Long(1) }); 389 390 assertEquals("Assert 5: did not find one Integer(1)", 1, Collections 391 .frequency(objects, new Integer(1))); 392 393 assertEquals("Assert 6: did not find one Long(1)", 1, Collections 394 .frequency(objects, new Long(1))); 395 396 assertEquals("Assert 7: did not find two null references", 2, 397 Collections.frequency(objects, null)); 398 } 399 400 /** 401 * @tests java.util.Collections#reverseOrder() 402 */ 403 @TestTargetNew( 404 level = TestLevel.COMPLETE, 405 notes = "", 406 method = "reverseOrder", 407 args = {} 408 ) 409 public void test_reverseOrder() { 410 Comparator<String> roc = Collections.reverseOrder(); 411 assertNotNull("Assert 0: comparator must not be null", roc); 412 413 assertTrue("Assert 1: comparator must implement Serializable", 414 roc instanceof Serializable); 415 416 String[] fixtureDesc = new String[] { "2", "1", "0" }; 417 String[] numbers = new String[] { "0", "1", "2" }; 418 Arrays.sort(numbers, roc); 419 assertTrue("Assert 2: the arrays are not equal, the sort failed", 420 Arrays.equals(fixtureDesc, numbers)); 421 } 422 423 /** 424 * @tests java.util.Collections#reverseOrder(java.util.Comparator) 425 */ 426 @TestTargetNew( 427 level = TestLevel.COMPLETE, 428 notes = "", 429 method = "reverseOrder", 430 args = {java.util.Comparator.class} 431 ) 432 public void test_reverseOrderLjava_util_Comparator() { 433 Comparator<String> roc = Collections 434 .reverseOrder(String.CASE_INSENSITIVE_ORDER); 435 assertNotNull("Assert 0: comparator must not be null", roc); 436 437 assertTrue("Assert 1: comparator must implement Serializable", 438 roc instanceof Serializable); 439 440 String[] fixtureDesc = new String[] { "2", "1", "0" }; 441 String[] numbers = new String[] { "0", "1", "2" }; 442 Arrays.sort(numbers, roc); 443 assertTrue("Assert 2: the arrays are not equal, the sort failed", 444 Arrays.equals(fixtureDesc, numbers)); 445 446 roc = Collections.reverseOrder(null); 447 assertNotNull("Assert 3: comparator must not be null", roc); 448 449 assertTrue("Assert 4: comparator must implement Serializable", 450 roc instanceof Serializable); 451 452 numbers = new String[] { "0", "1", "2" }; 453 Arrays.sort(numbers, roc); 454 assertTrue("Assert 5: the arrays are not equal, the sort failed", 455 Arrays.equals(fixtureDesc, numbers)); 456 } 457 458 class Mock_Collection implements Collection { 459 public boolean add(Object o) { 460 throw new UnsupportedOperationException(); 461 } 462 463 public boolean addAll(Collection c) { 464 return false; 465 } 466 467 public void clear() { 468 } 469 470 public boolean contains(Object o) { 471 return false; 472 } 473 474 public boolean containsAll(Collection c) { 475 return false; 476 } 477 478 public boolean isEmpty() { 479 return false; 480 } 481 482 public Iterator iterator() { 483 return null; 484 } 485 486 public boolean remove(Object o) { 487 return false; 488 } 489 490 public boolean removeAll(Collection c) { 491 return false; 492 } 493 494 public boolean retainAll(Collection c) { 495 return false; 496 } 497 498 public int size() { 499 return 0; 500 } 501 502 public Object[] toArray() { 503 return null; 504 } 505 506 public Object[] toArray(Object[] a) { 507 return null; 508 } 509 } 510 511 class Mock_WrongCollection implements Collection { 512 final String wrongElement = "Wrong element"; 513 public boolean add(Object o) { 514 if (o.equals(wrongElement)) throw new IllegalArgumentException(); 515 if (o == null) throw new NullPointerException(); 516 return false; 517 } 518 519 public boolean addAll(Collection c) { 520 return false; 521 } 522 523 public void clear() { 524 } 525 526 public boolean contains(Object o) { 527 return false; 528 } 529 530 public boolean containsAll(Collection c) { 531 return false; 532 } 533 534 public boolean isEmpty() { 535 return false; 536 } 537 538 public Iterator iterator() { 539 return null; 540 } 541 542 public boolean remove(Object o) { 543 return false; 544 } 545 546 public boolean removeAll(Collection c) { 547 return false; 548 } 549 550 public boolean retainAll(Collection c) { 551 return false; 552 } 553 554 public int size() { 555 return 0; 556 } 557 558 public Object[] toArray() { 559 return null; 560 } 561 562 public Object[] toArray(Object[] a) { 563 return null; 564 } 565 } 566 567 @TestTargetNew( 568 level = TestLevel.COMPLETE, 569 notes = "", 570 method = "addAll", 571 args = {java.util.Collection.class, java.lang.Object[].class} 572 ) 573 public void test_AddAll() { 574 List<Object> l = new ArrayList<Object>(); 575 assertFalse(Collections.addAll(l, new Object[] {})); 576 assertTrue(l.isEmpty()); 577 assertTrue(Collections.addAll(l, new Object[] { new Integer(1), 578 new Integer(2), new Integer(3) })); 579 assertFalse(l.isEmpty()); 580 assertTrue(l.equals(Arrays.asList(new Object[] { new Integer(1), 581 new Integer(2), new Integer(3) }))); 582 583 try { 584 Collections.addAll(null,new Object[] { new Integer(1), 585 new Integer(2), new Integer(3) }); 586 fail("NullPointerException expected"); 587 } catch (NullPointerException e) { 588 //fail 589 } 590 591 Collection c = new Mock_Collection(); 592 try { 593 Collections.addAll(c, new Object[] { new Integer(1), 594 new Integer(2), new Integer(3) }); 595 fail("UnsupportedOperationException expected"); 596 } catch (UnsupportedOperationException e) { 597 //expected 598 } 599 600 c = new Mock_WrongCollection (); 601 602 try { 603 Collections.addAll(c, new String[] { "String", 604 "Correct element", null }); 605 fail("NullPointerException expected"); 606 } catch (NullPointerException e) { 607 //fail 608 } 609 610 try { 611 Collections.addAll(c, new String[] { "String", 612 "Wrong element", "Correct element" }); 613 fail("IllegalArgumentException expected"); 614 } catch (IllegalArgumentException e) { 615 //fail 616 } 617 618 Collections.addAll(c, new String[] { "String", 619 "", "Correct element" }); 620 } 621 @TestTargetNew( 622 level = TestLevel.COMPLETE, 623 notes = "", 624 method = "disjoint", 625 args = {java.util.Collection.class, java.util.Collection.class} 626 ) 627 public void test_Disjoint() { 628 Object[] arr1 = new Object[10]; 629 for (int i = 0; i < arr1.length; i++) { 630 arr1[i] = new Integer(i); 631 } 632 Object[] arr2 = new Object[20]; 633 for (int i = 0; i < arr2.length; i++) { 634 arr2[i] = new Integer(100 + i); 635 } 636 Collection<Object> c1 = new ArrayList<Object>(); 637 Collection<Object> c2 = new ArrayList<Object>(); 638 Collections.addAll(c1, arr1); 639 Collections.addAll(c2, arr2); 640 assertTrue(Collections.disjoint(c1, c2)); 641 c1.add(arr2[10]); 642 assertFalse(Collections.disjoint(c1, c2)); 643 644 c1 = new LinkedList<Object>(); 645 c2 = new LinkedList<Object>(); 646 Collections.addAll(c1, arr1); 647 Collections.addAll(c2, arr2); 648 assertTrue(Collections.disjoint(c1, c2)); 649 c1.add(arr2[10]); 650 assertFalse(Collections.disjoint(c1, c2)); 651 652 c1 = new TreeSet<Object>(); 653 c2 = new TreeSet<Object>(); 654 Collections.addAll(c1, arr1); 655 Collections.addAll(c2, arr2); 656 assertTrue(Collections.disjoint(c1, c2)); 657 c1.add(arr2[10]); 658 assertFalse(Collections.disjoint(c1, c2)); 659 660 c1 = new HashSet<Object>(); 661 c2 = new HashSet<Object>(); 662 Collections.addAll(c1, arr1); 663 Collections.addAll(c2, arr2); 664 assertTrue(Collections.disjoint(c1, c2)); 665 c1.add(arr2[10]); 666 assertFalse(Collections.disjoint(c1, c2)); 667 668 c1 = new LinkedList<Object>(); 669 c2 = new TreeSet<Object>(); 670 Collections.addAll(c1, arr1); 671 Collections.addAll(c2, arr2); 672 assertTrue(Collections.disjoint(c1, c2)); 673 c1.add(arr2[10]); 674 assertFalse(Collections.disjoint(c1, c2)); 675 676 c1 = new Vector<Object>(); 677 c2 = new HashSet<Object>(); 678 Collections.addAll(c1, arr1); 679 Collections.addAll(c2, arr2); 680 assertTrue(Collections.disjoint(c1, c2)); 681 c1.add(arr2[10]); 682 assertFalse(Collections.disjoint(c1, c2)); 683 684 try { 685 Collections.disjoint(c1, null); 686 fail("NullPointerException expected"); 687 } catch (NullPointerException e) { 688 //expected 689 } 690 691 try { 692 Collections.disjoint(null, c2); 693 fail("NullPointerException expected"); 694 } catch (NullPointerException e) { 695 //expected 696 } 697 } 698 699 /** 700 * @tests java.util.Collections.EmptyList#readResolve() 701 */ 702 @TestTargetNew( 703 level = TestLevel.PARTIAL_COMPLETE, 704 notes = "", 705 method = "!SerializationSelf", 706 args = {} 707 ) 708 public void test_EmptyList_readResolve() throws Exception { 709 SerializationTest.verifySelf(Collections.EMPTY_LIST, comparator); 710 } 711 712 /** 713 * @tests java.util.Collections.EmptyMap#readResolve() 714 */ 715 @TestTargetNew( 716 level = TestLevel.PARTIAL_COMPLETE, 717 notes = "", 718 method = "!SerializationSelf", 719 args = {} 720 ) 721 public void test_EmptyMap_readResolve() throws Exception { 722 SerializationTest.verifySelf(Collections.EMPTY_MAP, comparator); 723 } 724 725 /** 726 * @tests java.util.Collections.EmptySet#readResolve() 727 */ 728 @TestTargetNew( 729 level = TestLevel.PARTIAL_COMPLETE, 730 notes = "", 731 method = "!SerializationSelf", 732 args = {} 733 ) 734 public void test_EmptySet_readResolve() throws Exception { 735 SerializationTest.verifySelf(Collections.EMPTY_SET, comparator); 736 } 737 738 @TestTargetNew( 739 level = TestLevel.PARTIAL_COMPLETE, 740 notes = "", 741 method = "!SerializationGolden", 742 args = {} 743 ) 744 public void test_checkedCollectionSerializationCompatability() throws Exception { 745 Collection<String> c = Collections.emptySet(); 746 c = Collections.checkedCollection(c, String.class); 747 SerializationTester.assertCompabilityEquals(c, "/serialization/org/apache/harmony/luni/tests/java/util/Collections_CheckedCollection.golden.ser"); 748 } 749 @TestTargetNew( 750 level = TestLevel.PARTIAL_COMPLETE, 751 notes = "", 752 method = "!SerializationGolden", 753 args = {} 754 ) 755 public void test_checkedListRandomAccessSerializationCompatability() throws Exception { 756 List<String> c = new ArrayList<String>(); 757 assertTrue(c instanceof RandomAccess); 758 c = Collections.checkedList(c, String.class); 759 SerializationTester.assertCompabilityEquals(c, "/serialization/org/apache/harmony/luni/tests/java/util/Collections_CheckedListRandomAccess.golden.ser"); 760 } 761 @TestTargetNew( 762 level = TestLevel.PARTIAL_COMPLETE, 763 notes = "", 764 method = "!SerializationGolden", 765 args = {} 766 ) 767 public void test_checkedListSerializationCompatability() throws Exception { 768 List<String> c = new LinkedList<String>(); 769 assertFalse(c instanceof RandomAccess); 770 c = Collections.checkedList(c, String.class); 771 SerializationTester.assertCompabilityEquals(c, "/serialization/org/apache/harmony/luni/tests/java/util/Collections_CheckedList.golden.ser"); 772 } 773 @TestTargetNew( 774 level = TestLevel.PARTIAL_COMPLETE, 775 notes = "", 776 method = "!SerializationGolden", 777 args = {} 778 ) 779 public void test_checkedSetSerializationCompatability() throws Exception { 780 Set<String> c = new HashSet<String>(); 781 assertFalse(c instanceof SortedSet); 782 c = Collections.checkedSet(c, String.class); 783 SerializationTester.assertCompabilityEquals(c, "/serialization/org/apache/harmony/luni/tests/java/util/Collections_CheckedSet.golden.ser"); 784 } 785 @TestTargetNew( 786 level = TestLevel.PARTIAL_COMPLETE, 787 notes = "", 788 method = "!SerializationGolden", 789 args = {} 790 ) 791 public void test_checkedMapSerializationCompatability() throws Exception { 792 Map<String, String> c = new HashMap<String, String>(); 793 assertFalse(c instanceof SortedMap); 794 c = Collections.checkedMap(c, String.class, String.class); 795 SerializationTester.assertCompabilityEquals(c, "/serialization/org/apache/harmony/luni/tests/java/util/Collections_CheckedMap.golden.ser"); 796 } 797 @TestTargetNew( 798 level = TestLevel.PARTIAL_COMPLETE, 799 notes = "", 800 method = "!SerializationGolden", 801 args = {} 802 ) 803 public void test_checkedSortedSetSerializationCompatability() throws Exception { 804 SortedSet<String> c = new TreeSet<String>(); 805 c = Collections.checkedSortedSet(c, String.class); 806 SerializationTester.assertCompabilityEquals(c, "/serialization/org/apache/harmony/luni/tests/java/util/Collections_CheckedSortedSet.golden.ser"); 807 } 808 @TestTargetNew( 809 level = TestLevel.PARTIAL_COMPLETE, 810 notes = "", 811 method = "!SerializationGolden", 812 args = {} 813 ) 814 public void test_checkedSortedMapSerializationCompatability() throws Exception { 815 SortedMap<String, String> c = new TreeMap<String, String>(); 816 c = Collections.checkedSortedMap(c, String.class, String.class); 817 SerializationTester.assertCompabilityEquals(c, "/serialization/org/apache/harmony/luni/tests/java/util/Collections_CheckedSortedMap.golden.ser"); 818 } 819 820 @TestTargetNew( 821 level = TestLevel.COMPLETE, 822 notes = "", 823 method = "checkedCollection", 824 args = {java.util.Collection.class, java.lang.Class.class} 825 ) 826 public void test_checkedCollectionLjava_util_CollectionLjava_lang_Class() { 827 ArrayList al = new ArrayList<Integer>(); 828 829 Collection c = Collections.checkedCollection(al, Integer.class); 830 831 c.add(new Integer(1)); 832 833 try { 834 c.add(new Double(3.14)); 835 fail("ClassCastException expected"); 836 } catch (ClassCastException e) { 837 //expected 838 } 839 } 840 841 @TestTargetNew( 842 level = TestLevel.COMPLETE, 843 notes = "", 844 method = "checkedList", 845 args = {java.util.List.class, java.lang.Class.class} 846 ) 847 public void test_checkedListLjava_util_ListLjava_lang_Class() { 848 ArrayList al = new ArrayList<Integer>(); 849 850 List l = Collections.checkedList(al, Integer.class); 851 852 l.add(new Integer(1)); 853 854 try { 855 l.add(new Double(3.14)); 856 fail("ClassCastException expected"); 857 } catch (ClassCastException e) { 858 //expected 859 } 860 } 861 862 @TestTargetNew( 863 level = TestLevel.COMPLETE, 864 notes = "", 865 method = "checkedMap", 866 args = {java.util.Map.class, java.lang.Class.class, java.lang.Class.class} 867 ) 868 public void test_checkedMapLjava_util_MapLjava_lang_ClassLjava_lang_Class() { 869 HashMap hm = new HashMap<Integer, String>(); 870 871 Map m = Collections.checkedMap(hm, Integer.class, String.class); 872 873 m.put(1, "one"); 874 m.put(2, "two"); 875 876 try { 877 m.put("wron key", null); 878 fail("ClassCastException expected"); 879 } catch (ClassCastException e) { 880 //expected 881 } 882 883 try { 884 m.put(3, new Double(3.14)); 885 fail("ClassCastException expected"); 886 } catch (ClassCastException e) { 887 //expected 888 } 889 } 890 891 @TestTargetNew( 892 level = TestLevel.COMPLETE, 893 notes = "", 894 method = "checkedSet", 895 args = {java.util.Set.class, java.lang.Class.class} 896 ) 897 public void test_checkedSetLjava_util_SetLjava_lang_Class() { 898 HashSet hs = new HashSet<Integer>(); 899 900 Set s = Collections.checkedSet(hs, Integer.class); 901 902 s.add(new Integer(1)); 903 904 try { 905 s.add(new Double(3.14)); 906 fail("ClassCastException expected"); 907 } catch (ClassCastException e) { 908 //expected 909 } 910 } 911 912 @TestTargetNew( 913 level = TestLevel.COMPLETE, 914 notes = "", 915 method = "checkedSortedMap", 916 args = {java.util.SortedMap.class, java.lang.Class.class, java.lang.Class.class} 917 ) 918 public void test_checkedSortedMapLjava_util_SortedMapLjava_lang_ClassLjava_lang_Class() { 919 TreeMap tm = new TreeMap<Integer, String>(); 920 921 SortedMap sm = Collections.checkedSortedMap(tm, Integer.class, String.class); 922 923 sm.put(1, "one"); 924 sm.put(2, "two"); 925 926 try { 927 sm.put("wron key", null); 928 fail("ClassCastException expected"); 929 } catch (ClassCastException e) { 930 //expected 931 } 932 933 try { 934 sm.put(3, new Double(3.14)); 935 fail("ClassCastException expected"); 936 } catch (ClassCastException e) { 937 //expected 938 } 939 } 940 941 @TestTargetNew( 942 level = TestLevel.COMPLETE, 943 notes = "", 944 method = "checkedSortedSet", 945 args = {java.util.SortedSet.class, java.lang.Class.class} 946 ) 947 public void test_checkedSortedSetLjava_util_SortedSetLjava_lang_Class() { 948 TreeSet ts = new TreeSet<Integer>(); 949 950 SortedSet ss = Collections.checkedSortedSet(ts, Integer.class); 951 952 ss.add(new Integer(1)); 953 954 try { 955 ss.add(new Double(3.14)); 956 fail("ClassCastException expected"); 957 } catch (ClassCastException e) { 958 //expected 959 } 960 } 961 962 @TestTargetNew( 963 level = TestLevel.COMPLETE, 964 notes = "", 965 method = "emptyList", 966 args = {} 967 ) 968 public void test_emptyList() { 969 List<String> ls = Collections.emptyList(); 970 List<Integer> li = Collections.emptyList(); 971 972 assertTrue(ls.equals(li)); 973 assertTrue(li.equals(Collections.EMPTY_LIST)); 974 } 975 976 @TestTargetNew( 977 level = TestLevel.COMPLETE, 978 notes = "", 979 method = "emptyMap", 980 args = {} 981 ) 982 public void test_emptyMap() { 983 Map<Integer, String> mis = Collections.emptyMap(); 984 Map<String, Integer> msi = Collections.emptyMap(); 985 986 assertTrue(mis.equals(msi)); 987 assertTrue(msi.equals(Collections.EMPTY_MAP)); 988 } 989 990 @TestTargetNew( 991 level = TestLevel.COMPLETE, 992 notes = "", 993 method = "emptySet", 994 args = {} 995 ) 996 public void test_emptySet() { 997 Set<String> ss = Collections.emptySet(); 998 Set<Integer> si = Collections.emptySet(); 999 1000 assertTrue(ss.equals(si)); 1001 assertTrue(si.equals(Collections.EMPTY_SET)); 1002 } 1003} 1004