KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > collections > 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;
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.map.AbstractTestMap;
24
25 /**
26  * Tests for ReferenceMap.
27  *
28  * @version $Revision: 1.19 $ $Date: 2004/05/03 22:03:06 $
29  *
30  * @author Paul Jack
31  */

32 public class TestReferenceMap extends AbstractTestMap {
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 "2.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
155     /** Tests whether purge values setting works */
156     public void testPurgeValues() throws Exception JavaDoc {
157         // many thanks to Juozas Baliuka for suggesting this method
158
Object JavaDoc key = new Object JavaDoc();
159         Object JavaDoc value = new Object JavaDoc();
160         
161         WeakReference JavaDoc keyReference = new WeakReference JavaDoc(key);
162         WeakReference JavaDoc valueReference = new WeakReference JavaDoc(value);
163         
164         Map JavaDoc testMap = new ReferenceMap(ReferenceMap.WEAK, ReferenceMap.HARD, true);
165         testMap.put(key, value);
166         
167         assertEquals("In map", value, testMap.get(key));
168         assertNotNull("Weak reference released early (1)", keyReference.get());
169         assertNotNull("Weak reference released early (2)", valueReference.get());
170         
171         // dereference strong references
172
key = null;
173         value = null;
174         
175         int iterations = 0;
176         int bytz = 2;
177         while(true) {
178             System.gc();
179             if(iterations++ > 50){
180                 fail("Max iterations reached before resource released.");
181             }
182             testMap.isEmpty();
183             if(
184                 keyReference.get() == null &&
185                 valueReference.get() == null) {
186                 break;
187                 
188             } else {
189                 // create garbage:
190
byte[] b = new byte[bytz];
191                 bytz = bytz * 2;
192             }
193         }
194     }
195     
196     private static void gc() {
197         try {
198             // trigger GC
199
byte[][] tooLarge = new byte[1000000000][1000000000];
200             fail("you have too much RAM");
201         } catch (OutOfMemoryError JavaDoc ex) {
202             System.gc(); // ignore
203
}
204     }
205 }
206
Popular Tags