KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > map > TestReferenceMap


1 /*
2  * Copyright 2001-2004 The Apache Software Foundation
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.collections.map;
17
18 import java.lang.ref.WeakReference JavaDoc;
19 import java.util.Map JavaDoc;
20
21 import junit.framework.Test;
22
23 import org.apache.commons.collections.BulkTest;
24
25 /**
26  * Tests for ReferenceMap.
27  *
28  * @version $Revision: 1.8 $ $Date: 2004/05/03 22:03:06 $
29  *
30  * @author Paul Jack
31  */

32 public class TestReferenceMap extends AbstractTestIterableMap {
33
34     public TestReferenceMap(String JavaDoc testName) {
35         super(testName);
36     }
37
38     public static Test suite() {
39         return BulkTest.makeSuite(TestReferenceMap.class);
40     }
41
42     public static void main(String JavaDoc args[]) {
43         String JavaDoc[] testCaseName = { TestReferenceMap.class.getName() };
44         junit.textui.TestRunner.main(testCaseName);
45     }
46
47     public Map JavaDoc makeEmptyMap() {
48         ReferenceMap map = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.WEAK);
49         return map;
50     }
51
52     public boolean isAllowNullKey() {
53         return false;
54     }
55
56     public boolean isAllowNullValue() {
57         return false;
58     }
59
60     public String JavaDoc getCompatibilityVersion() {
61         return "3.1";
62     }
63
64     //-----------------------------------------------------------------------
65
public void testNullHandling() {
66         resetFull();
67         assertEquals(null, map.get(null));
68         assertEquals(false, map.containsKey(null));
69         assertEquals(false, map.containsValue(null));
70         assertEquals(null, map.remove(null));
71         assertEquals(false, map.entrySet().contains(null));
72         assertEquals(false, map.keySet().contains(null));
73         assertEquals(false, map.values().contains(null));
74         try {
75             map.put(null, null);
76             fail();
77         } catch (NullPointerException JavaDoc ex) {}
78         try {
79             map.put(new Object JavaDoc(), null);
80             fail();
81         } catch (NullPointerException JavaDoc ex) {}
82         try {
83             map.put(null, new Object JavaDoc());
84             fail();
85         } catch (NullPointerException JavaDoc ex) {}
86     }
87
88     //-----------------------------------------------------------------------
89
/*
90     // Tests often fail because gc is uncontrollable
91
92     public void testPurge() {
93         ReferenceMap map = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.WEAK);
94         Object[] hard = new Object[10];
95         for (int i = 0; i < hard.length; i++) {
96             hard[i] = new Object();
97             map.put(hard[i], new Object());
98         }
99         gc();
100         assertTrue("map should be empty after purge of weak values", map.isEmpty());
101
102         for (int i = 0; i < hard.length; i++) {
103             map.put(new Object(), hard[i]);
104         }
105         gc();
106         assertTrue("map should be empty after purge of weak keys", map.isEmpty());
107
108         for (int i = 0; i < hard.length; i++) {
109             map.put(new Object(), hard[i]);
110             map.put(hard[i], new Object());
111         }
112
113         gc();
114         assertTrue("map should be empty after purge of weak keys and values", map.isEmpty());
115     }
116
117
118     public void testGetAfterGC() {
119         ReferenceMap map = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.WEAK);
120         for (int i = 0; i < 10; i++) {
121             map.put(new Integer(i), new Integer(i));
122         }
123
124         gc();
125         for (int i = 0; i < 10; i++) {
126             Integer I = new Integer(i);
127             assertTrue("map.containsKey should return false for GC'd element", !map.containsKey(I));
128             assertTrue("map.get should return null for GC'd element", map.get(I) == null);
129         }
130     }
131
132
133     public void testEntrySetIteratorAfterGC() {
134         ReferenceMap map = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.WEAK);
135         Object[] hard = new Object[10];
136         for (int i = 0; i < 10; i++) {
137             hard[i] = new Integer(10 + i);
138             map.put(new Integer(i), new Integer(i));
139             map.put(hard[i], hard[i]);
140         }
141
142         gc();
143         Iterator iterator = map.entrySet().iterator();
144         while (iterator.hasNext()) {
145             Map.Entry entry = (Map.Entry)iterator.next();
146             Integer key = (Integer)entry.getKey();
147             Integer value = (Integer)entry.getValue();
148             assertTrue("iterator should skip GC'd keys", key.intValue() >= 10);
149             assertTrue("iterator should skip GC'd values", value.intValue() >= 10);
150         }
151
152     }
153
154     public void testMapIteratorAfterGC() {
155         ReferenceMap map = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.WEAK);
156         Object[] hard = new Object[10];
157         for (int i = 0; i < 10; i++) {
158             hard[i] = new Integer(10 + i);
159             map.put(new Integer(i), new Integer(i));
160             map.put(hard[i], hard[i]);
161         }
162
163         gc();
164         MapIterator iterator = map.mapIterator();
165         while (iterator.hasNext()) {
166             Object key1 = iterator.next();
167             Integer key = (Integer) iterator.getKey();
168             Integer value = (Integer) iterator.getValue();
169             assertTrue("iterator keys should match", key == key1);
170             assertTrue("iterator should skip GC'd keys", key.intValue() >= 10);
171             assertTrue("iterator should skip GC'd values", value.intValue() >= 10);
172         }
173
174     }
175
176     public void testMapIteratorAfterGC2() {
177         ReferenceMap map = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.WEAK);
178         Object[] hard = new Object[10];
179         for (int i = 0; i < 10; i++) {
180             hard[i] = new Integer(10 + i);
181             map.put(new Integer(i), new Integer(i));
182             map.put(hard[i], hard[i]);
183         }
184
185         MapIterator iterator = map.mapIterator();
186         while (iterator.hasNext()) {
187             Object key1 = iterator.next();
188             gc();
189             Integer key = (Integer) iterator.getKey();
190             Integer value = (Integer) iterator.getValue();
191             assertTrue("iterator keys should match", key == key1);
192             assertTrue("iterator should skip GC'd keys", key.intValue() >= 10);
193             assertTrue("iterator should skip GC'd values", value.intValue() >= 10);
194         }
195
196     }
197 */

