KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > perseus > cache > TestCache


1 /**
2  * Copyright (C) 2001-2002
3  * - France Telecom R&D
4  * - Laboratoire Logiciels, Systemes, Reseaux - UMR 5526, CNRS-INPG-UJF
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19  *
20  * Release: 1.0
21  *
22  * Authors:
23  *
24  */

25
26 package org.objectweb.perseus.cache;
27
28 import junit.framework.TestCase;
29 import org.objectweb.perseus.cache.api.CacheEntry;
30 import org.objectweb.perseus.cache.api.CacheException;
31 import org.objectweb.perseus.cache.api.CacheEventListener;
32 import org.objectweb.perseus.cache.api.CacheEvent;
33 import org.objectweb.perseus.cache.lib.BasicCacheManager;
34 import org.objectweb.perseus.cache.lib.BasicCacheEntryFactory;
35 import org.objectweb.perseus.cache.replacement.lib.FIFOReplacementManager;
36 import org.objectweb.perseus.cache.replacement.lib.AbstractReplacementManager;
37 import org.objectweb.perseus.cache.replacement.lib.MRUReplacementManager;
38 import org.objectweb.perseus.cache.replacement.lib.LRUReplacementManager;
39 import org.objectweb.util.monolog.Monolog;
40 import org.objectweb.util.monolog.api.Logger;
41 import org.objectweb.util.monolog.api.LoggerFactory;
42 import org.objectweb.util.monolog.api.BasicLevel;
43
44 public class TestCache extends TestCase {
45
46     static final int FIFO = 0;
47     static final int LRU = 1;
48     static final int MRU = 2;
49
50     private BasicCacheManager cm;
51
52     Object JavaDoc id1, id2, id3, id4;
53     Object JavaDoc value1, value2, value3, value4;
54     static Logger logger = null;
55     static LoggerFactory loggerFactory = null;
56
57     public TestCache(String JavaDoc s) {
58         super(s);
59         if (logger == null) {
60             loggerFactory = Monolog.getMonologFactory();
61             logger = loggerFactory
62                     .getLogger("org.objectweb.perseus.cache.test");
63         }
64     }
65
66     protected void setUp() throws CacheException {
67         cm = new BasicCacheManager();
68         BasicCacheEntryFactory cef = new BasicCacheEntryFactory();
69         cm.bindFc("logger", logger);
70         cm.bindFc("monolog-factory", loggerFactory);
71         cm.bindFc(BasicCacheManager.CACHE_ENTRY_FACTORY_BINDING, cef);
72         cm.setMaxObjects(3);
73
74         id1 = new Integer JavaDoc(1);
75         id2 = new Integer JavaDoc(2);
76         id3 = new Integer JavaDoc(3);
77         id4 = new Integer JavaDoc(4);
78         value1 = new Object JavaDoc();
79         value2 = new Object JavaDoc();
80         value3 = new Object JavaDoc();
81         value4 = new Object JavaDoc();
82     }
83     protected void tearDown() throws Exception JavaDoc {
84         super.tearDown();
85         cm = null;
86         id1 = null;
87         id2 = null;
88         id3 = null;
89         id4 = null;
90         value1 = null;
91         value2 = null;
92         value3 = null;
93         value4 = null;
94     }
95
96
97     private void setUp(int type, int cacheSize, int autoCleanSize, int threshold) {
98         AbstractReplacementManager rm;
99         switch (type) {
100         case FIFO:
101             rm = new FIFOReplacementManager();
102             break;
103         case LRU:
104             rm = new LRUReplacementManager();
105             break;
106         default:
107             rm = new MRUReplacementManager();
108             break;
109         }
110         cm.bindFc(BasicCacheManager.REPLACEMENT_MANAGER_BINDING, rm);
111         cm.bindFc("logger", logger);
112         cm.bindFc("monolog-factory", loggerFactory);
113         rm.bindFc(AbstractReplacementManager.UNBIND_MANAGER_BINDING, cm);
114         rm.bindFc("logger", logger);
115         rm.bindFc("monolog-factory", loggerFactory);
116         try {
117             cm.setMaxObjects(cacheSize);
118             cm.setAutoCleanSize("" + autoCleanSize);
119             cm.setAutoCleanThreshold("" + threshold);
120         } catch (Exception JavaDoc e) {
121             fail(e.getMessage());
122         }
123     }
124
125     public void testCache1() throws CacheException {
126         setUp(FIFO, 3, 1, 0);
127         CacheEntry ce1 = cm.bind(id1, value1);
128         CacheEntry ce2 = cm.bind(id2, value2);
129
130         CacheEntry ce = cm.lookup(id1);
131         assertNotNull(ce);
132         assertTrue(ce.getCeIdentifier() == id1);
133         assertTrue(ce.getCeObject() == value1);
134
135         ce = cm.lookup(id3);
136         assertNull(ce);
137
138         try {
139             cm.bind(id1, value1);
140             fail("illegal rebind undetected");
141         } catch (CacheException e) {
142         }
143
144         try {
145             cm.bind(id1, value2);
146             fail("illegal rebind undetected");
147         } catch (CacheException e) {
148         }
149
150         cm.fix(ce1);
151         cm.unfix(ce1);
152
153         try {
154             cm.unfix(ce2);
155             fail("unfix protocol error undetected");
156         } catch (CacheException e) {
157         }
158
159         cm.touch(ce2);
160     }
161
162     public void testFIFOCache() throws CacheException {
163         setUp(FIFO, 3, 1, 0);
164         // fills in the cache
165
CacheEntry ce1 = cm.bind(id1, value1);
166         logger.log(BasicLevel.DEBUG,"ce1=" + ce1);
167         CacheEntry ce2 = cm.bind(id2, value2);
168         CacheEntry ce3 = cm.bind(id3, value3);
169         logger.log(BasicLevel.DEBUG,"ce3=" + ce3);
170
171         // discard reference to ce1 -> can be garbage collected
172
ce1 = null;
173         // bind id4 -> ce1 (first in) should be unbound
174
CacheEventListener cel = new TestCacheEventListener(id1);
175         cm.bindFc(BasicCacheManager.CACHE_LISTENER_BINDING, cel);
176         CacheEntry ce4 = cm.bind(id4, value4);
177         cm.unbindFc(BasicCacheManager.CACHE_LISTENER_BINDING);
178
179         // fix ce2
180
cm.fix(ce2);
181         // touch ce4 -> should not have any effect (FIFO replacement manager)
182
cm.touch(ce4);
183
184         // discard reference to ce3 -> can be garbage collected
185
ce3 = null;
186         // bind id1 -> ce3 (first in) should be unbound (ce2 is older but fixed)
187
cel = new TestCacheEventListener(id3);
188         cm.bindFc(BasicCacheManager.CACHE_LISTENER_BINDING, cel);
189         cm.bind(id1, value1);
190         cm.unbindFc(BasicCacheManager.CACHE_LISTENER_BINDING);
191     }
192
193     public void testLRUCache() throws CacheException {
194         setUp(LRU, 3, 1, 0);
195         CacheEntry ce1 = cm.bind(id1, value1);
196         logger.log(BasicLevel.DEBUG,"ce1=" + ce1);
197         CacheEntry ce2 = cm.bind(id2, value2);
198         CacheEntry ce3 = cm.bind(id3, value3);
199
200         // discard reference to ce1 -> can be garbage collected
201
ce1 = null;
202         // bind id4 -> ce1 (first touched) should be unbound
203
CacheEventListener cel = new TestCacheEventListener(id1);
204         cm.bindFc(BasicCacheManager.CACHE_LISTENER_BINDING, cel);
205         CacheEntry ce4 = cm.bind(id4, value4);
206         cm.unbindFc(BasicCacheManager.CACHE_LISTENER_BINDING);
207
208         // touch cache entries in reverse binding order
209
cm.touch(ce4);
210         cm.touch(ce3);
211         cm.touch(ce2);
212         // fix ce4
213
cm.fix(ce4);
214
215         // discard reference to ce3 -> can be garbage collected
216
ce3 = null;
217         // bind id1 -> ce3 (first touched) should be unbound (ce4 is older but fixed)
218
cel = new TestCacheEventListener(id3);
219         cm.bindFc(BasicCacheManager.CACHE_LISTENER_BINDING, cel);
220         cm.bind(id1, value1);
221         cm.unbindFc(BasicCacheManager.CACHE_LISTENER_BINDING);
222     }
223
224     public void testMRUCache() throws CacheException {
225         setUp(MRU, 3, 1, 0);
226         CacheEntry ce1 = cm.bind(id1, value1);
227         CacheEntry ce2 = cm.bind(id2, value2);
228         CacheEntry ce3 = cm.bind(id3, value3);
229         logger.log(BasicLevel.DEBUG,"ce3=" + ce3);
230
231         // discard reference to ce3 -> can be garbage collected
232
ce3 = null;
233         // bind id4 -> ce3 (last touched) should be unbound
234
CacheEventListener cel = new TestCacheEventListener(id3);
235         cm.bindFc(BasicCacheManager.CACHE_LISTENER_BINDING, cel);
236         CacheEntry ce4 = cm.bind(id4, value4);
237         cm.unbindFc(BasicCacheManager.CACHE_LISTENER_BINDING);
238
239         // touch cache entries in reverse binding order
240
cm.touch(ce4);
241         cm.touch(ce2);
242         cm.touch(ce1);
243         // fix ce1
244
cm.fix(ce1);
245
246         // discard reference to ce2 -> can be garbage collected
247
ce2 = null;
248         // bind id3 -> ce2 (last touched) should be unbound (ce1 is newer but fixed)
249
cel = new TestCacheEventListener(id2);
250         cm.bindFc(BasicCacheManager.CACHE_LISTENER_BINDING, cel);
251         cm.bind(id3, value3);
252         cm.unbindFc(BasicCacheManager.CACHE_LISTENER_BINDING);
253     }
254
255     public void testCacheFull() throws CacheException {
256         setUp(FIFO, 3, 1, 0);
257         cm.fix(cm.bind(id1, value1));
258         cm.fix(cm.bind(id2, value2));
259         cm.fix(cm.bind(id3, value3));
260         try {
261             cm.bind(id4, value4);
262             fail("Cache full error undetected");
263         } catch (CacheException e) {
264         }
265     }
266
267     static class TestCacheEventListener implements CacheEventListener {
268
269         private Object JavaDoc expected;
270
271         public TestCacheEventListener(Object JavaDoc expected) {
272             this.expected = expected;
273         }
274
275         public void entryBound(CacheEvent event) {
276         }
277
278         public void entryUnbound(CacheEvent event) {
279             if (event.getCeIdentifier() != expected) {
280                 throw new RuntimeException JavaDoc(
281                         "Unexpected entry unbound " + event.getCeIdentifier());
282             }
283         }
284     }
285
286     public void testCacheThreshold() throws CacheException {
287         setUp(FIFO, 3, 1, 1);
288         cm.bind(id1, value1);
289         cm.bind(id2, value2);
290         value1 = null;
291         Thread.yield();
292         try {
293             Thread.sleep(500);
294         } catch (InterruptedException JavaDoc e) {
295         }
296         Object JavaDoc o = cm.lookup(id1);
297         assertNull("element not unbound: " + o, o);
298         cm.getBgcleaner().stop();
299         try {
300             cm.getBgcleaner().getThread().join(1000);
301         } catch (InterruptedException JavaDoc e) {
302         }
303         assertTrue("The Background cleaner is aliver",
304                 !cm.getBgcleaner().getThread().isAlive());
305     }
306     public void testCacheThreshold2() throws CacheException {
307         setUp(FIFO, 3, 1, 2);
308         cm.bind(id1, value1);
309         cm.bind(id2, value2);
310         Thread.yield();
311         Object JavaDoc o = cm.lookup(id1);
312         assertNotNull("element not unbound: " + o, o);
313         cm.getBgcleaner().stop();
314         try {
315             cm.getBgcleaner().getThread().join(1000);
316         } catch (InterruptedException JavaDoc e) {
317         }
318         assertTrue("The Background cleaner is aliver",
319                 !cm.getBgcleaner().getThread().isAlive());
320     }
321 }
322
Popular Tags