1/* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18package org.apache.harmony.luni.tests.java.util; 19 20import java.lang.reflect.InvocationHandler; 21import java.lang.reflect.Method; 22import java.lang.reflect.Proxy; 23import java.util.AbstractMap; 24import java.util.ArrayList; 25import java.util.Arrays; 26import java.util.Collection; 27import java.util.HashMap; 28import java.util.Iterator; 29import java.util.Map; 30import java.util.Set; 31import java.util.TreeMap; 32 33import org.apache.harmony.testframework.serialization.SerializationTest; 34 35import tests.support.Support_MapTest2; 36import tests.support.Support_UnmodifiableCollectionTest; 37 38public class HashMapTest extends junit.framework.TestCase { 39 class MockMap extends AbstractMap { 40 public Set entrySet() { 41 return null; 42 } 43 public int size(){ 44 return 0; 45 } 46 } 47 48 private static class MockMapNull extends AbstractMap { 49 public Set entrySet() { 50 return null; 51 } 52 53 public int size() { 54 return 10; 55 } 56 } 57 58 interface MockInterface { 59 public String mockMethod(); 60 } 61 62 class MockClass implements MockInterface { 63 public String mockMethod() { 64 return "This is a MockClass"; 65 } 66 } 67 68 class MockHandler implements InvocationHandler { 69 70 Object obj; 71 72 public MockHandler(Object o) { 73 obj = o; 74 } 75 76 public Object invoke(Object proxy, Method m, Object[] args) 77 throws Throwable { 78 79 Object result = null; 80 81 try { 82 83 result = m.invoke(obj, args); 84 85 } catch (Exception e) { 86 e.printStackTrace(); 87 } finally { 88 89 } 90 return result; 91 } 92 93 } 94 95 96 HashMap hm; 97 98 final static int hmSize = 1000; 99 100 static Object[] objArray; 101 102 static Object[] objArray2; 103 { 104 objArray = new Object[hmSize]; 105 objArray2 = new Object[hmSize]; 106 for (int i = 0; i < objArray.length; i++) { 107 objArray[i] = new Integer(i); 108 objArray2[i] = objArray[i].toString(); 109 } 110 } 111 112 /** 113 * @tests java.util.HashMap#HashMap() 114 */ 115 public void test_Constructor() { 116 // Test for method java.util.HashMap() 117 new Support_MapTest2(new HashMap()).runTest(); 118 119 HashMap hm2 = new HashMap(); 120 assertEquals("Created incorrect HashMap", 0, hm2.size()); 121 } 122 123 /** 124 * @tests java.util.HashMap#HashMap(int) 125 */ 126 public void test_ConstructorI() { 127 // Test for method java.util.HashMap(int) 128 HashMap hm2 = new HashMap(5); 129 assertEquals("Created incorrect HashMap", 0, hm2.size()); 130 try { 131 new HashMap(-1); 132 } catch (IllegalArgumentException e) { 133 return; 134 } 135 fail( 136 "Failed to throw IllegalArgumentException for initial capacity < 0"); 137 138 HashMap empty = new HashMap(0); 139 assertNull("Empty hashmap access", empty.get("nothing")); 140 empty.put("something", "here"); 141 assertTrue("cannot get element", empty.get("something") == "here"); 142 } 143 144 /** 145 * @tests java.util.HashMap#HashMap(int, float) 146 */ 147 public void test_ConstructorIF() { 148 // Test for method java.util.HashMap(int, float) 149 HashMap hm2 = new HashMap(5, (float) 0.5); 150 assertEquals("Created incorrect HashMap", 0, hm2.size()); 151 try { 152 new HashMap(0, 0); 153 } catch (IllegalArgumentException e) { 154 return; 155 } 156 fail( 157 "Failed to throw IllegalArgumentException for initial load factor <= 0"); 158 159 HashMap empty = new HashMap(0, 0.75f); 160 assertNull("Empty hashtable access", empty.get("nothing")); 161 empty.put("something", "here"); 162 assertTrue("cannot get element", empty.get("something") == "here"); 163 } 164 165 /** 166 * @tests java.util.HashMap#HashMap(java.util.Map) 167 */ 168 public void test_ConstructorLjava_util_Map() { 169 // Test for method java.util.HashMap(java.util.Map) 170 Map myMap = new TreeMap(); 171 for (int counter = 0; counter < hmSize; counter++) 172 myMap.put(objArray2[counter], objArray[counter]); 173 HashMap hm2 = new HashMap(myMap); 174 for (int counter = 0; counter < hmSize; counter++) 175 assertTrue("Failed to construct correct HashMap", hm 176 .get(objArray2[counter]) == hm2.get(objArray2[counter])); 177 178 try { 179 Map mockMap = new MockMap(); 180 hm = new HashMap(mockMap); 181 fail("Should throw NullPointerException"); 182 } catch (NullPointerException e) { 183 //empty 184 } 185 186 HashMap map = new HashMap(); 187 map.put("a", "a"); 188 SubMap map2 = new SubMap(map); 189 assertTrue(map2.containsKey("a")); 190 assertTrue(map2.containsValue("a")); 191 } 192 193 /** 194 * @tests java.util.HashMap#clear() 195 */ 196 public void test_clear() { 197 hm.clear(); 198 assertEquals("Clear failed to reset size", 0, hm.size()); 199 for (int i = 0; i < hmSize; i++) 200 assertNull("Failed to clear all elements", 201 hm.get(objArray2[i])); 202 203 // Check clear on a large loaded map of Integer keys 204 HashMap<Integer, String> map = new HashMap<Integer, String>(); 205 for (int i = -32767; i < 32768; i++) { 206 map.put(i, "foobar"); 207 } 208 map.clear(); 209 assertEquals("Failed to reset size on large integer map", 0, hm.size()); 210 for (int i = -32767; i < 32768; i++) { 211 assertNull("Failed to clear integer map values", map.get(i)); 212 } 213 } 214 215 /** 216 * @tests java.util.HashMap#clone() 217 */ 218 public void test_clone() { 219 // Test for method java.lang.Object java.util.HashMap.clone() 220 HashMap hm2 = (HashMap) hm.clone(); 221 assertTrue("Clone answered equivalent HashMap", hm2 != hm); 222 for (int counter = 0; counter < hmSize; counter++) 223 assertTrue("Clone answered unequal HashMap", hm 224 .get(objArray2[counter]) == hm2.get(objArray2[counter])); 225 226 HashMap map = new HashMap(); 227 map.put("key", "value"); 228 // get the keySet() and values() on the original Map 229 Set keys = map.keySet(); 230 Collection values = map.values(); 231 assertEquals("values() does not work", 232 "value", values.iterator().next()); 233 assertEquals("keySet() does not work", 234 "key", keys.iterator().next()); 235 AbstractMap map2 = (AbstractMap) map.clone(); 236 map2.put("key", "value2"); 237 Collection values2 = map2.values(); 238 assertTrue("values() is identical", values2 != values); 239 // values() and keySet() on the cloned() map should be different 240 assertEquals("values() was not cloned", 241 "value2", values2.iterator().next()); 242 map2.clear(); 243 map2.put("key2", "value3"); 244 Set key2 = map2.keySet(); 245 assertTrue("keySet() is identical", key2 != keys); 246 assertEquals("keySet() was not cloned", 247 "key2", key2.iterator().next()); 248 249 // regresion test for HARMONY-4603 250 HashMap hashmap = new HashMap(); 251 MockClonable mock = new MockClonable(1); 252 hashmap.put(1, mock); 253 assertEquals(1, ((MockClonable) hashmap.get(1)).i); 254 HashMap hm3 = (HashMap)hashmap.clone(); 255 assertEquals(1, ((MockClonable) hm3.get(1)).i); 256 mock.i = 0; 257 assertEquals(0, ((MockClonable) hashmap.get(1)).i); 258 assertEquals(0, ((MockClonable) hm3.get(1)).i); 259 } 260 261 /** 262 * @tests java.util.HashMap#containsKey(java.lang.Object) 263 */ 264 public void test_containsKeyLjava_lang_Object() { 265 // Test for method boolean 266 // java.util.HashMap.containsKey(java.lang.Object) 267 assertTrue("Returned false for valid key", hm.containsKey(new Integer( 268 876).toString())); 269 assertTrue("Returned true for invalid key", !hm.containsKey("KKDKDKD")); 270 271 HashMap m = new HashMap(); 272 m.put(null, "test"); 273 assertTrue("Failed with null key", m.containsKey(null)); 274 assertTrue("Failed with missing key matching null hash", !m 275 .containsKey(new Integer(0))); 276 } 277 278 /** 279 * @tests java.util.HashMap#containsValue(java.lang.Object) 280 */ 281 public void test_containsValueLjava_lang_Object() { 282 // Test for method boolean 283 // java.util.HashMap.containsValue(java.lang.Object) 284 assertTrue("Returned false for valid value", hm 285 .containsValue(new Integer(875))); 286 assertTrue("Returned true for invalid valie", !hm 287 .containsValue(new Integer(-9))); 288 } 289 290 /** 291 * @tests java.util.HashMap#entrySet() 292 */ 293 public void test_entrySet() { 294 // Test for method java.util.Set java.util.HashMap.entrySet() 295 Set s = hm.entrySet(); 296 Iterator i = s.iterator(); 297 assertTrue("Returned set of incorrect size", hm.size() == s.size()); 298 while (i.hasNext()) { 299 Map.Entry m = (Map.Entry) i.next(); 300 assertTrue("Returned incorrect entry set", hm.containsKey(m 301 .getKey()) 302 && hm.containsValue(m.getValue())); 303 } 304 305 Iterator iter = s.iterator(); 306 s.remove(iter.next()); 307 assertEquals(1001, s.size()); 308 } 309 310 /** 311 * @tests java.util.HashMap#get(java.lang.Object) 312 */ 313 public void test_getLjava_lang_Object() { 314 // Test for method java.lang.Object 315 // java.util.HashMap.get(java.lang.Object) 316 assertNull("Get returned non-null for non existent key", 317 hm.get("T")); 318 hm.put("T", "HELLO"); 319 assertEquals("Get returned incorrect value for existing key", "HELLO", hm.get("T") 320 ); 321 322 HashMap m = new HashMap(); 323 m.put(null, "test"); 324 assertEquals("Failed with null key", "test", m.get(null)); 325 assertNull("Failed with missing key matching null hash", m 326 .get(new Integer(0))); 327 328 // Regression for HARMONY-206 329 ReusableKey k = new ReusableKey(); 330 HashMap map = new HashMap(); 331 k.setKey(1); 332 map.put(k, "value1"); 333 334 k.setKey(18); 335 assertNull(map.get(k)); 336 337 k.setKey(17); 338 assertNull(map.get(k)); 339 } 340 341 /** 342 * Tests for proxy object keys and values 343 */ 344 public void test_proxies() { 345 // Regression for HARMONY-6237 346 MockInterface proxyKey = (MockInterface) Proxy.newProxyInstance( 347 MockInterface.class.getClassLoader(), 348 new Class[] { MockInterface.class }, new MockHandler( 349 new MockClass())); 350 MockInterface proxyValue = (MockInterface) Proxy.newProxyInstance( 351 MockInterface.class.getClassLoader(), 352 new Class[] { MockInterface.class }, new MockHandler( 353 new MockClass())); 354 355 // Proxy key 356 Object val = new Object(); 357 hm.put(proxyKey, val); 358 359 assertEquals("Failed with proxy object key", val, hm 360 .get(proxyKey)); 361 assertTrue("Failed to find proxy key", hm.containsKey(proxyKey)); 362 assertEquals("Failed to remove proxy object key", val, 363 hm.remove(proxyKey)); 364 assertFalse("Should not have found proxy key", hm.containsKey(proxyKey)); 365 366 // Proxy value 367 Object k = new Object(); 368 hm.put(k, proxyValue); 369 370 assertTrue("Failed to find proxy object as value", hm.containsValue(proxyValue)); 371 372 // Proxy key and value 373 HashMap map = new HashMap(); 374 map.put(proxyKey, proxyValue); 375 assertTrue("Failed to find proxy key", map.containsKey(proxyKey)); 376 assertEquals(1, map.size()); 377 Object[] entries = map.entrySet().toArray(); 378 Map.Entry entry = (Map.Entry)entries[0]; 379 assertTrue("Failed to find proxy association", map.entrySet().contains(entry)); 380 } 381 382 /** 383 * @tests java.util.HashMap#isEmpty() 384 */ 385 public void test_isEmpty() { 386 // Test for method boolean java.util.HashMap.isEmpty() 387 assertTrue("Returned false for new map", new HashMap().isEmpty()); 388 assertTrue("Returned true for non-empty", !hm.isEmpty()); 389 } 390 391 /** 392 * @tests java.util.HashMap#keySet() 393 */ 394 public void test_keySet() { 395 // Test for method java.util.Set java.util.HashMap.keySet() 396 Set s = hm.keySet(); 397 assertTrue("Returned set of incorrect size()", s.size() == hm.size()); 398 for (int i = 0; i < objArray.length; i++) 399 assertTrue("Returned set does not contain all keys", s 400 .contains(objArray[i].toString())); 401 402 HashMap m = new HashMap(); 403 m.put(null, "test"); 404 assertTrue("Failed with null key", m.keySet().contains(null)); 405 assertNull("Failed with null key", m.keySet().iterator().next()); 406 407 Map map = new HashMap(101); 408 map.put(new Integer(1), "1"); 409 map.put(new Integer(102), "102"); 410 map.put(new Integer(203), "203"); 411 Iterator it = map.keySet().iterator(); 412 Integer remove1 = (Integer) it.next(); 413 it.hasNext(); 414 it.remove(); 415 Integer remove2 = (Integer) it.next(); 416 it.remove(); 417 ArrayList list = new ArrayList(Arrays.asList(new Integer[] { 418 new Integer(1), new Integer(102), new Integer(203) })); 419 list.remove(remove1); 420 list.remove(remove2); 421 assertTrue("Wrong result", it.next().equals(list.get(0))); 422 assertEquals("Wrong size", 1, map.size()); 423 assertTrue("Wrong contents", map.keySet().iterator().next().equals( 424 list.get(0))); 425 426 Map map2 = new HashMap(101); 427 map2.put(new Integer(1), "1"); 428 map2.put(new Integer(4), "4"); 429 Iterator it2 = map2.keySet().iterator(); 430 Integer remove3 = (Integer) it2.next(); 431 Integer next; 432 if (remove3.intValue() == 1) 433 next = new Integer(4); 434 else 435 next = new Integer(1); 436 it2.hasNext(); 437 it2.remove(); 438 assertTrue("Wrong result 2", it2.next().equals(next)); 439 assertEquals("Wrong size 2", 1, map2.size()); 440 assertTrue("Wrong contents 2", map2.keySet().iterator().next().equals( 441 next)); 442 } 443 444 /** 445 * @tests java.util.HashMap#put(java.lang.Object, java.lang.Object) 446 */ 447 public void test_putLjava_lang_ObjectLjava_lang_Object() { 448 hm.put("KEY", "VALUE"); 449 assertEquals("Failed to install key/value pair", "VALUE", hm.get("KEY")); 450 451 HashMap<Object,Object> m = new HashMap<Object,Object>(); 452 m.put(new Short((short) 0), "short"); 453 m.put(null, "test"); 454 m.put(new Integer(0), "int"); 455 assertEquals("Failed adding to bucket containing null", "short", m 456 .get(new Short((short) 0))); 457 assertEquals("Failed adding to bucket containing null2", "int", m 458 .get(new Integer(0))); 459 460 // Check my actual key instance is returned 461 HashMap<Integer, String> map = new HashMap<Integer, String>(); 462 for (int i = -32767; i < 32768; i++) { 463 map.put(i, "foobar"); 464 } 465 Integer myKey = new Integer(0); 466 // Put a new value at the old key position 467 map.put(myKey, "myValue"); 468 assertTrue(map.containsKey(myKey)); 469 assertEquals("myValue", map.get(myKey)); 470 boolean found = false; 471 for (Iterator<Integer> itr = map.keySet().iterator(); itr.hasNext();) { 472 Integer key = itr.next(); 473 if (found = key == myKey) { 474 break; 475 } 476 } 477 assertFalse("Should not find new key instance in hashmap", found); 478 479 // Add a new key instance and check it is returned 480 assertNotNull(map.remove(myKey)); 481 map.put(myKey, "myValue"); 482 assertTrue(map.containsKey(myKey)); 483 assertEquals("myValue", map.get(myKey)); 484 for (Iterator<Integer> itr = map.keySet().iterator(); itr.hasNext();) { 485 Integer key = itr.next(); 486 if (found = key == myKey) { 487 break; 488 } 489 } 490 assertTrue("Did not find new key instance in hashmap", found); 491 492 // Ensure keys with identical hashcode are stored separately 493 HashMap<Object,Object> objmap = new HashMap<Object, Object>(); 494 for (int i = 0; i < 32768; i++) { 495 objmap.put(i, "foobar"); 496 } 497 // Put non-equal object with same hashcode 498 MyKey aKey = new MyKey(); 499 assertNull(objmap.put(aKey, "value")); 500 assertNull(objmap.remove(new MyKey())); 501 assertEquals("foobar", objmap.get(0)); 502 assertEquals("value", objmap.get(aKey)); 503 } 504 505 static class MyKey { 506 public MyKey() { 507 super(); 508 } 509 510 public int hashCode() { 511 return 0; 512 } 513 } 514 /** 515 * @tests java.util.HashMap#putAll(java.util.Map) 516 */ 517 public void test_putAllLjava_util_Map() { 518 // Test for method void java.util.HashMap.putAll(java.util.Map) 519 HashMap hm2 = new HashMap(); 520 hm2.putAll(hm); 521 for (int i = 0; i < 1000; i++) 522 assertTrue("Failed to clear all elements", hm2.get( 523 new Integer(i).toString()).equals((new Integer(i)))); 524 525 Map mockMap = new MockMap(); 526 hm2 = new HashMap(); 527 hm2.putAll(mockMap); 528 assertEquals("Size should be 0", 0, hm2.size()); 529 } 530 531 /** 532 * @tests java.util.HashMap#putAll(java.util.Map) 533 */ 534 public void test_putAllLjava_util_Map_Null() { 535 HashMap hashMap = new HashMap(); 536 try { 537 hashMap.putAll(new MockMapNull()); 538 fail("Should throw NullPointerException"); 539 } catch (NullPointerException e) { 540 // expected. 541 } 542 543 try { 544 hashMap = new HashMap(new MockMapNull()); 545 fail("Should throw NullPointerException"); 546 } catch (NullPointerException e) { 547 // expected. 548 } 549 } 550 551 /** 552 * @tests java.util.HashMap#remove(java.lang.Object) 553 */ 554 public void test_removeLjava_lang_Object() { 555 int size = hm.size(); 556 Integer y = new Integer(9); 557 Integer x = ((Integer) hm.remove(y.toString())); 558 assertTrue("Remove returned incorrect value", x.equals(new Integer(9))); 559 assertNull("Failed to remove given key", hm.get(new Integer(9))); 560 assertTrue("Failed to decrement size", hm.size() == (size - 1)); 561 assertNull("Remove of non-existent key returned non-null", hm 562 .remove("LCLCLC")); 563 564 HashMap m = new HashMap(); 565 m.put(null, "test"); 566 assertNull("Failed with same hash as null", 567 m.remove(new Integer(0))); 568 assertEquals("Failed with null key", "test", m.remove(null)); 569 570 HashMap<Integer, Object> map = new HashMap<Integer, Object>(); 571 for (int i = 0; i < 32768; i++) { 572 map.put(i, "const"); 573 } 574 Object[] values = new Object[32768]; 575 for (int i = 0; i < 32768; i++) { 576 values[i] = new Object(); 577 map.put(i, values[i]); 578 } 579 for (int i = 32767; i >= 0; i--) { 580 assertEquals("Failed to remove same value", values[i], map.remove(i)); 581 } 582 583 // Ensure keys with identical hashcode are removed properly 584 map = new HashMap<Integer, Object>(); 585 for (int i = -32767; i < 32768; i++) { 586 map.put(i, "foobar"); 587 } 588 // Remove non equal object with same hashcode 589 assertNull(map.remove(new MyKey())); 590 assertEquals("foobar", map.get(0)); 591 map.remove(0); 592 assertNull(map.get(0)); 593 } 594 595 /** 596 * Compatibility test to ensure we rehash the same way as the RI. 597 * Not required by the spec, but some apps seem sensitive to it. 598 */ 599 public void test_rehash() { 600 // This map should rehash on adding the ninth element. 601 HashMap<MyKey, Integer> hm = new HashMap<MyKey, Integer>(10, 0.5f); 602 603 // Ordered set of keys. 604 MyKey[] keyOrder = new MyKey[9]; 605 for (int i = 0; i < keyOrder.length; i++) { 606 keyOrder[i] = new MyKey(); 607 } 608 609 // Store eight elements 610 for (int i = 0; i < 8; i++) { 611 hm.put(keyOrder[i], i); 612 } 613 // Check expected ordering (inverse of adding order) 614 MyKey[] returnedKeys = hm.keySet().toArray(new MyKey[8]); 615 for (int i = 0; i < 8; i++) { 616 assertSame(keyOrder[i], returnedKeys[7 - i]); 617 } 618 619 // The next put causes a rehash 620 hm.put(keyOrder[8], 8); 621 // Check expected new ordering (adding order) 622 returnedKeys = hm.keySet().toArray(new MyKey[8]); 623 for (int i = 0; i < 9; i++) { 624 assertSame(keyOrder[i], returnedKeys[i]); 625 } 626 } 627 628 /** 629 * @tests java.util.HashMap#size() 630 */ 631 public void test_size() { 632 // Test for method int java.util.HashMap.size() 633 assertTrue("Returned incorrect size", 634 hm.size() == (objArray.length + 2)); 635 } 636 637 /** 638 * @tests java.util.HashMap#values() 639 */ 640 public void test_values() { 641 // Test for method java.util.Collection java.util.HashMap.values() 642 Collection c = hm.values(); 643 assertTrue("Returned collection of incorrect size()", c.size() == hm 644 .size()); 645 for (int i = 0; i < objArray.length; i++) 646 assertTrue("Returned collection does not contain all keys", c 647 .contains(objArray[i])); 648 649 HashMap myHashMap = new HashMap(); 650 for (int i = 0; i < 100; i++) 651 myHashMap.put(objArray2[i], objArray[i]); 652 Collection values = myHashMap.values(); 653 new Support_UnmodifiableCollectionTest( 654 "Test Returned Collection From HashMap.values()", values) 655 .runTest(); 656 values.remove(new Integer(0)); 657 assertTrue( 658 "Removing from the values collection should remove from the original map", 659 !myHashMap.containsValue(new Integer(0))); 660 661 } 662 663 /** 664 * @tests java.util.AbstractMap#toString() 665 */ 666 public void test_toString() { 667 668 HashMap m = new HashMap(); 669 m.put(m, m); 670 String result = m.toString(); 671 assertTrue("should contain self ref", result.indexOf("(this") > -1); 672 } 673 674 static class ReusableKey { 675 private int key = 0; 676 677 public void setKey(int key) { 678 this.key = key; 679 } 680 681 public int hashCode() { 682 return key; 683 } 684 685 public boolean equals(Object o) { 686 if (o == this) { 687 return true; 688 } 689 if (!(o instanceof ReusableKey)) { 690 return false; 691 } 692 return key == ((ReusableKey) o).key; 693 } 694 } 695 696 public void test_Map_Entry_hashCode() { 697 //Related to HARMONY-403 698 HashMap<Integer, Integer> map = new HashMap<Integer, Integer>(10); 699 Integer key = new Integer(1); 700 Integer val = new Integer(2); 701 map.put(key, val); 702 int expected = key.hashCode() ^ val.hashCode(); 703 assertEquals(expected, map.hashCode()); 704 key = new Integer(4); 705 val = new Integer(8); 706 map.put(key, val); 707 expected += key.hashCode() ^ val.hashCode(); 708 assertEquals(expected, map.hashCode()); 709 } 710 711 class MockClonable implements Cloneable{ 712 public int i; 713 714 public MockClonable(int i) { 715 this.i = i; 716 } 717 718 @Override 719 protected Object clone() throws CloneNotSupportedException { 720 return new MockClonable(i); 721 } 722 } 723 724 /* 725 * Regression test for HY-4750 726 */ 727 public void test_EntrySet() { 728 HashMap map = new HashMap(); 729 map.put(new Integer(1), "ONE"); 730 731 Set entrySet = map.entrySet(); 732 Iterator e = entrySet.iterator(); 733 Object real = e.next(); 734 Map.Entry copyEntry = new MockEntry(); 735 assertEquals(real, copyEntry); 736 assertTrue(entrySet.contains(copyEntry)); 737 738 entrySet.remove(copyEntry); 739 assertFalse(entrySet.contains(copyEntry)); 740 } 741 742 private static class MockEntry implements Map.Entry { 743 744 public Object getKey() { 745 return new Integer(1); 746 } 747 748 public Object getValue() { 749 return "ONE"; 750 } 751 752 public Object setValue(Object object) { 753 return null; 754 } 755 } 756 757 /** 758 * Sets up the fixture, for example, open a network connection. This method 759 * is called before a test is executed. 760 */ 761 protected void setUp() { 762 hm = new HashMap(); 763 for (int i = 0; i < objArray.length; i++) 764 hm.put(objArray2[i], objArray[i]); 765 hm.put("test", null); 766 hm.put(null, "test"); 767 } 768 769 770 class SubMap<K, V> extends HashMap<K, V> { 771 public SubMap(Map<? extends K, ? extends V> m) { 772 super(m); 773 } 774 775 public V put(K key, V value) { 776 throw new UnsupportedOperationException(); 777 } 778 } 779 780 /** 781 * @tests serialization/deserialization. 782 */ 783 public void testSerializationSelf() throws Exception { 784 HashMap<String, String> hm = new HashMap<String, String>(); 785 hm.put("key", "value"); 786 787 SerializationTest.verifySelf(hm); 788 789 // regression for HARMONY-1583 790 hm.put(null, "null"); 791 SerializationTest.verifySelf(hm); 792 } 793 794 /** 795 * @tests serialization/deserialization compatibility with RI. 796 */ 797 public void testSerializationCompatibility() throws Exception { 798 HashMap<String, String> hm = new HashMap<String, String>(); 799 hm.put("key", "value"); 800 801 SerializationTest.verifyGolden(this, hm); 802 } 803 804} 805