198
199     /** Tests whether purge values setting works */
200     public void testPurgeValues() throws Exception JavaDoc {
201         // many thanks to Juozas Baliuka for suggesting this method
202
Object JavaDoc key = new Object JavaDoc();
203         Object JavaDoc value = new Object JavaDoc();
204         
205         WeakReference JavaDoc keyReference = new WeakReference JavaDoc(key);
206         WeakReference JavaDoc valueReference = new WeakReference JavaDoc(value);
207         
208         Map JavaDoc testMap = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.HARD, true);
209         testMap.put(key, value);
210         
211         assertEquals("In map", value, testMap.get(key));
212         assertNotNull("Weak reference released early (1)", keyReference.get());
213         assertNotNull("Weak reference released early (2)", valueReference.get());
214         
215         // dereference strong references
216
key = null;
217         value = null;
218         
219         int iterations = 0;
220         int bytz = 2;
221         while(true) {
222             System.gc();
223             if(iterations++ > 50){
224                 fail("Max iterations reached before resource released.");
225             }
226             testMap.isEmpty();
227             if(
228                 keyReference.get() == null &&
229                 valueReference.get() == null) {
230                 break;
231                 
232             } else {
233                 // create garbage:
234
byte[] b = new byte[bytz];
235                 bytz = bytz * 2;
236             }
237         }
238     }
239
240     private static void gc() {
241         try {
242             // trigger GC
243
byte[][] tooLarge = new byte[1000000000][1000000000];
244             fail("you have too much RAM");
245         } catch (OutOfMemoryError JavaDoc ex) {
246             System.gc(); // ignore
247
}
248     }
249
250 }
251
Popular Tags