1 package org.apache.turbine.services.cache; 2 17 18 import junit.awtui.TestRunner; 20 import junit.framework.Test; 21 import junit.framework.TestSuite; 22 import org.apache.cactus.ServletTestCase; 23 24 import org.apache.turbine.services.TurbineServices; 26 import org.apache.turbine.services.cache.GlobalCacheService; 27 import org.apache.turbine.services.cache.CachedObject; 28 import org.apache.turbine.services.cache.ObjectExpiredException; 29 import org.apache.turbine.services.cache.Refreshable; 30 import org.apache.turbine.services.cache.RefreshableCachedObject; 31 import org.apache.turbine.Turbine; 32 33 39 public class TurbineCacheTest extends ServletTestCase { 40 41 private Turbine turbine = null; 42 private static final String cacheKey = new String ("CacheKey"); 43 private static final String cacheKey_2 = new String ("CacheKey_2"); 44 private static final long TURBINE_CACHE_REFRESH = 5000; private static final long TEST_EXPIRETIME = TURBINE_CACHE_REFRESH + 1000; 46 private static final long TEST_TIMETOLIVE = TEST_EXPIRETIME * 5; 47 52 public TurbineCacheTest( String name ) 53 { 54 super( name ); 55 } 56 57 62 public static void main(String args[]) 63 { 64 TestRunner.main(new String [] { TurbineCacheTest.class.getName() }); 65 } 66 67 73 public static Test suite() 74 { 75 return new TestSuite( TurbineCacheTest.class ); 77 } 78 79 84 protected void setUp() 85 throws Exception 86 { 87 super.setUp(); 88 config.setInitParameter("properties", 89 "/WEB-INF/conf/TurbineCacheTest.properties"); 90 turbine = new Turbine(); 91 turbine.init(config); 92 } 93 94 97 protected void tearDown() 98 throws Exception 99 { 100 turbine.destroy(); 101 super.tearDown(); 102 } 103 104 108 public void testSimpleAddGetCacheObject() throws Exception 109 { 110 String testString = new String ( "This is a test"); 111 Object retrievedObject = null; 112 CachedObject cacheObject1 = null; 113 114 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 115 .getInstance() 116 .getService( GlobalCacheService.SERVICE_NAME ); 117 118 cacheObject1 = new CachedObject(testString); 120 assertNotNull( "Failed to create a cachable object 1", cacheObject1); 121 122 globalCache.addObject(cacheKey, cacheObject1); 124 125 retrievedObject = globalCache.getObject(cacheKey); 127 assertNotNull( "Did not retrieved a cached object 1", retrievedObject); 128 assertTrue( "Did not retrieved a correct, expected cached object 1", retrievedObject == cacheObject1); 129 130 globalCache.removeObject(cacheKey); 132 133 retrievedObject = null; 135 cacheObject1 = null; 136 try 137 { 138 retrievedObject = globalCache.getObject(cacheKey); 139 assertNull( "Retrieved the deleted cached object 1 and did not get expected ObjectExpiredException", retrievedObject); 140 assertNotNull( "Did not get expected ObjectExpiredException retrieving a deleted object", retrievedObject); 141 } 142 catch (ObjectExpiredException e) 143 { 144 assertNull( "Retrieved the deleted cached object 1, but caught expected ObjectExpiredException exception", retrievedObject); 145 } 146 catch (Exception e) 147 { 148 throw e; 149 } 150 151 globalCache.removeObject(cacheKey); 153 } 154 155 160 public void test2ObjectAddGetCachedObject() throws Exception 161 { 162 String testString = new String ( "This is a test"); 163 Object retrievedObject = null; 164 CachedObject cacheObject1 = null; 165 CachedObject cacheObject2 = null; 166 167 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 168 .getInstance() 169 .getService( GlobalCacheService.SERVICE_NAME ); 170 171 cacheObject1 = new CachedObject(testString); 173 assertNotNull( "Failed to create a cachable object 1", cacheObject1); 174 globalCache.addObject(cacheKey, cacheObject1); 175 retrievedObject = globalCache.getObject(cacheKey); 176 assertNotNull( "Did not retrieved a cached object 1", retrievedObject); 177 assertEquals( "Did not retrieved correct cached object", cacheObject1, retrievedObject); 178 179 cacheObject2 = new CachedObject(testString); 181 assertNotNull( "Failed to create a cachable object 2", cacheObject2); 182 globalCache.addObject(cacheKey_2, cacheObject2); 183 retrievedObject = globalCache.getObject(cacheKey_2); 184 assertNotNull( "Did not retrieved a cached object 2", retrievedObject); 185 assertEquals( "Did not retrieved correct cached object 2", cacheObject2, retrievedObject); 186 187 retrievedObject = globalCache.getObject(cacheKey); 189 assertNotNull( "Did not retrieved a cached object 1. Attempt #2", retrievedObject); 190 assertEquals( "Did not retrieved correct cached object 1. Attempt #2", cacheObject1, retrievedObject); 191 192 retrievedObject = globalCache.getObject(cacheKey); 194 assertNotNull( "Did not retrieved a cached object 1. Attempt #3", retrievedObject); 195 assertEquals( "Did not retrieved correct cached object 1. Attempt #3", cacheObject1, retrievedObject); 196 197 retrievedObject = globalCache.getObject(cacheKey_2); 199 assertNotNull( "Did not retrieved a cached object 2. Attempt #2", retrievedObject); 200 assertEquals( "Did not retrieved correct cached object 2 Attempt #2", cacheObject2, retrievedObject); 201 202 globalCache.removeObject(cacheKey); 204 globalCache.removeObject(cacheKey_2); 205 } 206 207 213 public void testObjectExpiration() throws Exception 214 { 215 String testString = new String ( "This is a test"); 216 Object retrievedObject = null; 217 CachedObject cacheObject = null; 218 219 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 220 .getInstance() 221 .getService( GlobalCacheService.SERVICE_NAME ); 222 223 cacheObject = new CachedObject(testString, 1000); 225 assertNotNull( "Failed to create a cachable object", cacheObject); 226 long addTime = System.currentTimeMillis(); 227 globalCache.addObject(cacheKey, cacheObject); 228 229 try 231 { 232 retrievedObject = null; 233 retrievedObject = globalCache.getObject(cacheKey); 234 assertNotNull( "Did not retrieved a cached object", retrievedObject); 235 assertEquals( "Did not retrieved correct cached object", cacheObject, retrievedObject); 236 } 237 catch (ObjectExpiredException e) 238 { 239 assertTrue( "Object expired early ( " + (System.currentTimeMillis() - addTime) + " millis)", false); 240 } 241 catch (Exception e) 242 { 243 throw e; 244 } 245 246 Thread.sleep(1500); 248 249 try 251 { 252 retrievedObject = null; 253 retrievedObject = globalCache.getObject(cacheKey); 254 assertNull( "Retrieved the expired cached object and did not get expected ObjectExpiredException", retrievedObject); 255 assertNotNull( "Did not get expected ObjectExpiredException retrieving an expired object", retrievedObject); 256 } 257 catch (ObjectExpiredException e) 258 { 259 assertNull( "Retrieved the expired cached object, but caught expected ObjectExpiredException exception", retrievedObject); 260 } 261 catch (Exception e) 262 { 263 throw e; 264 } 265 266 globalCache.removeObject(cacheKey); 268 } 269 270 277 public void testCacheFlush() throws Exception 278 { 279 String testString = new String ( "This is a test"); 280 Object retrievedObject = null; 281 CachedObject cacheObject = null; 282 283 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 284 .getInstance() 285 .getService( GlobalCacheService.SERVICE_NAME ); 286 287 cacheObject = new CachedObject(testString, (TURBINE_CACHE_REFRESH*5) + 1); 289 assertNotNull( "Failed to create a cachable object", cacheObject); 290 long addTime = System.currentTimeMillis(); 291 globalCache.addObject(cacheKey, cacheObject); 292 293 Thread.sleep(TURBINE_CACHE_REFRESH + 1); 295 assertTrue("No object in cache before flush", (0 < globalCache.getNumberOfObjects())); 296 297 globalCache.flushCache(); 299 300 Thread.sleep((TURBINE_CACHE_REFRESH * 2) + 1); 302 assertEquals("After refresh", 0, globalCache.getNumberOfObjects()); 303 304 globalCache.removeObject(cacheKey); 306 } 307 308 312 public void testObjectCount() throws Exception 313 { 314 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 315 .getInstance() 316 .getService( GlobalCacheService.SERVICE_NAME ); 317 assertNotNull("Could not retrive cache service.", globalCache); 318 319 long expireTime = TURBINE_CACHE_REFRESH + TURBINE_CACHE_REFRESH/2; 321 CachedObject cacheObject = new CachedObject("This is a test", expireTime); 322 assertNotNull( "Failed to create a cachable object", cacheObject); 323 324 globalCache.addObject(cacheKey, cacheObject); 325 assertEquals("After adding 1 Object", 1, globalCache.getNumberOfObjects()); 326 327 Thread.sleep(TURBINE_CACHE_REFRESH + TURBINE_CACHE_REFRESH/3); 329 assertEquals("After one refresh", 1, globalCache.getNumberOfObjects()); 330 331 Thread.sleep((TURBINE_CACHE_REFRESH * 2) + TURBINE_CACHE_REFRESH/3); 333 assertEquals("After three refreshes", 0, globalCache.getNumberOfObjects()); 334 } 335 336 346 public void testRefreshableObject() throws Exception 347 { 348 String testString = new String ( "This is a test"); 349 Object retrievedObject = null; 350 RefreshableCachedObject cacheObject = null; 351 352 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 353 .getInstance() 354 .getService( GlobalCacheService.SERVICE_NAME ); 355 356 cacheObject = new RefreshableCachedObject(new RefreshableObject(), TEST_EXPIRETIME); 358 assertNotNull( "Failed to create a cachable object", cacheObject); 359 long addTime = System.currentTimeMillis(); 360 globalCache.addObject(cacheKey, cacheObject); 361 362 try 364 { 365 retrievedObject = null; 366 retrievedObject = globalCache.getObject(cacheKey); 367 assertNotNull( "Did not retrieved a cached object", retrievedObject); 368 assertEquals( "Did not retrieved correct cached object", cacheObject, retrievedObject); 369 } 370 catch (ObjectExpiredException e) 371 { 372 assertTrue( "Object expired early ( " + (System.currentTimeMillis() - addTime) + " millis)", false); 373 } 374 catch (Exception e) 375 { 376 throw e; 377 } 378 379 Thread.sleep(TEST_EXPIRETIME + 1000); 381 382 try 384 { 385 retrievedObject = null; 386 retrievedObject = globalCache.getObject(cacheKey); 387 assertNotNull( "Did not retrieved a cached object, after sleep", retrievedObject); 388 assertNotNull( "Cached object has no contents, after sleep.", ((RefreshableCachedObject)retrievedObject).getContents()); 389 assertTrue( "Object did not refresh.", ( ((RefreshableObject)((RefreshableCachedObject)retrievedObject).getContents()).getRefreshCount() > 0)); 390 } 391 catch (ObjectExpiredException e) 392 { 393 assertTrue( "Received unexpected ObjectExpiredException exception " 394 + "when retrieving refreshable object after ( " 395 + (System.currentTimeMillis() - addTime) + " millis)", false); 396 } 397 catch (Exception e) 398 { 399 throw e; 400 } 401 402 for (int i=0; i<100; i++) 404 { 405 Thread.sleep(1000); 407 try 409 { 410 retrievedObject = null; 411 retrievedObject = globalCache.getObject(cacheKey); 412 assertNotNull( "Did not retrieved a cached object, after sleep", retrievedObject); 413 assertNotNull( "Cached object has no contents, after sleep.", ((RefreshableCachedObject)retrievedObject).getContents()); 414 assertTrue( "Object did not refresh.", ( ((RefreshableObject)((RefreshableCachedObject)retrievedObject).getContents()).getRefreshCount() > 0)); 415 } 416 catch (ObjectExpiredException e) 417 { 418 assertTrue( "Received unexpected ObjectExpiredException exception " 419 + "when retrieving refreshable object after ( " 420 + (System.currentTimeMillis() - addTime) + " millis)", false); 421 } 422 catch (Exception e) 423 { 424 throw e; 425 } 426 } 427 globalCache.removeObject(cacheKey); 429 } 430 431 439 public void testRefreshableTimeToLive() throws Exception 440 { 441 String testString = new String ( "This is a test"); 442 Object retrievedObject = null; 443 RefreshableCachedObject cacheObject = null; 444 445 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 446 .getInstance() 447 .getService( GlobalCacheService.SERVICE_NAME ); 448 449 cacheObject = new RefreshableCachedObject(new RefreshableObject(), TEST_EXPIRETIME); 451 assertNotNull( "Failed to create a cachable object", cacheObject); 452 cacheObject.setTTL(TEST_TIMETOLIVE); 453 454 assertEquals( "Returned TimeToLive", TEST_TIMETOLIVE, cacheObject.getTTL()); 456 457 long addTime = System.currentTimeMillis(); 459 globalCache.addObject(cacheKey, cacheObject); 460 461 try 463 { 464 retrievedObject = null; 465 retrievedObject = globalCache.getObject(cacheKey); 466 assertNotNull( "Did not retrieved a cached object", retrievedObject); 467 assertEquals( "Did not retrieved correct cached object", cacheObject, retrievedObject); 468 } 469 catch (ObjectExpiredException e) 470 { 471 assertTrue( "Object expired early ( " + (System.currentTimeMillis() - addTime) + " millis)", false); 472 } 473 catch (Exception e) 474 { 475 throw e; 476 } 477 478 Thread.sleep(TEST_TIMETOLIVE - 2000); 480 481 try 483 { 484 retrievedObject = null; 485 retrievedObject = globalCache.getObject(cacheKey); 486 assertNotNull( "Did not retrieved a cached object, after sleep", retrievedObject); 487 assertNotNull( "Cached object has no contents, after sleep.", ((RefreshableCachedObject)retrievedObject).getContents()); 488 assertTrue( "Object did not refresh.", ( ((RefreshableObject)((RefreshableCachedObject)retrievedObject).getContents()).getRefreshCount() > 0)); 489 } 490 catch (ObjectExpiredException e) 491 { 492 assertTrue( "Received unexpected ObjectExpiredException exception " 493 + "when retrieving refreshable object after ( " 494 + (System.currentTimeMillis() - addTime) + " millis)", false); 495 } 496 catch (Exception e) 497 { 498 throw e; 499 } 500 501 Thread.sleep(TEST_TIMETOLIVE +5000); 503 504 try 506 { 507 retrievedObject = null; 508 retrievedObject = globalCache.getObject(cacheKey); 509 assertNull( "Retrieved a cached object, after exceeding TimeToLive", retrievedObject); 510 } 511 catch (ObjectExpiredException e) 512 { 513 assertNull( "Retrieved the expired cached object, but caught expected ObjectExpiredException exception", retrievedObject); 514 } 515 catch (Exception e) 516 { 517 throw e; 518 } 519 } 520 521 524 class RefreshableObject implements Refreshable 525 { 526 527 private int refreshCount = 0; 528 529 532 public void refresh() 533 { 534 this.refreshCount++; 535 } 536 537 542 public int getRefreshCount() 543 { 544 return this.refreshCount; 545 } 546 } 547 } 548 | Popular Tags |