KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > rift > coad > lib > cache > CacheRegistryTest


1 /*
2  * CoadunationLib: The coaduntion implementation library.
3  * Copyright (C) 2006 Rift IT Contracting
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  *
19  * CacheRegistryTest.java
20  *
21  * JUnit based test
22  */

23
24 package com.rift.coad.lib.cache;
25
26 import junit.framework.*;
27 import java.util.Date JavaDoc;
28 import java.util.HashMap JavaDoc;
29 import java.util.HashSet JavaDoc;
30 import java.util.Iterator JavaDoc;
31 import java.util.Map JavaDoc;
32 import java.util.Set JavaDoc;
33 import java.util.Vector JavaDoc;
34 import java.net.URL JavaDoc;
35 import java.net.URLClassLoader JavaDoc;
36 import org.apache.log4j.Logger;
37 import com.rift.coad.lib.configuration.ConfigurationFactory;
38 import com.rift.coad.lib.configuration.Configuration;
39 import com.rift.coad.lib.thread.BasicThread;
40 import com.rift.coad.lib.thread.ThreadStateMonitor;
41 import com.rift.coad.lib.thread.CoadunationThreadGroup;
42 import com.rift.coad.lib.security.UserSession;
43 import com.rift.coad.lib.security.user.UserSessionManager;
44 import com.rift.coad.lib.security.user.UserStoreManager;
45 import com.rift.coad.lib.security.ThreadsPermissionContainer;
46 import com.rift.coad.lib.security.login.handlers.PasswordInfoHandler;
47 import com.rift.coad.lib.security.SessionManager;
48 import com.rift.coad.lib.security.RoleManager;
49 import com.rift.coad.lib.security.Validator;
50 import com.rift.coad.lib.security.login.LoginManager;
51
52
53 /**
54  *
55  * @author mincemeat
56  */

