{
String testString = new String( "This is a test");
Object retrievedObject = null;
RefreshableCachedObject cacheObject = null;
GlobalCacheService globalCache = (GlobalCacheService)TurbineServices
.getInstance()
.getService( GlobalCacheService.SERVICE_NAME );
// Create and add Object that expires in TEST_EXPIRETIME millis.
cacheObject = new RefreshableCachedObject(new RefreshableObject(), TEST_EXPIRETIME);
assertNotNull( "Failed to create a cachable object", cacheObject);
long addTime = System.currentTimeMillis();
globalCache.addObject(cacheKey, cacheObject);
// Try to get un-expired object
try
{
retrievedObject = null;
retrievedObject = globalCache.getObject(cacheKey);
assertNotNull( "Did not retrieved a cached object", retrievedObject);
assertEquals( "Did not retrieved correct cached object", cacheObject, retrievedObject);
}
catch (ObjectExpiredException e)
{
assertTrue( "Object expired early ( " + (System.currentTimeMillis() - addTime) + " millis)", false);
}
catch (Exception e)
{
throw e;
}
// Wait 1 Turbine cache refresh + 1 second.
Thread.sleep(TEST_EXPIRETIME + 1000);
// Try to get expired object
try
{
retrievedObject = null;
retrievedObject = globalCache.getObject(cacheKey);
assertNotNull( "Did not retrieved a cached object, after sleep", retrievedObject);
assertNotNull( "Cached object has no contents, after sleep.", ((RefreshableCachedObject)retrievedObject).getContents());
assertTrue( "Object did not refresh.", ( ((RefreshableObject)((RefreshableCachedObject)retrievedObject).getContents()).getRefreshCount() > 0));
}
catch (ObjectExpiredException e)
{
assertTrue( "Received unexpected ObjectExpiredException exception "
+ "when retrieving refreshable object after ( "
+ (System.currentTimeMillis() - addTime) + " millis)", false);
}
catch (Exception e)
{
throw e;
}
// See if object will expires (testing every second for 100 seconds. It sould not!
for (int i=0; i<100; i++)
{
Thread.sleep(1000); // Sleep 0.5 seconds
// Try to get expired object
try
{
retrievedObject = null;
retrievedObject = globalCache.getObject(cacheKey);
assertNotNull( "Did not retrieved a cached object, after sleep", retrievedObject);
assertNotNull( "Cached object has no contents, after sleep.", ((RefreshableCachedObject)retrievedObject).getContents());
assertTrue( "Object did not refresh.", ( ((RefreshableObject)((RefreshableCachedObject)retrievedObject).getContents()).getRefreshCount() > 0));
}
catch (ObjectExpiredException e)
{
assertTrue( "Received unexpected ObjectExpiredException exception "
+ "when retrieving refreshable object after ( "
+ (System.currentTimeMillis() - addTime) + " millis)", false);
}
catch (Exception e)
{
throw e;
}
}
// Remove objects
globalCache.removeObject(cacheKey);
}