1 23 24 package com.rift.coad.lib.cache; 25 26 import junit.framework.*; 27 import java.util.Date ; 28 import java.util.HashMap ; 29 import java.util.HashSet ; 30 import java.util.Iterator ; 31 import java.util.Map ; 32 import java.util.Set ; 33 import java.util.Vector ; 34 import java.net.URL ; 35 import java.net.URLClassLoader ; 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 57 public class CacheRegistryTest extends TestCase { 58 59 public static class CacheEntry { 60 private Date touchTime = new Date (); 62 private String name = null; 63 64 69 public CacheEntry (String name) { 70 this.name = name; 71 } 72 73 74 77 public String getName() { 78 return name; 79 } 80 81 82 85 public synchronized void touch() { 86 touchTime = new Date (); 87 } 88 89 90 97 public synchronized boolean isExpired(Date 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 cacheEntries = new HashMap (); 108 109 112 public TestCache() { 113 114 } 115 116 119 public synchronized void garbageCollect() { 120 Iterator iter = cacheEntries.keySet().iterator(); 121 Date expiryDate = new Date (new Date ().getTime() - 500); 122 while(iter.hasNext()) { 123 String key = (String )iter.next(); 124 CacheEntry entry = (CacheEntry)cacheEntries.get(key); 125 if (entry.isExpired(expiryDate)) { 126 cacheEntries.remove(key); 128 iter = cacheEntries.keySet().iterator(); 129 } 130 } 131 } 132 133 134 137 public synchronized void clear() { 138 cacheEntries.clear(); 139 } 140 141 142 148 public boolean contains(Object cacheEntry) { 149 return cacheEntries.containsValue(cacheEntry); 150 } 151 152 153 158 public synchronized void add(CacheEntry entry) { 159 cacheEntries.put(entry.getName(),entry); 160 } 161 162 163 169 public synchronized CacheEntry get(String name) { 170 return (CacheEntry)cacheEntries.get(name); 171 } 172 173 } 174 175 176 public static class TestCache2 implements Cache { 177 178 private Map cacheEntries = new HashMap (); 179 180 183 public TestCache2() { 184 185 } 186 187 190 public synchronized void garbageCollect() { 191 Iterator iter = cacheEntries.keySet().iterator(); 192 Date expiryDate = new Date (new Date ().getTime() - 500); 193 while(iter.hasNext()) { 194 String key = (String )iter.next(); 195 CacheEntry entry = (CacheEntry)cacheEntries.get(key); 196 if (entry.isExpired(expiryDate)) { 197 cacheEntries.remove(key); 199 iter = cacheEntries.keySet().iterator(); 200 } 201 } 202 } 203 204 205 208 public synchronized void clear() { 209 cacheEntries.clear(); 210 } 211 212 213 219 public boolean contains(Object cacheEntry) { 220 return cacheEntries.containsValue(cacheEntry); 221 } 222 223 224 229 public synchronized void add(CacheEntry entry) { 230 cacheEntries.put(entry.getName(),entry); 231 } 232 233 234 240 public synchronized CacheEntry get(String name) { 241 return (CacheEntry)cacheEntries.get(name); 242 } 243 244 } 245 246 public CacheRegistryTest(String testName) { 247 super(testName); 248 } 249 250 protected void setUp() throws Exception { 251 } 252 253 protected void tearDown() throws Exception { 254 } 255 256 public static Test suite() { 257 TestSuite suite = new TestSuite(CacheRegistryTest.class); 258 259 return suite; 260 } 261 262 265 public void testCache() throws Exception { 266 System.out.println("init"); 267 268 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 RoleManager.getInstance(); 278 279 CoadunationThreadGroup threadGroup = new CoadunationThreadGroup(sessionManager, 281 userStoreManager); 282 283 CacheRegistry result = CacheRegistry.init(threadGroup); 285 assertEquals(CacheRegistry.getInstance(), result); 286 287 ClassLoader loader1 = new URLClassLoader (new URL [] {}); 289 ClassLoader loader2 = new URLClassLoader (new URL [] {}); 290 291 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 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 } 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 } 372 } 373 374 375 } 376 | Popular Tags |