KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > nutch > util > TestSoftHashMap


1 /* Copyright (c) 2003 The Nutch Organization. All rights reserved. */
2 /* Use subject to the conditions in http://www.nutch.org/LICENSE.txt. */
3
4 package net.nutch.util;
5
6 import junit.framework.TestCase;
7
8 import java.util.ArrayList JavaDoc;
9 import java.util.Iterator JavaDoc;
10
11 /**
12  * Unit tests for SoftHashMap.
13  */

14 public class TestSoftHashMap extends TestCase {
15
16   // set to true to get flood of status messages on stderr- useful
17
// for seeing when JVM is collecting everything.
18
private static final boolean verbose= false;
19
20   // 1kB for int[]
21
private static final int TEST_VALUE_ARRAY_SIZE= 1024 / 4;
22
23   private static final int BASIC_OPS_SIZE= 10;
24
25   private boolean keyHasBeenFinalized;
26   private boolean valHasBeenFinalized;
27
28   private class TestKey {
29     Integer JavaDoc key;
30     boolean notify;
31
32     TestKey(Integer JavaDoc key, boolean notify) {
33       this.key= key;
34       this.notify= notify;
35     }
36
37     protected void finalize() {
38       if (notify)
39         TestSoftHashMap.this.keyFinalized(key);
40     }
41
42     public int hashCode() {
43       return key.hashCode();
44     }
45
46     public boolean equals(Object JavaDoc o) {
47       if (o == null)
48         return false;
49       if ( !(o instanceof TestKey) )
50         return false;
51       TestKey other= (TestKey) o;
52       return (this.key.equals(other.key));
53     }
54
55     public String JavaDoc toString() {
56       return "Key:"+key;
57     }
58
59   }
60
61   private class TestValue implements SoftHashMap.FinalizationNotifier {
62     int[] val;
63     boolean notify;
64     ArrayList JavaDoc finalizationListeners;
65
66     TestValue(int key, boolean notify) {
67       this.val= new int[TEST_VALUE_ARRAY_SIZE];
68       this.val[0]= key;
69       this.notify= notify;
70       this.finalizationListeners= new ArrayList JavaDoc();
71     }
72
73     public void addFinalizationListener(SoftHashMap.FinalizationListener
74                                         listener) {
75       finalizationListeners.add(listener);
76     }
77
78     protected void finalize() {
79       if (notify)
80         TestSoftHashMap.this.valFinalized(val[0]);
81       for (Iterator JavaDoc iter= finalizationListeners.iterator();
82            iter.hasNext() ; ) {
83         SoftHashMap.FinalizationListener l=
84           (SoftHashMap.FinalizationListener) iter.next();
85         l.finalizationOccurring();
86       }
87     }
88
89     boolean isMyKey(int key) {
90       return key == val[0];
91     }
92
93     public String JavaDoc toString() {
94       return "Val:"+val[0];
95     }
96
97   }
98
99   public TestSoftHashMap(String JavaDoc name) {
100     super(name);
101   }
102
103   public void testBasicOps() {
104     SoftHashMap shm= new SoftHashMap();
105
106     // cache keys & vals so they don't go away
107
TestKey[] keys= new TestKey[BASIC_OPS_SIZE];
108     TestValue[] vals= new TestValue[BASIC_OPS_SIZE];
109
110     for (int i= 0; i < BASIC_OPS_SIZE; i++) {
111       keys[i]= new TestKey(new Integer JavaDoc(i), false);
112       vals[i]= new TestValue(i, false);
113       shm.put(keys[i], vals[i]);
114     }
115
116     for (int i= 0; i < BASIC_OPS_SIZE; i++) {
117       TestValue v= (TestValue) shm.get(new TestKey(new Integer JavaDoc(i), false));
118       assertTrue("got back null, expecting value! (key= "+i+")", v != null);
119       assertTrue("got back wrong value (isMyKey())!", v.isMyKey(i));
120       assertTrue("got back wrong value (!=)!", v == vals[i]);
121       assertTrue("contains key doesn't have " + i,
122                  shm.containsKey(new TestKey(new Integer JavaDoc(i), false)));
123       assertTrue("isEmpty returns true when it shouldn't",
124                  !shm.isEmpty());
125     }
126
127     Object JavaDoc removed= shm.remove(
128       new TestKey(new Integer JavaDoc(BASIC_OPS_SIZE - 1), false));
129     if (verbose)
130       System.err.println("removed: " + removed);
131     TestValue v= (TestValue)
132       shm.get(new TestKey(new Integer JavaDoc(BASIC_OPS_SIZE), false));
133     assertTrue("got back val after delete!", v == null);
134
135     int size= shm.size();
136     assertTrue("got bad value from size(); returned " + size,
137                size == (BASIC_OPS_SIZE - 1) );
138
139     shm.clear();
140     assertTrue("isEmpty returns false when it shouldn't",
141                shm.isEmpty());
142   }
143
144   public void testExpiry() {
145     if (verbose)
146       System.err.println("entering testExpiry()");
147     SoftHashMap shm= new SoftHashMap();
148
149     valHasBeenFinalized= false;
150     keyHasBeenFinalized= false;
151     int i= 0;
152
153     try {
154       while (!valHasBeenFinalized) {
155     if (verbose)
156       System.err.println("(!v) trying to put " + i);
157         shm.put(new TestKey(new Integer JavaDoc(i), true), new TestValue(i, true));
158         i++;
159     if (verbose)
160       System.err.println("after adding " + i
161                  + " items, size is " + shm.size());
162       }
163       while (!keyHasBeenFinalized) {
164     if (verbose)
165       System.err.println("(!k) trying to put " + i);
166         shm.put(new TestKey(new Integer JavaDoc(i), true), new TestValue(i, true));
167         i++;
168       }
169
170       // sleep and busy loop to see if JVM goes on collecting stuff...
171
if (verbose)
172     System.err.println("sleeping... ");
173       Thread.sleep(20 * 1000);
174       if (verbose)
175     System.err.println("busy looping...");
176       int j;
177       for (j= 0; j < 2000000; j++) {
178     i+= j;
179       }
180       if (verbose)
181     System.err.println("done, j=" + j);
182
183     } catch (Exception JavaDoc e) {
184       System.err.println("caught exception");
185       e.printStackTrace();
186     } finally {
187       if (verbose)
188     System.err.println("out of put loops");
189     }
190
191   }
192
193   void keyFinalized(Integer JavaDoc key) {
194     if (verbose)
195       System.err.println("notified of finalized key: " + key);
196     keyHasBeenFinalized= true;
197   }
198
199   void valFinalized(int key) {
200     if (verbose)
201       System.err.println("notified of finalized value for: " + key);
202     valHasBeenFinalized= true;
203   }
204
205   public static final void main(String JavaDoc[] a) {
206     TestSoftHashMap t= new TestSoftHashMap("test");
207     t.testExpiry();
208   }
209
210 }
211
Popular Tags