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 tests.api.java.util; 19 20 import dalvik.annotation.TestTargetNew; 21 import dalvik.annotation.TestTargets; 22 import dalvik.annotation.TestLevel; 23 import dalvik.annotation.TestTargetClass; 24 25 import java.util.ArrayList; 26 import java.util.Arrays; 27 import java.util.Collection; 28 import java.util.ConcurrentModificationException; 29 import java.util.Enumeration; 30 import java.util.HashSet; 31 import java.util.Hashtable; 32 import java.util.Iterator; 33 import java.util.Map; 34 import java.util.NoSuchElementException; 35 import java.util.Set; 36 import java.util.TreeMap; 37 import java.util.Vector; 38 import java.util.Collections; 39 40 import tests.api.java.util.HashMapTest.ReusableKey; 41 import tests.support.Support_MapTest2; 42 import tests.support.Support_UnmodifiableCollectionTest; 43 44 @TestTargetClass(Hashtable.class) 45 public class HashtableTest extends junit.framework.TestCase { 46 47 private Hashtable ht10; 48 49 private Hashtable ht100; 50 51 private Hashtable htfull; 52 53 private Vector keyVector; 54 55 private Vector elmVector; 56 57 private String h10sVal; 58 59 /** 60 * @tests java.util.Hashtable#Hashtable() 61 */ 62 @TestTargetNew( 63 level = TestLevel.COMPLETE, 64 notes = "", 65 method = "Hashtable", 66 args = {} 67 ) test_Constructor()68 public void test_Constructor() { 69 // Test for method java.util.Hashtable() 70 new Support_MapTest2(new Hashtable()).runTest(); 71 72 Hashtable h = new Hashtable(); 73 74 assertEquals("Created incorrect hashtable", 0, h.size()); 75 } 76 77 /** 78 * @tests java.util.Hashtable#Hashtable(int) 79 */ 80 @TestTargetNew( 81 level = TestLevel.COMPLETE, 82 notes = "", 83 method = "Hashtable", 84 args = {int.class} 85 ) test_ConstructorI()86 public void test_ConstructorI() { 87 // Test for method java.util.Hashtable(int) 88 Hashtable h = new Hashtable(9); 89 90 assertEquals("Created incorrect hashtable", 0, h.size()); 91 92 Hashtable empty = new Hashtable(0); 93 assertNull("Empty hashtable access", empty.get("nothing")); 94 empty.put("something", "here"); 95 assertTrue("cannot get element", empty.get("something") == "here"); 96 97 try { 98 new Hashtable(-1); 99 fail("IllegalArgumentException expected"); 100 } catch (IllegalArgumentException e) { 101 //expected 102 } 103 } 104 105 /** 106 * @tests java.util.Hashtable#Hashtable(int, float) 107 */ 108 @TestTargetNew( 109 level = TestLevel.COMPLETE, 110 notes = "", 111 method = "Hashtable", 112 args = {int.class, float.class} 113 ) test_ConstructorIF()114 public void test_ConstructorIF() { 115 // Test for method java.util.Hashtable(int, float) 116 Hashtable h = new java.util.Hashtable(10, 0.5f); 117 assertEquals("Created incorrect hashtable", 0, h.size()); 118 119 Hashtable empty = new Hashtable(0, 0.75f); 120 assertNull("Empty hashtable access", empty.get("nothing")); 121 empty.put("something", "here"); 122 assertTrue("cannot get element", empty.get("something") == "here"); 123 124 try { 125 new Hashtable(-1, 0.75f); 126 fail("IllegalArgumentException expected"); 127 } catch (IllegalArgumentException e) { 128 //expected 129 } 130 131 try { 132 new Hashtable(0, -0.75f); 133 fail("IllegalArgumentException expected"); 134 } catch (IllegalArgumentException e) { 135 //expected 136 } 137 } 138 139 /** 140 * @tests java.util.Hashtable#Hashtable(java.util.Map) 141 */ 142 @TestTargetNew( 143 level = TestLevel.COMPLETE, 144 notes = "", 145 method = "Hashtable", 146 args = {java.util.Map.class} 147 ) test_ConstructorLjava_util_Map()148 public void test_ConstructorLjava_util_Map() { 149 // Test for method java.util.Hashtable(java.util.Map) 150 Map map = new TreeMap(); 151 Object firstVal = "Gabba"; 152 Object secondVal = new Integer(5); 153 map.put("Gah", firstVal); 154 map.put("Ooga", secondVal); 155 Hashtable ht = new Hashtable(map); 156 assertTrue("a) Incorrect Hashtable constructed", 157 ht.get("Gah") == firstVal); 158 assertTrue("b) Incorrect Hashtable constructed", 159 ht.get("Ooga") == secondVal); 160 161 try { 162 new Hashtable(null); 163 fail("NullPointerException expected"); 164 } catch (NullPointerException e) { 165 //expected 166 } 167 } 168 169 /** 170 * @tests java.util.Hashtable#Hashtable(java.util.Map) 171 */ 172 @TestTargetNew( 173 level = TestLevel.COMPLETE, 174 notes = "", 175 method = "Hashtable", 176 args = {java.util.Map.class} 177 ) test_ConversionConstructorNullValue()178 public void test_ConversionConstructorNullValue() { 179 Map<String, Void> map = Collections.singletonMap("Dog", null); 180 try { 181 new Hashtable<String, Void>(map); 182 fail("NullPointerException expected"); 183 } catch (NullPointerException e) { 184 //expected 185 } 186 } 187 /** 188 * @tests java.util.Hashtable#clear() 189 */ 190 @TestTargetNew( 191 level = TestLevel.COMPLETE, 192 notes = "", 193 method = "clear", 194 args = {} 195 ) test_clear()196 public void test_clear() { 197 // Test for method void java.util.Hashtable.clear() 198 Hashtable h = hashtableClone(htfull); 199 h.clear(); 200 assertEquals("Hashtable was not cleared", 0, h.size()); 201 Enumeration el = h.elements(); 202 Enumeration keys = h.keys(); 203 assertTrue("Hashtable improperly cleared", !el.hasMoreElements() 204 && !(keys.hasMoreElements())); 205 } 206 207 /** 208 * @tests java.util.Hashtable#clone() 209 */ 210 @TestTargetNew( 211 level = TestLevel.COMPLETE, 212 notes = "", 213 method = "clone", 214 args = {} 215 ) test_clone()216 public void test_clone() { 217 // Test for method java.lang.Object java.util.Hashtable.clone() 218 219 Hashtable h = (Hashtable) htfull.clone(); 220 assertTrue("Clone different size than original", h.size() == htfull 221 .size()); 222 223 Enumeration org = htfull.keys(); 224 Enumeration cpy = h.keys(); 225 226 String okey, ckey; 227 while (org.hasMoreElements()) { 228 assertTrue("Key comparison failed", (okey = (String) org 229 .nextElement()).equals(ckey = (String) cpy.nextElement())); 230 assertTrue("Value comparison failed", ((String) htfull.get(okey)) 231 .equals((String) h.get(ckey))); 232 } 233 assertTrue("Copy has more keys than original", !cpy.hasMoreElements()); 234 } 235 236 /** 237 * @tests java.util.Hashtable#contains(java.lang.Object) 238 */ 239 @TestTargetNew( 240 level = TestLevel.COMPLETE, 241 notes = "", 242 method = "contains", 243 args = {java.lang.Object.class} 244 ) test_containsLjava_lang_Object()245 public void test_containsLjava_lang_Object() { 246 // Test for method boolean 247 // java.util.Hashtable.contains(java.lang.Object) 248 assertTrue("Element not found", ht10.contains("Val 7")); 249 assertTrue("Invalid element found", !ht10.contains("ZZZZZZZZZZZZZZZZ")); 250 251 try { 252 ht10.contains(null); 253 fail("NullPointerException expected"); 254 } catch (NullPointerException e) { 255 //expected 256 } 257 } 258 259 /** 260 * @tests java.util.Hashtable#containsKey(java.lang.Object) 261 */ 262 @TestTargetNew( 263 level = TestLevel.COMPLETE, 264 notes = "", 265 method = "containsKey", 266 args = {java.lang.Object.class} 267 ) test_containsKeyLjava_lang_Object()268 public void test_containsKeyLjava_lang_Object() { 269 // Test for method boolean 270 // java.util.Hashtable.containsKey(java.lang.Object) 271 272 assertTrue("Failed to find key", htfull.containsKey("FKey 4")); 273 assertTrue("Failed to find key", !htfull.containsKey("FKey 99")); 274 275 try { 276 htfull.containsKey(null); 277 fail("NullPointerException expected"); 278 } catch (NullPointerException e) { 279 //expected 280 } 281 } 282 283 /** 284 * @tests java.util.Hashtable#containsValue(java.lang.Object) 285 */ 286 @TestTargetNew( 287 level = TestLevel.COMPLETE, 288 notes = "", 289 method = "containsValue", 290 args = {java.lang.Object.class} 291 ) test_containsValueLjava_lang_Object()292 public void test_containsValueLjava_lang_Object() { 293 // Test for method boolean 294 // java.util.Hashtable.containsValue(java.lang.Object) 295 Enumeration e = elmVector.elements(); 296 while (e.hasMoreElements()) 297 assertTrue("Returned false for valid value", ht10.containsValue(e 298 .nextElement())); 299 assertTrue("Returned true for invalid value", !ht10 300 .containsValue(new Object())); 301 302 try { 303 ht10.containsValue(null); 304 fail("NullPointerException expected"); 305 } catch (NullPointerException ee) { 306 //expected 307 } 308 } 309 310 /** 311 * @tests java.util.Hashtable#elements() 312 */ 313 @TestTargetNew( 314 level = TestLevel.COMPLETE, 315 notes = "", 316 method = "elements", 317 args = {} 318 ) test_elements()319 public void test_elements() { 320 // Test for method java.util.Enumeration java.util.Hashtable.elements() 321 Enumeration elms = ht10.elements(); 322 int i = 0; 323 while (elms.hasMoreElements()) { 324 String s = (String) elms.nextElement(); 325 assertTrue("Missing key from enumeration", elmVector.contains(s)); 326 ++i; 327 } 328 329 assertEquals("All keys not retrieved", 10, ht10.size()); 330 } 331 332 // BEGIN android-removed 333 // implementation dependent 334 // /** 335 // * @tests java.util.Hashtable#elements() 336 // */ 337 // @TestTargetNew( 338 // level = TestLevel.COMPLETE, 339 // notes = "", 340 // method = "elements", 341 // args = {} 342 // ) 343 // public void test_elements_subtest0() { 344 // // this is the reference implementation behavior 345 // final Hashtable ht = new Hashtable(7); 346 // ht.put("1", "a"); 347 // // these three elements hash to the same bucket in a 7 element Hashtable 348 // ht.put("2", "b"); 349 // ht.put("9", "c"); 350 // ht.put("12", "d"); 351 // // Hashtable looks like: 352 // // 0: "1" 353 // // 1: "12" -> "9" -> "2" 354 // Enumeration en = ht.elements(); 355 // // cache the first entry 356 // en.hasMoreElements(); 357 // ht.remove("12"); 358 // ht.remove("9"); 359 // boolean exception = false; 360 // try { 361 // // cached "12" 362 // Object result = en.nextElement(); 363 // assertNull("unexpected: " + result, result); 364 // // next is removed "9" 365 // result = en.nextElement(); 366 // assertNull("unexpected: " + result, result); 367 // result = en.nextElement(); 368 // assertTrue("unexpected: " + result, "b".equals(result)); 369 // } catch (NoSuchElementException e) { 370 // exception = true; 371 // } 372 // assertTrue("unexpected NoSuchElementException", !exception); 373 // } 374 // END android-removed 375 376 /** 377 * @tests java.util.Hashtable#entrySet() 378 */ 379 @TestTargetNew( 380 level = TestLevel.COMPLETE, 381 notes = "", 382 method = "entrySet", 383 args = {} 384 ) test_entrySet()385 public void test_entrySet() { 386 // Test for method java.util.Set java.util.Hashtable.entrySet() 387 Set s = ht10.entrySet(); 388 Set s2 = new HashSet(); 389 Iterator i = s.iterator(); 390 while (i.hasNext()) 391 s2.add(((Map.Entry) i.next()).getValue()); 392 Enumeration e = elmVector.elements(); 393 while (e.hasMoreElements()) 394 assertTrue("Returned incorrect entry set", s2.contains(e 395 .nextElement())); 396 // BEGIN android-removed 397 // implementation dependent 398 // assertEquals("Not synchronized", 399 // "java.util.Collections$SynchronizedSet", s.getClass().getName()); 400 // END android-removed 401 402 boolean exception = false; 403 try { 404 ((Map.Entry) ht10.entrySet().iterator().next()).setValue(null); 405 } catch (NullPointerException e1) { 406 exception = true; 407 } 408 assertTrue( 409 "Should not be able to assign null to a Hashtable entrySet() Map.Entry", 410 exception); 411 } 412 413 /** 414 * @tests java.util.Hashtable#equals(java.lang.Object) 415 */ 416 @TestTargetNew( 417 level = TestLevel.COMPLETE, 418 notes = "", 419 method = "equals", 420 args = {java.lang.Object.class} 421 ) test_equalsLjava_lang_Object()422 public void test_equalsLjava_lang_Object() { 423 // Test for method boolean java.util.Hashtable.equals(java.lang.Object) 424 Hashtable h = hashtableClone(ht10); 425 assertTrue("Returned false for equal tables", ht10.equals(h)); 426 assertTrue("Returned true for unequal tables", !ht10.equals(htfull)); 427 } 428 429 /** 430 * @tests java.util.Hashtable#get(java.lang.Object) 431 */ 432 @TestTargetNew( 433 level = TestLevel.COMPLETE, 434 notes = "", 435 method = "get", 436 args = {java.lang.Object.class} 437 ) test_getLjava_lang_Object()438 public void test_getLjava_lang_Object() { 439 // Test for method java.lang.Object 440 // java.util.Hashtable.get(java.lang.Object) 441 Hashtable h = hashtableClone(htfull); 442 assertEquals("Could not retrieve element", "FVal 2", ((String) h.get("FKey 2")) 443 ); 444 445 // BEGIN android-removed 446 // implementation dependent 447 // // Regression for HARMONY-262 448 // ReusableKey k = new ReusableKey(); 449 // Hashtable h2 = new Hashtable(); 450 // k.setKey(1); 451 // h2.put(k, "value1"); 452 // 453 // k.setKey(13); 454 // assertNull(h2.get(k)); 455 // 456 // k.setKey(12); 457 // assertNull(h2.get(k)); 458 // 459 // try { 460 // h2.get(null); 461 // fail("NullPointerException expected"); 462 // } catch (NullPointerException e) { 463 // //expected 464 // } 465 // END android-removed 466 } 467 468 /** 469 * @tests java.util.Hashtable#hashCode() 470 */ 471 @TestTargetNew( 472 level = TestLevel.COMPLETE, 473 notes = "", 474 method = "hashCode", 475 args = {} 476 ) test_hashCode()477 public void test_hashCode() { 478 // Test for method int java.util.Hashtable.hashCode() 479 Set entrySet = ht10.entrySet(); 480 Iterator iterator = entrySet.iterator(); 481 int expectedHash; 482 for (expectedHash = 0; iterator.hasNext(); expectedHash += iterator 483 .next().hashCode()) 484 ; 485 assertTrue("Incorrect hashCode returned. Wanted: " + expectedHash 486 + " got: " + ht10.hashCode(), expectedHash == ht10.hashCode()); 487 } 488 489 /** 490 * @tests java.util.Hashtable#isEmpty() 491 */ 492 @TestTargetNew( 493 level = TestLevel.COMPLETE, 494 notes = "", 495 method = "isEmpty", 496 args = {} 497 ) test_isEmpty()498 public void test_isEmpty() { 499 // Test for method boolean java.util.Hashtable.isEmpty() 500 501 assertTrue("isEmpty returned incorrect value", !ht10.isEmpty()); 502 assertTrue("isEmpty returned incorrect value", 503 new java.util.Hashtable().isEmpty()); 504 505 final Hashtable ht = new Hashtable(); 506 ht.put("0", ""); 507 Thread t1 = new Thread() { 508 public void run() { 509 while (!ht.isEmpty()) 510 ; 511 ht.put("final", ""); 512 } 513 }; 514 t1.start(); 515 for (int i = 1; i < 10000; i++) { 516 synchronized (ht) { 517 ht.remove(String.valueOf(i - 1)); 518 ht.put(String.valueOf(i), ""); 519 } 520 int size; 521 if ((size = ht.size()) != 1) { 522 String result = "Size is not 1: " + size + " " + ht; 523 // terminate the thread 524 ht.clear(); 525 fail(result); 526 } 527 } 528 // terminate the thread 529 ht.clear(); 530 } 531 532 /** 533 * @tests java.util.Hashtable#keys() 534 */ 535 @TestTargetNew( 536 level = TestLevel.COMPLETE, 537 notes = "", 538 method = "keys", 539 args = {} 540 ) test_keys()541 public void test_keys() { 542 // Test for method java.util.Enumeration java.util.Hashtable.keys() 543 544 Enumeration keys = ht10.keys(); 545 int i = 0; 546 while (keys.hasMoreElements()) { 547 String s = (String) keys.nextElement(); 548 assertTrue("Missing key from enumeration", keyVector.contains(s)); 549 ++i; 550 } 551 552 assertEquals("All keys not retrieved", 10, ht10.size()); 553 } 554 555 /** 556 * @tests java.util.Hashtable#keys() 557 */ 558 @TestTargetNew( 559 level = TestLevel.PARTIAL_COMPLETE, 560 notes = "", 561 method = "keys", 562 args = {} 563 ) test_keys_subtest0()564 public void test_keys_subtest0() { 565 // this is the reference implementation behavior 566 final Hashtable ht = new Hashtable(3); 567 ht.put("initial", ""); 568 Enumeration en = ht.keys(); 569 en.hasMoreElements(); 570 ht.remove("initial"); 571 boolean exception = false; 572 try { 573 Object result = en.nextElement(); 574 assertTrue("unexpected: " + result, "initial".equals(result)); 575 } catch (NoSuchElementException e) { 576 exception = true; 577 } 578 assertTrue("unexpected NoSuchElementException", !exception); 579 } 580 581 /** 582 * @tests java.util.Hashtable#keySet() 583 */ 584 @TestTargetNew( 585 level = TestLevel.COMPLETE, 586 notes = "", 587 method = "keySet", 588 args = {} 589 ) test_keySet()590 public void test_keySet() { 591 // Test for method java.util.Set java.util.Hashtable.keySet() 592 Set s = ht10.keySet(); 593 Enumeration e = keyVector.elements(); 594 while (e.hasMoreElements()) 595 assertTrue("Returned incorrect key set", s 596 .contains(e.nextElement())); 597 598 // BEGIN android-removed 599 // implementation dependent 600 // assertEquals("Not synchronized", 601 // "java.util.Collections$SynchronizedSet", s.getClass().getName()); 602 // END android-removed 603 604 Map map = new Hashtable(101); 605 map.put(new Integer(1), "1"); 606 map.put(new Integer(102), "102"); 607 map.put(new Integer(203), "203"); 608 Iterator it = map.keySet().iterator(); 609 Integer remove1 = (Integer) it.next(); 610 it.remove(); 611 Integer remove2 = (Integer) it.next(); 612 it.remove(); 613 ArrayList list = new ArrayList(Arrays.asList(new Integer[] { 614 new Integer(1), new Integer(102), new Integer(203) })); 615 list.remove(remove1); 616 list.remove(remove2); 617 assertTrue("Wrong result", it.next().equals(list.get(0))); 618 assertEquals("Wrong size", 1, map.size()); 619 assertTrue("Wrong contents", map.keySet().iterator().next().equals( 620 list.get(0))); 621 622 Map map2 = new Hashtable(101); 623 map2.put(new Integer(1), "1"); 624 map2.put(new Integer(4), "4"); 625 Iterator it2 = map2.keySet().iterator(); 626 Integer remove3 = (Integer) it2.next(); 627 Integer next; 628 if (remove3.intValue() == 1) 629 next = new Integer(4); 630 else 631 next = new Integer(1); 632 it2.hasNext(); 633 it2.remove(); 634 assertTrue("Wrong result 2", it2.next().equals(next)); 635 assertEquals("Wrong size 2", 1, map2.size()); 636 assertTrue("Wrong contents 2", map2.keySet().iterator().next().equals( 637 next)); 638 } 639 640 /** 641 * @tests java.util.Hashtable#keySet() 642 */ 643 @TestTargetNew( 644 level = TestLevel.PARTIAL_COMPLETE, 645 notes = "", 646 method = "keySet", 647 args = {} 648 ) test_keySet_subtest0()649 public void test_keySet_subtest0() { 650 Set s1 = ht10.keySet(); 651 assertTrue("should contain key", s1.remove("Key 0")); 652 assertTrue("should not contain key", !s1.remove("Key 0")); 653 654 final int iterations = 10000; 655 final Hashtable ht = new Hashtable(); 656 Thread t1 = new Thread() { 657 public void run() { 658 for (int i = 0; i < iterations; i++) { 659 ht.put(String.valueOf(i), ""); 660 ht.remove(String.valueOf(i)); 661 } 662 } 663 }; 664 t1.start(); 665 Set set = ht.keySet(); 666 for (int i = 0; i < iterations; i++) { 667 Iterator it = set.iterator(); 668 try { 669 it.next(); 670 it.remove(); 671 int size; 672 // ensure removing with the iterator doesn't corrupt the 673 // Hashtable 674 if ((size = ht.size()) < 0) { 675 fail("invalid size: " + size); 676 } 677 } catch (NoSuchElementException e) { 678 } catch (ConcurrentModificationException e) { 679 } 680 } 681 } 682 683 // BEGIN android-removed 684 // implementation dependent 685 // /** 686 // * @tests java.util.Hashtable#keySet() 687 // */ 688 // @TestTargetNew( 689 // level = TestLevel.PARTIAL_COMPLETE, 690 // notes = "", 691 // method = "keySet", 692 // args = {} 693 // ) 694 // public void test_keySet_subtest1() { 695 // // this is the reference implementation behavior 696 // final Hashtable ht = new Hashtable(7); 697 // ht.put("1", "a"); 698 // // these three elements hash to the same bucket in a 7 element Hashtable 699 // ht.put("2", "b"); 700 // ht.put("9", "c"); 701 // ht.put("12", "d"); 702 // // Hashtable looks like: 703 // // 0: "1" 704 // // 1: "12" -> "9" -> "2" 705 // Enumeration en = ht.elements(); 706 // // cache the first entry 707 // en.hasMoreElements(); 708 // Iterator it = ht.keySet().iterator(); 709 // // this is mostly a copy of the test in test_elements_subtest0() 710 // // test removing with the iterator does not null the values 711 // while (it.hasNext()) { 712 // String key = (String) it.next(); 713 // if ("12".equals(key) || "9".equals(key)) { 714 // it.remove(); 715 // } 716 // } 717 // it.remove(); 718 // boolean exception = false; 719 // try { 720 // // cached "12" 721 // Object result = en.nextElement(); 722 // assertTrue("unexpected: " + result, "d".equals(result)); 723 // // next is removed "9" 724 // result = en.nextElement(); 725 // assertTrue("unexpected: " + result, "c".equals(result)); 726 // result = en.nextElement(); 727 // assertTrue("unexpected: " + result, "b".equals(result)); 728 // } catch (NoSuchElementException e) { 729 // exception = true; 730 // } 731 // assertTrue("unexpected NoSuchElementException", !exception); 732 // } 733 // END android-removed 734 735 /** 736 * @tests java.util.Hashtable#put(java.lang.Object, java.lang.Object) 737 */ 738 @TestTargetNew( 739 level = TestLevel.COMPLETE, 740 notes = "", 741 method = "put", 742 args = {java.lang.Object.class, java.lang.Object.class} 743 ) test_putLjava_lang_ObjectLjava_lang_Object()744 public void test_putLjava_lang_ObjectLjava_lang_Object() { 745 // Test for method java.lang.Object 746 // java.util.Hashtable.put(java.lang.Object, java.lang.Object) 747 Hashtable h = hashtableClone(ht100); 748 Integer key = new Integer(100); 749 h.put("Value 100", key); 750 assertTrue("Key/Value not inserted", h.size() == 1 && (h.contains(key))); 751 752 // Put into "full" table 753 h = hashtableClone(htfull); 754 h.put("Value 100", key); 755 assertTrue("Key/Value not inserted into full table", h.size() == 8 756 && (h.contains(key))); 757 758 try { 759 h.put(null, key); 760 fail("NullPointerException expected"); 761 } catch (NullPointerException e) { 762 //expected 763 } 764 765 try { 766 h.put("Value 100", null); 767 fail("NullPointerException expected"); 768 } catch (NullPointerException e) { 769 //expected 770 } 771 } 772 773 /** 774 * @tests java.util.Hashtable#putAll(java.util.Map) 775 */ 776 @TestTargetNew( 777 level = TestLevel.COMPLETE, 778 notes = "", 779 method = "putAll", 780 args = {java.util.Map.class} 781 ) test_putAllLjava_util_Map()782 public void test_putAllLjava_util_Map() { 783 // Test for method void java.util.Hashtable.putAll(java.util.Map) 784 Hashtable h = new Hashtable(); 785 h.putAll(ht10); 786 Enumeration e = keyVector.elements(); 787 while (e.hasMoreElements()) { 788 Object x = e.nextElement(); 789 assertTrue("Failed to put all elements", h.get(x).equals( 790 ht10.get(x))); 791 } 792 793 try { 794 h.putAll(null); 795 fail("NullPointerException expected"); 796 } catch (NullPointerException ee) { 797 //expected 798 } 799 } 800 801 /** 802 * @tests java.util.Hashtable#remove(java.lang.Object) 803 */ 804 @TestTargetNew( 805 level = TestLevel.COMPLETE, 806 notes = "", 807 method = "remove", 808 args = {java.lang.Object.class} 809 ) test_removeLjava_lang_Object()810 public void test_removeLjava_lang_Object() { 811 // Test for method java.lang.Object 812 // java.util.Hashtable.remove(java.lang.Object) 813 Hashtable h = hashtableClone(htfull); 814 Object k = h.remove("FKey 0"); 815 assertTrue("Remove failed", !h.containsKey("FKey 0") || k == null); 816 assertNull(h.remove("FKey 0")); 817 818 try { 819 h.remove(null); 820 fail("NullPointerException expected"); 821 } catch (NullPointerException e) { 822 //expected 823 } 824 } 825 826 /** 827 * @tests java.util.Hashtable#size() 828 */ 829 @TestTargetNew( 830 level = TestLevel.COMPLETE, 831 notes = "", 832 method = "size", 833 args = {} 834 ) test_size()835 public void test_size() { 836 // Test for method int java.util.Hashtable.size() 837 assertTrue("Returned invalid size", ht10.size() == 10 838 && (ht100.size() == 0)); 839 840 final Hashtable ht = new Hashtable(); 841 ht.put("0", ""); 842 Thread t1 = new Thread() { 843 public void run() { 844 while (ht.size() > 0) 845 ; 846 ht.put("final", ""); 847 } 848 }; 849 t1.start(); 850 for (int i = 1; i < 10000; i++) { 851 synchronized (ht) { 852 ht.remove(String.valueOf(i - 1)); 853 ht.put(String.valueOf(i), ""); 854 } 855 int size; 856 if ((size = ht.size()) != 1) { 857 String result = "Size is not 1: " + size + " " + ht; 858 // terminate the thread 859 ht.clear(); 860 fail(result); 861 } 862 } 863 // terminate the thread 864 ht.clear(); 865 } 866 867 /** 868 * @tests java.util.Hashtable#toString() 869 */ 870 @TestTargetNew( 871 level = TestLevel.COMPLETE, 872 notes = "", 873 method = "toString", 874 args = {} 875 ) test_toString()876 public void test_toString() { 877 // Test for method java.lang.String java.util.Hashtable.toString() 878 Hashtable h = new Hashtable(); 879 assertEquals("Incorrect toString for Empty table", 880 "{}", h.toString()); 881 882 h.put("one", "1"); 883 h.put("two", h); 884 h.put(h, "3"); 885 h.put(h, h); 886 String result = h.toString(); 887 assertTrue("should contain self ref", result.indexOf("(this") > -1); 888 } 889 890 /** 891 * @tests java.util.Hashtable#values() 892 */ 893 @TestTargetNew( 894 level = TestLevel.COMPLETE, 895 notes = "", 896 method = "values", 897 args = {} 898 ) test_values()899 public void test_values() { 900 // Test for method java.util.Collection java.util.Hashtable.values() 901 Collection c = ht10.values(); 902 Enumeration e = elmVector.elements(); 903 while (e.hasMoreElements()) 904 assertTrue("Returned incorrect values", c.contains(e.nextElement())); 905 906 // BEGIN android-removed 907 // implementation dependent 908 // assertEquals("Not synchronized", 909 // "java.util.Collections$SynchronizedCollection", c.getClass().getName()); 910 // END android-removed 911 912 Hashtable myHashtable = new Hashtable(); 913 for (int i = 0; i < 100; i++) 914 myHashtable.put(new Integer(i), new Integer(i)); 915 Collection values = myHashtable.values(); 916 new Support_UnmodifiableCollectionTest( 917 "Test Returned Collection From Hashtable.values()", values) 918 .runTest(); 919 values.remove(new Integer(0)); 920 assertTrue( 921 "Removing from the values collection should remove from the original map", 922 !myHashtable.containsValue(new Integer(0))); 923 } 924 925 /** 926 * Regression Test for JIRA 2181 927 */ 928 @TestTargets({ 929 @TestTargetNew( 930 level = TestLevel.PARTIAL_COMPLETE, 931 notes = "", 932 method = "entrySet", 933 args = {} 934 ), 935 @TestTargetNew( 936 level = TestLevel.PARTIAL_COMPLETE, 937 notes = "", 938 method = "remove", 939 args = {java.lang.Object.class} 940 ) 941 }) test_entrySet_remove()942 public void test_entrySet_remove() 943 { 944 Hashtable<String,String> hashtable = new Hashtable<String,String>(); 945 hashtable.put("my.nonexistent.prop", "AAA"); 946 hashtable.put( "parse.error", "BBB" ); 947 Iterator<Map.Entry<String,String>> iterator = 948 hashtable.entrySet().iterator(); 949 while(iterator.hasNext()) 950 { 951 Map.Entry entry = iterator.next(); 952 final Object value = entry.getValue(); 953 if(value.equals("AAA")) 954 { 955 iterator.remove(); 956 } 957 } 958 assertFalse(hashtable.containsKey("my.nonexistent.prop")); 959 } 960 961 class Mock_Hashtable extends Hashtable { 962 boolean flag = false; 963 Mock_Hashtable(int i)964 public Mock_Hashtable(int i) { 965 super(i); 966 } 967 968 @Override rehash()969 protected void rehash() { 970 flag = true; 971 super.rehash(); 972 } 973 isRehashed()974 public boolean isRehashed() { 975 return flag; 976 } 977 } 978 979 @TestTargetNew( 980 level = TestLevel.COMPLETE, 981 notes = "", 982 method = "rehash", 983 args = {} 984 ) test_rehash()985 public void test_rehash() { 986 Mock_Hashtable mht = new Mock_Hashtable(5); 987 988 assertFalse(mht.isRehashed()); 989 for(int i = 0; i < 10; i++) { 990 mht.put(i, "New value"); 991 } 992 assertTrue(mht.isRehashed()); 993 } 994 hashtableClone(Hashtable s)995 protected Hashtable hashtableClone(Hashtable s) { 996 return (Hashtable) s.clone(); 997 } 998 999 /** 1000 * Sets up the fixture, for example, open a network connection. This method 1001 * is called before a test is executed. 1002 */ setUp()1003 protected void setUp() { 1004 1005 ht10 = new Hashtable(10); 1006 ht100 = new Hashtable(100); 1007 htfull = new Hashtable(10); 1008 keyVector = new Vector(10); 1009 elmVector = new Vector(10); 1010 1011 for (int i = 0; i < 10; i++) { 1012 ht10.put("Key " + i, "Val " + i); 1013 keyVector.addElement("Key " + i); 1014 elmVector.addElement("Val " + i); 1015 } 1016 1017 for (int i = 0; i < 7; i++) 1018 htfull.put("FKey " + i, "FVal " + i); 1019 } 1020 1021 /** 1022 * Tears down the fixture, for example, close a network connection. This 1023 * method is called after a test is executed. 1024 */ tearDown()1025 protected void tearDown() { 1026 ht10 = null; 1027 ht100 = null; 1028 htfull = null; 1029 keyVector = null; 1030 elmVector = null; 1031 } 1032 } 1033