IdentityHashMapTest.java revision 561ee011997c6c2f1befbfaa9d5f0a99771c1d63
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.io.Serializable; 21import java.util.Collection; 22import java.util.HashSet; 23import java.util.IdentityHashMap; 24import java.util.Iterator; 25import java.util.Map; 26import java.util.Set; 27import java.util.TreeSet; 28import java.util.Map.Entry; 29 30import org.apache.harmony.testframework.serialization.SerializationTest; 31import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert; 32 33public class IdentityHashMapTest extends junit.framework.TestCase { 34 35 /** 36 * @tests java.util.IdentityHashMap#containsKey(java.lang.Object) 37 * @tests java.util.IdentityHashMap#containsValue(java.lang.Object) 38 * @tests java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object) 39 * @tests java.util.IdentityHashMap#get(java.lang.Object) 40 */ 41 public void test_null_Keys_and_Values() { 42 // tests with null keys and values 43 IdentityHashMap map = new IdentityHashMap(); 44 Object result; 45 46 // null key and null value 47 result = map.put(null, null); 48 assertTrue("testA can not find null key", map.containsKey(null)); 49 assertTrue("testA can not find null value", map.containsValue(null)); 50 assertNull("testA can not get null value for null key", 51 map.get(null)); 52 assertNull("testA put returned wrong value", result); 53 54 // null value 55 String value = "a value"; 56 result = map.put(null, value); 57 assertTrue("testB can not find null key", map.containsKey(null)); 58 assertTrue("testB can not find a value with null key", map 59 .containsValue(value)); 60 assertTrue("testB can not get value for null key", 61 map.get(null) == value); 62 assertNull("testB put returned wrong value", result); 63 64 // a null key 65 String key = "a key"; 66 result = map.put(key, null); 67 assertTrue("testC can not find a key with null value", map 68 .containsKey(key)); 69 assertTrue("testC can not find null value", map.containsValue(null)); 70 assertNull("testC can not get null value for key", map.get(key)); 71 assertNull("testC put returned wrong value", result); 72 73 // another null key 74 String anothervalue = "another value"; 75 result = map.put(null, anothervalue); 76 assertTrue("testD can not find null key", map.containsKey(null)); 77 assertTrue("testD can not find a value with null key", map 78 .containsValue(anothervalue)); 79 assertTrue("testD can not get value for null key", 80 map.get(null) == anothervalue); 81 assertTrue("testD put returned wrong value", result == value); 82 83 // remove a null key 84 result = map.remove(null); 85 assertTrue("testE remove returned wrong value", result == anothervalue); 86 assertTrue("testE should not find null key", !map.containsKey(null)); 87 assertTrue("testE should not find a value with null key", !map 88 .containsValue(anothervalue)); 89 assertNull("testE should not get value for null key", 90 map.get(null)); 91 } 92 93 /** 94 * @tests java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object) 95 */ 96 public void test_putLjava_lang_ObjectLjava_lang_Object() { 97 IdentityHashMap<Object, Object> map = new IdentityHashMap<Object, Object>(); 98 99 // Test null as a key. 100 Object value = "Some value"; 101 map.put(null, value); 102 assertSame("Assert 0: Failure getting null key", value, map.get(null)); 103 104 // Test null as a value 105 Object key = "Some key"; 106 map.put(key, null); 107 assertNull("Assert 1: Failure getting null value", map.get(key)); 108 } 109 110 /** 111 * @tests java.util.IdentityHashMap#remove(java.lang.Object) 112 * @tests java.util.IdentityHashMap#keySet() 113 */ 114 public void test_remove() { 115 IdentityHashMap map = new IdentityHashMap(); 116 map.put(null, null); 117 map.put("key1", "value1"); 118 map.put("key2", "value2"); 119 map.remove("key1"); 120 121 assertTrue("Did not remove key1", !map.containsKey("key1")); 122 assertTrue("Did not remove the value for key1", !map 123 .containsValue("value1")); 124 125 assertTrue("Modified key2", map.get("key2") != null 126 && map.get("key2") == "value2"); 127 assertNull("Modified null entry", map.get(null)); 128 } 129 130 /** 131 * @tests java.util.IdentityHashMapTest#remove(java.lang.Object) 132 */ 133 public void test_removeLjava_lang_Object() { 134 // Regression for HARMONY-37 135 IdentityHashMap<String, String> hashMap = new IdentityHashMap<String, String>(); 136 hashMap.remove("absent"); 137 assertEquals("Assert 0: Size is incorrect", 0, hashMap.size()); 138 139 hashMap.put("key", "value"); 140 hashMap.remove("key"); 141 assertEquals("Assert 1: After removing non-null element size is incorrect", 0, hashMap.size()); 142 143 hashMap.put(null, null); 144 assertEquals("Assert 2: adding literal null failed", 1, hashMap.size()); 145 hashMap.remove(null); 146 assertEquals("Assert 3: After removing null element size is incorrect", 0, hashMap.size()); 147 } 148 149 /** 150 * @tests java.util.IdentityHashMap#entrySet() 151 * @tests java.util.IdentityHashMap#keySet() 152 * @tests java.util.IdentityHashMap#values() 153 */ 154 public void test_sets() { 155 // tests with null keys and values 156 IdentityHashMap map = new IdentityHashMap(); 157 158 // null key and null value 159 map.put("key", "value"); 160 map.put(null, null); 161 map.put("a key", null); 162 map.put("another key", null); 163 164 Set keyset = map.keySet(); 165 Collection valueset = map.values(); 166 Set entries = map.entrySet(); 167 Iterator it = entries.iterator(); 168 while (it.hasNext()) { 169 Map.Entry entry = (Map.Entry) it.next(); 170 assertTrue("EntrySetIterator can not find entry ", entries 171 .contains(entry)); 172 173 assertTrue("entry key not found in map", map.containsKey(entry 174 .getKey())); 175 assertTrue("entry value not found in map", map.containsValue(entry 176 .getValue())); 177 178 assertTrue("entry key not found in the keyset", keyset 179 .contains(entry.getKey())); 180 assertTrue("entry value not found in the valueset", valueset 181 .contains(entry.getValue())); 182 } 183 } 184 185 /** 186 * @tests java.util.IdentityHashMap#entrySet() 187 * @tests java.util.IdentityHashMap#remove(java.lang.Object) 188 */ 189 public void test_entrySet_removeAll() { 190 IdentityHashMap map = new IdentityHashMap(); 191 for (int i = 0; i < 1000; i++) { 192 map.put(new Integer(i), new Integer(i)); 193 } 194 Set set = map.entrySet(); 195 196 set.removeAll(set); 197 assertEquals("did not remove all elements in the map", 0, map.size()); 198 assertTrue("did not remove all elements in the entryset", set.isEmpty()); 199 200 Iterator it = set.iterator(); 201 assertTrue("entrySet iterator still has elements", !it.hasNext()); 202 } 203 204 /** 205 * @tests java.util.IdentityHashMap#keySet() 206 * @tests java.util.IdentityHashMap#clear() 207 */ 208 public void test_keySet_clear() { 209 IdentityHashMap map = new IdentityHashMap(); 210 for (int i = 0; i < 1000; i++) { 211 map.put(new Integer(i), new Integer(i)); 212 } 213 Set set = map.keySet(); 214 set.clear(); 215 216 assertEquals("did not remove all elements in the map", 0, map.size()); 217 assertTrue("did not remove all elements in the keyset", set.isEmpty()); 218 219 Iterator it = set.iterator(); 220 assertTrue("keySet iterator still has elements", !it.hasNext()); 221 } 222 223 /** 224 * @tests java.util.IdentityHashMap#values() 225 */ 226 public void test_values() { 227 228 IdentityHashMap map = new IdentityHashMap(); 229 for (int i = 0; i < 10; i++) { 230 map.put(new Integer(i), new Integer(i)); 231 } 232 233 Integer key = new Integer(20); 234 Integer value = new Integer(40); 235 map.put(key, value); 236 237 Collection vals = map.values(); 238 boolean result = vals.remove(key); 239 assertTrue("removed entries incorrectly", map.size() == 11 && !result); 240 assertTrue("removed key incorrectly", map.containsKey(key)); 241 assertTrue("removed value incorrectly", map.containsValue(value)); 242 243 result = vals.remove(value); 244 assertTrue("Did not remove entry as expected", map.size() == 10 245 && result); 246 assertTrue("Did not remove key as expected", !map.containsKey(key)); 247 assertTrue("Did not remove value as expected", !map 248 .containsValue(value)); 249 250 // put an equivalent key to a value 251 key = new Integer(1); 252 value = new Integer(100); 253 map.put(key, value); 254 255 result = vals.remove(key); 256 assertTrue("TestB. removed entries incorrectly", map.size() == 11 257 && !result); 258 assertTrue("TestB. removed key incorrectly", map.containsKey(key)); 259 assertTrue("TestB. removed value incorrectly", map.containsValue(value)); 260 261 result = vals.remove(value); 262 assertTrue("TestB. Did not remove entry as expected", map.size() == 10 263 && result); 264 assertTrue("TestB. Did not remove key as expected", !map 265 .containsKey(key)); 266 assertTrue("TestB. Did not remove value as expected", !map 267 .containsValue(value)); 268 269 vals.clear(); 270 assertEquals("Did not remove all entries as expected", 0, map.size()); 271 } 272 273 /** 274 * @tests java.util.IdentityHashMap#keySet() 275 * @tests java.util.IdentityHashMap#remove(java.lang.Object) 276 */ 277 public void test_keySet_removeAll() { 278 IdentityHashMap map = new IdentityHashMap(); 279 for (int i = 0; i < 1000; i++) { 280 map.put(new Integer(i), new Integer(i)); 281 } 282 Set set = map.keySet(); 283 set.removeAll(set); 284 285 assertEquals("did not remove all elements in the map", 0, map.size()); 286 assertTrue("did not remove all elements in the keyset", set.isEmpty()); 287 288 Iterator it = set.iterator(); 289 assertTrue("keySet iterator still has elements", !it.hasNext()); 290 } 291 292 /** 293 * @tests java.util.IdentityHashMap#keySet() 294 */ 295 public void test_keySet_retainAll() { 296 IdentityHashMap map = new IdentityHashMap(); 297 for (int i = 0; i < 1000; i++) { 298 map.put(new Integer(i), new Integer(i)); 299 } 300 Set set = map.keySet(); 301 302 // retain all the elements 303 boolean result = set.retainAll(set); 304 assertTrue("retain all should return false", !result); 305 assertEquals("did not retain all", 1000, set.size()); 306 307 // send empty set to retainAll 308 result = set.retainAll(new TreeSet()); 309 assertTrue("retain all should return true", result); 310 assertEquals("did not remove all elements in the map", 0, map.size()); 311 assertTrue("did not remove all elements in the keyset", set.isEmpty()); 312 313 Iterator it = set.iterator(); 314 assertTrue("keySet iterator still has elements", !it.hasNext()); 315 } 316 317 /** 318 * @tests java.util.IdentityHashMap#keySet() 319 * @tests java.util.IdentityHashMap#remove(java.lang.Object) 320 */ 321 public void test_keyset_remove() { 322 IdentityHashMap map = new IdentityHashMap(); 323 324 Integer key = new Integer(21); 325 326 map.put(new Integer(1), null); 327 map.put(new Integer(11), null); 328 map.put(key, null); 329 map.put(new Integer(31), null); 330 map.put(new Integer(41), null); 331 map.put(new Integer(51), null); 332 map.put(new Integer(61), null); 333 map.put(new Integer(71), null); 334 map.put(new Integer(81), null); 335 map.put(new Integer(91), null); 336 337 Set set = map.keySet(); 338 339 Set newset = new HashSet(); 340 Iterator it = set.iterator(); 341 while (it.hasNext()) { 342 Object element = it.next(); 343 if (element == key) { 344 it.remove(); 345 } else 346 newset.add(element); 347 } 348 int size = newset.size(); 349 assertTrue("keyset and newset don't have same size", 350 newset.size() == size); 351 assertTrue("element is in newset ", !newset.contains(key)); 352 assertTrue("element not removed from keyset", !set.contains(key)); 353 assertTrue("element not removed from map", !map.containsKey(key)); 354 355 assertTrue("newset and keyset do not have same elements 1", newset 356 .equals(set)); 357 assertTrue("newset and keyset do not have same elements 2", set 358 .equals(newset)); 359 } 360 361 public void test_clone_scenario1() { 362 IdentityHashMap hashMap = new IdentityHashMap(); 363 assertEquals(0, hashMap.hashCode()); 364 Object cloneHashMap = hashMap.clone(); 365 ((IdentityHashMap) cloneHashMap).put("key", "value"); 366 assertEquals(0, hashMap.hashCode()); 367 assertTrue(0 != cloneHashMap.hashCode()); 368 } 369 370 public void test_clone_scenario2() { 371 IdentityHashMap hashMap = new IdentityHashMap(); 372 assertEquals(0, hashMap.hashCode()); 373 Object cloneHashMap = hashMap.clone(); 374 hashMap.put("key", "value"); 375 assertEquals(1, hashMap.size()); 376 assertEquals(0, ((IdentityHashMap) cloneHashMap).size()); 377 assertEquals("value", hashMap.get("key")); 378 assertNull(((IdentityHashMap) cloneHashMap).get("key")); 379 assertTrue(0 != hashMap.hashCode()); 380 assertEquals(0, cloneHashMap.hashCode()); 381 } 382 383 public void test_clone_scenario3() { 384 IdentityHashMap hashMap = new IdentityHashMap(); 385 assertEquals(0, hashMap.hashCode()); 386 hashMap.put("key", "value"); 387 Object cloneHashMap = hashMap.clone(); 388 assertEquals(1, hashMap.size()); 389 assertEquals(1, ((IdentityHashMap) cloneHashMap).size()); 390 assertEquals("value", hashMap.get("key")); 391 assertEquals("value", ((IdentityHashMap) cloneHashMap).get("key")); 392 assertEquals(hashMap.hashCode(), cloneHashMap.hashCode()); 393 } 394 395 public void test_clone_scenario4() { 396 IdentityHashMap hashMap = new IdentityHashMap(); 397 Object cloneHashMap = hashMap.clone(); 398 assertNull(((IdentityHashMap) cloneHashMap).get((Object) null)); 399 hashMap.put((Object) null, cloneHashMap); 400 assertNull(((IdentityHashMap) cloneHashMap).get((Object) null)); 401 assertEquals(cloneHashMap, hashMap.get((Object) null)); 402 } 403 404 public void test_clone_scenario5() throws Exception { 405 IdentityHashMap hashMap = new IdentityHashMap(); 406 Object cloneHashMap = hashMap.clone(); 407 assertNull(hashMap.remove((Object) null)); 408 ((IdentityHashMap) cloneHashMap).put((Object) null, cloneHashMap); 409 assertNull(hashMap.remove((Object) null)); 410 assertEquals(cloneHashMap, ((IdentityHashMap) cloneHashMap) 411 .get((Object) null)); 412 } 413 414 /* 415 * Regression test for HARMONY-6419 416 */ 417 public void test_underlyingMap() { 418 IdentityHashMap<String, String> ihm = new IdentityHashMap<String, String>(); 419 String key = "key"; 420 String value = "value"; 421 ihm.put(key, value); 422 423 Set<Entry<String, String>> set = ihm.entrySet(); 424 assertEquals(1, set.size()); 425 426 Entry<String, String> entry = set.iterator().next(); 427 428 String newValue = "newvalue"; 429 entry.setValue(newValue); 430 assertSame(newValue, ihm.get(key)); 431 } 432 433 // comparator for IdentityHashMap objects 434 private static final SerializableAssert COMPARATOR = new SerializableAssert() { 435 public void assertDeserialized(Serializable initial, 436 Serializable deserialized) { 437 438 IdentityHashMap init = (IdentityHashMap) initial; 439 IdentityHashMap desr = (IdentityHashMap) deserialized; 440 441 assertEquals("Size", init.size(), desr.size()); 442 } 443 }; 444 445 /** 446 * @tests serialization/deserialization compatibility with RI. 447 */ 448 public void testSerializationCompatibility() throws Exception { 449 IdentityHashMap<String, String> identityHashMap = new IdentityHashMap<String, String>(); 450 identityHashMap.put("key1", "value1"); 451 identityHashMap.put("key2", "value2"); 452 identityHashMap.put("key3", "value3"); 453 454 SerializationTest.verifyGolden(this, identityHashMap, COMPARATOR); 455 } 456} 457