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 18 package org.apache.harmony.tests.java.util; 19 20 import org.apache.harmony.testframework.serialization.SerializationTest; 21 import tests.support.Support_MapTest2; 22 import tests.support.Support_UnmodifiableCollectionTest; 23 import java.io.Serializable; 24 import java.text.CollationKey; 25 import java.text.Collator; 26 import java.util.AbstractMap; 27 import java.util.Collection; 28 import java.util.Comparator; 29 import java.util.HashMap; 30 import java.util.Iterator; 31 import java.util.LinkedList; 32 import java.util.List; 33 import java.util.Map; 34 import java.util.Map.Entry; 35 import java.util.NavigableMap; 36 import java.util.NavigableSet; 37 import java.util.NoSuchElementException; 38 import java.util.Set; 39 import java.util.SortedMap; 40 import java.util.TreeMap; 41 42 public class TreeMapTest extends junit.framework.TestCase { 43 44 public static class ReversedComparator implements Comparator { compare(Object o1, Object o2)45 public int compare(Object o1, Object o2) { 46 return -(((Comparable) o1).compareTo(o2)); 47 } 48 equals(Object o1, Object o2)49 public boolean equals(Object o1, Object o2) { 50 return (((Comparable) o1).compareTo(o2)) == 0; 51 } 52 } 53 54 // Regression for Harmony-1026 55 public static class MockComparator<T extends Comparable<T>> implements 56 Comparator<T>, Serializable { 57 compare(T o1, T o2)58 public int compare(T o1, T o2) { 59 if (o1 == o2) { 60 return 0; 61 } 62 if (null == o1 || null == o2) { 63 return -1; 64 } 65 T c1 = o1; 66 T c2 = o2; 67 return c1.compareTo(c2); 68 } 69 } 70 71 // Regression for Harmony-1161 72 class MockComparatorNullTolerable implements Comparator<String> { 73 compare(String o1, String o2)74 public int compare(String o1, String o2) { 75 if (o1 == o2) { 76 return 0; 77 } 78 if (null == o1) { 79 return -1; 80 } 81 if (null == o2) { // comparator should be symmetric 82 return 1; 83 } 84 return o1.compareTo(o2); 85 } 86 } 87 88 TreeMap tm; 89 90 Object objArray[] = new Object[1000]; 91 92 /** 93 * java.util.TreeMap#TreeMap() 94 */ test_Constructor()95 public void test_Constructor() { 96 // Test for method java.util.TreeMap() 97 new Support_MapTest2(new TreeMap()).runTest(); 98 99 assertTrue("New treeMap non-empty", new TreeMap().isEmpty()); 100 } 101 102 /** 103 * java.util.TreeMap#TreeMap(java.util.Comparator) 104 */ test_ConstructorLjava_util_Comparator()105 public void test_ConstructorLjava_util_Comparator() { 106 // Test for method java.util.TreeMap(java.util.Comparator) 107 Comparator comp = new ReversedComparator(); 108 TreeMap reversedTreeMap = new TreeMap(comp); 109 assertTrue("TreeMap answered incorrect comparator", reversedTreeMap 110 .comparator() == comp); 111 reversedTreeMap.put(new Integer(1).toString(), new Integer(1)); 112 reversedTreeMap.put(new Integer(2).toString(), new Integer(2)); 113 assertTrue("TreeMap does not use comparator (firstKey was incorrect)", 114 reversedTreeMap.firstKey().equals(new Integer(2).toString())); 115 assertTrue("TreeMap does not use comparator (lastKey was incorrect)", 116 reversedTreeMap.lastKey().equals(new Integer(1).toString())); 117 118 } 119 120 /** 121 * java.util.TreeMap#TreeMap(java.util.Map) 122 */ test_ConstructorLjava_util_Map()123 public void test_ConstructorLjava_util_Map() { 124 // Test for method java.util.TreeMap(java.util.Map) 125 TreeMap myTreeMap = new TreeMap(new HashMap(tm)); 126 assertTrue("Map is incorrect size", myTreeMap.size() == objArray.length); 127 for (Object element : objArray) { 128 assertTrue("Map has incorrect mappings", myTreeMap.get( 129 element.toString()).equals(element)); 130 } 131 } 132 133 /** 134 * java.util.TreeMap#TreeMap(java.util.SortedMap) 135 */ test_ConstructorLjava_util_SortedMap()136 public void test_ConstructorLjava_util_SortedMap() { 137 // Test for method java.util.TreeMap(java.util.SortedMap) 138 Comparator comp = new ReversedComparator(); 139 TreeMap reversedTreeMap = new TreeMap(comp); 140 reversedTreeMap.put(new Integer(1).toString(), new Integer(1)); 141 reversedTreeMap.put(new Integer(2).toString(), new Integer(2)); 142 TreeMap anotherTreeMap = new TreeMap(reversedTreeMap); 143 assertTrue("New tree map does not answer correct comparator", 144 anotherTreeMap.comparator() == comp); 145 assertTrue("TreeMap does not use comparator (firstKey was incorrect)", 146 anotherTreeMap.firstKey().equals(new Integer(2).toString())); 147 assertTrue("TreeMap does not use comparator (lastKey was incorrect)", 148 anotherTreeMap.lastKey().equals(new Integer(1).toString())); 149 150 } 151 152 /** 153 * java.util.TreeMap#clear() 154 */ test_clear()155 public void test_clear() { 156 // Test for method void java.util.TreeMap.clear() 157 tm.clear(); 158 assertEquals("Cleared map returned non-zero size", 0, tm.size()); 159 } 160 161 /** 162 * java.util.TreeMap#clone() 163 */ test_clone()164 public void test_clone() { 165 // Test for method java.lang.Object java.util.TreeMap.clone() 166 TreeMap clonedMap = (TreeMap) tm.clone(); 167 assertTrue("Cloned map does not equal the original map", clonedMap 168 .equals(tm)); 169 assertTrue("Cloned map is the same reference as the original map", 170 clonedMap != tm); 171 for (Object element : objArray) { 172 assertTrue("Cloned map contains incorrect elements", clonedMap 173 .get(element.toString()) == tm.get(element.toString())); 174 } 175 176 TreeMap map = new TreeMap(); 177 map.put("key", "value"); 178 // get the keySet() and values() on the original Map 179 Set keys = map.keySet(); 180 Collection values = map.values(); 181 assertEquals("values() does not work", "value", values.iterator() 182 .next()); 183 assertEquals("keySet() does not work", "key", keys.iterator().next()); 184 AbstractMap map2 = (AbstractMap) map.clone(); 185 map2.put("key", "value2"); 186 Collection values2 = map2.values(); 187 assertTrue("values() is identical", values2 != values); 188 // values() and keySet() on the cloned() map should be different 189 assertEquals("values() was not cloned", "value2", values2.iterator() 190 .next()); 191 map2.clear(); 192 map2.put("key2", "value3"); 193 Set key2 = map2.keySet(); 194 assertTrue("keySet() is identical", key2 != keys); 195 assertEquals("keySet() was not cloned", "key2", key2.iterator().next()); 196 } 197 198 /** 199 * java.util.TreeMap#comparator() 200 */ test_comparator()201 public void test_comparator() { 202 // Test for method java.util.Comparator java.util.TreeMap.comparator()\ 203 Comparator comp = new ReversedComparator(); 204 TreeMap reversedTreeMap = new TreeMap(comp); 205 assertTrue("TreeMap answered incorrect comparator", reversedTreeMap 206 .comparator() == comp); 207 reversedTreeMap.put(new Integer(1).toString(), new Integer(1)); 208 reversedTreeMap.put(new Integer(2).toString(), new Integer(2)); 209 assertTrue("TreeMap does not use comparator (firstKey was incorrect)", 210 reversedTreeMap.firstKey().equals(new Integer(2).toString())); 211 assertTrue("TreeMap does not use comparator (lastKey was incorrect)", 212 reversedTreeMap.lastKey().equals(new Integer(1).toString())); 213 } 214 215 /** 216 * java.util.TreeMap#containsKey(java.lang.Object) 217 */ test_containsKeyLjava_lang_Object()218 public void test_containsKeyLjava_lang_Object() { 219 // Test for method boolean 220 // java.util.TreeMap.containsKey(java.lang.Object) 221 assertTrue("Returned false for valid key", tm.containsKey("95")); 222 assertTrue("Returned true for invalid key", !tm.containsKey("XXXXX")); 223 } 224 225 /** 226 * java.util.TreeMap#containsValue(java.lang.Object) 227 */ test_containsValueLjava_lang_Object()228 public void test_containsValueLjava_lang_Object() { 229 // Test for method boolean 230 // java.util.TreeMap.containsValue(java.lang.Object) 231 assertTrue("Returned false for valid value", tm 232 .containsValue(objArray[986])); 233 assertTrue("Returned true for invalid value", !tm 234 .containsValue(new Object())); 235 } 236 237 /** 238 * java.util.TreeMap#entrySet() 239 */ test_entrySet()240 public void test_entrySet() { 241 // Test for method java.util.Set java.util.TreeMap.entrySet() 242 Set anEntrySet = tm.entrySet(); 243 Iterator entrySetIterator = anEntrySet.iterator(); 244 assertTrue("EntrySet is incorrect size", 245 anEntrySet.size() == objArray.length); 246 Map.Entry entry; 247 while (entrySetIterator.hasNext()) { 248 entry = (Map.Entry) entrySetIterator.next(); 249 assertTrue("EntrySet does not contain correct mappings", tm 250 .get(entry.getKey()) == entry.getValue()); 251 } 252 } 253 254 /** 255 * java.util.TreeMap#firstKey() 256 */ test_firstKey()257 public void test_firstKey() { 258 // Test for method java.lang.Object java.util.TreeMap.firstKey() 259 assertEquals("Returned incorrect first key", "0", tm.firstKey()); 260 } 261 262 /** 263 * java.util.TreeMap#get(java.lang.Object) 264 */ test_getLjava_lang_Object()265 public void test_getLjava_lang_Object() { 266 // Test for method java.lang.Object 267 // java.util.TreeMap.get(java.lang.Object) 268 Object o = new Object(); 269 tm.put("Hello", o); 270 assertTrue("Failed to get mapping", tm.get("Hello") == o); 271 272 // Test for the same key & same value 273 tm = new TreeMap(); 274 Object o2 = new Object(); 275 Integer key1 = 1; 276 Integer key2 = 2; 277 assertNull(tm.put(key1, o)); 278 assertNull(tm.put(key2, o)); 279 assertEquals(2, tm.values().size()); 280 assertEquals(2, tm.keySet().size()); 281 assertSame(tm.get(key1), tm.get(key2)); 282 assertSame(o, tm.put(key1, o2)); 283 assertSame(o2, tm.get(key1)); 284 } 285 286 /** 287 * java.util.TreeMap#headMap(java.lang.Object) 288 */ test_headMapLjava_lang_Object()289 public void test_headMapLjava_lang_Object() { 290 // Test for method java.util.SortedMap 291 // java.util.TreeMap.headMap(java.lang.Object) 292 Map head = tm.headMap("100"); 293 assertEquals("Returned map of incorrect size", 3, head.size()); 294 assertTrue("Returned incorrect elements", head.containsKey("0") 295 && head.containsValue(new Integer("1")) 296 && head.containsKey("10")); 297 298 // Regression for Harmony-1026 299 TreeMap<Integer, Double> map = new TreeMap<Integer, Double>( 300 new MockComparator()); 301 map.put(1, 2.1); 302 map.put(2, 3.1); 303 map.put(3, 4.5); 304 map.put(7, 21.3); 305 map.put(null, null); 306 307 SortedMap<Integer, Double> smap = map.headMap(null); 308 assertEquals(0, smap.size()); 309 310 Set<Integer> keySet = smap.keySet(); 311 assertEquals(0, keySet.size()); 312 313 Set<Map.Entry<Integer, Double>> entrySet = smap.entrySet(); 314 assertEquals(0, entrySet.size()); 315 316 Collection<Double> valueCollection = smap.values(); 317 assertEquals(0, valueCollection.size()); 318 319 // Regression for Harmony-1066 320 assertTrue(head instanceof Serializable); 321 322 // Regression for ill-behaved collator 323 Collator c = new Collator() { 324 @Override 325 public int compare(String o1, String o2) { 326 if (o1 == null) { 327 return 0; 328 } 329 return o1.compareTo(o2); 330 } 331 332 @Override 333 public CollationKey getCollationKey(String string) { 334 return null; 335 } 336 337 @Override 338 public int hashCode() { 339 return 0; 340 } 341 }; 342 343 TreeMap<String, String> treemap = new TreeMap<String, String>(c); 344 assertEquals(0, treemap.headMap(null).size()); 345 346 treemap = new TreeMap(); 347 SortedMap<String, String> headMap = treemap.headMap("100"); 348 headMap.headMap("100"); 349 350 SortedMap<Integer, Integer> intMap, sub; 351 int size = 16; 352 intMap = new TreeMap<Integer, Integer>(); 353 for (int i = 0; i < size; i++) { 354 intMap.put(i, i); 355 } 356 sub = intMap.headMap(-1); 357 assertEquals("size should be zero", sub.size(), 0); 358 assertTrue("submap should be empty", sub.isEmpty()); 359 try { 360 sub.firstKey(); 361 fail("java.util.NoSuchElementException should be thrown"); 362 } catch (java.util.NoSuchElementException e) { 363 } 364 365 TreeMap t = new TreeMap(); 366 try { 367 SortedMap th = t.headMap(null); 368 fail("Should throw a NullPointerException"); 369 } catch (NullPointerException npe) { 370 // expected 371 } 372 373 try { 374 sub.lastKey(); 375 fail("java.util.NoSuchElementException should be thrown"); 376 } catch (java.util.NoSuchElementException e) { 377 } 378 379 size = 256; 380 intMap = new TreeMap<Integer, Integer>(); 381 for (int i = 0; i < size; i++) { 382 intMap.put(i, i); 383 } 384 sub = intMap.headMap(-1); 385 assertEquals("size should be zero", sub.size(), 0); 386 assertTrue("submap should be empty", sub.isEmpty()); 387 try { 388 sub.firstKey(); 389 fail("java.util.NoSuchElementException should be thrown"); 390 } catch (java.util.NoSuchElementException e) { 391 } 392 393 try { 394 sub.lastKey(); 395 fail("java.util.NoSuchElementException should be thrown"); 396 } catch (java.util.NoSuchElementException e) { 397 } 398 399 } 400 401 /** 402 * java.util.TreeMap#keySet() 403 */ test_keySet()404 public void test_keySet() { 405 // Test for method java.util.Set java.util.TreeMap.keySet() 406 Set ks = tm.keySet(); 407 assertTrue("Returned set of incorrect size", 408 ks.size() == objArray.length); 409 for (int i = 0; i < tm.size(); i++) { 410 assertTrue("Returned set is missing keys", ks.contains(new Integer( 411 i).toString())); 412 } 413 } 414 415 /** 416 * java.util.TreeMap#lastKey() 417 */ test_lastKey()418 public void test_lastKey() { 419 // Test for method java.lang.Object java.util.TreeMap.lastKey() 420 assertTrue("Returned incorrect last key", tm.lastKey().equals( 421 objArray[objArray.length - 1].toString())); 422 assertNotSame(objArray[objArray.length - 1].toString(), tm.lastKey()); 423 assertEquals(objArray[objArray.length - 2].toString(), tm 424 .headMap("999").lastKey()); 425 assertEquals(objArray[objArray.length - 1].toString(), tm 426 .tailMap("123").lastKey()); 427 assertEquals(objArray[objArray.length - 2].toString(), tm.subMap("99", 428 "999").lastKey()); 429 } 430 test_lastKey_after_subMap()431 public void test_lastKey_after_subMap() { 432 TreeMap<String, String> tm = new TreeMap<String, String>(); 433 tm.put("001", "VAL001"); 434 tm.put("003", "VAL003"); 435 tm.put("002", "VAL002"); 436 SortedMap<String, String> sm = tm; 437 String firstKey = (String) sm.firstKey(); 438 String lastKey = ""; 439 for (int i = 1; i <= tm.size(); i++) { 440 try { 441 lastKey = (String) sm.lastKey(); 442 } catch (NoSuchElementException excep) { 443 fail("NoSuchElementException thrown when there are elements in the map"); 444 } 445 sm = sm.subMap(firstKey, lastKey); 446 } 447 } 448 449 /** 450 * java.util.TreeMap#put(java.lang.Object, java.lang.Object) 451 */ test_remove_throwsWhenNotComparable()452 public void test_remove_throwsWhenNotComparable() { 453 // Test for method java.lang.Object 454 // java.util.TreeMap.put(java.lang.Object, java.lang.Object) 455 Object o = new Object(); 456 tm = new TreeMap(); 457 try { 458 tm.remove(o); 459 fail("should throw ClassCastException"); 460 } catch (ClassCastException e) { 461 //expected 462 } 463 } 464 465 /** 466 * java.util.TreeMap#putAll(java.util.Map) 467 */ test_putAllLjava_util_Map()468 public void test_putAllLjava_util_Map() { 469 // Test for method void java.util.TreeMap.putAll(java.util.Map) 470 TreeMap x = new TreeMap(); 471 x.putAll(tm); 472 assertTrue("Map incorrect size after put", x.size() == tm.size()); 473 for (Object element : objArray) { 474 assertTrue("Failed to put all elements", x.get(element.toString()) 475 .equals(element)); 476 } 477 } 478 479 /** 480 * java.util.TreeMap#remove(java.lang.Object) 481 */ test_removeLjava_lang_Object()482 public void test_removeLjava_lang_Object() { 483 // Test for method java.lang.Object 484 // java.util.TreeMap.remove(java.lang.Object) 485 tm.remove("990"); 486 assertTrue("Failed to remove mapping", !tm.containsKey("990")); 487 488 } 489 490 /** 491 * java.util.TreeMap#size() 492 */ test_size()493 public void test_size() { 494 // Test for method int java.util.TreeMap.size() 495 assertEquals("Returned incorrect size", 1000, tm.size()); 496 assertEquals("Returned incorrect size", 447, tm.headMap("500").size()); 497 assertEquals("Returned incorrect size", 1000, tm.headMap("null").size()); 498 assertEquals("Returned incorrect size", 0, tm.headMap("").size()); 499 assertEquals("Returned incorrect size", 448, tm.headMap("500a").size()); 500 assertEquals("Returned incorrect size", 553, tm.tailMap("500").size()); 501 assertEquals("Returned incorrect size", 0, tm.tailMap("null").size()); 502 assertEquals("Returned incorrect size", 1000, tm.tailMap("").size()); 503 assertEquals("Returned incorrect size", 552, tm.tailMap("500a").size()); 504 assertEquals("Returned incorrect size", 111, tm.subMap("500", "600") 505 .size()); 506 try { 507 tm.subMap("null", "600"); 508 fail("Should throw an IllegalArgumentException"); 509 } catch (IllegalArgumentException e) { 510 // expected 511 } 512 assertEquals("Returned incorrect size", 1000, tm.subMap("", "null") 513 .size()); 514 } 515 516 /** 517 * java.util.TreeMap#subMap(java.lang.Object, java.lang.Object) 518 */ test_subMapLjava_lang_ObjectLjava_lang_Object()519 public void test_subMapLjava_lang_ObjectLjava_lang_Object() { 520 // Test for method java.util.SortedMap 521 // java.util.TreeMap.subMap(java.lang.Object, java.lang.Object) 522 SortedMap subMap = tm.subMap(objArray[100].toString(), objArray[109] 523 .toString()); 524 assertEquals("subMap is of incorrect size", 9, subMap.size()); 525 for (int counter = 100; counter < 109; counter++) { 526 assertTrue("SubMap contains incorrect elements", subMap.get( 527 objArray[counter].toString()).equals(objArray[counter])); 528 } 529 530 try { 531 tm.subMap(objArray[9].toString(), objArray[1].toString()); 532 fail("end key less than start key should throw IllegalArgumentException"); 533 } catch (IllegalArgumentException e) { 534 // Expected 535 } 536 537 // Regression for Harmony-1161 538 TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>( 539 new MockComparatorNullTolerable()); 540 treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$ 541 treeMapWithNull.put(null, "value2"); //$NON-NLS-1$ 542 SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null, 543 "key1"); //$NON-NLS-1$ 544 assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$ 545 546 // Regression test for typo in lastKey method 547 SortedMap<String, String> map = new TreeMap<String, String>(); 548 map.put("1", "one"); //$NON-NLS-1$ //$NON-NLS-2$ 549 map.put("2", "two"); //$NON-NLS-1$ //$NON-NLS-2$ 550 map.put("3", "three"); //$NON-NLS-1$ //$NON-NLS-2$ 551 assertEquals("3", map.lastKey()); 552 SortedMap<String, String> sub = map.subMap("1", "3"); //$NON-NLS-1$ //$NON-NLS-2$ 553 assertEquals("2", sub.lastKey()); //$NON-NLS-1$ 554 555 // NOTE: The contract of this method allows us to throw either 556 // an NPE or a class cast exception. 557 TreeMap t = new TreeMap(); 558 try { 559 t.subMap(null, new Object()); 560 fail("Should throw a ClassCastException"); 561 } catch (ClassCastException npe) { 562 // expected 563 } 564 } 565 566 567 /** 568 * java.util.TreeMap#subMap(java.lang.Object, java.lang.Object) 569 */ test_subMap_Iterator()570 public void test_subMap_Iterator() { 571 TreeMap<String, String> map = new TreeMap<String, String>(); 572 573 String[] keys = { "1", "2", "3" }; 574 String[] values = { "one", "two", "three" }; 575 for (int i = 0; i < keys.length; i++) { 576 map.put(keys[i], values[i]); 577 } 578 579 assertEquals(3, map.size()); 580 581 Map subMap = map.subMap("", "test"); 582 assertEquals(3, subMap.size()); 583 584 Set entrySet = subMap.entrySet(); 585 Iterator iter = entrySet.iterator(); 586 int size = 0; 587 while (iter.hasNext()) { 588 Map.Entry<String, String> entry = (Map.Entry<String, String>) iter 589 .next(); 590 assertTrue(map.containsKey(entry.getKey())); 591 assertTrue(map.containsValue(entry.getValue())); 592 size++; 593 } 594 assertEquals(map.size(), size); 595 596 Set<String> keySet = subMap.keySet(); 597 iter = keySet.iterator(); 598 size = 0; 599 while (iter.hasNext()) { 600 String key = (String) iter.next(); 601 assertTrue(map.containsKey(key)); 602 size++; 603 } 604 assertEquals(map.size(), size); 605 } 606 607 608 /** 609 * java.util.TreeMap#tailMap(java.lang.Object) 610 */ test_tailMapLjava_lang_Object()611 public void test_tailMapLjava_lang_Object() { 612 // Test for method java.util.SortedMap 613 // java.util.TreeMap.tailMap(java.lang.Object) 614 Map tail = tm.tailMap(objArray[900].toString()); 615 assertTrue("Returned map of incorrect size : " + tail.size(), tail 616 .size() == (objArray.length - 900) + 9); 617 for (int i = 900; i < objArray.length; i++) { 618 assertTrue("Map contains incorrect entries", tail 619 .containsValue(objArray[i])); 620 } 621 622 // Regression for Harmony-1066 623 assertTrue(tail instanceof Serializable); 624 625 SortedMap<Integer, Integer> intMap, sub; 626 int size = 16; 627 intMap = new TreeMap<Integer, Integer>(); 628 for (int i = 0; i < size; i++) { 629 intMap.put(i, i); 630 } 631 sub = intMap.tailMap(size); 632 assertEquals("size should be zero", sub.size(), 0); 633 assertTrue("submap should be empty", sub.isEmpty()); 634 try { 635 sub.firstKey(); 636 fail("java.util.NoSuchElementException should be thrown"); 637 } catch (java.util.NoSuchElementException e) { 638 } 639 640 TreeMap t = new TreeMap(); 641 try { 642 SortedMap th = t.tailMap(null); 643 fail("Should throw a NullPointerException"); 644 } catch (NullPointerException npe) { 645 // expected 646 } 647 648 try { 649 sub.lastKey(); 650 fail("java.util.NoSuchElementException should be thrown"); 651 } catch (java.util.NoSuchElementException e) { 652 } 653 654 size = 256; 655 intMap = new TreeMap<Integer, Integer>(); 656 for (int i = 0; i < size; i++) { 657 intMap.put(i, i); 658 } 659 sub = intMap.tailMap(size); 660 assertEquals("size should be zero", sub.size(), 0); 661 assertTrue("submap should be empty", sub.isEmpty()); 662 try { 663 sub.firstKey(); 664 fail("java.util.NoSuchElementException should be thrown"); 665 } catch (java.util.NoSuchElementException e) { 666 } 667 668 try { 669 sub.lastKey(); 670 fail("java.util.NoSuchElementException should be thrown"); 671 } catch (java.util.NoSuchElementException e) { 672 } 673 674 } 675 676 /** 677 * java.util.TreeMap#values() 678 */ test_values()679 public void test_values() { 680 // Test for method java.util.Collection java.util.TreeMap.values() 681 Collection vals = tm.values(); 682 vals.iterator(); 683 assertTrue("Returned collection of incorrect size", 684 vals.size() == objArray.length); 685 for (Object element : objArray) { 686 assertTrue("Collection contains incorrect elements", vals 687 .contains(element)); 688 } 689 assertEquals(1000, vals.size()); 690 int j = 0; 691 for (Iterator iter = vals.iterator(); iter.hasNext(); ) { 692 Object element = (Object) iter.next(); 693 j++; 694 } 695 assertEquals(1000, j); 696 697 vals = tm.descendingMap().values(); 698 vals.iterator(); 699 assertTrue("Returned collection of incorrect size", 700 vals.size() == objArray.length); 701 for (Object element : objArray) { 702 assertTrue("Collection contains incorrect elements", vals 703 .contains(element)); 704 } 705 assertEquals(1000, vals.size()); 706 j = 0; 707 for (Iterator iter = vals.iterator(); iter.hasNext(); ) { 708 Object element = (Object) iter.next(); 709 j++; 710 } 711 assertEquals(1000, j); 712 713 TreeMap myTreeMap = new TreeMap(); 714 for (int i = 0; i < 100; i++) { 715 myTreeMap.put(objArray[i], objArray[i]); 716 } 717 Collection values = myTreeMap.values(); 718 new Support_UnmodifiableCollectionTest( 719 "Test Returned Collection From TreeMap.values()", values) 720 .runTest(); 721 values.remove(new Integer(0)); 722 assertTrue( 723 "Removing from the values collection should remove from the original map", 724 !myTreeMap.containsValue(new Integer(0))); 725 assertEquals(99, values.size()); 726 j = 0; 727 for (Iterator iter = values.iterator(); iter.hasNext(); ) { 728 Object element = (Object) iter.next(); 729 j++; 730 } 731 assertEquals(99, j); 732 733 } 734 735 /** 736 * java.util.TreeMap the values() method in sub maps 737 */ test_subMap_values_size()738 public void test_subMap_values_size() { 739 TreeMap myTreeMap = new TreeMap(); 740 for (int i = 0; i < 1000; i++) { 741 myTreeMap.put(i, objArray[i]); 742 } 743 // Test for method values() in subMaps 744 Collection vals = myTreeMap.subMap(200, 400).values(); 745 assertTrue("Returned collection of incorrect size", vals.size() == 200); 746 for (int i = 200; i < 400; i++) { 747 assertTrue("Collection contains incorrect elements" + i, vals 748 .contains(objArray[i])); 749 } 750 assertEquals(200, vals.toArray().length); 751 vals.remove(objArray[300]); 752 assertTrue( 753 "Removing from the values collection should remove from the original map", 754 !myTreeMap.containsValue(objArray[300])); 755 assertTrue("Returned collection of incorrect size", vals.size() == 199); 756 assertEquals(199, vals.toArray().length); 757 758 myTreeMap.put(300, objArray[300]); 759 // Test for method values() in subMaps 760 vals = myTreeMap.headMap(400).values(); 761 assertEquals("Returned collection of incorrect size", vals.size(), 400); 762 for (int i = 0; i < 400; i++) { 763 assertTrue("Collection contains incorrect elements " + i, vals 764 .contains(objArray[i])); 765 } 766 assertEquals(400, vals.toArray().length); 767 vals.remove(objArray[300]); 768 assertTrue( 769 "Removing from the values collection should remove from the original map", 770 !myTreeMap.containsValue(objArray[300])); 771 assertTrue("Returned collection of incorrect size", vals.size() == 399); 772 assertEquals(399, vals.toArray().length); 773 774 myTreeMap.put(300, objArray[300]); 775 // Test for method values() in subMaps 776 vals = myTreeMap.tailMap(400).values(); 777 assertEquals("Returned collection of incorrect size", vals.size(), 600); 778 for (int i = 400; i < 1000; i++) { 779 assertTrue("Collection contains incorrect elements " + i, vals 780 .contains(objArray[i])); 781 } 782 assertEquals(600, vals.toArray().length); 783 vals.remove(objArray[600]); 784 assertTrue( 785 "Removing from the values collection should remove from the original map", 786 !myTreeMap.containsValue(objArray[600])); 787 assertTrue("Returned collection of incorrect size", vals.size() == 599); 788 assertEquals(599, vals.toArray().length); 789 790 791 myTreeMap.put(600, objArray[600]); 792 // Test for method values() in subMaps 793 vals = myTreeMap.descendingMap().headMap(400).values(); 794 assertEquals("Returned collection of incorrect size", vals.size(), 599); 795 for (int i = 401; i < 1000; i++) { 796 assertTrue("Collection contains incorrect elements " + i, vals 797 .contains(objArray[i])); 798 } 799 assertEquals(599, vals.toArray().length); 800 vals.remove(objArray[600]); 801 assertTrue( 802 "Removing from the values collection should remove from the original map", 803 !myTreeMap.containsValue(objArray[600])); 804 assertTrue("Returned collection of incorrect size", vals.size() == 598); 805 assertEquals(598, vals.toArray().length); 806 807 myTreeMap.put(600, objArray[600]); 808 // Test for method values() in subMaps 809 vals = myTreeMap.descendingMap().tailMap(400).values(); 810 assertEquals("Returned collection of incorrect size", vals.size(), 401); 811 for (int i = 0; i <= 400; i++) { 812 assertTrue("Collection contains incorrect elements " + i, vals 813 .contains(objArray[i])); 814 } 815 assertEquals(401, vals.toArray().length); 816 vals.remove(objArray[300]); 817 assertTrue( 818 "Removing from the values collection should remove from the original map", 819 !myTreeMap.containsValue(objArray[300])); 820 assertTrue("Returned collection of incorrect size", vals.size() == 400); 821 assertEquals(400, vals.toArray().length); 822 823 } 824 825 /** 826 * java.util.TreeMap#subMap() 827 */ test_subMap_Iterator2()828 public void test_subMap_Iterator2() { 829 TreeMap<String, String> map = new TreeMap<String, String>(); 830 831 String[] keys = { "1", "2", "3" }; 832 String[] values = { "one", "two", "three" }; 833 for (int i = 0; i < keys.length; i++) { 834 map.put(keys[i], values[i]); 835 } 836 837 assertEquals(3, map.size()); 838 839 Map subMap = map.subMap("", "test"); 840 assertEquals(3, subMap.size()); 841 842 Set entrySet = subMap.entrySet(); 843 Iterator iter = entrySet.iterator(); 844 int size = 0; 845 while (iter.hasNext()) { 846 Map.Entry<String, String> entry = (Map.Entry<String, String>) iter 847 .next(); 848 assertTrue(map.containsKey(entry.getKey())); 849 assertTrue(map.containsValue(entry.getValue())); 850 size++; 851 } 852 assertEquals(map.size(), size); 853 854 Set<String> keySet = subMap.keySet(); 855 iter = keySet.iterator(); 856 size = 0; 857 while (iter.hasNext()) { 858 String key = (String) iter.next(); 859 assertTrue(map.containsKey(key)); 860 size++; 861 } 862 assertEquals(map.size(), size); 863 } 864 865 /** 866 * java.util.TreeMap#SerializationTest() 867 */ 868 // Regression for Harmony-1066 test_SubMap_Serializable()869 public void test_SubMap_Serializable() throws Exception { 870 TreeMap<Integer, Double> map = new TreeMap<Integer, Double>(); 871 map.put(1, 2.1); 872 map.put(2, 3.1); 873 map.put(3, 4.5); 874 map.put(7, 21.3); 875 SortedMap<Integer, Double> headMap = map.headMap(3); 876 assertTrue(headMap instanceof Serializable); 877 assertFalse(headMap instanceof TreeMap); 878 assertTrue(headMap instanceof SortedMap); 879 880 assertFalse(headMap.entrySet() instanceof Serializable); 881 assertFalse(headMap.keySet() instanceof Serializable); 882 assertFalse(headMap.values() instanceof Serializable); 883 884 // This assertion will fail on RI. This is a bug of RI. 885 SerializationTest.verifySelf(headMap); 886 } 887 888 /** 889 * {@link java.util.TreeMap#firstEntry()} 890 */ test_firstEntry()891 public void test_firstEntry() throws Exception { 892 Integer testint = new Integer(-1); 893 Integer testint10000 = new Integer(-10000); 894 Integer testint9999 = new Integer(-9999); 895 assertEquals(objArray[0].toString(), tm.firstEntry().getKey()); 896 assertEquals(objArray[0], tm.firstEntry().getValue()); 897 tm.put(testint.toString(), testint); 898 assertEquals(testint.toString(), tm.firstEntry().getKey()); 899 assertEquals(testint, tm.firstEntry().getValue()); 900 tm.put(testint10000.toString(), testint10000); 901 assertEquals(testint.toString(), tm.firstEntry().getKey()); 902 assertEquals(testint, tm.firstEntry().getValue()); 903 tm.put(testint9999.toString(), testint9999); 904 assertEquals(testint.toString(), tm.firstEntry().getKey()); 905 Entry entry = tm.firstEntry(); 906 assertEquals(testint, entry.getValue()); 907 assertEntry(entry); 908 tm.clear(); 909 assertNull(tm.firstEntry()); 910 } 911 912 /** 913 * {@link java.util.TreeMap#lastEntry() 914 */ test_lastEntry()915 public void test_lastEntry() throws Exception { 916 Integer testint10000 = new Integer(10000); 917 Integer testint9999 = new Integer(9999); 918 assertEquals(objArray[999].toString(), tm.lastEntry().getKey()); 919 assertEquals(objArray[999], tm.lastEntry().getValue()); 920 tm.put(testint10000.toString(), testint10000); 921 assertEquals(objArray[999].toString(), tm.lastEntry().getKey()); 922 assertEquals(objArray[999], tm.lastEntry().getValue()); 923 tm.put(testint9999.toString(), testint9999); 924 assertEquals(testint9999.toString(), tm.lastEntry().getKey()); 925 Entry entry = tm.lastEntry(); 926 assertEquals(testint9999, entry.getValue()); 927 assertEntry(entry); 928 tm.clear(); 929 assertNull(tm.lastEntry()); 930 } 931 932 /** 933 * {@link java.util.TreeMap#pollFirstEntry() 934 */ test_pollFirstEntry()935 public void test_pollFirstEntry() throws Exception { 936 Integer testint = new Integer(-1); 937 Integer testint10000 = new Integer(-10000); 938 Integer testint9999 = new Integer(-9999); 939 assertEquals(objArray[0].toString(), tm.pollFirstEntry().getKey()); 940 assertEquals(objArray[1], tm.pollFirstEntry().getValue()); 941 assertEquals(objArray[10], tm.pollFirstEntry().getValue()); 942 tm.put(testint.toString(), testint); 943 tm.put(testint10000.toString(), testint10000); 944 assertEquals(testint.toString(), tm.pollFirstEntry().getKey()); 945 assertEquals(testint10000, tm.pollFirstEntry().getValue()); 946 tm.put(testint9999.toString(), testint9999); 947 assertEquals(testint9999.toString(), tm.pollFirstEntry().getKey()); 948 Entry entry = tm.pollFirstEntry(); 949 assertEntry(entry); 950 assertEquals(objArray[100], entry.getValue()); 951 tm.clear(); 952 assertNull(tm.pollFirstEntry()); 953 } 954 955 /** 956 * {@link java.util.TreeMap#pollLastEntry() 957 */ test_pollLastEntry()958 public void test_pollLastEntry() throws Exception { 959 Integer testint10000 = new Integer(10000); 960 Integer testint9999 = new Integer(9999); 961 assertEquals(objArray[999].toString(), tm.pollLastEntry().getKey()); 962 assertEquals(objArray[998], tm.pollLastEntry().getValue()); 963 assertEquals(objArray[997], tm.pollLastEntry().getValue()); 964 tm.put(testint10000.toString(), testint10000); 965 assertEquals(objArray[996], tm.pollLastEntry().getValue()); 966 tm.put(testint9999.toString(), testint9999); 967 assertEquals(testint9999.toString(), tm.pollLastEntry().getKey()); 968 Entry entry = tm.pollLastEntry(); 969 assertEquals(objArray[995], entry.getValue()); 970 assertEntry(entry); 971 tm.clear(); 972 assertNull(tm.pollLastEntry()); 973 } 974 975 /** 976 * {@link java.util.TreeMap#lowerEntry(Object) 977 */ test_lowerEntry()978 public void test_lowerEntry() throws Exception { 979 Integer testint10000 = new Integer(10000); 980 Integer testint9999 = new Integer(9999); 981 assertEquals(objArray[999], tm.lowerEntry(testint9999.toString()) 982 .getValue()); 983 assertEquals(objArray[100], tm.lowerEntry(testint10000.toString()) 984 .getValue()); 985 tm.put(testint10000.toString(), testint10000); 986 tm.put(testint9999.toString(), testint9999); 987 assertEquals(objArray[999], tm.lowerEntry(testint9999.toString()) 988 .getValue()); 989 Entry entry = tm.lowerEntry(testint10000.toString()); 990 assertEquals(objArray[100], entry.getValue()); 991 assertEntry(entry); 992 try { 993 tm.lowerEntry(testint10000); 994 fail("should throw ClassCastException"); 995 } catch (ClassCastException e) { 996 // expected 997 } 998 try { 999 tm.lowerEntry(null); 1000 fail("should throw NullPointerException"); 1001 } catch (NullPointerException e) { 1002 // expected 1003 } 1004 tm.clear(); 1005 assertNull(tm.lowerEntry(testint9999.toString())); 1006 } 1007 1008 /** 1009 * {@link java.util.TreeMap#lowerKey(Object) 1010 */ test_lowerKey()1011 public void test_lowerKey() throws Exception { 1012 Integer testint10000 = new Integer(10000); 1013 Integer testint9999 = new Integer(9999); 1014 assertEquals(objArray[999].toString(), tm.lowerKey(testint9999 1015 .toString())); 1016 assertEquals(objArray[100].toString(), tm.lowerKey(testint10000 1017 .toString())); 1018 tm.put(testint10000.toString(), testint10000); 1019 tm.put(testint9999.toString(), testint9999); 1020 assertEquals(objArray[999].toString(), tm.lowerKey(testint9999 1021 .toString())); 1022 assertEquals(objArray[100].toString(), tm.lowerKey(testint10000 1023 .toString())); 1024 try { 1025 tm.lowerKey(testint10000); 1026 fail("should throw ClassCastException"); 1027 } catch (ClassCastException e) { 1028 // expected 1029 } 1030 try { 1031 tm.lowerKey(null); 1032 fail("should throw NullPointerException"); 1033 } catch (NullPointerException e) { 1034 // expected 1035 } 1036 tm.clear(); 1037 assertNull(tm.lowerKey(testint9999.toString())); 1038 } 1039 1040 /** 1041 * {@link java.util.TreeMap#floorEntry(Object) 1042 */ test_floorEntry()1043 public void test_floorEntry() throws Exception { 1044 Integer testint10000 = new Integer(10000); 1045 Integer testint9999 = new Integer(9999); 1046 assertEquals(objArray[999], tm.floorEntry(testint9999.toString()) 1047 .getValue()); 1048 assertEquals(objArray[100], tm.floorEntry(testint10000.toString()) 1049 .getValue()); 1050 tm.put(testint10000.toString(), testint10000); 1051 tm.put(testint9999.toString(), testint9999); 1052 assertEquals(testint9999, tm.floorEntry(testint9999.toString()) 1053 .getValue()); 1054 Entry entry = tm.floorEntry(testint10000.toString()); 1055 assertEquals(testint10000, entry.getValue()); 1056 assertEntry(entry); 1057 try { 1058 tm.floorEntry(testint10000); 1059 fail("should throw ClassCastException"); 1060 } catch (ClassCastException e) { 1061 // expected 1062 } 1063 try { 1064 tm.floorEntry(null); 1065 fail("should throw NullPointerException"); 1066 } catch (NullPointerException e) { 1067 // expected 1068 } 1069 tm.clear(); 1070 assertNull(tm.floorEntry(testint9999.toString())); 1071 } 1072 1073 /** 1074 * {@link java.util.TreeMap#floorKey(Object) 1075 */ test_floorKey()1076 public void test_floorKey() throws Exception { 1077 Integer testint10000 = new Integer(10000); 1078 Integer testint9999 = new Integer(9999); 1079 assertEquals(objArray[999].toString(), tm.floorKey(testint9999 1080 .toString())); 1081 assertEquals(objArray[100].toString(), tm.floorKey(testint10000 1082 .toString())); 1083 tm.put(testint10000.toString(), testint10000); 1084 tm.put(testint9999.toString(), testint9999); 1085 assertEquals(testint9999.toString(), tm 1086 .floorKey(testint9999.toString())); 1087 assertEquals(testint10000.toString(), tm.floorKey(testint10000 1088 .toString())); 1089 try { 1090 tm.floorKey(testint10000); 1091 fail("should throw ClassCastException"); 1092 } catch (ClassCastException e) { 1093 // expected 1094 } 1095 try { 1096 tm.floorKey(null); 1097 fail("should throw NullPointerException"); 1098 } catch (NullPointerException e) { 1099 // expected 1100 } 1101 tm.clear(); 1102 assertNull(tm.floorKey(testint9999.toString())); 1103 } 1104 1105 /** 1106 * {@link java.util.TreeMap#ceilingEntry(Object) 1107 */ test_ceilingEntry()1108 public void test_ceilingEntry() throws Exception { 1109 Integer testint100 = new Integer(100); 1110 Integer testint = new Integer(-1); 1111 assertEquals(objArray[0], tm.ceilingEntry(testint.toString()) 1112 .getValue()); 1113 assertEquals(objArray[100], tm.ceilingEntry(testint100.toString()) 1114 .getValue()); 1115 tm.put(testint.toString(), testint); 1116 tm.put(testint100.toString(), testint); 1117 assertEquals(testint, tm.ceilingEntry(testint.toString()).getValue()); 1118 Entry entry = tm.ceilingEntry(testint100.toString()); 1119 assertEquals(testint, entry.getValue()); 1120 assertEntry(entry); 1121 try { 1122 tm.ceilingEntry(testint100); 1123 fail("should throw ClassCastException"); 1124 } catch (ClassCastException e) { 1125 // expected 1126 } 1127 try { 1128 tm.ceilingEntry(null); 1129 fail("should throw NullPointerException"); 1130 } catch (NullPointerException e) { 1131 // expected 1132 } 1133 tm.clear(); 1134 assertNull(tm.ceilingEntry(testint.toString())); 1135 } 1136 1137 /** 1138 * {@link java.util.TreeMap#ceilingKey(Object) 1139 */ test_ceilingKey()1140 public void test_ceilingKey() throws Exception { 1141 Integer testint100 = new Integer(100); 1142 Integer testint = new Integer(-1); 1143 assertEquals(objArray[0].toString(), tm.ceilingKey(testint.toString())); 1144 assertEquals(objArray[100].toString(), tm.ceilingKey(testint100 1145 .toString())); 1146 tm.put(testint.toString(), testint); 1147 tm.put(testint100.toString(), testint); 1148 assertEquals(testint.toString(), tm.ceilingKey(testint.toString())); 1149 assertEquals(testint100.toString(), tm 1150 .ceilingKey(testint100.toString())); 1151 try { 1152 tm.ceilingKey(testint100); 1153 fail("should throw ClassCastException"); 1154 } catch (ClassCastException e) { 1155 // expected 1156 } 1157 try { 1158 tm.ceilingKey(null); 1159 fail("should throw NullPointerException"); 1160 } catch (NullPointerException e) { 1161 // expected 1162 } 1163 tm.clear(); 1164 assertNull(tm.ceilingKey(testint.toString())); 1165 } 1166 1167 /** 1168 * {@link java.util.TreeMap#higherEntry(Object) 1169 */ test_higherEntry()1170 public void test_higherEntry() throws Exception { 1171 Integer testint9999 = new Integer(9999); 1172 Integer testint10000 = new Integer(10000); 1173 Integer testint100 = new Integer(100); 1174 Integer testint = new Integer(-1); 1175 assertEquals(objArray[0], tm.higherEntry(testint.toString()).getValue()); 1176 assertEquals(objArray[101], tm.higherEntry(testint100.toString()) 1177 .getValue()); 1178 assertEquals(objArray[101], tm.higherEntry(testint10000.toString()) 1179 .getValue()); 1180 tm.put(testint9999.toString(), testint); 1181 tm.put(testint100.toString(), testint); 1182 tm.put(testint10000.toString(), testint); 1183 assertEquals(objArray[0], tm.higherEntry(testint.toString()).getValue()); 1184 assertEquals(testint, tm.higherEntry(testint100.toString()).getValue()); 1185 Entry entry = tm.higherEntry(testint10000.toString()); 1186 assertEquals(objArray[101], entry.getValue()); 1187 assertEntry(entry); 1188 assertNull(tm.higherEntry(testint9999.toString())); 1189 try { 1190 tm.higherEntry(testint100); 1191 fail("should throw ClassCastException"); 1192 } catch (ClassCastException e) { 1193 // expected 1194 } 1195 try { 1196 tm.higherEntry(null); 1197 fail("should throw NullPointerException"); 1198 } catch (NullPointerException e) { 1199 // expected 1200 } 1201 tm.clear(); 1202 assertNull(tm.higherEntry(testint.toString())); 1203 } 1204 1205 /** 1206 * {@link java.util.TreeMap#higherKey(Object) 1207 */ test_higherKey()1208 public void test_higherKey() throws Exception { 1209 Integer testint9999 = new Integer(9999); 1210 Integer testint10000 = new Integer(10000); 1211 Integer testint100 = new Integer(100); 1212 Integer testint = new Integer(-1); 1213 assertEquals(objArray[0].toString(), tm.higherKey(testint.toString())); 1214 assertEquals(objArray[101].toString(), tm.higherKey(testint100 1215 .toString())); 1216 assertEquals(objArray[101].toString(), tm.higherKey(testint10000 1217 .toString())); 1218 tm.put(testint9999.toString(), testint); 1219 tm.put(testint100.toString(), testint); 1220 tm.put(testint10000.toString(), testint); 1221 assertEquals(objArray[0].toString(), tm.higherKey(testint.toString())); 1222 assertEquals(testint10000.toString(), tm.higherKey(testint100 1223 .toString())); 1224 assertEquals(objArray[101].toString(), tm.higherKey(testint10000 1225 .toString())); 1226 assertNull(tm.higherKey(testint9999.toString())); 1227 try { 1228 tm.higherKey(testint100); 1229 fail("should throw ClassCastException"); 1230 } catch (ClassCastException e) { 1231 // expected 1232 } 1233 try { 1234 tm.higherKey(null); 1235 fail("should throw NullPointerException"); 1236 } catch (NullPointerException e) { 1237 // expected 1238 } 1239 tm.clear(); 1240 assertNull(tm.higherKey(testint.toString())); 1241 } 1242 test_navigableKeySet()1243 public void test_navigableKeySet() throws Exception { 1244 Integer testint9999 = new Integer(9999); 1245 Integer testint10000 = new Integer(10000); 1246 Integer testint100 = new Integer(100); 1247 Integer testint0 = new Integer(0); 1248 NavigableSet set = tm.navigableKeySet(); 1249 assertFalse(set.contains(testint9999.toString())); 1250 tm.put(testint9999.toString(), testint9999); 1251 assertTrue(set.contains(testint9999.toString())); 1252 tm.remove(testint9999.toString()); 1253 assertFalse(set.contains(testint9999.toString())); 1254 try { 1255 set.add(new Object()); 1256 fail("should throw UnsupportedOperationException"); 1257 } catch (UnsupportedOperationException e) { 1258 // expected 1259 } 1260 try { 1261 set.add(null); 1262 fail("should throw UnsupportedOperationException"); 1263 } catch (UnsupportedOperationException e) { 1264 // expected 1265 } 1266 try { 1267 set.addAll(null); 1268 fail("should throw UnsupportedOperationException"); 1269 } catch (NullPointerException e) { 1270 // expected 1271 } 1272 Collection collection = new LinkedList(); 1273 set.addAll(collection); 1274 try { 1275 collection.add(new Object()); 1276 set.addAll(collection); 1277 fail("should throw UnsupportedOperationException"); 1278 } catch (UnsupportedOperationException e) { 1279 // expected 1280 } 1281 set.remove(testint100.toString()); 1282 assertFalse(tm.containsKey(testint100.toString())); 1283 assertTrue(tm.containsKey(testint0.toString())); 1284 Iterator iter = set.iterator(); 1285 iter.next(); 1286 iter.remove(); 1287 assertFalse(tm.containsKey(testint0.toString())); 1288 collection.add(new Integer(200).toString()); 1289 set.retainAll(collection); 1290 assertEquals(1, tm.size()); 1291 set.removeAll(collection); 1292 assertEquals(0, tm.size()); 1293 tm.put(testint10000.toString(), testint10000); 1294 assertEquals(1, tm.size()); 1295 set.clear(); 1296 assertEquals(0, tm.size()); 1297 } 1298 assertEntry(Entry entry)1299 private void assertEntry(Entry entry) { 1300 try { 1301 entry.setValue(new Object()); 1302 fail("should throw UnsupportedOperationException"); 1303 } catch (UnsupportedOperationException e) { 1304 // expected 1305 } 1306 assertEquals((entry.getKey() == null ? 0 : entry.getKey().hashCode()) 1307 ^ (entry.getValue() == null ? 0 : entry.getValue().hashCode()), 1308 entry.hashCode()); 1309 assertEquals(entry.toString(), entry.getKey() + "=" + entry.getValue()); 1310 } 1311 1312 /** 1313 * java.util.TreeMap#subMap(java.lang.Object, boolean, 1314 *java.lang.Object, boolean) 1315 */ test_subMapLjava_lang_ObjectZLjava_lang_ObjectZ()1316 public void test_subMapLjava_lang_ObjectZLjava_lang_ObjectZ() { 1317 // normal case 1318 SortedMap subMap = tm.subMap(objArray[100].toString(), true, 1319 objArray[109].toString(), true); 1320 assertEquals("subMap is of incorrect size", 10, subMap.size()); 1321 subMap = tm.subMap(objArray[100].toString(), true, objArray[109] 1322 .toString(), false); 1323 assertEquals("subMap is of incorrect size", 9, subMap.size()); 1324 for (int counter = 100; counter < 109; counter++) { 1325 assertTrue("SubMap contains incorrect elements", subMap.get( 1326 objArray[counter].toString()).equals(objArray[counter])); 1327 } 1328 subMap = tm.subMap(objArray[100].toString(), false, objArray[109] 1329 .toString(), true); 1330 assertEquals("subMap is of incorrect size", 9, subMap.size()); 1331 assertNull(subMap.get(objArray[100].toString())); 1332 1333 // Exceptions 1334 try { 1335 tm.subMap(objArray[9].toString(), true, objArray[1].toString(), 1336 true); 1337 fail("should throw IllegalArgumentException"); 1338 } catch (IllegalArgumentException e) { 1339 // expected 1340 } 1341 try { 1342 tm.subMap(objArray[9].toString(), false, objArray[1].toString(), 1343 false); 1344 fail("should throw IllegalArgumentException"); 1345 } catch (IllegalArgumentException e) { 1346 // expected 1347 } 1348 try { 1349 tm.subMap(null, true, null, true); 1350 fail("should throw NullPointerException"); 1351 } catch (NullPointerException e) { 1352 // expected 1353 } 1354 try { 1355 tm.subMap(null, false, objArray[100], true); 1356 fail("should throw NullPointerException"); 1357 } catch (NullPointerException e) { 1358 // expected 1359 } 1360 try { 1361 tm.subMap(new LinkedList(), false, objArray[100], true); 1362 fail("should throw ClassCastException"); 1363 } catch (ClassCastException e) { 1364 // expected 1365 } 1366 1367 // use integer elements to test 1368 TreeMap<Integer, String> treeMapInt = new TreeMap<Integer, String>(); 1369 assertEquals(0, treeMapInt.subMap(new Integer(-1), true, 1370 new Integer(100), true).size()); 1371 for (int i = 0; i < 100; i++) { 1372 treeMapInt.put(new Integer(i), new Integer(i).toString()); 1373 } 1374 SortedMap<Integer, String> result = treeMapInt.subMap(new Integer(-1), 1375 true, new Integer(100), true); 1376 assertEquals(100, result.size()); 1377 result.put(new Integer(-1), new Integer(-1).toString()); 1378 assertEquals(101, result.size()); 1379 assertEquals(101, treeMapInt.size()); 1380 result = treeMapInt 1381 .subMap(new Integer(50), true, new Integer(60), true); 1382 assertEquals(11, result.size()); 1383 try { 1384 result.put(new Integer(-2), new Integer(-2).toString()); 1385 fail("should throw IllegalArgumentException"); 1386 } catch (IllegalArgumentException e) { 1387 // expected 1388 } 1389 assertEquals(11, result.size()); 1390 treeMapInt.remove(new Integer(50)); 1391 assertEquals(100, treeMapInt.size()); 1392 assertEquals(10, result.size()); 1393 result.remove(new Integer(60)); 1394 assertEquals(99, treeMapInt.size()); 1395 assertEquals(9, result.size()); 1396 SortedMap<Integer, String> result2 = null; 1397 try { 1398 result2 = result.subMap(new Integer(-2), new Integer(100)); 1399 fail("should throw IllegalArgumentException"); 1400 } catch (IllegalArgumentException e) { 1401 // expected 1402 } 1403 result2 = result.subMap(new Integer(50), new Integer(60)); 1404 assertEquals(9, result2.size()); 1405 1406 // sub map of sub map 1407 NavigableMap<Integer, Object> mapIntObj = new TreeMap<Integer, Object>(); 1408 for (int i = 0; i < 10; ++i) { 1409 mapIntObj.put(i, new Object()); 1410 } 1411 mapIntObj = mapIntObj.subMap(5, false, 9, true); 1412 assertEquals(4, mapIntObj.size()); 1413 mapIntObj = mapIntObj.subMap(5, false, 9, true); 1414 assertEquals(4, mapIntObj.size()); 1415 mapIntObj = mapIntObj.subMap(5, false, 6, false); 1416 assertEquals(0, mapIntObj.size()); 1417 1418 // a special comparator dealing with null key 1419 tm = new TreeMap(new Comparator() { 1420 public int compare(Object o1, Object o2) { 1421 if (o1 == null) { 1422 return -1; 1423 } 1424 if (o2 == null) { 1425 return 1; 1426 } 1427 return ((String) o1).compareTo((String) o2); 1428 } 1429 }); 1430 tm.put(null, -1); 1431 tm.put(new String("1st"), 1); 1432 tm.put(new String("2nd"), 2); 1433 tm.put(new String("3rd"), 3); 1434 SortedMap s = tm.subMap(null, "3rd"); 1435 assertEquals(3, s.size()); 1436 assertTrue(s.containsValue(-1)); 1437 assertTrue(s.containsValue(1)); 1438 assertTrue(s.containsValue(2)); 1439 assertFalse(s.containsKey(null)); 1440 1441 s = tm.descendingMap(); 1442 s = s.subMap("3rd", null); 1443 assertFalse(s.containsKey(null)); 1444 assertTrue(s.containsKey("1st")); 1445 assertTrue(s.containsKey("2nd")); 1446 assertTrue(s.containsKey("3rd")); 1447 } 1448 test_subMap_NullTolerableComparator()1449 public void test_subMap_NullTolerableComparator() { 1450 // Null Tolerable Comparator 1451 TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>( 1452 new MockComparatorNullTolerable()); 1453 treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$ 1454 treeMapWithNull.put(null, "value2"); //$NON-NLS-1$ 1455 SortedMap<String, String> subMapWithNull = treeMapWithNull.subMap(null, 1456 true, "key1", true); //$NON-NLS-1$ 1457 1458 // RI fails here 1459 assertEquals("Size of subMap should be 2:", 2, subMapWithNull.size()); //$NON-NLS-1$ 1460 assertEquals("value1", subMapWithNull.get("key1")); 1461 assertEquals("value2", subMapWithNull.get(null)); 1462 treeMapWithNull.put("key0", "value2"); 1463 treeMapWithNull.put("key3", "value3"); 1464 treeMapWithNull.put("key4", "value4"); 1465 treeMapWithNull.put("key5", "value5"); 1466 treeMapWithNull.put("key6", "value6"); 1467 assertEquals("Size of subMap should be 3:", 3, subMapWithNull.size()); //$NON-NLS-1$ 1468 subMapWithNull = treeMapWithNull.subMap(null, false, "key1", true); //$NON-NLS-1$ 1469 assertEquals("Size of subMap should be 2:", 2, subMapWithNull.size()); //$NON-NLS-1$ 1470 } 1471 1472 1473 /** 1474 * java.util.TreeMap#headMap(java.lang.Object, boolea) 1475 */ test_headMapLjava_lang_ObjectZL()1476 public void test_headMapLjava_lang_ObjectZL() { 1477 // normal case 1478 SortedMap subMap = tm.headMap(objArray[100].toString(), true); 1479 assertEquals("subMap is of incorrect size", 4, subMap.size()); 1480 subMap = tm.headMap(objArray[109].toString(), true); 1481 assertEquals("subMap is of incorrect size", 13, subMap.size()); 1482 for (int counter = 100; counter < 109; counter++) { 1483 assertTrue("SubMap contains incorrect elements", subMap.get( 1484 objArray[counter].toString()).equals(objArray[counter])); 1485 } 1486 subMap = tm.headMap(objArray[100].toString(), false); 1487 assertEquals("subMap is of incorrect size", 3, subMap.size()); 1488 assertNull(subMap.get(objArray[100].toString())); 1489 1490 // Exceptions 1491 assertEquals(0, tm.headMap("", true).size()); 1492 assertEquals(0, tm.headMap("", false).size()); 1493 1494 try { 1495 tm.headMap(null, true); 1496 fail("should throw NullPointerException"); 1497 } catch (NullPointerException e) { 1498 // expected 1499 } 1500 try { 1501 tm.headMap(null, false); 1502 fail("should throw NullPointerException"); 1503 } catch (NullPointerException e) { 1504 // expected 1505 } 1506 try { 1507 tm.headMap(new Object(), true); 1508 fail("should throw ClassCastException"); 1509 } catch (ClassCastException e) { 1510 // expected 1511 } 1512 try { 1513 tm.headMap(new Object(), false); 1514 fail("should throw ClassCastException"); 1515 } catch (ClassCastException e) { 1516 // expected 1517 } 1518 1519 // use integer elements to test 1520 TreeMap<Integer, String> treeMapInt = new TreeMap<Integer, String>(); 1521 assertEquals(0, treeMapInt.headMap(new Integer(-1), true).size()); 1522 for (int i = 0; i < 100; i++) { 1523 treeMapInt.put(new Integer(i), new Integer(i).toString()); 1524 } 1525 SortedMap<Integer, String> result = treeMapInt 1526 .headMap(new Integer(101)); 1527 assertEquals(100, result.size()); 1528 try { 1529 result.put(new Integer(101), new Integer(101).toString()); 1530 fail("should throw IllegalArgumentException"); 1531 } catch (IllegalArgumentException e) { 1532 // expected 1533 } 1534 assertEquals(100, result.size()); 1535 assertEquals(100, treeMapInt.size()); 1536 result = treeMapInt.headMap(new Integer(50), true); 1537 assertEquals(51, result.size()); 1538 result.put(new Integer(-1), new Integer(-1).toString()); 1539 assertEquals(52, result.size()); 1540 1541 treeMapInt.remove(new Integer(40)); 1542 assertEquals(100, treeMapInt.size()); 1543 assertEquals(51, result.size()); 1544 result.remove(new Integer(30)); 1545 assertEquals(99, treeMapInt.size()); 1546 assertEquals(50, result.size()); 1547 SortedMap<Integer, String> result2 = null; 1548 try { 1549 result.subMap(new Integer(-2), new Integer(100)); 1550 fail("should throw IllegalArgumentException"); 1551 } catch (IllegalArgumentException e) { 1552 // expected 1553 } 1554 try { 1555 result.subMap(new Integer(1), new Integer(100)); 1556 fail("should throw IllegalArgumentException"); 1557 } catch (IllegalArgumentException e) { 1558 // expected 1559 } 1560 result2 = result.subMap(new Integer(-2), new Integer(48)); 1561 assertEquals(47, result2.size()); 1562 1563 result2 = result.subMap(new Integer(40), new Integer(50)); 1564 assertEquals(9, result2.size()); 1565 1566 // Null Tolerable Comparator 1567 TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>( 1568 new MockComparatorNullTolerable()); 1569 treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$ 1570 treeMapWithNull.put(null, "value2"); //$NON-NLS-1$ 1571 SortedMap<String, String> subMapWithNull = treeMapWithNull.headMap( 1572 null, true); //$NON-NLS-1$ 1573 assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$ 1574 assertEquals(null, subMapWithNull.get("key1")); 1575 assertEquals("value2", subMapWithNull.get(null)); 1576 treeMapWithNull.put("key0", "value2"); 1577 treeMapWithNull.put("key3", "value3"); 1578 treeMapWithNull.put("key4", "value4"); 1579 treeMapWithNull.put("key5", "value5"); 1580 treeMapWithNull.put("key6", "value6"); 1581 assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$ 1582 subMapWithNull = treeMapWithNull.subMap(null, false, "key1", true); //$NON-NLS-1$ 1583 assertEquals("Size of subMap should be 2:", 2, subMapWithNull.size()); //$NON-NLS-1$ 1584 1585 // head map of head map 1586 NavigableMap<Integer, Object> original = new TreeMap<Integer, Object>(); 1587 for (int i = 0; i < 10; ++i) { 1588 original.put(i, new Object()); 1589 } 1590 NavigableMap<Integer, Object> mapIntObj = original.headMap(5, false); 1591 assertEquals(5, mapIntObj.size()); 1592 mapIntObj = mapIntObj.headMap(5, false); 1593 assertEquals(5, mapIntObj.size()); 1594 try { 1595 mapIntObj = mapIntObj.tailMap(5, false); 1596 fail("IllegalArgumentException expected: key falls outside restricted range"); 1597 } catch (IllegalArgumentException expected) { 1598 } 1599 1600 assertEquals(0, original.headMap(0, false).size()); 1601 } 1602 1603 /** 1604 * java.util.TreeMap#tailMap(java.lang.Object, boolea) 1605 */ test_tailMapLjava_lang_ObjectZL()1606 public void test_tailMapLjava_lang_ObjectZL() { 1607 // normal case 1608 SortedMap subMap = tm.tailMap(objArray[100].toString(), true); 1609 assertEquals("subMap is of incorrect size", 997, subMap.size()); 1610 subMap = tm.tailMap(objArray[109].toString(), true); 1611 assertEquals("subMap is of incorrect size", 988, subMap.size()); 1612 for (int counter = 119; counter > 110; counter--) { 1613 assertTrue("SubMap contains incorrect elements", subMap.get( 1614 objArray[counter].toString()).equals(objArray[counter])); 1615 } 1616 subMap = tm.tailMap(objArray[100].toString(), false); 1617 assertEquals("subMap is of incorrect size", 996, subMap.size()); 1618 assertNull(subMap.get(objArray[100].toString())); 1619 1620 // Exceptions 1621 assertEquals(1000, tm.tailMap("", true).size()); 1622 assertEquals(1000, tm.tailMap("", false).size()); 1623 1624 try { 1625 tm.tailMap(null, true); 1626 fail("should throw NullPointerException"); 1627 } catch (NullPointerException e) { 1628 // expected 1629 } 1630 try { 1631 tm.tailMap(null, false); 1632 fail("should throw NullPointerException"); 1633 } catch (NullPointerException e) { 1634 // expected 1635 } 1636 try { 1637 tm.tailMap(new Object(), true); 1638 fail("should throw ClassCastException"); 1639 } catch (ClassCastException e) { 1640 // expected 1641 } 1642 try { 1643 tm.tailMap(new Object(), false); 1644 fail("should throw ClassCastException"); 1645 } catch (ClassCastException e) { 1646 // expected 1647 } 1648 1649 // use integer elements to test 1650 TreeMap<Integer, String> treeMapInt = new TreeMap<Integer, String>(); 1651 assertEquals(0, treeMapInt.tailMap(new Integer(-1), true).size()); 1652 for (int i = 0; i < 100; i++) { 1653 treeMapInt.put(new Integer(i), new Integer(i).toString()); 1654 } 1655 SortedMap<Integer, String> result = treeMapInt.tailMap(new Integer(1)); 1656 assertEquals(99, result.size()); 1657 try { 1658 result.put(new Integer(-1), new Integer(-1).toString()); 1659 fail("should throw IllegalArgumentException"); 1660 } catch (IllegalArgumentException e) { 1661 // expected 1662 } 1663 assertEquals(99, result.size()); 1664 assertEquals(100, treeMapInt.size()); 1665 result = treeMapInt.tailMap(new Integer(50), true); 1666 assertEquals(50, result.size()); 1667 result.put(new Integer(101), new Integer(101).toString()); 1668 assertEquals(51, result.size()); 1669 1670 treeMapInt.remove(new Integer(60)); 1671 assertEquals(100, treeMapInt.size()); 1672 assertEquals(50, result.size()); 1673 result.remove(new Integer(70)); 1674 assertEquals(99, treeMapInt.size()); 1675 assertEquals(49, result.size()); 1676 SortedMap<Integer, String> result2 = null; 1677 try { 1678 result2 = result.subMap(new Integer(-2), new Integer(100)); 1679 fail("should throw IllegalArgumentException"); 1680 } catch (IllegalArgumentException e) { 1681 // expected 1682 } 1683 result2 = result.subMap(new Integer(60), new Integer(70)); 1684 assertEquals(9, result2.size()); 1685 1686 // Null Tolerable Comparator 1687 TreeMap<String, String> treeMapWithNull = new TreeMap<String, String>( 1688 new MockComparatorNullTolerable()); 1689 treeMapWithNull.put("key1", "value1"); //$NON-NLS-1$ //$NON-NLS-2$ 1690 treeMapWithNull.put(null, "value2"); //$NON-NLS-1$ 1691 SortedMap<String, String> subMapWithNull = treeMapWithNull.tailMap( 1692 "key1", true); //$NON-NLS-1$ 1693 assertEquals("Size of subMap should be 1:", 1, subMapWithNull.size()); //$NON-NLS-1$ 1694 assertEquals("value1", subMapWithNull.get("key1")); 1695 assertEquals(null, subMapWithNull.get(null)); 1696 treeMapWithNull.put("key0", "value2"); 1697 treeMapWithNull.put("key3", "value3"); 1698 treeMapWithNull.put("key4", "value4"); 1699 treeMapWithNull.put("key5", "value5"); 1700 treeMapWithNull.put("key6", "value6"); 1701 assertEquals("Size of subMap should be 5:", 5, subMapWithNull.size()); //$NON-NLS-1$ 1702 subMapWithNull = treeMapWithNull.subMap(null, false, "key1", true); //$NON-NLS-1$ 1703 assertEquals("Size of subMap should be 2:", 2, subMapWithNull.size()); //$NON-NLS-1$ 1704 1705 // tail map of tail map 1706 NavigableMap<Integer, Object> original = new TreeMap<Integer, Object>(); 1707 for (int i = 0; i < 10; ++i) { 1708 original.put(i, new Object()); 1709 } 1710 NavigableMap<Integer, Object> mapIntObj = original.tailMap(5, false); 1711 assertEquals(4, mapIntObj.size()); 1712 mapIntObj = mapIntObj.tailMap(5, false); 1713 assertEquals(4, mapIntObj.size()); 1714 try { 1715 mapIntObj = mapIntObj.headMap(5, false); 1716 fail("IllegalArgumentException expected: key falls outside restricted range"); 1717 } catch (IllegalArgumentException expected) { 1718 } 1719 1720 assertEquals(0, original.headMap(0, false).size()); 1721 } 1722 test_descendingMap_subMap()1723 public void test_descendingMap_subMap() throws Exception { 1724 TreeMap<Integer, Object> tm = new TreeMap<Integer, Object>(); 1725 for (int i = 0; i < 10; ++i) { 1726 tm.put(i, new Object()); 1727 } 1728 NavigableMap<Integer, Object> descMap = tm.descendingMap(); 1729 assertEquals(7, descMap.subMap(8, true, 1, false).size()); 1730 assertEquals(4, descMap.headMap(6, true).size()); 1731 assertEquals(2, descMap.tailMap(2, false).size()); 1732 1733 // sub map of sub map of descendingMap 1734 NavigableMap<Integer, Object> mapIntObj = new TreeMap<Integer, Object>(); 1735 for (int i = 0; i < 10; ++i) { 1736 mapIntObj.put(i, new Object()); 1737 } 1738 mapIntObj = mapIntObj.descendingMap(); 1739 NavigableMap<Integer, Object> subMapIntObj = mapIntObj.subMap(9, true, 1740 5, false); 1741 assertEquals(4, subMapIntObj.size()); 1742 subMapIntObj = subMapIntObj.subMap(9, true, 5, false); 1743 assertEquals(4, subMapIntObj.size()); 1744 subMapIntObj = subMapIntObj.subMap(6, false, 5, false); 1745 assertEquals(0, subMapIntObj.size()); 1746 1747 subMapIntObj = mapIntObj.headMap(5, false); 1748 assertEquals(4, subMapIntObj.size()); 1749 subMapIntObj = subMapIntObj.headMap(5, false); 1750 assertEquals(4, subMapIntObj.size()); 1751 try { 1752 subMapIntObj = subMapIntObj.tailMap(5, false); 1753 fail("IllegalArgumentException expected: key falls outside restricted range"); 1754 } catch (IllegalArgumentException expected) { 1755 } 1756 1757 subMapIntObj = mapIntObj.tailMap(5, false); 1758 assertEquals(5, subMapIntObj.size()); 1759 subMapIntObj = subMapIntObj.tailMap(5, false); 1760 assertEquals(5, subMapIntObj.size()); 1761 try { 1762 subMapIntObj = subMapIntObj.headMap(5, false); 1763 fail("IllegalArgumentException expected: key falls outside restricted range"); 1764 } catch (IllegalArgumentException expected) { 1765 } 1766 } 1767 illegalFirstNullKeyMapTester(NavigableMap<String, String> map)1768 private void illegalFirstNullKeyMapTester(NavigableMap<String, String> map) { 1769 try { 1770 map.get(null); 1771 fail("Should throw NullPointerException"); 1772 } catch (NullPointerException e) { 1773 // expected 1774 } 1775 try { 1776 map.put("NormalKey", "value"); 1777 fail("Should throw NullPointerException"); 1778 } catch (NullPointerException e) { 1779 // expected 1780 } 1781 Set<String> keySet = map.keySet(); 1782 assertTrue(!keySet.isEmpty()); 1783 assertEquals(1, keySet.size()); 1784 for (String key : keySet) { 1785 assertEquals(key, null); 1786 try { 1787 map.get(key); 1788 fail("Should throw NullPointerException"); 1789 } catch (NullPointerException e) { 1790 // ignore 1791 } 1792 } 1793 Set<Entry<String, String>> entrySet = map.entrySet(); 1794 assertTrue(!entrySet.isEmpty()); 1795 assertEquals(1, entrySet.size()); 1796 for (Entry<String, String> entry : entrySet) { 1797 assertEquals(null, entry.getKey()); 1798 assertEquals("NullValue", entry.getValue()); 1799 } 1800 Collection<String> values = map.values(); 1801 assertTrue(!values.isEmpty()); 1802 assertEquals(1, values.size()); 1803 for (String value : values) { 1804 assertEquals("NullValue", value); 1805 } 1806 1807 try { 1808 map.headMap(null, true); 1809 fail("Should throw NullPointerException"); 1810 } catch (NullPointerException e) { 1811 // ignore 1812 } 1813 try { 1814 map.headMap(null, false); 1815 fail("Should throw NullPointerException"); 1816 } catch (NullPointerException e) { 1817 // ignore 1818 } 1819 1820 try { 1821 map.subMap(null, false, null, false); 1822 fail("Should throw NullPointerException"); 1823 } catch (NullPointerException e) { 1824 // ignore 1825 } 1826 try { 1827 map.subMap(null, true, null, true); 1828 fail("Should throw NullPointerException"); 1829 } catch (NullPointerException e) { 1830 // ignore 1831 } 1832 try { 1833 map.tailMap(null, true); 1834 fail("Should throw NullPointerException"); 1835 } catch (NullPointerException e) { 1836 // ignore 1837 } 1838 try { 1839 map.tailMap(null, false); 1840 fail("Should throw NullPointerException"); 1841 } catch (NullPointerException e) { 1842 // ignore 1843 } 1844 } 1845 1846 /** 1847 * Tests equals() method. 1848 * Tests that no ClassCastException will be thrown in all cases. 1849 * Regression test for HARMONY-1639. 1850 */ test_equals()1851 public void test_equals() throws Exception { 1852 // comparing TreeMaps with different object types 1853 Map m1 = new TreeMap(); 1854 Map m2 = new TreeMap(); 1855 m1.put("key1", "val1"); 1856 m1.put("key2", "val2"); 1857 m2.put(new Integer(1), "val1"); 1858 m2.put(new Integer(2), "val2"); 1859 assertFalse("Maps should not be equal 1", m1.equals(m2)); 1860 assertFalse("Maps should not be equal 2", m2.equals(m1)); 1861 1862 // comparing TreeMap with HashMap 1863 m1 = new TreeMap(); 1864 m2 = new HashMap(); 1865 m1.put("key", "val"); 1866 m2.put(new Object(), "val"); 1867 assertFalse("Maps should not be equal 3", m1.equals(m2)); 1868 assertFalse("Maps should not be equal 4", m2.equals(m1)); 1869 } 1870 test_invalidKeys()1871 public void test_invalidKeys() throws Exception { 1872 // comparing TreeMaps with not-comparable objects inside 1873 TreeMap m1 = new TreeMap(); 1874 try { 1875 m1.put(new Object(), "val1"); 1876 fail("ClassCastException expected"); 1877 } catch (ClassCastException expected) { 1878 1879 } 1880 } 1881 test_remove_from_iterator()1882 public void test_remove_from_iterator() throws Exception { 1883 Set set = tm.keySet(); 1884 Iterator iter = set.iterator(); 1885 iter.next(); 1886 iter.remove(); 1887 try { 1888 iter.remove(); 1889 fail("should throw IllegalStateException"); 1890 } catch (IllegalStateException e) { 1891 // expected 1892 } 1893 } 1894 1895 /** 1896 * Tests entrySet().contains() method behaviour with respect to entries 1897 * with null values. 1898 * Regression test for HARMONY-5788. 1899 */ test_entrySet_contains()1900 public void test_entrySet_contains() throws Exception { 1901 TreeMap master = new TreeMap<String, String>(); 1902 TreeMap test_map = new TreeMap<String, String>(); 1903 1904 master.put("null", null); 1905 Object[] entry = master.entrySet().toArray(); 1906 assertFalse("Empty map should not contain the null-valued entry", 1907 test_map.entrySet().contains(entry[0])); 1908 1909 Map<String, String> submap = test_map.subMap("a", "z"); 1910 entry = master.entrySet().toArray(); 1911 assertFalse("Empty submap should not contain the null-valued entry", 1912 submap.entrySet().contains(entry[0])); 1913 1914 test_map.put("null", null); 1915 assertTrue("entrySet().containsAll(...) should work with null values", 1916 test_map.entrySet().containsAll(master.entrySet())); 1917 1918 master.clear(); 1919 master.put("null", '0'); 1920 entry = master.entrySet().toArray(); 1921 assertFalse("Null-valued entry should not equal non-null-valued entry", 1922 test_map.entrySet().contains(entry[0])); 1923 } 1924 test_iterator_next_()1925 public void test_iterator_next_() { 1926 Map m = tm.subMap("0", "1"); 1927 Iterator it = m.entrySet().iterator(); 1928 assertEquals("0=0", it.next().toString()); 1929 while (it.hasNext()) { 1930 } 1931 try { 1932 it.next(); 1933 fail("should throw java.util.NoSuchElementException"); 1934 } catch (Exception e) { 1935 assertTrue(e instanceof java.util.NoSuchElementException); 1936 } 1937 } 1938 test_empty_subMap()1939 public void test_empty_subMap() throws Exception { 1940 TreeMap<Float, List<Integer>> tm = new TreeMap<Float, List<Integer>>(); 1941 SortedMap<Float, List<Integer>> sm = tm.tailMap(1.1f); 1942 assertTrue(sm.values().size() == 0); 1943 } 1944 1945 public static TreeMap treeMap = new TreeMap(); 1946 test_values_1()1947 public void test_values_1() { 1948 treeMap.put("firstKey", "firstValue"); 1949 treeMap.put("secondKey", "secondValue"); 1950 treeMap.put("thirdKey", "thirdValue"); 1951 Object firstKey = treeMap.firstKey(); 1952 SortedMap subMap = ((SortedMap) treeMap).subMap(firstKey, firstKey); 1953 Iterator iter = subMap.values().iterator(); 1954 } 1955 1956 /** 1957 * Sets up the fixture, for example, open a network connection. This method 1958 * is called before a test is executed. 1959 */ 1960 @Override setUp()1961 protected void setUp() { 1962 tm = new TreeMap(); 1963 for (int i = 0; i < objArray.length; i++) { 1964 Object x = objArray[i] = new Integer(i); 1965 tm.put(x.toString(), x); 1966 } 1967 } 1968 } 1969