TreeMapTest.java revision 89c1feb0a69a7707b271086e749975b3f7acacf7
1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package tests.api.java.util; 19 20import dalvik.annotation.TestTarget; 21import dalvik.annotation.TestInfo; 22import dalvik.annotation.TestLevel; 23import dalvik.annotation.TestTargetClass; 24 25import java.io.Serializable; 26import java.text.CollationKey; 27import java.text.Collator; 28import java.util.AbstractMap; 29import java.util.Collection; 30import java.util.Comparator; 31import java.util.HashMap; 32import java.util.Iterator; 33import java.util.Map; 34import java.util.Set; 35import java.util.SortedMap; 36import java.util.TreeMap; 37 38import org.apache.harmony.testframework.serialization.SerializationTest; 39 40import tests.support.Support_MapTest2; 41import tests.support.Support_UnmodifiableCollectionTest; 42 43@TestTargetClass(TreeMap.class) 44public class TreeMapTest extends junit.framework.TestCase { 45 46 public static class ReversedComparator implements Comparator { 47 public int compare(Object o1, Object o2) { 48 return -(((Comparable) o1).compareTo(o2)); 49 } 50 51 public boolean equals(Object o1, Object o2) { 52 return (((Comparable) o1).compareTo(o2)) == 0; 53 } 54 } 55 56 // Regression for Harmony-1026 57 public static class MockComparator<T extends Comparable<T>> implements 58 Comparator<T>, Serializable { 59 60 public int compare(T o1, T o2) { 61 if (o1 == o2) { 62 return 0; 63 } 64 if (null == o1 || null == o2) { 65 return -1; 66 } 67 T c1 = o1; 68 T c2 = o2; 69 return c1.compareTo(c2); 70 } 71 } 72 73 // Regression for Harmony-1161 74 class MockComparatorNullTolerable implements Comparator<String> { 75 76 public int compare(String o1, String o2) { 77 if (o1 == o2) { 78 return 0; 79 } 80 if (null == o1) { 81 return -1; 82 } 83 return o1.compareTo(o2); 84 } 85 } 86 87 TreeMap tm; 88 89 Object objArray[] = new Object[1000]; 90 91 /** 92 * @tests java.util.TreeMap#TreeMap() 93 */ 94 @TestInfo( 95 level = TestLevel.COMPLETE, 96 purpose = "", 97 targets = { 98 @TestTarget( 99 methodName = "TreeMap", 100 methodArgs = {} 101 ) 102 }) 103 public void test_Constructor() { 104 // Test for method java.util.TreeMap() 105 new Support_MapTest2(new TreeMap()).runTest(); 106 107 assertTrue("New treeMap non-empty", new TreeMap().isEmpty()); 108 } 109 110 /** 111 * @tests java.util.TreeMap#TreeMap(java.util.Comparator) 112 */ 113 @TestInfo( 114 level = TestLevel.COMPLETE, 115 purpose = "", 116 targets = { 117 @TestTarget( 118 methodName = "TreeMap", 119 methodArgs = {java.util.Comparator.class} 120 ) 121 }) 122 public void test_ConstructorLjava_util_Comparator() { 123 // Test for method java.util.TreeMap(java.util.Comparator) 124 Comparator comp = new ReversedComparator(); 125 TreeMap reversedTreeMap = new TreeMap(comp); 126 assertTrue("TreeMap answered incorrect comparator", reversedTreeMap 127 .comparator() == comp); 128 reversedTreeMap.put(new Integer(1).toString(), new Integer(1)); 129 reversedTreeMap.put(new Integer(2).toString(), new Integer(2)); 130 assertTrue("TreeMap does not use comparator (firstKey was incorrect)", 131 reversedTreeMap.firstKey().equals(new Integer(2).toString())); 132 assertTrue("TreeMap does not use comparator (lastKey was incorrect)", 133 reversedTreeMap.lastKey().equals(new Integer(1).toString())); 134 135 } 136 137 /** 138 * @tests java.util.TreeMap#TreeMap(java.util.Map) 139 */ 140 @TestInfo( 141 level = TestLevel.PARTIAL, 142 purpose = "Doesn't check exceptions.", 143 targets = { 144 @TestTarget( 145 methodName = "TreeMap", 146 methodArgs = {java.util.Map.class} 147 ) 148 }) 149 public void test_ConstructorLjava_util_Map() { 150 // Test for method java.util.TreeMap(java.util.Map) 151 TreeMap myTreeMap = new TreeMap(new HashMap(tm)); 152 assertTrue("Map is incorrect size", myTreeMap.size() == objArray.length); 153 for (Object element : objArray) { 154 assertTrue("Map has incorrect mappings", myTreeMap.get( 155 element.toString()).equals(element)); 156 } 157 } 158 159 /** 160 * @tests java.util.TreeMap#TreeMap(java.util.SortedMap) 161 */ 162 @TestInfo( 163 level = TestLevel.PARTIAL, 164 purpose = "NullPointerException checking missed.", 165 targets = { 166 @TestTarget( 167 methodName = "TreeMap", 168 methodArgs = {java.util.SortedMap.class} 169 ) 170 }) 171 public void test_ConstructorLjava_util_SortedMap() { 172 // Test for method java.util.TreeMap(java.util.SortedMap) 173 Comparator comp = new ReversedComparator(); 174 TreeMap reversedTreeMap = new TreeMap(comp); 175 reversedTreeMap.put(new Integer(1).toString(), new Integer(1)); 176 reversedTreeMap.put(new Integer(2).toString(), new Integer(2)); 177 TreeMap anotherTreeMap = new TreeMap(reversedTreeMap); 178 assertTrue("New tree map does not answer correct comparator", 179 anotherTreeMap.comparator() == comp); 180 assertTrue("TreeMap does not use comparator (firstKey was incorrect)", 181 anotherTreeMap.firstKey().equals(new Integer(2).toString())); 182 assertTrue("TreeMap does not use comparator (lastKey was incorrect)", 183 anotherTreeMap.lastKey().equals(new Integer(1).toString())); 184 185 } 186 187 /** 188 * @tests java.util.TreeMap#clear() 189 */ 190 @TestInfo( 191 level = TestLevel.COMPLETE, 192 purpose = "", 193 targets = { 194 @TestTarget( 195 methodName = "clear", 196 methodArgs = {} 197 ) 198 }) 199 public void test_clear() { 200 // Test for method void java.util.TreeMap.clear() 201 tm.clear(); 202 assertEquals("Cleared map returned non-zero size", 0, tm.size()); 203 } 204 205 /** 206 * @tests java.util.TreeMap#clone() 207 */ 208 @TestInfo( 209 level = TestLevel.COMPLETE, 210 purpose = "", 211 targets = { 212 @TestTarget( 213 methodName = "clone", 214 methodArgs = {} 215 ) 216 }) 217 public void test_clone() { 218 // Test for method java.lang.Object java.util.TreeMap.clone() 219 TreeMap clonedMap = (TreeMap) tm.clone(); 220 assertTrue("Cloned map does not equal the original map", clonedMap 221 .equals(tm)); 222 assertTrue("Cloned map is the same reference as the original map", 223 clonedMap != tm); 224 for (Object element : objArray) { 225 assertTrue("Cloned map contains incorrect elements", clonedMap 226 .get(element.toString()) == tm.get(element.toString())); 227 } 228 229 TreeMap map = new TreeMap(); 230 map.put("key", "value"); 231 // get the keySet() and values() on the original Map 232 Set keys = map.keySet(); 233 Collection values = map.values(); 234 assertEquals("values() does not work", "value", values.iterator() 235 .next()); 236 assertEquals("keySet() does not work", "key", keys.iterator().next()); 237 AbstractMap map2 = (AbstractMap) map.clone(); 238 map2.put("key", "value2"); 239 Collection values2 = map2.values(); 240 assertTrue("values() is identical", values2 != values); 241 // values() and keySet() on the cloned() map should be different 242 assertEquals("values() was not cloned", "value2", values2.iterator() 243 .next()); 244 map2.clear(); 245 map2.put("key2", "value3"); 246 Set key2 = map2.keySet(); 247 assertTrue("keySet() is identical", key2 != keys); 248 assertEquals("keySet() was not cloned", "key2", key2.iterator().next()); 249 } 250 251 /** 252 * @tests java.util.TreeMap#comparator() 253 */ 254 @TestInfo( 255 level = TestLevel.COMPLETE, 256 purpose = "", 257 targets = { 258 @TestTarget( 259 methodName = "comparator", 260 methodArgs = {} 261 ) 262 }) 263 public void test_comparator() { 264 // Test for method java.util.Comparator java.util.TreeMap.comparator()\ 265 Comparator comp = new ReversedComparator(); 266 TreeMap reversedTreeMap = new TreeMap(comp); 267 assertTrue("TreeMap answered incorrect comparator", reversedTreeMap 268 .comparator() == comp); 269 reversedTreeMap.put(new Integer(1).toString(), new Integer(1)); 270 reversedTreeMap.put(new Integer(2).toString(), new Integer(2)); 271 assertTrue("TreeMap does not use comparator (firstKey was incorrect)", 272 reversedTreeMap.firstKey().equals(new Integer(2).toString())); 273 assertTrue("TreeMap does not use comparator (lastKey was incorrect)", 274 reversedTreeMap.lastKey().equals(new Integer(1).toString())); 275 } 276 277 /** 278 * @tests java.util.TreeMap#containsKey(java.lang.Object) 279 */ 280 @TestInfo( 281 level = TestLevel.PARTIAL, 282 purpose = "[Doesn't check exceptions and Objects (not only String) as " + 283 "a parameter.]", 284 targets = { 285 @TestTarget( 286 methodName = "containsKey", 287 methodArgs = {java.lang.Object.class} 288 ) 289 }) 290 public void test_containsKeyLjava_lang_Object() { 291 // Test for method boolean 292 // java.util.TreeMap.containsKey(java.lang.Object) 293 assertTrue("Returned false for valid key", tm.containsKey("95")); 294 assertTrue("Returned true for invalid key", !tm.containsKey("XXXXX")); 295 } 296 297 /** 298 * @tests java.util.TreeMap#containsValue(java.lang.Object) 299 */ 300 @TestInfo( 301 level = TestLevel.COMPLETE, 302 purpose = "", 303 targets = { 304 @TestTarget( 305 methodName = "containsValue", 306 methodArgs = {java.lang.Object.class} 307 ) 308 }) 309 public void test_containsValueLjava_lang_Object() { 310 // Test for method boolean 311 // java.util.TreeMap.containsValue(java.lang.Object) 312 assertTrue("Returned false for valid value", tm 313 .containsValue(objArray[986])); 314 assertTrue("Returned true for invalid value", !tm 315 .containsValue(new Object())); 316 } 317 318 /** 319 * @tests java.util.TreeMap#entrySet() 320 */ 321 @TestInfo( 322 level = TestLevel.COMPLETE, 323 purpose = "", 324 targets = { 325 @TestTarget( 326 methodName = "entrySet", 327 methodArgs = {} 328 ) 329 }) 330 public void test_entrySet() { 331 // Test for method java.util.Set java.util.TreeMap.entrySet() 332 Set anEntrySet = tm.entrySet(); 333 Iterator entrySetIterator = anEntrySet.iterator(); 334 assertTrue("EntrySet is incorrect size", 335 anEntrySet.size() == objArray.length); 336 Map.Entry entry; 337 while (entrySetIterator.hasNext()) { 338 entry = (Map.Entry) entrySetIterator.next(); 339 assertTrue("EntrySet does not contain correct mappings", tm 340 .get(entry.getKey()) == entry.getValue()); 341 } 342 } 343 344 /** 345 * @tests java.util.TreeMap#firstKey() 346 */ 347 @TestInfo( 348 level = TestLevel.PARTIAL, 349 purpose = "NoSuchElementException checking missed.", 350 targets = { 351 @TestTarget( 352 methodName = "firstKey", 353 methodArgs = {} 354 ) 355 }) 356 public void test_firstKey() { 357 // Test for method java.lang.Object java.util.TreeMap.firstKey() 358 assertEquals("Returned incorrect first key", "0", tm.firstKey()); 359 } 360 361 /** 362 * @tests java.util.TreeMap#get(java.lang.Object) 363 */ 364 @TestInfo( 365 level = TestLevel.PARTIAL, 366 purpose = "Exceptions checking missed.", 367 targets = { 368 @TestTarget( 369 methodName = "get", 370 methodArgs = {java.lang.Object.class} 371 ) 372 }) 373 public void test_getLjava_lang_Object() { 374 // Test for method java.lang.Object 375 // java.util.TreeMap.get(java.lang.Object) 376 Object o = new Object(); 377 tm.put("Hello", o); 378 assertTrue("Failed to get mapping", tm.get("Hello") == o); 379 380 } 381 382 /** 383 * @tests java.util.TreeMap#headMap(java.lang.Object) 384 */ 385 @TestInfo( 386 level = TestLevel.PARTIAL, 387 purpose = "Exceptions checking missed.", 388 targets = { 389 @TestTarget( 390 methodName = "headMap", 391 methodArgs = {Object.class} 392 ) 393 }) 394 public void test_headMapLjava_lang_Object() { 395 // Test for method java.util.SortedMap 396 // java.util.TreeMap.headMap(java.lang.Object) 397 Map head = tm.headMap("100"); 398 assertEquals("Returned map of incorrect size", 3, head.size()); 399 assertTrue("Returned incorrect elements", head.containsKey("0") 400 && head.containsValue(new Integer("1")) 401 && head.containsKey("10")); 402 403 // Regression for Harmony-1026 404 TreeMap<Integer, Double> map = new TreeMap<Integer, Double>( 405 new MockComparator()); 406 map.put(1, 2.1); 407 map.put(2, 3.1); 408 map.put(3, 4.5); 409 map.put(7, 21.3); 410 map.put(null, null); 411 412 SortedMap<Integer, Double> smap = map.headMap(null); 413 assertEquals(0, smap.size()); 414 415 Set<Integer> keySet = smap.keySet(); 416 assertEquals(0, keySet.size()); 417 418 Set<Map.Entry<Integer, Double>> entrySet = smap.entrySet(); 419 assertEquals(0, entrySet.size()); 420 421 Collection<Double> valueCollection = smap.values(); 422 assertEquals(0, valueCollection.size()); 423 424 // Regression for Harmony-1066 425 assertTrue(head instanceof Serializable); 426 427 // Regression for ill-behaved collator 428 Collator c = new Collator() { 429 @Override 430 public int compare(String o1, String o2) { 431 if (o1 == null) { 432 return 0; 433 } 434 return o1.compareTo(o2); 435 } 436 437 @Override 438 public CollationKey getCollationKey(String string) { 439 return null; 440 } 441 442 @Override 443 public int hashCode() { 444 return 0; 445 } 446 }; 447 448 TreeMap<String, String> treemap = new TreeMap<String, String>(c); 449 assertEquals(0, treemap.headMap(null).size()); 450 451 treemap = new TreeMap(); 452 SortedMap<String, String> headMap = treemap.headMap("100"); 453 headMap.headMap("100"); 454 } 455 456 /** 457 * @tests java.util.TreeMap#keySet() 458 */ 459 @TestInfo( 460 level = TestLevel.COMPLETE, 461 purpose = "", 462 targets = { 463 @TestTarget( 464 methodName = "keySet", 465 methodArgs = {} 466 ) 467 }) 468 public void test_keySet() { 469 // Test for method java.util.Set java.util.TreeMap.keySet() 470 Set ks = tm.keySet(); 471 assertTrue("Returned set of incorrect size", 472 ks.size() == objArray.length); 473 for (int i = 0; i < tm.size(); i++) { 474 assertTrue("Returned set is missing keys", ks.contains(new Integer( 475 i).toString())); 476 } 477 } 478 479 /** 480 * @tests java.util.TreeMap#lastKey() 481 */ 482 @TestInfo( 483 level = TestLevel.PARTIAL, 484 purpose = "NoSuchElementException checking missed.", 485 targets = { 486 @TestTarget( 487 methodName = "lastKey", 488 methodArgs = {} 489 ) 490 }) 491 public void test_lastKey() { 492 // Test for method java.lang.Object java.util.TreeMap.lastKey() 493 assertTrue("Returned incorrect last key", tm.lastKey().equals( 494 objArray[objArray.length - 1].toString())); 495 } 496 497 /** 498 * @tests java.util.TreeMap#put(java.lang.Object, java.lang.Object) 499 */ 500 @TestInfo( 501 level = TestLevel.PARTIAL, 502 purpose = "NullPointerException is not verified.", 503 targets = { 504 @TestTarget( 505 methodName = "put", 506 methodArgs = {Object.class, Object.class} 507 ) 508 }) 509 public void test_putLjava_lang_ObjectLjava_lang_Object() { 510 // Test for method java.lang.Object 511 // java.util.TreeMap.put(java.lang.Object, java.lang.Object) 512 Object o = new Object(); 513 tm.put("Hello", o); 514 assertTrue("Failed to put mapping", tm.get("Hello") == o); 515 516 // regression for Harmony-780 517 tm = new TreeMap(); 518 assertNull(tm.put(new Object(), new Object())); 519 try { 520 tm.put(new Integer(1), new Object()); 521 fail("should throw ClassCastException"); 522 } catch (ClassCastException e) { 523 // expected 524 } 525 526 tm = new TreeMap(); 527 assertNull(tm.put(new Integer(1), new Object())); 528 529 // regression for Harmony-2474 530 tm = new TreeMap(); 531 tm.remove(o); 532 } 533 534 /** 535 * @tests java.util.TreeMap#putAll(java.util.Map) 536 */ 537 @TestInfo( 538 level = TestLevel.PARTIAL, 539 purpose = "ClassCastException & NullPointerException checking missed.", 540 targets = { 541 @TestTarget( 542 methodName = "putAll", 543 methodArgs = {java.util.Map.class} 544 ) 545 }) 546 public void test_putAllLjava_util_Map() { 547 // Test for method void java.util.TreeMap.putAll(java.util.Map) 548 TreeMap x = new TreeMap(); 549 x.putAll(tm); 550 assertTrue("Map incorrect size after put", x.size() == tm.size()); 551 for (Object element : objArray) { 552 assertTrue("Failed to put all elements", x.get(element.toString()) 553 .equals(element)); 554 } 555 } 556 557 /** 558 * @tests java.util.TreeMap#remove(java.lang.Object) 559 */ 560 @TestInfo( 561 level = TestLevel.PARTIAL, 562 purpose = "ClassCastException & NullPointerException checking missed.", 563 targets = { 564 @TestTarget( 565 methodName = "remove", 566 methodArgs = {java.lang.Object.class} 567 ) 568 }) 569 public void test_removeLjava_lang_Object() { 570 // Test for method java.lang.Object 571 // java.util.TreeMap.remove(java.lang.Object) 572 tm.remove("990"); 573 assertTrue("Failed to remove mapping", !tm.containsKey("990")); 574 575 } 576 577 /** 578 * @tests java.util.TreeMap#size() 579 */ 580 @TestInfo( 581 level = TestLevel.COMPLETE, 582 purpose = "", 583 targets = { 584 @TestTarget( 585 methodName = "size", 586 methodArgs = {} 587 ) 588 }) 589 public void test_size() { 590 // Test for method int java.util.TreeMap.size() 591 assertEquals("Returned incorrect size", 1000, tm.size()); 592 } 593 594 /** 595 * @tests java.util.TreeMap#subMap(java.lang.Object, java.lang.Object) 596 */ 597 @TestInfo( 598 level = TestLevel.PARTIAL, 599 purpose = "ClassCastException and NullPointerException are not verified.", 600 targets = { 601 @TestTarget( 602 methodName = "subMap", 603 methodArgs = {Object.class, Object.class} 604 ) 605 }) 606 public void test_subMapLjava_lang_ObjectLjava_lang_Object() { 607 // Test for method java.util.SortedMap 608 // java.util.TreeMap.subMap(java.lang.Object, java.lang.Object) 609 SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109] 610 .toString()); 611 assertEquals("subMap is of incorrect size", 9, subMap.size()); 612 for (int counter = 100; counter < 109; counter++) { 613 assertTrue("SubMap contains incorrect elements", subMap.get( 614 objArray[counter].toString()).equals(objArray[counter])); 615 } 616 617 try { 618 tm.subMap(objArray[9].toString(), objArray[1].toString()); 619 fail("end key less than start key should throw IllegalArgumentException"); 620 } catch (IllegalArgumentException e) { 621 // Expected 622 } 623 624 // Regression for Harmony-1161 625 TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>( 626 new MockComparatorNullTolerable()); 627 treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$ 628 treeMapWithNull.put(null, "value2"); //$NON-NLS-1$ 629 SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null, 630 "key1"); //$NON-NLS-1$ 631 assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$ 632 633 // Regression test for typo in lastKey method 634 SortedMap<String, String> map = new TreeMap<String, String>(); 635 map.put("1", "one"); //$NON-NLS-1$ //$NON-NLS-2$ 636 map.put("2", "two"); //$NON-NLS-1$ //$NON-NLS-2$ 637 map.put("3", "three"); //$NON-NLS-1$ //$NON-NLS-2$ 638 assertEquals("3", map.lastKey()); 639 SortedMap<String, String> sub = map.subMap("1", "3"); //$NON-NLS-1$ //$NON-NLS-2$ 640 assertEquals("2", sub.lastKey()); //$NON-NLS-1$ 641 } 642 643 /** 644 * @tests java.util.TreeMap#tailMap(java.lang.Object) 645 */ 646 @TestInfo( 647 level = TestLevel.PARTIAL, 648 purpose = "Exceptions are not tested.", 649 targets = { 650 @TestTarget( 651 methodName = "tailMap", 652 methodArgs = {Object.class} 653 ) 654 }) 655 public void test_tailMapLjava_lang_Object() { 656 // Test for method java.util.SortedMap 657 // java.util.TreeMap.tailMap(java.lang.Object) 658 Map tail = tm.tailMap(objArray[900].toString()); 659 assertTrue("Returned map of incorrect size : " + tail.size(), tail 660 .size() == (objArray.length - 900) + 9); 661 for (int i = 900; i < objArray.length; i++) { 662 assertTrue("Map contains incorrect entries", tail 663 .containsValue(objArray[i])); 664 } 665 666 // Regression for Harmony-1066 667 assertTrue(tail instanceof Serializable); 668 } 669 670 /** 671 * @tests java.util.TreeMap#values() 672 */ 673 @TestInfo( 674 level = TestLevel.COMPLETE, 675 purpose = "", 676 targets = { 677 @TestTarget( 678 methodName = "values", 679 methodArgs = {} 680 ) 681 }) 682 public void test_values() { 683 // Test for method java.util.Collection java.util.TreeMap.values() 684 Collection vals = tm.values(); 685 vals.iterator(); 686 assertTrue("Returned collection of incorrect size", 687 vals.size() == objArray.length); 688 for (Object element : objArray) { 689 assertTrue("Collection contains incorrect elements", vals 690 .contains(element)); 691 } 692 693 TreeMap myTreeMap = new TreeMap(); 694 for (int i = 0; i < 100; i++) { 695 myTreeMap.put(objArray[i], objArray[i]); 696 } 697 Collection values = myTreeMap.values(); 698 new Support_UnmodifiableCollectionTest( 699 "Test Returned Collection From TreeMap.values()", values) 700 .runTest(); 701 values.remove(new Integer(0)); 702 assertTrue( 703 "Removing from the values collection should remove from the original map", 704 !myTreeMap.containsValue(new Integer(0))); 705 } 706 707 /** 708 * @tests java.util.TreeMap#SerializationTest() 709 */ 710 @TestInfo( 711 level = TestLevel.COMPLETE, 712 purpose = "Verifies serialization/deserialization.", 713 targets = { 714 @TestTarget( 715 methodName = "!SerializationSelf", 716 methodArgs = {} 717 ) 718 }) 719 // Regression for Harmony-1066 720 public void test_SubMap_Serializable() throws Exception { 721 TreeMap<Integer, Double> map = new TreeMap<Integer, Double>(); 722 map.put(1, 2.1); 723 map.put(2, 3.1); 724 map.put(3, 4.5); 725 map.put(7, 21.3); 726 SortedMap<Integer, Double> headMap = map.headMap(3); 727 assertTrue(headMap instanceof Serializable); 728 assertFalse(headMap instanceof TreeMap); 729 assertTrue(headMap instanceof SortedMap); 730 731 assertFalse(headMap.entrySet() instanceof Serializable); 732 assertFalse(headMap.keySet() instanceof Serializable); 733 assertFalse(headMap.values() instanceof Serializable); 734 735 // This assertion will fail on RI. This is a bug of RI. 736 SerializationTest.verifySelf(headMap); 737 } 738 739 /** 740 * Tests equals() method. 741 * Tests that no ClassCastException will be thrown in all cases. 742 * Regression test for HARMONY-1639. 743 */ 744 @TestInfo( 745 level = TestLevel.COMPLETE, 746 purpose = "", 747 targets = { 748 @TestTarget( 749 methodName = "equals", 750 methodArgs = {Object.class} 751 ) 752 }) 753 public void _test_equals() throws Exception { 754 // comparing TreeMaps with different object types 755 Map m1 = new TreeMap(); 756 Map m2 = new TreeMap(); 757 m1.put("key1", "val1"); 758 m1.put("key2", "val2"); 759 m2.put(new Integer(1), "val1"); 760 m2.put(new Integer(2), "val2"); 761 assertFalse("Maps should not be equal 1", m1.equals(m2)); 762 assertFalse("Maps should not be equal 2", m2.equals(m1)); 763 764 // comparing TreeMap with HashMap 765 m1 = new TreeMap(); 766 m2 = new HashMap(); 767 m1.put("key", "val"); 768 m2.put(new Object(), "val"); 769 assertFalse("Maps should not be equal 3", m1.equals(m2)); 770 assertFalse("Maps should not be equal 4", m2.equals(m1)); 771 772 // comparing TreeMaps with not-comparable objects inside 773 m1 = new TreeMap(); 774 m2 = new TreeMap(); 775 m1.put(new Object(), "val1"); 776 m2.put(new Object(), "val1"); 777 assertFalse("Maps should not be equal 5", m1.equals(m2)); 778 assertFalse("Maps should not be equal 6", m2.equals(m1)); 779 } 780 781 /** 782 * Sets up the fixture, for example, open a network connection. This method 783 * is called before a test is executed. 784 */ 785 @Override 786 protected void setUp() { 787 tm = new TreeMap(); 788 for (int i = 0; i < objArray.length; i++) { 789 Object x = objArray[i] = new Integer(i); 790 tm.put(x.toString(), x); 791 } 792 } 793 794} 795