57 public class CacheRegistryTest extends TestCase {
58     
59     public static class CacheEntry {
60         // the member variables
61
private Date JavaDoc touchTime = new Date JavaDoc();
62         private String JavaDoc name = null;
63         
64         /**
65          * The constructor of the cache entry.
66          *
67          * @param name The name of the entry.
68          */

69         public CacheEntry (String JavaDoc name) {
70             this.name = name;
71         }
72         
73         
74         /**
75          * This method returns the name of this object.
76          */

77         public String JavaDoc getName() {
78             return name;
79         }
80         
81         
82         /**
83          * The touch time.
84          */

85         public synchronized void touch() {
86             touchTime = new Date JavaDoc();
87         }
88         
89         
90         /**
91          * This method returns true if this object is expired.
92          *
93          * @return TRUE if expired FALSE if not.
94          * @param expiryDate The date to be new than in order to remain in
95          * memory.
96          */

97         public synchronized boolean isExpired(Date JavaDoc expiryDate) {
98             if (touchTime.getTime() < expiryDate.getTime()) {
99                 return true;
100             }
101             return false;
102         }
103     }
104     
105     public static class TestCache implements Cache {
106         
107         private Map JavaDoc cacheEntries = new HashMap JavaDoc();
108         
109         /**
110          * The constructor of the test cache.
111          */

112         public TestCache() {
113             
114         }
115         
116         /**
117          * This method is called to perform garbage collection on the cache entries.
118          */

119         public synchronized void garbageCollect() {
120             Iterator JavaDoc iter = cacheEntries.keySet().iterator();
121             Date JavaDoc expiryDate = new Date JavaDoc(new Date JavaDoc().getTime() - 500);
122             while(iter.hasNext()) {
123                 String JavaDoc key = (String JavaDoc)iter.next();
124                 CacheEntry entry = (CacheEntry)cacheEntries.get(key);
125                 if (entry.isExpired(expiryDate)) {
126                     // remove an reset
127
cacheEntries.remove(key);
128                     iter = cacheEntries.keySet().iterator();
129                 }
130             }
131         }
132
133
134         /**
135          * This method is called to forcibly remove everything from the cache.
136          */

137         public synchronized void clear() {
138             cacheEntries.clear();
139         }
140         
141         
142         /**
143          * This mehtod returns true if the cache contains the checked entry.
144          *
145          * @return TRUE if the cache contains the checked entry.
146          * @param cacheEntry The entry to perform the check for.
147          */

148         public boolean contains(Object JavaDoc cacheEntry) {
149             return cacheEntries.containsValue(cacheEntry);
150         }
151         
152         
153         /**
154          * This method is responsible for adding an entry to the cache.
155          *
156          * @param entry The entry to add to the cache.
157          */

158         public synchronized void add(CacheEntry entry) {
159             cacheEntries.put(entry.getName(),entry);
160         }
161         
162         
163         /**
164          * This method returns the cache entry by name
165          *
166          * @return The CacheEntry to retrieve.
167          * @param name The name of the cache entry to retrieve.
168          */

169         public synchronized CacheEntry get(String JavaDoc name) {
170             return (CacheEntry)cacheEntries.get(name);
171         }
172         
173     }
174     
175     
176     public static class TestCache2 implements Cache {
177         
178         private Map JavaDoc cacheEntries = new HashMap JavaDoc();
179         
180         /**
181          * The constructor of the test cache.
182          */

183         public TestCache2() {
184             
185         }
186         
187         /**
188          * This method is called to perform garbage collection on the cache entries.
189          */

190         public synchronized void garbageCollect() {
191             Iterator JavaDoc iter = cacheEntries.keySet().iterator();
192             Date JavaDoc expiryDate = new Date JavaDoc(new Date JavaDoc().getTime() - 500);
193             while(iter.hasNext()) {
194                 String JavaDoc key = (String JavaDoc)iter.next();
195                 CacheEntry entry = (CacheEntry)cacheEntries.get(key);
196                 if (entry.isExpired(expiryDate)) {
197                     // remove an reset
198
cacheEntries.remove(key);
199                     iter = cacheEntries.keySet().iterator();
200                 }
201             }
202         }
203
204
205         /**
206          * This method is called to forcibly remove everything from the cache.
207          */

208         public synchronized void clear() {
209             cacheEntries.clear();
210         }
211         
212         
213         /**
214          * This mehtod returns true if the cache contains the checked entry.
215          *
216          * @return TRUE if the cache contains the checked entry.
217          * @param cacheEntry The entry to perform the check for.
218          */

219         public boolean contains(Object JavaDoc cacheEntry) {
220             return cacheEntries.containsValue(cacheEntry);
221         }
222         
223         
224         /**
225          * This method is responsible for adding an entry to the cache.
226          *
227          * @param entry The entry to add to the cache.
228          */

229         public synchronized void add(CacheEntry entry) {
230             cacheEntries.put(entry.getName(),entry);
231         }
232         
233         
234         /**
235          * This method returns the cache entry by name
236          *
237          * @return The CacheEntry to retrieve.
238          * @param name The name of the cache entry to retrieve.
239          */

240         public synchronized CacheEntry get(String JavaDoc name) {
241             return (CacheEntry)cacheEntries.get(name);
242         }
243         
244     }
245     
246     public CacheRegistryTest(String JavaDoc testName) {
247         super(testName);
248     }
249
250     protected void setUp() throws Exception JavaDoc {
251     }
252
253     protected void tearDown() throws Exception JavaDoc {
254     }
255
256     public static Test suite() {
257         TestSuite suite = new TestSuite(CacheRegistryTest.class);
258         
259         return suite;
260     }
261
262     /**
263      * Test of init method, of class com.rift.coad.lib.cache.CacheRegistry.
264      */

265     public void testCache() throws Exception JavaDoc {
266         System.out.println("init");
267         
268         // initialize the thread permissions
269
ThreadsPermissionContainer permissions = new ThreadsPermissionContainer();
270         SessionManager.init(permissions);
271         UserStoreManager userStoreManager = new UserStoreManager();
272         UserSessionManager sessionManager = new UserSessionManager(permissions,
273                 userStoreManager);
274         LoginManager.init(sessionManager,userStoreManager);
275         
276         // add a user to the session for the current thread
277
RoleManager.getInstance();
278         
279         // instanciate the thread group
280
CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager,
281                 userStoreManager);
282         
283         // check the singleton
284
CacheRegistry result = CacheRegistry.init(threadGroup);
285         assertEquals(CacheRegistry.getInstance(), result);
286         
287         // init class loaders
288
ClassLoader JavaDoc loader1 = new URLClassLoader JavaDoc(new URL JavaDoc[] {});
289         ClassLoader JavaDoc loader2 = new URLClassLoader JavaDoc(new URL JavaDoc[] {});
290         
291         // set the current thread
292
Thread.currentThread().setContextClassLoader(loader1);
293         result.initCache();
294         TestCache testCache = (TestCache)result.getCache(TestCache.class);
295         TestCache2 testCache2 = (TestCache2)result.getCache(TestCache2.class);
296         testCache.add(new CacheEntry("bob"));
297         testCache.add(new CacheEntry("fred"));
298         testCache2.add(new CacheEntry("mary"));
299         testCache2.add(new CacheEntry("jill"));
300         
301         CacheEntry fred = testCache.get("fred");
302         CacheEntry mary = testCache2.get("mary");
303         
304         Thread.currentThread().setContextClassLoader(loader2);
305         result.initCache();
306         TestCache testCache_2 = (TestCache)result.getCache(TestCache.class);
307         TestCache2 testCache2_2 = (TestCache2)result.getCache(TestCache2.class);
308         testCache_2.add(new CacheEntry("ben"));
309         testCache_2.add(new CacheEntry("john"));
310         testCache2_2.add(new CacheEntry("tamzin"));
311         testCache2_2.add(new CacheEntry("chevaughn"));
312         
313         CacheEntry ben = testCache_2.get("ben");
314         CacheEntry chevaughn = testCache2_2.get("chevaughn");
315         
316         // loop for 2 seconds
317
for (int index = 0; index < 4; index++) {
318             fred.touch();
319             mary.touch();
320             ben.touch();
321             chevaughn.touch();
322             Thread.sleep(450);
323         }
324         
325         
326         Thread.currentThread().setContextClassLoader(loader1);
327         assertEquals(result.getCache(TestCache.class), testCache);
328         assertEquals(result.getCache(TestCache2.class), testCache2);
329         
330         assertEquals(testCache.get("bob"),null);
331         assertEquals(testCache.get("fred"),fred);
332         assertEquals(testCache2.get("mary"),mary);
333         assertEquals(testCache2.get("jill"),null);
334         
335         
336         Thread.currentThread().setContextClassLoader(loader2);
337         assertEquals(result.getCache(TestCache.class), testCache_2);
338         assertEquals(result.getCache(TestCache2.class), testCache2_2);
339         
340         assertEquals(testCache_2.get("ben"),ben);
341         assertEquals(testCache_2.get("john"),null);
342         assertEquals(testCache2_2.get("tamzin"),null);
343         assertEquals(testCache2_2.get("chevaughn"),chevaughn);
344         
345         result.terminateCache();
346         Thread.sleep(100);
347         assertEquals(testCache_2.get("ben"),null);
348         assertEquals(testCache_2.get("john"),null);
349         assertEquals(testCache2_2.get("tamzin"),null);
350         assertEquals(testCache2_2.get("chevaughn"),null);
351         
352         try {
353             result.getCache(TestCache.class);
354             fail("The cache is still valid for this class loader");
355         } catch (CacheException ex) {
356             // ignore
357
}
358         
359         Thread.currentThread().setContextClassLoader(loader1);
360         result.shutdown();
361         
362         assertEquals(testCache_2.get("ben"),null);
363         assertEquals(testCache_2.get("john"),null);
364         assertEquals(testCache2_2.get("tamzin"),null);
365         assertEquals(testCache2_2.get("chevaughn"),null);
366         try {
367             result.getCache(TestCache.class);
368             fail("The cache is still valid for this class loader");
369         } catch (CacheException ex) {
370             // ignore
371
}
372     }
373
374     
375 }
376
Popular Tags