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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 package org.apache.harmony.luni.tests.java.util;
20 import java.io.Serializable;
21 import java.util.Collection;
22 import java.util.HashSet;
23 import java.util.IdentityHashMap;
24 import java.util.Iterator;
27 import java.util.TreeSet;
29 import org.apache.harmony.testframework.serialization.SerializationTest;
30 import org.apache.harmony.testframework.serialization.SerializationTest.SerializableAssert;
32 public class IdentityHashMapTest extends junit.framework.TestCase {
35 * @tests java.util.IdentityHashMap#containsKey(java.lang.Object)
36 * @tests java.util.IdentityHashMap#containsValue(java.lang.Object)
37 * @tests java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object)
38 * @tests java.util.IdentityHashMap#get(java.lang.Object)
40 public void test_null_Keys_and_Values() {
41 // tests with null keys and values
42 IdentityHashMap map = new IdentityHashMap();
45 // null key and null value
46 result = map.put(null, null);
47 assertTrue("testA can not find null key", map.containsKey(null));
48 assertTrue("testA can not find null value", map.containsValue(null));
49 assertNull("testA can not get null value for null key",
51 assertNull("testA put returned wrong value", result);
54 String value = "a value";
55 result = map.put(null, value);
56 assertTrue("testB can not find null key", map.containsKey(null));
57 assertTrue("testB can not find a value with null key", map
58 .containsValue(value));
59 assertTrue("testB can not get value for null key",
60 map.get(null) == value);
61 assertNull("testB put returned wrong value", result);
65 result = map.put(key, null);
66 assertTrue("testC can not find a key with null value", map
68 assertTrue("testC can not find null value", map.containsValue(null));
69 assertNull("testC can not get null value for key", map.get(key));
70 assertNull("testC put returned wrong value", result);
73 String anothervalue = "another value";
74 result = map.put(null, anothervalue);
75 assertTrue("testD can not find null key", map.containsKey(null));
76 assertTrue("testD can not find a value with null key", map
77 .containsValue(anothervalue));
78 assertTrue("testD can not get value for null key",
79 map.get(null) == anothervalue);
80 assertTrue("testD put returned wrong value", result == value);
83 result = map.remove(null);
84 assertTrue("testE remove returned wrong value", result == anothervalue);
85 assertTrue("testE should not find null key", !map.containsKey(null));
86 assertTrue("testE should not find a value with null key", !map
87 .containsValue(anothervalue));
88 assertNull("testE should not get value for null key",
93 * @tests java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object)
95 public void test_putLjava_lang_ObjectLjava_lang_Object() {
96 IdentityHashMap<Object, Object> map = new IdentityHashMap<Object, Object>();
98 // Test null as a key.
99 Object value = "Some value";
100 map.put(null, value);
101 assertSame("Assert 0: Failure getting null key", value, map.get(null));
103 // Test null as a value
104 Object key = "Some key";
106 assertNull("Assert 1: Failure getting null value", map.get(key));
110 * @tests java.util.IdentityHashMap#remove(java.lang.Object)
111 * @tests java.util.IdentityHashMap#keySet()
113 public void test_remove() {
114 IdentityHashMap map = new IdentityHashMap();
116 map.put("key1", "value1");
117 map.put("key2", "value2");
120 assertTrue("Did not remove key1", !map.containsKey("key1"));
121 assertTrue("Did not remove the value for key1", !map
122 .containsValue("value1"));
124 assertTrue("Modified key2", map.get("key2") != null
125 && map.get("key2") == "value2");
126 assertNull("Modified null entry", map.get(null));
130 * @tests java.util.IdentityHashMapTest#remove(java.lang.Object)
132 public void test_removeLjava_lang_Object() {
133 // Regression for HARMONY-37
134 IdentityHashMap<String, String> hashMap = new IdentityHashMap<String, String>();
135 hashMap.remove("absent");
136 assertEquals("Assert 0: Size is incorrect", 0, hashMap.size());
138 hashMap.put("key", "value");
139 hashMap.remove("key");
140 assertEquals("Assert 1: After removing non-null element size is incorrect", 0, hashMap.size());
142 hashMap.put(null, null);
143 assertEquals("Assert 2: adding literal null failed", 1, hashMap.size());
144 hashMap.remove(null);
145 assertEquals("Assert 3: After removing null element size is incorrect", 0, hashMap.size());
149 * @tests java.util.IdentityHashMap#entrySet()
150 * @tests java.util.IdentityHashMap#keySet()
151 * @tests java.util.IdentityHashMap#values()
153 public void test_sets() {
154 // tests with null keys and values
155 IdentityHashMap map = new IdentityHashMap();
157 // null key and null value
158 map.put("key", "value");
160 map.put("a key", null);
161 map.put("another key", null);
163 Set keyset = map.keySet();
164 Collection valueset = map.values();
165 Set entries = map.entrySet();
166 Iterator it = entries.iterator();
167 while (it.hasNext()) {
168 Map.Entry entry = (Map.Entry) it.next();
169 assertTrue("EntrySetIterator can not find entry ", entries
172 assertTrue("entry key not found in map", map.containsKey(entry
174 assertTrue("entry value not found in map", map.containsValue(entry
177 assertTrue("entry key not found in the keyset", keyset
178 .contains(entry.getKey()));
179 assertTrue("entry value not found in the valueset", valueset
180 .contains(entry.getValue()));
185 * @tests java.util.IdentityHashMap#entrySet()
186 * @tests java.util.IdentityHashMap#remove(java.lang.Object)
188 public void test_entrySet_removeAll() {
189 IdentityHashMap map = new IdentityHashMap();
190 for (int i = 0; i < 1000; i++) {
191 map.put(new Integer(i), new Integer(i));
193 Set set = map.entrySet();
196 assertEquals("did not remove all elements in the map", 0, map.size());
197 assertTrue("did not remove all elements in the entryset", set.isEmpty());
199 Iterator it = set.iterator();
200 assertTrue("entrySet iterator still has elements", !it.hasNext());
204 * @tests java.util.IdentityHashMap#keySet()
205 * @tests java.util.IdentityHashMap#clear()
207 public void test_keySet_clear() {
208 IdentityHashMap map = new IdentityHashMap();
209 for (int i = 0; i < 1000; i++) {
210 map.put(new Integer(i), new Integer(i));
212 Set set = map.keySet();
215 assertEquals("did not remove all elements in the map", 0, map.size());
216 assertTrue("did not remove all elements in the keyset", set.isEmpty());
218 Iterator it = set.iterator();
219 assertTrue("keySet iterator still has elements", !it.hasNext());
223 * @tests java.util.IdentityHashMap#values()
225 public void test_values() {
227 IdentityHashMap map = new IdentityHashMap();
228 for (int i = 0; i < 10; i++) {
229 map.put(new Integer(i), new Integer(i));
232 Integer key = new Integer(20);
233 Integer value = new Integer(40);
236 Collection vals = map.values();
237 boolean result = vals.remove(key);
238 assertTrue("removed entries incorrectly", map.size() == 11 && !result);
239 assertTrue("removed key incorrectly", map.containsKey(key));
240 assertTrue("removed value incorrectly", map.containsValue(value));
242 result = vals.remove(value);
243 assertTrue("Did not remove entry as expected", map.size() == 10
245 assertTrue("Did not remove key as expected", !map.containsKey(key));
246 assertTrue("Did not remove value as expected", !map
247 .containsValue(value));
249 // put an equivalent key to a value
250 key = new Integer(1);
251 value = new Integer(100);
254 result = vals.remove(key);
255 assertTrue("TestB. removed entries incorrectly", map.size() == 11
257 assertTrue("TestB. removed key incorrectly", map.containsKey(key));
258 assertTrue("TestB. removed value incorrectly", map.containsValue(value));
260 result = vals.remove(value);
261 assertTrue("TestB. Did not remove entry as expected", map.size() == 10
263 assertTrue("TestB. Did not remove key as expected", !map
265 assertTrue("TestB. Did not remove value as expected", !map
266 .containsValue(value));
269 assertEquals("Did not remove all entries as expected", 0, map.size());
273 * @tests java.util.IdentityHashMap#keySet()
274 * @tests java.util.IdentityHashMap#remove(java.lang.Object)
276 public void test_keySet_removeAll() {
277 IdentityHashMap map = new IdentityHashMap();
278 for (int i = 0; i < 1000; i++) {
279 map.put(new Integer(i), new Integer(i));
281 Set set = map.keySet();
284 assertEquals("did not remove all elements in the map", 0, map.size());
285 assertTrue("did not remove all elements in the keyset", set.isEmpty());
287 Iterator it = set.iterator();
288 assertTrue("keySet iterator still has elements", !it.hasNext());
292 * @tests java.util.IdentityHashMap#keySet()
294 public void test_keySet_retainAll() {
295 IdentityHashMap map = new IdentityHashMap();
296 for (int i = 0; i < 1000; i++) {
297 map.put(new Integer(i), new Integer(i));
299 Set set = map.keySet();
301 // retain all the elements
302 boolean result = set.retainAll(set);
303 assertTrue("retain all should return false", !result);
304 assertEquals("did not retain all", 1000, set.size());
306 // send empty set to retainAll
307 result = set.retainAll(new TreeSet());
308 assertTrue("retain all should return true", result);
309 assertEquals("did not remove all elements in the map", 0, map.size());
310 assertTrue("did not remove all elements in the keyset", set.isEmpty());
312 Iterator it = set.iterator();
313 assertTrue("keySet iterator still has elements", !it.hasNext());
317 * @tests java.util.IdentityHashMap#keySet()
318 * @tests java.util.IdentityHashMap#remove(java.lang.Object)
320 public void test_keyset_remove() {
321 IdentityHashMap map = new IdentityHashMap();
323 Integer key = new Integer(21);
325 map.put(new Integer(1), null);
326 map.put(new Integer(11), null);
328 map.put(new Integer(31), null);
329 map.put(new Integer(41), null);
330 map.put(new Integer(51), null);
331 map.put(new Integer(61), null);
332 map.put(new Integer(71), null);
333 map.put(new Integer(81), null);
334 map.put(new Integer(91), null);
336 Set set = map.keySet();
338 Set newset = new HashSet();
339 Iterator it = set.iterator();
340 while (it.hasNext()) {
341 Object element = it.next();
342 if (element == key) {
347 int size = newset.size();
348 assertTrue("keyset and newset don't have same size",
349 newset.size() == size);
350 assertTrue("element is in newset ", !newset.contains(key));
351 assertTrue("element not removed from keyset", !set.contains(key));
352 assertTrue("element not removed from map", !map.containsKey(key));
354 assertTrue("newset and keyset do not have same elements 1", newset
356 assertTrue("newset and keyset do not have same elements 2", set
360 public void test_clone_scenario1() {
361 IdentityHashMap hashMap = new IdentityHashMap();
362 assertEquals(0, hashMap.hashCode());
363 Object cloneHashMap = hashMap.clone();
364 ((IdentityHashMap) cloneHashMap).put("key", "value");
365 assertEquals(0, hashMap.hashCode());
366 assertTrue(0 != cloneHashMap.hashCode());
369 public void test_clone_scenario2() {
370 IdentityHashMap hashMap = new IdentityHashMap();
371 assertEquals(0, hashMap.hashCode());
372 Object cloneHashMap = hashMap.clone();
373 hashMap.put("key", "value");
374 assertEquals(1, hashMap.size());
375 assertEquals(0, ((IdentityHashMap) cloneHashMap).size());
376 assertEquals("value", hashMap.get("key"));
377 assertNull(((IdentityHashMap) cloneHashMap).get("key"));
378 assertTrue(0 != hashMap.hashCode());
379 assertEquals(0, cloneHashMap.hashCode());
382 public void test_clone_scenario3() {
383 IdentityHashMap hashMap = new IdentityHashMap();
384 assertEquals(0, hashMap.hashCode());
385 hashMap.put("key", "value");
386 Object cloneHashMap = hashMap.clone();
387 assertEquals(1, hashMap.size());
388 assertEquals(1, ((IdentityHashMap) cloneHashMap).size());
389 assertEquals("value", hashMap.get("key"));
390 assertEquals("value", ((IdentityHashMap) cloneHashMap).get("key"));
391 assertEquals(hashMap.hashCode(), cloneHashMap.hashCode());
394 public void test_clone_scenario4() {
395 IdentityHashMap hashMap = new IdentityHashMap();
396 Object cloneHashMap = hashMap.clone();
397 assertNull(((IdentityHashMap) cloneHashMap).get((Object) null));
398 hashMap.put((Object) null, cloneHashMap);
399 assertNull(((IdentityHashMap) cloneHashMap).get((Object) null));
400 assertEquals(cloneHashMap, hashMap.get((Object) null));
403 public void test_clone_scenario5() throws Exception {
404 IdentityHashMap hashMap = new IdentityHashMap();
405 Object cloneHashMap = hashMap.clone();
406 assertNull(hashMap.remove((Object) null));
407 ((IdentityHashMap) cloneHashMap).put((Object) null, cloneHashMap);
408 assertNull(hashMap.remove((Object) null));
409 assertEquals(cloneHashMap, ((IdentityHashMap) cloneHashMap)
410 .get((Object) null));
413 // comparator for IdentityHashMap objects
414 private static final SerializableAssert COMPARATOR = new SerializableAssert() {
415 public void assertDeserialized(Serializable initial,
416 Serializable deserialized) {
418 IdentityHashMap init = (IdentityHashMap) initial;
419 IdentityHashMap desr = (IdentityHashMap) deserialized;
421 assertEquals("Size", init.size(), desr.size());
426 * @tests serialization/deserialization compatibility with RI.
428 public void testSerializationCompatibility() throws Exception {
429 IdentityHashMap<String, String> identityHashMap = new IdentityHashMap<String, String>();
430 identityHashMap.put("key1", "value1");
431 identityHashMap.put("key2", "value2");
432 identityHashMap.put("key3", "value3");
434 SerializationTest.verifyGolden(this, identityHashMap, COMPARATOR);