KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * Copyright 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.Iterator JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import junit.framework.Test;
23
24 import org.apache.commons.collections.BulkTest;
25 import org.apache.commons.collections.IterableMap;
26
27 /**
28  * Tests for ReferenceIdentityMap.
29  *
30  * @version $Revision: 1.3 $
31  *
32  * @author Paul Jack
33  * @author Stephen Colebourne
34  */

35 public class TestReferenceIdentityMap extends AbstractTestIterableMap {
36
37     private static final Integer JavaDoc I1A = new Integer JavaDoc(1);
38     private static final Integer JavaDoc I1B = new Integer JavaDoc(1);
39     private static final Integer JavaDoc I2A = new Integer JavaDoc(2);
40     private static final Integer JavaDoc I2B = new Integer JavaDoc(2);
41
42     public TestReferenceIdentityMap(String JavaDoc testName) {
43         super(testName);
44     }
45
46     public static Test suite() {
47         return BulkTest.makeSuite(TestReferenceIdentityMap.class);
48     }
49
50     public static void main(String JavaDoc args[]) {
51         String JavaDoc[] testCaseName = { TestReferenceIdentityMap.class.getName() };
52         junit.textui.TestRunner.main(testCaseName);
53     }
54
55     public Map JavaDoc makeEmptyMap() {
56         ReferenceIdentityMap map = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.WEAK);
57         return map;
58     }
59     
60     public Map JavaDoc makeConfirmedMap() {
61         // Testing against another [collections] class generally isn't a good idea,
62
// but the alternative is a JDK1.4 dependency in the tests
63
return new IdentityMap();
64     }
65
66     public boolean isAllowNullKey() {
67         return false;
68     }
69
70     public boolean isAllowNullValue() {
71         return false;
72     }
73
74     public String JavaDoc getCompatibilityVersion() {
75         return "3.1";
76     }
77
78     //-----------------------------------------------------------------------
79
public void testBasics() {
80         IterableMap map = new ReferenceIdentityMap(ReferenceIdentityMap.HARD, ReferenceIdentityMap.HARD);
81         assertEquals(0, map.size());
82         
83         map.put(I1A, I2A);
84         assertEquals(1, map.size());
85         assertSame(I2A, map.get(I1A));
86         assertSame(null, map.get(I1B));
87         assertEquals(true, map.containsKey(I1A));
88         assertEquals(false, map.containsKey(I1B));
89         assertEquals(true, map.containsValue(I2A));
90         assertEquals(false, map.containsValue(I2B));
91         
92         map.put(I1A, I2B);
93         assertEquals(1, map.size());
94         assertSame(I2B, map.get(I1A));
95         assertSame(null, map.get(I1B));
96         assertEquals(true, map.containsKey(I1A));
97         assertEquals(false, map.containsKey(I1B));
98         assertEquals(false, map.containsValue(I2A));
99         assertEquals(true, map.containsValue(I2B));
100         
101         map.put(I1B, I2B);
102         assertEquals(2, map.size());
103         assertSame(I2B, map.get(I1A));
104         assertSame(I2B, map.get(I1B));
105         assertEquals(true, map.containsKey(I1A));
106         assertEquals(true, map.containsKey(I1B));
107         assertEquals(false, map.containsValue(I2A));
108         assertEquals(true, map.containsValue(I2B));
109     }
110     
111     //-----------------------------------------------------------------------
112
public void testHashEntry() {
113         IterableMap map = new ReferenceIdentityMap(ReferenceIdentityMap.HARD, ReferenceIdentityMap.HARD);
114         
115         map.put(I1A, I2A);
116         map.put(I1B, I2A);
117         
118         Map.Entry JavaDoc entry1 = (Map.Entry JavaDoc) map.entrySet().iterator().next();
119         Iterator JavaDoc it = map.entrySet().iterator();
120         Map.Entry JavaDoc entry2 = (Map.Entry JavaDoc) it.next();
121         Map.Entry JavaDoc entry3 = (Map.Entry JavaDoc) it.next();
122         
123         assertEquals(true, entry1.equals(entry2));
124         assertEquals(true, entry2.equals(entry1));
125         assertEquals(false, entry1.equals(entry3));
126     }
127     
128     
129     //-----------------------------------------------------------------------
130
public void testNullHandling() {
131         resetFull();
132         assertEquals(null, map.get(null));
133         assertEquals(false, map.containsKey(null));
134         assertEquals(false, map.containsValue(null));
135         assertEquals(null, map.remove(null));
136         assertEquals(false, map.entrySet().contains(null));
137         assertEquals(false, map.keySet().contains(null));
138         assertEquals(false, map.values().contains(null));
139         try {
140             map.put(null, null);
141             fail();
142         } catch (NullPointerException JavaDoc ex) {}
143         try {
144             map.put(new Object JavaDoc(), null);
145             fail();
146         } catch (NullPointerException JavaDoc ex) {}
147         try {
148             map.put(null, new Object JavaDoc());
149             fail();
150         } catch (NullPointerException JavaDoc ex) {}
151     }
152
153     //-----------------------------------------------------------------------
154
/*
155     // Tests often fail because gc is uncontrollable
156
157     public void testPurge() {
158         ReferenceIdentityMap map = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.WEAK);
159         Object[] hard = new Object[10];
160         for (int i = 0; i < hard.length; i++) {
161             hard[i] = new Object();
162             map.put(hard[i], new Object());
163         }
164         gc();
165         assertTrue("map should be empty after purge of weak values", map.isEmpty());
166
167         for (int i = 0; i < hard.length; i++) {
168             map.put(new Object(), hard[i]);
169         }
170         gc();
171         assertTrue("map should be empty after purge of weak keys", map.isEmpty());
172
173         for (int i = 0; i < hard.length; i++) {
174             map.put(new Object(), hard[i]);
175             map.put(hard[i], new Object());
176         }
177
178         gc();
179         assertTrue("map should be empty after purge of weak keys and values", map.isEmpty());
180     }
181
182
183     public void testGetAfterGC() {
184         ReferenceIdentityMap map = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.WEAK);
185         for (int i = 0; i < 10; i++) {
186             map.put(new Integer(i), new Integer(i));
187         }
188
189         gc();
190         for (int i = 0; i < 10; i++) {
191             Integer I = new Integer(i);
192             assertTrue("map.containsKey should return false for GC'd element", !map.containsKey(I));
193             assertTrue("map.get should return null for GC'd element", map.get(I) == null);
194         }
195     }
196
197
198     public void testEntrySetIteratorAfterGC() {
199         ReferenceIdentityMap map = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.WEAK);
200         Object[] hard = new Object[10];
201         for (int i = 0; i < 10; i++) {
202             hard[i] = new Integer(10 + i);
203             map.put(new Integer(i), new Integer(i));
204             map.put(hard[i], hard[i]);
205         }
206
207         gc();
208         Iterator iterator = map.entrySet().iterator();
209         while (iterator.hasNext()) {
210             Map.Entry entry = (Map.Entry)iterator.next();
211             Integer key = (Integer)entry.getKey();
212             Integer value = (Integer)entry.getValue();
213             assertTrue("iterator should skip GC'd keys", key.intValue() >= 10);
214             assertTrue("iterator should skip GC'd values", value.intValue() >= 10);
215         }
216
217     }
218
219     public void testMapIteratorAfterGC() {
220         ReferenceIdentityMap map = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.WEAK);
221         Object[] hard = new Object[10];
222         for (int i = 0; i < 10; i++) {
223             hard[i] = new Integer(10 + i);
224             map.put(new Integer(i), new Integer(i));
225             map.put(hard[i], hard[i]);
226         }
227
228         gc();
229         MapIterator iterator = map.mapIterator();
230         while (iterator.hasNext()) {
231             Object key1 = iterator.next();
232             Integer key = (Integer) iterator.getKey();
233             Integer value = (Integer) iterator.getValue();
234             assertTrue("iterator keys should match", key == key1);
235             assertTrue("iterator should skip GC'd keys", key.intValue() >= 10);
236             assertTrue("iterator should skip GC'd values", value.intValue() >= 10);
237         }
238
239     }
240
241     public void testMapIteratorAfterGC2() {
242         ReferenceIdentityMap map = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.WEAK);
243         Object[] hard = new Object[10];
244         for (int i = 0; i < 10; i++) {
245             hard[i] = new Integer(10 + i);
246             map.put(new Integer(i), new Integer(i));
247             map.put(hard[i], hard[i]);
248         }
249
250         MapIterator iterator = map.mapIterator();
251         while (iterator.hasNext()) {
252             Object key1 = iterator.next();
253             gc();
254             Integer key = (Integer) iterator.getKey();
255             Integer value = (Integer) iterator.getValue();
256             assertTrue("iterator keys should match", key == key1);
257             assertTrue("iterator should skip GC'd keys", key.intValue() >= 10);
258             assertTrue("iterator should skip GC'd values", value.intValue() >= 10);
259         }
260
261     }
262 */

