001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.luni.tests.java.util;
019:
020: import java.io.Serializable;
021: import java.util.AbstractMap;
022: import java.util.ArrayList;
023: import java.util.Arrays;
024: import java.util.Collection;
025: import java.util.HashMap;
026: import java.util.IdentityHashMap;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Set;
030: import java.util.TreeMap;
031:
032: import tests.support.Support_MapTest2;
033:
034: import org.apache.harmony.testframework.serialization.SerializationTest;
035:
036: public class IdentityHashMap2Test extends junit.framework.TestCase {
037: private static final String ID = "hello";
038:
039: class MockMap extends AbstractMap {
040: public Set entrySet() {
041: return null;
042: }
043:
044: public int size() {
045: return 0;
046: }
047: }
048:
049: /*
050: * TODO: change all the statements testing the keys and values with equals()
051: * method to check for reference equality instead
052: */
053:
054: IdentityHashMap hm;
055:
056: final static int hmSize = 1000;
057:
058: static Object[] objArray;
059:
060: static Object[] objArray2;
061: {
062: objArray = new Object[hmSize];
063: objArray2 = new Object[hmSize];
064: for (int i = 0; i < objArray.length; i++) {
065: objArray[i] = new Integer(i);
066: objArray2[i] = objArray[i].toString();
067: }
068: }
069:
070: /**
071: * @tests java.util.IdentityHashMap#IdentityHashMap()
072: */
073: public void test_Constructor() {
074: // Test for method java.util.IdentityHashMap()
075: new Support_MapTest2(new IdentityHashMap()).runTest();
076:
077: IdentityHashMap hm2 = new IdentityHashMap();
078: assertEquals("Created incorrect IdentityHashMap", 0, hm2.size());
079: }
080:
081: /**
082: * @tests java.util.IdentityHashMap#IdentityHashMap(int)
083: */
084: public void test_ConstructorI() {
085: // Test for method java.util.IdentityHashMap(int)
086: IdentityHashMap hm2 = new IdentityHashMap(5);
087: assertEquals("Created incorrect IdentityHashMap", 0, hm2.size());
088: try {
089: new IdentityHashMap(-1);
090: } catch (IllegalArgumentException e) {
091: return;
092: }
093: fail("Failed to throw IllegalArgumentException for initial capacity < 0");
094:
095: IdentityHashMap empty = new IdentityHashMap(0);
096: assertNull("Empty IdentityHashMap access", empty.get("nothing"));
097: empty.put("something", "here");
098: assertTrue("cannot get element",
099: empty.get("something") == "here");
100: }
101:
102: /**
103: * @tests java.util.IdentityHashMap#IdentityHashMap(java.util.Map)
104: */
105: public void test_ConstructorLjava_util_Map() {
106: // Test for method java.util.IdentityHashMap(java.util.Map)
107: Map myMap = new TreeMap();
108: for (int counter = 0; counter < hmSize; counter++)
109: myMap.put(objArray2[counter], objArray[counter]);
110: IdentityHashMap hm2 = new IdentityHashMap(myMap);
111: for (int counter = 0; counter < hmSize; counter++)
112: assertTrue("Failed to construct correct IdentityHashMap",
113: hm.get(objArray2[counter]) == hm2
114: .get(objArray2[counter]));
115:
116: Map mockMap = new MockMap();
117: hm2 = new IdentityHashMap(mockMap);
118: assertEquals("Size should be 0", 0, hm2.size());
119: }
120:
121: /**
122: * @tests java.util.IdentityHashMap#clear()
123: */
124: public void test_clear() {
125: // Test for method void java.util.IdentityHashMap.clear()
126: hm.clear();
127: assertEquals("Clear failed to reset size", 0, hm.size());
128: for (int i = 0; i < hmSize; i++)
129: assertNull("Failed to clear all elements", hm
130: .get(objArray2[i]));
131:
132: }
133:
134: /**
135: * @tests java.util.IdentityHashMap#clone()
136: */
137: public void test_clone() {
138: // Test for method java.lang.Object java.util.IdentityHashMap.clone()
139: IdentityHashMap hm2 = (IdentityHashMap) hm.clone();
140: assertTrue("Clone answered equivalent IdentityHashMap",
141: hm2 != hm);
142: for (int counter = 0; counter < hmSize; counter++)
143: assertTrue("Clone answered unequal IdentityHashMap", hm
144: .get(objArray2[counter]) == hm2
145: .get(objArray2[counter]));
146:
147: IdentityHashMap map = new IdentityHashMap();
148: map.put("key", "value");
149: // get the keySet() and values() on the original Map
150: Set keys = map.keySet();
151: Collection values = map.values();
152: assertEquals("values() does not work", "value", values
153: .iterator().next());
154: assertEquals("keySet() does not work", "key", keys.iterator()
155: .next());
156: AbstractMap map2 = (AbstractMap) map.clone();
157: map2.put("key", "value2");
158: Collection values2 = map2.values();
159: assertTrue("values() is identical", values2 != values);
160: // values() and keySet() on the cloned() map should be different
161: assertEquals("values() was not cloned", "value2", values2
162: .iterator().next());
163: map2.clear();
164: map2.put("key2", "value3");
165: Set key2 = map2.keySet();
166: assertTrue("keySet() is identical", key2 != keys);
167: assertEquals("keySet() was not cloned", "key2", key2.iterator()
168: .next());
169: }
170:
171: /**
172: * @tests java.util.IdentityHashMap#containsKey(java.lang.Object)
173: */
174: public void test_containsKeyLjava_lang_Object() {
175: // Test for method boolean
176: // java.util.IdentityHashMap.containsKey(java.lang.Object)
177: assertTrue("Returned false for valid key", hm
178: .containsKey(objArray2[23]));
179: assertTrue("Returned true for copy of valid key", !hm
180: .containsKey(new Integer(23).toString()));
181: assertTrue("Returned true for invalid key", !hm
182: .containsKey("KKDKDKD"));
183:
184: IdentityHashMap m = new IdentityHashMap();
185: m.put(null, "test");
186: assertTrue("Failed with null key", m.containsKey(null));
187: assertTrue("Failed with missing key matching null hash", !m
188: .containsKey(new Integer(0)));
189: }
190:
191: /**
192: * @tests java.util.IdentityHashMap#containsValue(java.lang.Object)
193: */
194: public void test_containsValueLjava_lang_Object() {
195: // Test for method boolean
196: // java.util.IdentityHashMap.containsValue(java.lang.Object)
197: assertTrue("Returned false for valid value", hm
198: .containsValue(objArray[19]));
199: assertTrue("Returned true for invalid valie", !hm
200: .containsValue(new Integer(-9)));
201: }
202:
203: /**
204: * @tests java.util.IdentityHashMap#entrySet()
205: */
206: public void test_entrySet() {
207: // Test for method java.util.Set java.util.IdentityHashMap.entrySet()
208: Set s = hm.entrySet();
209: Iterator i = s.iterator();
210: assertTrue("Returned set of incorrect size", hm.size() == s
211: .size());
212: while (i.hasNext()) {
213: Map.Entry m = (Map.Entry) i.next();
214: assertTrue("Returned incorrect entry set", hm.containsKey(m
215: .getKey())
216: && hm.containsValue(m.getValue()));
217: }
218: }
219:
220: /**
221: * @tests java.util.IdentityHashMap#get(java.lang.Object)
222: */
223: public void test_getLjava_lang_Object() {
224: // Test for method java.lang.Object
225: // java.util.IdentityHashMap.get(java.lang.Object)
226: assertNull("Get returned non-null for non existent key", hm
227: .get("T"));
228: hm.put("T", "HELLO");
229: assertEquals("Get returned incorecct value for existing key",
230: "HELLO", hm.get("T"));
231:
232: IdentityHashMap m = new IdentityHashMap();
233: m.put(null, "test");
234: assertEquals("Failed with null key", "test", m.get(null));
235: assertNull("Failed with missing key matching null hash", m
236: .get(new Integer(0)));
237: }
238:
239: /**
240: * @tests java.util.IdentityHashMap#isEmpty()
241: */
242: public void test_isEmpty() {
243: // Test for method boolean java.util.IdentityHashMap.isEmpty()
244: assertTrue("Returned false for new map", new IdentityHashMap()
245: .isEmpty());
246: assertTrue("Returned true for non-empty", !hm.isEmpty());
247: }
248:
249: /**
250: * @tests java.util.IdentityHashMap#keySet()
251: */
252: public void test_keySet() {
253: // Test for method java.util.Set java.util.IdentityHashMap.keySet()
254: Set s = hm.keySet();
255: assertTrue("Returned set of incorrect size()", s.size() == hm
256: .size());
257: for (int i = 0; i < objArray.length; i++) {
258: assertTrue("Returned set does not contain all keys", s
259: .contains(objArray2[i]));
260: }
261:
262: IdentityHashMap m = new IdentityHashMap();
263: m.put(null, "test");
264: assertTrue("Failed with null key", m.keySet().contains(null));
265: assertNull("Failed with null key", m.keySet().iterator().next());
266:
267: Map map = new IdentityHashMap(101);
268: map.put(new Integer(1), "1");
269: map.put(new Integer(102), "102");
270: map.put(new Integer(203), "203");
271: Iterator it = map.keySet().iterator();
272: Integer remove1 = (Integer) it.next();
273: it.hasNext();
274: it.remove();
275: Integer remove2 = (Integer) it.next();
276: it.remove();
277: ArrayList list = new ArrayList(Arrays.asList(new Integer[] {
278: new Integer(1), new Integer(102), new Integer(203) }));
279: list.remove(remove1);
280: list.remove(remove2);
281: assertTrue("Wrong result", it.next().equals(list.get(0)));
282: assertEquals("Wrong size", 1, map.size());
283: assertTrue("Wrong contents", map.keySet().iterator().next()
284: .equals(list.get(0)));
285:
286: Map map2 = new IdentityHashMap(101);
287: map2.put(new Integer(1), "1");
288: map2.put(new Integer(4), "4");
289: Iterator it2 = map2.keySet().iterator();
290: Integer remove3 = (Integer) it2.next();
291: Integer next;
292: if (remove3.intValue() == 1)
293: next = new Integer(4);
294: else
295: next = new Integer(1);
296: it2.hasNext();
297: it2.remove();
298: assertTrue("Wrong result 2", it2.next().equals(next));
299: assertEquals("Wrong size 2", 1, map2.size());
300: assertTrue("Wrong contents 2", map2.keySet().iterator().next()
301: .equals(next));
302: }
303:
304: /**
305: * @tests java.util.IdentityHashMap#put(java.lang.Object, java.lang.Object)
306: */
307: public void test_putLjava_lang_ObjectLjava_lang_Object() {
308: // Test for method java.lang.Object
309: // java.util.IdentityHashMap.put(java.lang.Object, java.lang.Object)
310: hm.put("KEY", "VALUE");
311: assertEquals("Failed to install key/value pair", "VALUE", hm
312: .get("KEY"));
313:
314: IdentityHashMap m = new IdentityHashMap();
315: Short s0 = new Short((short) 0);
316: m.put(s0, "short");
317: m.put(null, "test");
318: Integer i0 = new Integer(0);
319: m.put(i0, "int");
320: assertEquals("Failed adding to bucket containing null",
321: "short", m.get(s0));
322: assertEquals("Failed adding to bucket containing null2", "int",
323: m.get(i0));
324: }
325:
326: /**
327: * @tests java.util.IdentityHashMap#putAll(java.util.Map)
328: */
329: public void test_putAllLjava_util_Map() {
330: // Test for method void java.util.IdentityHashMap.putAll(java.util.Map)
331: IdentityHashMap hm2 = new IdentityHashMap();
332: hm2.putAll(hm);
333: for (int i = 0; i < 1000; i++)
334: assertTrue("Failed to clear all elements", hm2.get(
335: objArray2[i]).equals((new Integer(i))));
336:
337: hm2 = new IdentityHashMap();
338: Map mockMap = new MockMap();
339: hm2.putAll(mockMap);
340: assertEquals("Size should be 0", 0, hm2.size());
341: }
342:
343: /**
344: * @tests java.util.IdentityHashMap#remove(java.lang.Object)
345: */
346: public void test_removeLjava_lang_Object() {
347: // Test for method java.lang.Object
348: // java.util.IdentityHashMap.remove(java.lang.Object)
349: int size = hm.size();
350: Integer x = ((Integer) hm.remove(objArray2[9]));
351: assertTrue("Remove returned incorrect value", x
352: .equals(new Integer(9)));
353: assertNull("Failed to remove given key", hm.get(objArray2[9]));
354: assertTrue("Failed to decrement size", hm.size() == (size - 1));
355: assertNull("Remove of non-existent key returned non-null", hm
356: .remove("LCLCLC"));
357:
358: IdentityHashMap m = new IdentityHashMap();
359: m.put(null, "test");
360: assertNull("Failed with same hash as null", m
361: .remove(objArray[0]));
362: assertEquals("Failed with null key", "test", m.remove(null));
363: }
364:
365: /**
366: * @tests java.util.IdentityHashMap#size()
367: */
368: public void test_size() {
369: // Test for method int java.util.IdentityHashMap.size()
370: assertEquals("Returned incorrect size, ",
371: (objArray.length + 2), hm.size());
372: }
373:
374: /**
375: * @tests java.util.IdentityHashMap#equals(java.lang.Object)
376: */
377: public void test_equalsLjava_lang_Object() {
378: IdentityHashMap mapOne = new IdentityHashMap();
379: IdentityHashMap mapTwo = new IdentityHashMap();
380: IdentityHashMap mapThree = new IdentityHashMap();
381: IdentityHashMap mapFour = new IdentityHashMap();
382:
383: String one = "one";
384: String alsoOne = new String(one); // use the new operator to ensure a
385: // new reference is constructed
386: String two = "two";
387: String alsoTwo = new String(two); // use the new operator to ensure a
388: // new reference is constructed
389:
390: mapOne.put(one, two);
391: mapFour.put(one, two);
392:
393: // these two are not equal to the above two
394: mapTwo.put(alsoOne, two);
395: mapThree.put(one, alsoTwo);
396:
397: assertEquals("failure of equality of IdentityHashMaps", mapOne,
398: mapFour);
399: assertTrue(
400: "failure of non-equality of IdentityHashMaps one and two",
401: !mapOne.equals(mapTwo));
402: assertTrue(
403: "failure of non-equality of IdentityHashMaps one and three",
404: !mapOne.equals(mapThree));
405: assertTrue(
406: "failure of non-equality of IdentityHashMaps two and three",
407: !mapTwo.equals(mapThree));
408:
409: HashMap hashMapTwo = new HashMap();
410: HashMap hashMapThree = new HashMap();
411: hashMapTwo.put(alsoOne, two);
412: hashMapThree.put(one, alsoTwo);
413:
414: assertTrue(
415: "failure of non-equality of IdentityHashMaps one and Hashmap two",
416: !mapOne.equals(hashMapTwo));
417: assertTrue(
418: "failure of non-equality of IdentityHashMaps one and Hashmap three",
419: !mapOne.equals(hashMapThree));
420: }
421:
422: /**
423: * @tests java.util.IdentityHashMap#values()
424: */
425: public void test_values() {
426: // Test for method java.util.Collection
427: // java.util.IdentityHashMap.values()
428: Collection c = hm.values();
429: assertTrue("Returned collection of incorrect size()",
430: c.size() == hm.size());
431: for (int i = 0; i < objArray.length; i++)
432: assertTrue("Returned collection does not contain all keys",
433: c.contains(objArray[i]));
434:
435: IdentityHashMap myIdentityHashMap = new IdentityHashMap();
436: for (int i = 0; i < 100; i++)
437: myIdentityHashMap.put(objArray2[i], objArray[i]);
438: Collection values = myIdentityHashMap.values();
439: values.remove(objArray[0]);
440: assertTrue(
441: "Removing from the values collection should remove from the original map",
442: !myIdentityHashMap.containsValue(objArray2[0]));
443:
444: }
445:
446: /**
447: * Sets up the fixture, for example, open a network connection. This method
448: * is called before a test is executed.
449: */
450: protected void setUp() {
451: hm = new IdentityHashMap();
452: for (int i = 0; i < objArray.length; i++)
453: hm.put(objArray2[i], objArray[i]);
454: hm.put("test", null);
455: hm.put(null, "test");
456: }
457:
458: private static final SerializationTest.SerializableAssert comparator = new SerializationTest.SerializableAssert() {
459:
460: public void assertDeserialized(Serializable initial,
461: Serializable deserialized) {
462: IdentityHashMap<String, String> initialMap = (IdentityHashMap<String, String>) initial;
463: IdentityHashMap<String, String> deseriaMap = (IdentityHashMap<String, String>) deserialized;
464: assertEquals("should be equal", initialMap.size(),
465: deseriaMap.size());
466: }
467:
468: };
469: }
|