1 16 17 18 package org.apache.jetspeed.services.portletcache; 19 20 import junit.framework.Test; 22 import junit.framework.TestSuite; 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.util.TurbineConfig; 32 import org.apache.turbine.util.StringUtils; 33 34 import org.apache.jetspeed.test.JetspeedTestCase; 36 37 43 public class TestTurbineCache extends JetspeedTestCase { 44 45 private static final String cacheKey = new String ("CacheKey"); 46 private static final String cacheKey_2 = new String ("CacheKey_2"); 47 private static final long TURBINE_CACHE_REFRESH = 5000; private static final long TEST_EXPIRETIME = TURBINE_CACHE_REFRESH + 1000; 49 private static final long TEST_TIMETOLIVE = TEST_EXPIRETIME * 5; 50 51 55 private static TurbineConfig config = null; 56 57 61 static 62 { 63 try 64 { 65 config = new TurbineConfig( "webapp", 66 "/WEB-INF/conf/TurbineResources.properties"); 67 config.init(); 68 } 69 catch (Exception e) 70 { 71 fail(StringUtils.stackTrace(e)); 72 } 73 } 74 75 80 public TestTurbineCache( String name ) { 81 super( name ); 82 } 83 84 89 public static void main(String args[]) { 90 junit.awtui.TestRunner.main( new String [] { TestTurbineCache.class.getName() } ); 91 } 92 93 public void setup() { 94 System.out.println("Caching test may take a few minutes"); 96 } 97 103 public static Test suite() { 104 return new TestSuite( TestTurbineCache.class ); 106 } 107 108 112 public void testSimpleAddGetCacheObject() throws Exception { 113 String testString = new String ( "This is a test"); 114 Object retrievedObject = null; 115 CachedObject cacheObject1 = null; 116 117 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 118 .getInstance() 119 .getService( GlobalCacheService.SERVICE_NAME ); 120 121 cacheObject1 = new CachedObject(testString); 123 assertNotNull( "Failed to create a cachable object 1", cacheObject1); 124 125 globalCache.addObject(cacheKey, cacheObject1); 127 128 retrievedObject = globalCache.getObject(cacheKey); 130 assertNotNull( "Did not retrieved a cached object 1", retrievedObject); 131 assertTrue( "Did not retrieved a correct, expected cached object 1", retrievedObject == cacheObject1); 132 133 globalCache.removeObject(cacheKey); 135 136 retrievedObject = null; 138 cacheObject1 = null; 139 try { 140 retrievedObject = globalCache.getObject(cacheKey); 141 assertNull( "Retrieved the deleted cached object 1 and did not get expected ObjectExpiredException", retrievedObject); 142 assertNotNull( "Did not get expected ObjectExpiredException retrieving a deleted object", retrievedObject); 143 } catch (ObjectExpiredException e) { 144 assertNull( "Retrieved the deleted cached object 1, but caught expected ObjectExpiredException exception", retrievedObject); 145 } catch (Exception e) { 146 throw e; 147 } 148 149 globalCache.removeObject(cacheKey); 151 } 152 153 158 public void test2ObjectAddGetCachedObject() throws Exception { 159 String testString = new String ( "This is a test"); 160 Object retrievedObject = null; 161 CachedObject cacheObject1 = null; 162 CachedObject cacheObject2 = null; 163 164 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 165 .getInstance() 166 .getService( GlobalCacheService.SERVICE_NAME ); 167 168 cacheObject1 = new CachedObject(testString); 170 assertNotNull( "Failed to create a cachable object 1", cacheObject1); 171 globalCache.addObject(cacheKey, cacheObject1); 172 retrievedObject = globalCache.getObject(cacheKey); 173 assertNotNull( "Did not retrieved a cached object 1", retrievedObject); 174 assertEquals( "Did not retrieved correct cached object", cacheObject1, retrievedObject); 175 176 cacheObject2 = new CachedObject(testString); 178 assertNotNull( "Failed to create a cachable object 2", cacheObject2); 179 globalCache.addObject(cacheKey_2, cacheObject2); 180 retrievedObject = globalCache.getObject(cacheKey_2); 181 assertNotNull( "Did not retrieved a cached object 2", retrievedObject); 182 assertEquals( "Did not retrieved correct cached object 2", cacheObject2, retrievedObject); 183 184 retrievedObject = globalCache.getObject(cacheKey); 186 assertNotNull( "Did not retrieved a cached object 1. Attempt #2", retrievedObject); 187 assertEquals( "Did not retrieved correct cached object 1. Attempt #2", cacheObject1, retrievedObject); 188 189 retrievedObject = globalCache.getObject(cacheKey); 191 assertNotNull( "Did not retrieved a cached object 1. Attempt #3", retrievedObject); 192 assertEquals( "Did not retrieved correct cached object 1. Attempt #3", cacheObject1, retrievedObject); 193 194 retrievedObject = globalCache.getObject(cacheKey_2); 196 assertNotNull( "Did not retrieved a cached object 2. Attempt #2", retrievedObject); 197 assertEquals( "Did not retrieved correct cached object 2 Attempt #2", cacheObject2, retrievedObject); 198 199 globalCache.removeObject(cacheKey); 201 globalCache.removeObject(cacheKey_2); 202 } 203 204 210 public void testObjectExpiration() throws Exception { 211 String testString = new String ( "This is a test"); 212 Object retrievedObject = null; 213 CachedObject cacheObject = null; 214 215 System.out.println("This test will take a few mintes"); 217 218 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 219 .getInstance() 220 .getService( GlobalCacheService.SERVICE_NAME ); 221 222 cacheObject = new CachedObject(testString, 1000); 224 assertNotNull( "Failed to create a cachable object", cacheObject); 225 long addTime = System.currentTimeMillis(); 226 globalCache.addObject(cacheKey, cacheObject); 227 228 try { 230 retrievedObject = null; 231 retrievedObject = globalCache.getObject(cacheKey); 232 assertNotNull( "Did not retrieved a cached object", retrievedObject); 233 assertEquals( "Did not retrieved correct cached object", cacheObject, retrievedObject); 234 } catch (ObjectExpiredException e) { 235 assertTrue( "Object expired early ( " + (System.currentTimeMillis() - addTime) + " millis)", false); 236 } catch (Exception e) { 237 throw e; 238 } 239 240 Thread.sleep(1500); 242 243 try { 245 retrievedObject = null; 246 retrievedObject = globalCache.getObject(cacheKey); 247 assertNull( "Retrieved the expired cached object and did not get expected ObjectExpiredException", retrievedObject); 248 assertNotNull( "Did not get expected ObjectExpiredException retrieving an expired object", retrievedObject); 249 } catch (ObjectExpiredException e) { 250 assertNull( "Retrieved the expired cached object, but caught expected ObjectExpiredException exception", retrievedObject); 251 } catch (Exception e) { 252 throw e; 253 } 254 255 globalCache.removeObject(cacheKey); 257 } 258 259 266 267 public void testCacheFlush() throws Exception { 268 String testString = new String ( "This is a test"); 269 Object retrievedObject = null; 270 CachedObject cacheObject = null; 271 272 273 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 274 .getInstance() 275 .getService( GlobalCacheService.SERVICE_NAME ); 276 277 cacheObject = new CachedObject(testString, (TURBINE_CACHE_REFRESH*5) + 1); 279 assertNotNull( "Failed to create a cachable object", cacheObject); 280 long addTime = System.currentTimeMillis(); 281 globalCache.addObject(cacheKey, cacheObject); 282 283 Thread.sleep(TURBINE_CACHE_REFRESH + 1); 285 assertTrue("No object in cache before flush", (0 < globalCache.getNumberOfObjects())); 286 287 globalCache.flushCache(); 289 290 Thread.sleep((TURBINE_CACHE_REFRESH * 2) + 1); 292 assertEquals("After refresh", 0, globalCache.getNumberOfObjects()); 293 globalCache.removeObject(cacheKey); 295 } 296 297 304 305 public void testObjectCount() throws Exception { 306 307 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 308 .getInstance() 309 .getService( GlobalCacheService.SERVICE_NAME ); 310 assertNotNull("Could not retrive cache service.", globalCache); 311 312 long expireTime = TURBINE_CACHE_REFRESH + TURBINE_CACHE_REFRESH/2; 314 CachedObject cacheObject = new CachedObject("This is a test", expireTime); 315 assertNotNull( "Failed to create a cachable object", cacheObject); 316 317 globalCache.addObject(cacheKey, cacheObject); 318 assertEquals("After adding 1 Object", 1, globalCache.getNumberOfObjects()); 319 320 Thread.sleep(TURBINE_CACHE_REFRESH + TURBINE_CACHE_REFRESH/3); 322 assertEquals("After one refresh", 1, globalCache.getNumberOfObjects()); 323 324 Thread.sleep((TURBINE_CACHE_REFRESH * 2) + TURBINE_CACHE_REFRESH/3); 326 assertEquals("After three refreshes", 0, globalCache.getNumberOfObjects()); 327 } 328 329 339 public void testRefreshableObject() throws Exception { 340 String testString = new String ( "This is a test"); 341 Object retrievedObject = null; 342 RefreshableCachedObject cacheObject = null; 343 long addTime = 0; 344 345 boolean turbineCachePatchApplied = false; 347 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 348 .getInstance() 349 .getService( GlobalCacheService.SERVICE_NAME ); 350 351 cacheObject = new RefreshableCachedObject(new RefreshableObject(), TEST_EXPIRETIME); 353 assertNotNull( "Failed to create a cachable object", cacheObject); 354 globalCache.addObject(cacheKey, cacheObject); 355 addTime = System.currentTimeMillis(); 356 357 try { 359 retrievedObject = null; 360 retrievedObject = globalCache.getObject(cacheKey); 361 assertNotNull( "Did not retrieved a cached object", retrievedObject); 362 assertEquals( "Did not retrieved correct cached object", cacheObject, retrievedObject); 363 } catch (ObjectExpiredException e) { 364 assertTrue( "Object expired early ( " + (System.currentTimeMillis() - addTime) + " millis)", false); 365 } catch (Exception e) { 366 throw e; 367 } 368 369 Thread.sleep(TEST_EXPIRETIME + 1000); 371 372 try { 374 retrievedObject = null; 375 retrievedObject = globalCache.getObject(cacheKey); 376 assertNotNull( "Did not retrieved a cached object, after sleep", retrievedObject); 377 assertNotNull( "Cached object has no contents, after sleep.", ((RefreshableCachedObject)retrievedObject).getContents()); 378 assertTrue( "Object did not refresh.", ( ((RefreshableObject)((RefreshableCachedObject)retrievedObject).getContents()).getRefreshCount() > 0)); 379 } catch (ObjectExpiredException e) { 380 assertTrue( "Received unexpected ObjectExpiredException exception " 381 + "when retrieving refreshable object after ( " 382 + (System.currentTimeMillis() - addTime) + " millis)", false); 383 } catch (Exception e) { 384 throw e; 385 } 386 387 for (int i=0; (i<100); i++) { 389 Thread.sleep(1000); 391 try { 393 retrievedObject = null; 394 retrievedObject = globalCache.getObject(cacheKey); 395 assertNotNull( "Did not retrieved a cached object, after sleep", retrievedObject); 396 assertNotNull( "Cached object has no contents, after sleep.", ((RefreshableCachedObject)retrievedObject).getContents()); 397 assertTrue( "Object did not refresh.", ( ((RefreshableObject)((RefreshableCachedObject)retrievedObject).getContents()).getRefreshCount() > 0)); 398 } catch (ObjectExpiredException e) { 399 assertTrue( "Received unexpected ObjectExpiredException exception " 400 + "when retrieving refreshable object after ( " 401 + (System.currentTimeMillis() - addTime) + " millis)", false); 402 } catch (Exception e) { 403 throw e; 404 } 405 } 406 407 globalCache.removeObject(cacheKey); 409 } 410 411 419 public void testRefreshableTimeToLive() throws Exception { 420 String testString = new String ( "This is a test"); 421 Object retrievedObject = null; 422 RefreshableCachedObject cacheObject = null; 423 long addTime = 0; 424 425 GlobalCacheService globalCache = (GlobalCacheService)TurbineServices 426 .getInstance() 427 .getService( GlobalCacheService.SERVICE_NAME ); 428 429 cacheObject = new RefreshableCachedObject(new RefreshableObject(), TEST_EXPIRETIME); 431 assertNotNull( "Failed to create a cachable object", cacheObject); 432 cacheObject.setTTL(TEST_TIMETOLIVE); 433 434 assertEquals( "Returned TimeToLive", TEST_TIMETOLIVE, cacheObject.getTTL()); 436 437 globalCache.addObject(cacheKey, cacheObject); 439 addTime = System.currentTimeMillis(); 440 441 try { 443 retrievedObject = null; 444 retrievedObject = globalCache.getObject(cacheKey); 445 assertNotNull( "Did not retrieved a cached object", retrievedObject); 446 assertEquals( "Did not retrieved correct cached object", cacheObject, retrievedObject); 447 } catch (ObjectExpiredException e) { 448 assertTrue( "Object expired early ( " + (System.currentTimeMillis() - addTime) + " millis)", false); 449 } catch (Exception e) { 450 throw e; 451 } 452 453 Thread.sleep(TEST_TIMETOLIVE - 2000); 455 456 try { 458 retrievedObject = null; 459 retrievedObject = globalCache.getObject(cacheKey); 460 assertNotNull( "Did not retrieved a cached object, after sleep", retrievedObject); 461 assertNotNull( "Cached object has no contents, after sleep.", ((RefreshableCachedObject)retrievedObject).getContents()); 462 assertTrue( "Object did not refresh.", ( ((RefreshableObject)((RefreshableCachedObject)retrievedObject).getContents()).getRefreshCount() > 0)); 463 } catch (ObjectExpiredException e) { 464 assertTrue( "Received unexpected ObjectExpiredException exception " 465 + "when retrieving refreshable object after ( " 466 + (System.currentTimeMillis() - addTime) + " millis)", false); 467 } catch (Exception e) { 468 throw e; 469 } 470 471 Thread.sleep(TEST_TIMETOLIVE +5000); 473 474 try { 476 retrievedObject = null; 477 retrievedObject = globalCache.getObject(cacheKey); 478 assertNull( "Retrieved a cached object, after exceeding TimeToLive", retrievedObject); 479 } catch (ObjectExpiredException e) { 480 assertNull( "Retrieved the expired cached object, but caught expected ObjectExpiredException exception", retrievedObject); 481 } catch (Exception e) { 482 throw e; 483 } 484 } 485 486 489 class RefreshableObject implements Refreshable { 490 491 private int refreshCount = 0; 492 493 496 public void refresh() { 497 this.refreshCount++; 498 } 499 500 505 public int getRefreshCount() { 506 return this.refreshCount; 507 } 508 509 } 510 511 } 512 | Popular Tags |