263
264     /** Tests whether purge values setting works */
265     public void testPurgeValues() throws Exception JavaDoc {
266         // many thanks to Juozas Baliuka for suggesting this method
267
Object JavaDoc key = new Object JavaDoc();
268         Object JavaDoc value = new Object JavaDoc();
269         
270         WeakReference JavaDoc keyReference = new WeakReference JavaDoc(key);
271         WeakReference JavaDoc valueReference = new WeakReference JavaDoc(value);
272         
273         Map JavaDoc testMap = new ReferenceIdentityMap(ReferenceIdentityMap.WEAK, ReferenceIdentityMap.HARD, true);
274         testMap.put(key, value);
275         
276         assertEquals("In map", value, testMap.get(key));
277         assertNotNull("Weak reference released early (1)", keyReference.get());
278         assertNotNull("Weak reference released early (2)", valueReference.get());
279         
280         // dereference strong references
281
key = null;
282         value = null;
283         
284         int iterations = 0;
285         int bytz = 2;
286         while(true) {
287             System.gc();
288             if(iterations++ > 50){
289                 fail("Max iterations reached before resource released.");
290             }
291             testMap.isEmpty();
292             if(
293                 keyReference.get() == null &&
294                 valueReference.get() == null) {
295                 break;
296                 
297             } else {
298                 // create garbage:
299
byte[] b = new byte[bytz];
300                 bytz = bytz * 2;
301             }
302         }
303     }
304
305     private static void gc() {
306         try {
307             // trigger GC
308
byte[][] tooLarge = new byte[1000000000][1000000000];
309             fail("you have too much RAM");
310         } catch (OutOfMemoryError JavaDoc ex) {
311             System.gc(); // ignore
312
}
313     }
314
315 }
316
Popular Tags