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