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.util.AbstractMap;
021: import java.util.Arrays;
022: import java.util.Collection;
023: import java.util.Iterator;
024: import java.util.List;
025: import java.util.Map;
026: import java.util.Set;
027: import java.util.WeakHashMap;
028:
029: import tests.support.Support_MapTest2;
030:
031: public class WeakHashMapTest extends junit.framework.TestCase {
032: class MockMap extends AbstractMap {
033: public Set entrySet() {
034: return null;
035: }
036:
037: public int size() {
038: return 0;
039: }
040: }
041:
042: Object[] keyArray = new Object[100];
043:
044: Object[] valueArray = new Object[100];
045:
046: WeakHashMap whm;
047:
048: Object[] KEY_ARRAY;
049:
050: Object[] VALUE_ARRAY;
051:
052: /**
053: * @tests java.util.WeakHashMap#WeakHashMap()
054: */
055: public void test_Constructor() {
056: // Test for method java.util.WeakHashMap()
057: new Support_MapTest2(new WeakHashMap()).runTest();
058:
059: whm = new WeakHashMap();
060: for (int i = 0; i < 100; i++)
061: whm.put(keyArray[i], valueArray[i]);
062: for (int i = 0; i < 100; i++)
063: assertTrue("Incorrect value retrieved", whm
064: .get(keyArray[i]) == valueArray[i]);
065:
066: }
067:
068: /**
069: * @tests java.util.WeakHashMap#WeakHashMap(int)
070: */
071: public void test_ConstructorI() {
072: // Test for method java.util.WeakHashMap(int)
073: whm = new WeakHashMap(50);
074: for (int i = 0; i < 100; i++)
075: whm.put(keyArray[i], valueArray[i]);
076: for (int i = 0; i < 100; i++)
077: assertTrue("Incorrect value retrieved", whm
078: .get(keyArray[i]) == valueArray[i]);
079:
080: WeakHashMap empty = new WeakHashMap(0);
081: assertNull("Empty weakhashmap access", empty.get("nothing"));
082: empty.put("something", "here");
083: assertTrue("cannot get element",
084: empty.get("something") == "here");
085: }
086:
087: /**
088: * @tests java.util.WeakHashMap#WeakHashMap(int, float)
089: */
090: public void test_ConstructorIF() {
091: // Test for method java.util.WeakHashMap(int, float)
092: whm = new WeakHashMap(50, 0.5f);
093: for (int i = 0; i < 100; i++)
094: whm.put(keyArray[i], valueArray[i]);
095: for (int i = 0; i < 100; i++)
096: assertTrue("Incorrect value retrieved", whm
097: .get(keyArray[i]) == valueArray[i]);
098:
099: WeakHashMap empty = new WeakHashMap(0, 0.75f);
100: assertNull("Empty hashtable access", empty.get("nothing"));
101: empty.put("something", "here");
102: assertTrue("cannot get element",
103: empty.get("something") == "here");
104: }
105:
106: /**
107: * @tests java.util.WeakHashMap#WeakHashMap(java.util.Map)
108: */
109: public void test_ConstructorLjava_util_Map() {
110: Map mockMap = new MockMap();
111: WeakHashMap map = new WeakHashMap(mockMap);
112: assertEquals("Size should be 0", 0, map.size());
113: }
114:
115: /**
116: * @tests java.util.WeakHashMap#clear()
117: */
118: public void test_clear() {
119: // Test for method boolean java.util.WeakHashMap.clear()
120: whm = new WeakHashMap();
121: for (int i = 0; i < 100; i++)
122: whm.put(keyArray[i], valueArray[i]);
123: whm.clear();
124: assertTrue("Cleared map should be empty", whm.isEmpty());
125: for (int i = 0; i < 100; i++)
126: assertNull("Cleared map should only return null", whm
127: .get(keyArray[i]));
128:
129: }
130:
131: /**
132: * @tests java.util.WeakHashMap#containsKey(java.lang.Object)
133: */
134: public void test_containsKeyLjava_lang_Object() {
135: // Test for method boolean java.util.WeakHashMap.containsKey()
136: whm = new WeakHashMap();
137: for (int i = 0; i < 100; i++)
138: whm.put(keyArray[i], valueArray[i]);
139: for (int i = 0; i < 100; i++)
140: assertTrue("Should contain referenced key", whm
141: .containsKey(keyArray[i]));
142: keyArray[25] = null;
143: keyArray[50] = null;
144: }
145:
146: /**
147: * @tests java.util.WeakHashMap#containsValue(java.lang.Object)
148: */
149: public void test_containsValueLjava_lang_Object() {
150: // Test for method boolean java.util.WeakHashMap.containsValue()
151: whm = new WeakHashMap();
152: for (int i = 0; i < 100; i++)
153: whm.put(keyArray[i], valueArray[i]);
154: for (int i = 0; i < 100; i++)
155: assertTrue("Should contain referenced value", whm
156: .containsValue(valueArray[i]));
157: keyArray[25] = null;
158: keyArray[50] = null;
159: }
160:
161: /**
162: * @tests java.util.WeakHashMap#entrySet()
163: */
164: public void test_entrySet() {
165: WeakHashMap<Object, Object> weakMap = new WeakHashMap<Object, Object>();
166: KEY_ARRAY = new Object[100];
167: VALUE_ARRAY = new Object[100];
168: for (int i = 0; i < 100; i++) {
169: KEY_ARRAY[i] = new Integer(i);
170: VALUE_ARRAY[i] = new Long(i);
171: weakMap.put(KEY_ARRAY[i], VALUE_ARRAY[i]);
172: }
173:
174: List<Object> keys = Arrays.asList(KEY_ARRAY);
175: List<Object> values = Arrays.asList(VALUE_ARRAY);
176:
177: // Check the entry set has correct size & content
178: Set<Map.Entry<Object, Object>> entrySet = weakMap.entrySet();
179: assertEquals("Assert 0: Incorrect number of entries returned",
180: 100, entrySet.size());
181: Iterator<Map.Entry<Object, Object>> it = entrySet.iterator();
182: while (it.hasNext()) {
183: Map.Entry<Object, Object> entry = it.next();
184: assertTrue("Assert 1: Invalid map entry key returned", keys
185: .contains(entry.getKey()));
186: assertTrue("Assert 2: Invalid map entry value returned",
187: values.contains(entry.getValue()));
188: assertTrue("Assert 3: Entry not in entry set", entrySet
189: .contains(entry));
190: }
191:
192: // Dereference list of key/value objects
193: keys = values = null;
194:
195: // Dereference a single key, then try to
196: // force a collection of the weak ref'd obj
197: KEY_ARRAY[50] = null;
198: int count = 0;
199: do {
200: System.gc();
201: System.gc();
202: Runtime.getRuntime().runFinalization();
203: count++;
204: } while (count <= 5 && entrySet.size() == 100);
205:
206: if ((count == 5) && (entrySet.size() == 100)) {
207: // We failed (or entrySet broken), so further tests not valid.
208: return;
209: }
210:
211: assertEquals("Assert 4: Incorrect number of entries after gc",
212: 99, entrySet.size());
213: assertSame("Assert 5: Entries not identical", entrySet
214: .iterator().next(), entrySet.iterator().next());
215:
216: // remove alternate entries using the iterator, and ensure the
217: // iteration count is consistent
218: int size = entrySet.size();
219: it = entrySet.iterator();
220: while (it.hasNext()) {
221: it.next();
222: it.remove();
223: size--;
224: if (it.hasNext()) {
225: it.next();
226: }
227:
228: }
229: assertEquals("Assert 6: entry set count mismatch", size,
230: entrySet.size());
231:
232: int entries = 0;
233: it = entrySet.iterator();
234: while (it.hasNext()) {
235: it.next();
236: entries++;
237: }
238: assertEquals("Assert 6: count mismatch", size, entries);
239:
240: it = entrySet.iterator();
241: while (it.hasNext()) {
242: it.next();
243: it.remove();
244: }
245: assertEquals("Assert 7: entry set not empty", 0, entrySet
246: .size());
247: assertTrue("Assert 8: iterator not empty", !entrySet
248: .iterator().hasNext());
249: }
250:
251: /**
252: * @tests java.util.WeakHashMap#entrySet()
253: */
254: public void test_entrySet_2() {
255: // Test for method java.util.Set java.util.WeakHashMap.entrySet()
256: whm = new WeakHashMap();
257: for (int i = 0; i < 100; i++)
258: whm.put(keyArray[i], valueArray[i]);
259: List keys = Arrays.asList(keyArray);
260: List values = Arrays.asList(valueArray);
261: Set entrySet = whm.entrySet();
262: assertTrue(
263: "Incorrect number of entries returned--wanted 100, got: "
264: + entrySet.size(), entrySet.size() == 100);
265: Iterator it = entrySet.iterator();
266: while (it.hasNext()) {
267: Map.Entry entry = (Map.Entry) it.next();
268: assertTrue("Invalid map entry returned--bad key", keys
269: .contains(entry.getKey()));
270: assertTrue("Invalid map entry returned--bad key", values
271: .contains(entry.getValue()));
272: }
273: keys = null;
274: values = null;
275: keyArray[50] = null;
276:
277: int count = 0;
278: do {
279: System.gc();
280: System.gc();
281: Runtime.getRuntime().runFinalization();
282: count++;
283: } while (count <= 5 && entrySet.size() == 100);
284:
285: assertTrue(
286: "Incorrect number of entries returned after gc--wanted 99, got: "
287: + entrySet.size(), entrySet.size() == 99);
288: }
289:
290: /**
291: * @tests java.util.WeakHashMap#get(java.lang.Object)
292: */
293: public void test_getLjava_lang_Object() {
294: // Test for method java.lang.Object
295: // java.util.WeakHashMap.get(java.lang.Object)
296: assertTrue("Used to test", true);
297: }
298:
299: /**
300: * @tests java.util.WeakHashMap#isEmpty()
301: */
302: public void test_isEmpty() {
303: // Test for method boolean java.util.WeakHashMap.isEmpty()
304: whm = new WeakHashMap();
305: assertTrue("New map should be empty", whm.isEmpty());
306: Object myObject = new Object();
307: whm.put(myObject, myObject);
308: assertTrue("Map should not be empty", !whm.isEmpty());
309: whm.remove(myObject);
310: assertTrue("Map with elements removed should be empty", whm
311: .isEmpty());
312: }
313:
314: /**
315: * @tests java.util.WeakHashMap#put(java.lang.Object, java.lang.Object)
316: */
317: public void test_putLjava_lang_ObjectLjava_lang_Object() {
318: // Test for method java.lang.Object
319: // java.util.WeakHashMap.put(java.lang.Object, java.lang.Object)
320: WeakHashMap map = new WeakHashMap();
321: map.put(null, "value"); // add null key
322: System.gc();
323: System.runFinalization();
324: map.remove("nothing"); // Cause objects in queue to be removed
325: assertEquals("null key was removed", 1, map.size());
326: }
327:
328: /**
329: * @tests java.util.WeakHashMap#putAll(java.util.Map)
330: */
331: public void test_putAllLjava_util_Map() {
332: Map mockMap = new MockMap();
333: WeakHashMap map = new WeakHashMap();
334: map.putAll(mockMap);
335: assertEquals("Size should be 0", 0, map.size());
336: }
337:
338: /**
339: * @tests java.util.WeakHashMap#remove(java.lang.Object)
340: */
341: public void test_removeLjava_lang_Object() {
342: // Test for method java.lang.Object
343: // java.util.WeakHashMap.remove(java.lang.Object)
344: whm = new WeakHashMap();
345: for (int i = 0; i < 100; i++)
346: whm.put(keyArray[i], valueArray[i]);
347:
348: assertTrue("Remove returned incorrect value", whm
349: .remove(keyArray[25]) == valueArray[25]);
350: assertNull("Remove returned incorrect value", whm
351: .remove(keyArray[25]));
352: assertEquals("Size should be 99 after remove", 99, whm.size());
353: }
354:
355: /**
356: * @tests java.util.WeakHashMap#size()
357: */
358: public void test_size() {
359: // Test for method int java.util.WeakHashMap.size()
360: assertTrue("Used to test", true);
361: }
362:
363: /**
364: * @tests java.util.WeakHashMap#keySet()
365: */
366: public void test_keySet() {
367: // Test for method java.util.Set java.util.WeakHashMap.keySet()
368: whm = new WeakHashMap();
369: for (int i = 0; i < 100; i++)
370: whm.put(keyArray[i], valueArray[i]);
371:
372: List keys = Arrays.asList(keyArray);
373: List values = Arrays.asList(valueArray);
374:
375: Set keySet = whm.keySet();
376: assertEquals("Incorrect number of keys returned,", 100, keySet
377: .size());
378: Iterator it = keySet.iterator();
379: while (it.hasNext()) {
380: Object key = it.next();
381: assertTrue("Invalid map entry returned--bad key", keys
382: .contains(key));
383: }
384: keys = null;
385: values = null;
386: keyArray[50] = null;
387:
388: int count = 0;
389: do {
390: System.gc();
391: System.gc();
392: Runtime.getRuntime().runFinalization();
393: count++;
394: } while (count <= 5 && keySet.size() == 100);
395:
396: assertEquals("Incorrect number of keys returned after gc,", 99,
397: keySet.size());
398: }
399:
400: /**
401: * Regression test for HARMONY-3883
402: * @tests java.util.WeakHashMap#keySet()
403: */
404: public void test_keySet_hasNext() {
405: WeakHashMap map = new WeakHashMap();
406: ConstantHashClass cl = new ConstantHashClass(2);
407: map.put(new ConstantHashClass(1), null);
408: map.put(cl, null);
409: map.put(new ConstantHashClass(3), null);
410: Iterator iter = map.keySet().iterator();
411: iter.next();
412: iter.next();
413: System.gc();
414: assertFalse("Wrong hasNext() value", iter.hasNext());
415: }
416:
417: static class ConstantHashClass {
418: private int id = 0;
419:
420: public ConstantHashClass(int id) {
421: this .id = id;
422: }
423:
424: public int hashCode() {
425: return 0;
426: }
427:
428: public String toString() {
429: return "ConstantHashClass[id=" + id + "]";
430: }
431: }
432:
433: /**
434: * @tests java.util.WeakHashMap#values()
435: */
436: public void test_values() {
437: // Test for method java.util.Set java.util.WeakHashMap.values()
438: whm = new WeakHashMap();
439: for (int i = 0; i < 100; i++)
440: whm.put(keyArray[i], valueArray[i]);
441:
442: List keys = Arrays.asList(keyArray);
443: List values = Arrays.asList(valueArray);
444:
445: Collection valuesCollection = whm.values();
446: assertEquals("Incorrect number of keys returned,", 100,
447: valuesCollection.size());
448: Iterator it = valuesCollection.iterator();
449: while (it.hasNext()) {
450: Object value = it.next();
451: assertTrue("Invalid map entry returned--bad value", values
452: .contains(value));
453: }
454: keys = null;
455: values = null;
456: keyArray[50] = null;
457:
458: int count = 0;
459: do {
460: System.gc();
461: System.gc();
462: Runtime.getRuntime().runFinalization();
463: count++;
464: } while (count <= 5 && valuesCollection.size() == 100);
465:
466: assertEquals("Incorrect number of keys returned after gc,", 99,
467: valuesCollection.size());
468: }
469:
470: /**
471: * Sets up the fixture, for example, open a network connection. This method
472: * is called before a test is executed.
473: */
474: protected void setUp() {
475: for (int i = 0; i < 100; i++) {
476: keyArray[i] = new Object();
477: valueArray[i] = new Object();
478: }
479:
480: }
481:
482: /**
483: * Tears down the fixture, for example, close a network connection. This
484: * method is called after a test is executed.
485: */
486: protected void tearDown() {
487: }
488: }
|