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 tests.api.java.util;
19
20import dalvik.annotation.TestTargetNew;
21import dalvik.annotation.TestTargets;
22import dalvik.annotation.TestLevel;
23import dalvik.annotation.TestTargetClass;
24
25import java.util.AbstractMap;
26import java.util.Arrays;
27import java.util.Collection;
28import java.util.Iterator;
29import java.util.List;
30import java.util.Map;
31import java.util.Set;
32import java.util.WeakHashMap;
33
34import tests.support.Support_MapTest2;
35
36@TestTargetClass(WeakHashMap.class)
37public class WeakHashMapTest extends junit.framework.TestCase {
38    class MockMap extends AbstractMap {
39        public Set entrySet() {
40            return null;
41        }
42        public int size(){
43            return 0;
44        }
45    }
46
47    Object[] keyArray = new Object[100];
48
49    Object[] valueArray = new Object[100];
50
51    WeakHashMap whm;
52
53    /**
54     * @tests java.util.WeakHashMap#WeakHashMap()
55     */
56    @TestTargets({
57        @TestTargetNew(
58            level = TestLevel.COMPLETE,
59            notes = "",
60            method = "WeakHashMap",
61            args = {}
62        ),
63        @TestTargetNew(
64            level = TestLevel.COMPLETE,
65            notes = "",
66            method = "get",
67            args = {java.lang.Object.class}
68        )
69    })
70    public void test_Constructor() {
71        // Test for method java.util.WeakHashMap()
72        new Support_MapTest2(new WeakHashMap()).runTest();
73
74        whm = new WeakHashMap();
75        for (int i = 0; i < 100; i++)
76            whm.put(keyArray[i], valueArray[i]);
77        for (int i = 0; i < 100; i++)
78            assertTrue("Incorrect value retrieved",
79                    whm.get(keyArray[i]) == valueArray[i]);
80
81    }
82
83    /**
84     * @tests java.util.WeakHashMap#WeakHashMap(int)
85     */
86    @TestTargets({
87        @TestTargetNew(
88            level = TestLevel.COMPLETE,
89            notes = "",
90            method = "WeakHashMap",
91            args = {int.class}
92        ),
93        @TestTargetNew(
94            level = TestLevel.COMPLETE,
95            notes = "",
96            method = "get",
97            args = {java.lang.Object.class}
98        )
99    })
100    public void test_ConstructorI() {
101        // Test for method java.util.WeakHashMap(int)
102        whm = new WeakHashMap(50);
103        for (int i = 0; i < 100; i++)
104            whm.put(keyArray[i], valueArray[i]);
105        for (int i = 0; i < 100; i++)
106            assertTrue("Incorrect value retrieved",
107                    whm.get(keyArray[i]) == valueArray[i]);
108
109        WeakHashMap empty = new WeakHashMap(0);
110        assertNull("Empty weakhashmap access", empty.get("nothing"));
111        empty.put("something", "here");
112        assertTrue("cannot get element", empty.get("something") == "here");
113
114        try {
115            new WeakHashMap(-50);
116            fail("IllegalArgumentException expected");
117        } catch (IllegalArgumentException e) {
118            //expected
119        }
120    }
121
122    /**
123     * @tests java.util.WeakHashMap#WeakHashMap(int, float)
124     */
125    @TestTargets({
126        @TestTargetNew(
127            level = TestLevel.COMPLETE,
128            notes = "",
129            method = "WeakHashMap",
130            args = {int.class, float.class}
131        ),
132        @TestTargetNew(
133            level = TestLevel.COMPLETE,
134            notes = "",
135            method = "get",
136            args = {java.lang.Object.class}
137        )
138    })
139    public void test_ConstructorIF() {
140        // Test for method java.util.WeakHashMap(int, float)
141        whm = new WeakHashMap(50, 0.5f);
142        for (int i = 0; i < 100; i++)
143            whm.put(keyArray[i], valueArray[i]);
144        for (int i = 0; i < 100; i++)
145            assertTrue("Incorrect value retrieved",
146                    whm.get(keyArray[i]) == valueArray[i]);
147
148        WeakHashMap empty = new WeakHashMap(0, 0.75f);
149        assertNull("Empty hashtable access", empty.get("nothing"));
150        empty.put("something", "here");
151        assertTrue("cannot get element", empty.get("something") == "here");
152
153        try {
154            new WeakHashMap(50, -0.5f);
155            fail("IllegalArgumentException expected");
156        } catch (IllegalArgumentException e) {
157            //expected
158        }
159
160        try {
161            new WeakHashMap(-50, 0.5f);
162            fail("IllegalArgumentException expected");
163        } catch (IllegalArgumentException e) {
164            //expected
165        }
166    }
167
168    /**
169     * @tests java.util.WeakHashMap#WeakHashMap(java.util.Map)
170     */
171    @TestTargetNew(
172        level = TestLevel.COMPLETE,
173        notes = "",
174        method = "WeakHashMap",
175        args = {java.util.Map.class}
176    )
177    public void test_ConstructorLjava_util_Map() {
178        Map mockMap = new MockMap();
179        WeakHashMap map = new WeakHashMap(mockMap);
180        assertEquals("Size should be 0", 0, map.size());
181
182        try {
183            new WeakHashMap(null);
184            fail("NullPointerException expected");
185        } catch (NullPointerException e) {
186            //expected
187        }
188    }
189
190    /**
191     * @tests java.util.WeakHashMap#clear()
192     */
193    @TestTargets({
194        @TestTargetNew(
195            level = TestLevel.COMPLETE,
196            notes = "",
197            method = "clear",
198            args = {}
199        ),
200        @TestTargetNew(
201            level = TestLevel.COMPLETE,
202            notes = "",
203            method = "get",
204            args = {java.lang.Object.class}
205        )
206    })
207    public void test_clear() {
208        // Test for method boolean java.util.WeakHashMap.clear()
209        whm = new WeakHashMap();
210        for (int i = 0; i < 100; i++)
211            whm.put(keyArray[i], valueArray[i]);
212        whm.clear();
213        assertTrue("Cleared map should be empty", whm.isEmpty());
214        for (int i = 0; i < 100; i++)
215            assertNull("Cleared map should only return null", whm
216                    .get(keyArray[i]));
217
218    }
219
220    /**
221     * @tests java.util.WeakHashMap#containsKey(java.lang.Object)
222     */
223    @TestTargetNew(
224        level = TestLevel.COMPLETE,
225        notes = "",
226        method = "containsKey",
227        args = {java.lang.Object.class}
228    )
229    public void test_containsKeyLjava_lang_Object() {
230        // Test for method boolean java.util.WeakHashMap.containsKey()
231        whm = new WeakHashMap();
232        for (int i = 0; i < 100; i++)
233            whm.put(keyArray[i], valueArray[i]);
234        for (int i = 0; i < 100; i++)
235            assertTrue("Should contain referenced key", whm
236                    .containsKey(keyArray[i]));
237        keyArray[25] = null;
238        keyArray[50] = null;
239    }
240
241    /**
242     * @tests java.util.WeakHashMap#containsValue(java.lang.Object)
243     */
244    @TestTargetNew(
245        level = TestLevel.COMPLETE,
246        notes = "",
247        method = "containsValue",
248        args = {java.lang.Object.class}
249    )
250    public void test_containsValueLjava_lang_Object() {
251        // Test for method boolean java.util.WeakHashMap.containsValue()
252        whm = new WeakHashMap();
253        for (int i = 0; i < 100; i++)
254            whm.put(keyArray[i], valueArray[i]);
255        for (int i = 0; i < 100; i++)
256            assertTrue("Should contain referenced value", whm
257                    .containsValue(valueArray[i]));
258        keyArray[25] = null;
259        keyArray[50] = null;
260    }
261
262    /**
263     * @tests java.util.WeakHashMap#entrySet()
264     */
265    @TestTargetNew(
266        level = TestLevel.COMPLETE,
267        notes = "",
268        method = "entrySet",
269        args = {}
270    )
271    public void test_entrySet() {
272        // Test for method java.util.Set java.util.WeakHashMap.entrySet()
273        whm = new WeakHashMap();
274        for (int i = 0; i < 100; i++)
275            whm.put(keyArray[i], valueArray[i]);
276        List keys = Arrays.asList(keyArray);
277        List values = Arrays.asList(valueArray);
278        Set entrySet = whm.entrySet();
279        assertTrue("Incorrect number of entries returned--wanted 100, got: "
280                + entrySet.size(), entrySet.size() == 100);
281        Iterator it = entrySet.iterator();
282        while (it.hasNext()) {
283            Map.Entry entry = (Map.Entry) it.next();
284            assertTrue("Invalid map entry returned--bad key", keys
285                    .contains(entry.getKey()));
286            assertTrue("Invalid map entry returned--bad key", values
287                    .contains(entry.getValue()));
288        }
289        keys = null;
290        values = null;
291        keyArray[50] = null;
292
293        int count = 0;
294        do {
295            System.gc();
296            System.gc();
297            Runtime.getRuntime().runFinalization();
298            count++;
299        } while (count <= 5 && entrySet.size() == 100);
300
301        assertTrue(
302                "Incorrect number of entries returned after gc--wanted 99, got: "
303                        + entrySet.size(), entrySet.size() == 99);
304    }
305
306    /**
307     * @tests java.util.WeakHashMap#isEmpty()
308     */
309    @TestTargetNew(
310        level = TestLevel.COMPLETE,
311        notes = "",
312        method = "isEmpty",
313        args = {}
314    )
315    public void test_isEmpty() {
316        // Test for method boolean java.util.WeakHashMap.isEmpty()
317        whm = new WeakHashMap();
318        assertTrue("New map should be empty", whm.isEmpty());
319        Object myObject = new Object();
320        whm.put(myObject, myObject);
321        assertTrue("Map should not be empty", !whm.isEmpty());
322        whm.remove(myObject);
323        assertTrue("Map with elements removed should be empty", whm.isEmpty());
324    }
325
326    /**
327     * @tests java.util.WeakHashMap#put(java.lang.Object, java.lang.Object)
328     */
329    @TestTargetNew(
330        level = TestLevel.COMPLETE,
331        notes = "",
332        method = "put",
333        args = {java.lang.Object.class, java.lang.Object.class}
334    )
335    public void test_putLjava_lang_ObjectLjava_lang_Object() {
336        // Test for method java.lang.Object
337        // java.util.WeakHashMap.put(java.lang.Object, java.lang.Object)
338        WeakHashMap map = new WeakHashMap();
339        map.put(null, "value"); // add null key
340        System.gc();
341        System.runFinalization();
342        map.remove("nothing"); // Cause objects in queue to be removed
343        assertEquals("null key was removed", 1, map.size());
344    }
345
346    /**
347     * @tests java.util.WeakHashMap#putAll(java.util.Map)
348     */
349    @TestTargetNew(
350        level = TestLevel.COMPLETE,
351        notes = "",
352        method = "putAll",
353        args = {java.util.Map.class}
354    )
355    public void test_putAllLjava_util_Map() {
356        Map mockMap=new MockMap();
357        WeakHashMap map = new WeakHashMap();
358        map.putAll(mockMap);
359        assertEquals("Size should be 0", 0, map.size());
360
361        try {
362            map.putAll(null);
363            fail("NullPointerException exected");
364        } catch (NullPointerException e) {
365            //expected
366        }
367    }
368
369    /**
370     * @tests java.util.WeakHashMap#remove(java.lang.Object)
371     */
372    @TestTargetNew(
373        level = TestLevel.COMPLETE,
374        notes = "",
375        method = "remove",
376        args = {java.lang.Object.class}
377    )
378    public void test_removeLjava_lang_Object() {
379        // Test for method java.lang.Object
380        // java.util.WeakHashMap.remove(java.lang.Object)
381        whm = new WeakHashMap();
382        for (int i = 0; i < 100; i++)
383            whm.put(keyArray[i], valueArray[i]);
384
385        assertTrue("Remove returned incorrect value",
386                whm.remove(keyArray[25]) == valueArray[25]);
387        assertNull("Remove returned incorrect value",
388                whm.remove(keyArray[25]));
389        assertEquals("Size should be 99 after remove", 99, whm.size());
390    }
391
392    /**
393     * @tests java.util.WeakHashMap#size()
394     */
395    @TestTargetNew(
396        level = TestLevel.COMPLETE,
397        notes = "",
398        method = "size",
399        args = {}
400    )
401    public void test_size() {
402        whm = new WeakHashMap();
403        assertEquals(0, whm.size());
404    }
405
406    /**
407     * @tests java.util.WeakHashMap#keySet()
408     */
409    @TestTargets({
410        @TestTargetNew(
411            level = TestLevel.COMPLETE,
412            notes = "",
413            method = "keySet",
414            args = {}
415        ),
416        @TestTargetNew(
417            level = TestLevel.COMPLETE,
418            notes = "",
419            method = "size",
420            args = {}
421        )
422    })
423    public void test_keySet() {
424        // Test for method java.util.Set java.util.WeakHashMap.keySet()
425        whm = new WeakHashMap();
426        for (int i = 0; i < 100; i++)
427            whm.put(keyArray[i], valueArray[i]);
428
429        List keys = Arrays.asList(keyArray);
430        List values = Arrays.asList(valueArray);
431
432        Set keySet = whm.keySet();
433        assertEquals("Incorrect number of keys returned,", 100, keySet.size());
434        Iterator it = keySet.iterator();
435        while (it.hasNext()) {
436            Object key = it.next();
437            assertTrue("Invalid map entry returned--bad key", keys
438                    .contains(key));
439        }
440        keys = null;
441        values = null;
442        keyArray[50] = null;
443
444        int count = 0;
445        do {
446            System.gc();
447            System.gc();
448            Runtime.getRuntime().runFinalization();
449            count++;
450        } while (count <= 5 && keySet.size() == 100);
451
452        assertEquals("Incorrect number of keys returned after gc,", 99, keySet
453                .size());
454    }
455
456    /**
457     * @tests java.util.WeakHashMap#values()
458     */
459    @TestTargets({
460        @TestTargetNew(
461            level = TestLevel.COMPLETE,
462            notes = "",
463            method = "values",
464            args = {}
465        ),
466        @TestTargetNew(
467            level = TestLevel.COMPLETE,
468            notes = "",
469            method = "size",
470            args = {}
471        )
472    })
473    public void test_values() {
474        // Test for method java.util.Set java.util.WeakHashMap.values()
475        whm = new WeakHashMap();
476        for (int i = 0; i < 100; i++)
477            whm.put(keyArray[i], valueArray[i]);
478
479        List keys = Arrays.asList(keyArray);
480        List values = Arrays.asList(valueArray);
481
482        Collection valuesCollection = whm.values();
483        assertEquals("Incorrect number of keys returned,", 100,
484                valuesCollection.size());
485        Iterator it = valuesCollection.iterator();
486        while (it.hasNext()) {
487            Object value = it.next();
488            assertTrue("Invalid map entry returned--bad value", values
489                    .contains(value));
490        }
491        keys = null;
492        values = null;
493        keyArray[50] = null;
494
495        int count = 0;
496        do {
497            System.gc();
498            System.gc();
499            Runtime.getRuntime().runFinalization();
500            count++;
501        } while (count <= 5 && valuesCollection.size() == 100);
502
503        assertEquals("Incorrect number of keys returned after gc,", 99,
504                valuesCollection.size());
505    }
506
507    /**
508     * Sets up the fixture, for example, open a network connection. This method
509     * is called before a test is executed.
510     */
511    protected void setUp() {
512        for (int i = 0; i < 100; i++) {
513            keyArray[i] = new Object();
514            valueArray[i] = new Object();
515        }
516
517    }
518
519    /**
520     * Tears down the fixture, for example, close a network connection. This
521     * method is called after a test is executed.
522     */
523    protected void tearDown() {
524    }
525}
526