You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@turbine.apache.org by tv...@apache.org on 2010/03/24 19:55:09 UTC
svn commit: r927153 - in /turbine/fulcrum/trunk/cache: ./
src/java/org/apache/fulcrum/cache/ src/java/org/apache/fulcrum/cache/impl/
src/test/ src/test/org/apache/fulcrum/cache/ xdocs/
Author: tv
Date: Wed Mar 24 18:55:08 2010
New Revision: 927153
URL: http://svn.apache.org/viewvc?rev=927153&view=rev
Log:
Implemented GlobalCacheService with ehcache, refactored implementation classes to be in the same package. all tests pass for me now. YMMV
Added:
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultGlobalCacheService.java
- copied, changed from r927140, turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/DefaultGlobalCacheService.java
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/EHCacheService.java
- copied, changed from r927140, turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java
Removed:
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/DefaultGlobalCacheService.java
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/EHCacheService.java
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java
Modified:
turbine/fulcrum/trunk/cache/pom.xml
turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java
turbine/fulcrum/trunk/cache/src/test/TestComponentConfig.xml
turbine/fulcrum/trunk/cache/src/test/TestRoleConfig.xml
turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java
turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/EHCacheTest.java
turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/JCSCacheTest.java
turbine/fulcrum/trunk/cache/xdocs/changes.xml
turbine/fulcrum/trunk/cache/xdocs/index.xml
Modified: turbine/fulcrum/trunk/cache/pom.xml
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/pom.xml?rev=927153&r1=927152&r2=927153&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/pom.xml (original)
+++ turbine/fulcrum/trunk/cache/pom.xml Wed Mar 24 18:55:08 2010
@@ -81,18 +81,17 @@
<optional>true</optional>
</dependency>
<dependency>
+ <groupId>commons-logging</groupId>
+ <artifactId>commons-logging</artifactId>
+ <version>1.1.1</version>
+ <optional>true</optional>
+ </dependency>
+ <dependency>
<groupId>org.apache.jcs</groupId>
<artifactId>jcs</artifactId>
<version>1.3</version>
<optional>true</optional>
</dependency>
- <dependency>
- <!-- pull in a newer version of commons-logging than JCS -->
- <groupId>commons-logging</groupId>
- <artifactId>commons-logging</artifactId>
- <version>1.1.1</version>
- <optional>true</optional>
- </dependency>
<!-- testing dependencies -->
<dependency>
<groupId>org.apache.fulcrum</groupId>
@@ -114,6 +113,12 @@
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<configuration>
+ <systemProperties>
+ <property>
+ <name>fulcrum.cache.skip.long.tests</name>
+ <value>true</value>
+ </property>
+ </systemProperties>
<additionalClasspathElements>
<additionalClasspathElement>${basedir}/src/test</additionalClasspathElement>
</additionalClasspathElements>
@@ -124,12 +129,6 @@
<testSourceDirectory>${basedir}/src/test</testSourceDirectory>
</build>
- <properties>
- <!-- This bits are used for the staging directory -->
- <fulcrum.release.version>1.1.0</fulcrum.release.version>
- <fulcrum.rc.version>RC1</fulcrum.rc.version>
- </properties>
-
<profiles>
<profile>
<id>rc</id>
@@ -143,5 +142,12 @@
</distributionManagement>
</profile>
</profiles>
-
+
+ <properties>
+ <!-- This bits are used for the staging directory -->
+ <fulcrum.release.version>1.1.0</fulcrum.release.version>
+ <fulcrum.rc.version>RC1</fulcrum.rc.version>
+ <fulcrum.deployment.protocol>scpexe</fulcrum.deployment.protocol>
+ </properties>
+
</project>
Modified: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java?rev=927153&r1=927152&r2=927153&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/GlobalCacheService.java Wed Mar 24 18:55:08 2010
@@ -33,12 +33,6 @@ import org.apache.avalon.framework.compo
*/
public interface GlobalCacheService extends Component
{
- /*
- * TODO: This GlobalCacheService should be usable by both the
- * DefaultEHCacheService and the DefaultGlobalCache. The CacheObject class
- * from GCS and the Element class from EHCS are very similar.
- */
-
/** Avalon role - used to id the component within the manager */
String ROLE = GlobalCacheService.class.getName();
Copied: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultGlobalCacheService.java (from r927140, turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/DefaultGlobalCacheService.java)
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultGlobalCacheService.java?p2=turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultGlobalCacheService.java&p1=turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/DefaultGlobalCacheService.java&r1=927140&r2=927153&rev=927153&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/DefaultGlobalCacheService.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultGlobalCacheService.java Wed Mar 24 18:55:08 2010
@@ -1,4 +1,4 @@
-package org.apache.fulcrum.cache;
+package org.apache.fulcrum.cache.impl;
/*
* Licensed to the Apache Software Foundation (ASF) under one
@@ -35,6 +35,10 @@ import org.apache.avalon.framework.confi
import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.thread.ThreadSafe;
+import org.apache.fulcrum.cache.CachedObject;
+import org.apache.fulcrum.cache.GlobalCacheService;
+import org.apache.fulcrum.cache.ObjectExpiredException;
+import org.apache.fulcrum.cache.RefreshableCachedObject;
/**
* This Service functions as a Global Cache. A global cache is a good place to
Copied: turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/EHCacheService.java (from r927140, turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java)
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/EHCacheService.java?p2=turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/EHCacheService.java&p1=turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java&r1=927140&r2=927153&rev=927153&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/DefaultEHCacheService.java (original)
+++ turbine/fulcrum/trunk/cache/src/java/org/apache/fulcrum/cache/impl/EHCacheService.java Wed Mar 24 18:55:08 2010
@@ -19,35 +19,86 @@ package org.apache.fulcrum.cache.impl;
* under the License.
*/
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+
import net.sf.ehcache.Cache;
import net.sf.ehcache.CacheManager;
+import net.sf.ehcache.Element;
import org.apache.avalon.framework.activity.Disposable;
import org.apache.avalon.framework.activity.Initializable;
+import org.apache.avalon.framework.configuration.Configurable;
+import org.apache.avalon.framework.configuration.Configuration;
+import org.apache.avalon.framework.configuration.ConfigurationException;
import org.apache.avalon.framework.logger.AbstractLogEnabled;
import org.apache.avalon.framework.thread.ThreadSafe;
-import org.apache.fulcrum.cache.EHCacheService;
+import org.apache.fulcrum.cache.CachedObject;
+import org.apache.fulcrum.cache.GlobalCacheService;
+import org.apache.fulcrum.cache.ObjectExpiredException;
+import org.apache.fulcrum.cache.RefreshableCachedObject;
/**
* Default implementation of EHCacheService
*
- * @author <a href="mailto:epughNOSPAM@opensourceconnections.com">Eric Pugh </a>
+ * @author <a href="mailto:epughNOSPAM@opensourceconnections.com">Eric Pugh</a>
+ * @author <a href="mailto:tv@apache.org">Thomas Vandahl</a>
*
*/
-public class DefaultEHCacheService extends AbstractLogEnabled implements
- EHCacheService, Disposable, Initializable, ThreadSafe
+public class EHCacheService extends AbstractLogEnabled implements
+ GlobalCacheService, Runnable, Configurable, Disposable, Initializable, ThreadSafe
{
+ /**
+ * Cache check frequency in Millis (1000 Millis = 1 second). Value must be >
+ * 0. Default = 5 seconds
+ */
+ public static final long DEFAULT_CACHE_CHECK_FREQUENCY = 5000; // 5 seconds
+
+ /**
+ * cacheCheckFrequency (default - 5 seconds)
+ */
+ private long cacheCheckFrequency;
+ /**
+ * Path name of the JCS configuration file
+ */
+ private String configFile;
+
+ /**
+ * Constant value which provides a cache name
+ */
+ private static final String DEFAULT_CACHE_NAME = "fulcrum";
+
+ /**
+ * A cache name
+ */
+ private String cacheName;
+
+ /** thread for refreshing stale items in the cache */
+ private Thread refreshing;
+
+ /** flag to stop the housekeeping thread when the component is disposed. */
+ private boolean continueThread;
+
+ /** The EHCache manager instance */
private CacheManager cacheManager;
- public CacheManager getCacheManager()
- {
- return this.cacheManager;
- }
+ /** A cache instance */
+ private Cache cache;
- public Cache getCache(String cacheName)
+ // ---------------- Avalon Lifecycle Methods ---------------------
+
+ /**
+ * @see org.apache.avalon.framework.configuration.Configurable#configure(org.apache.avalon.framework.configuration.Configuration)
+ */
+ public void configure(Configuration config) throws ConfigurationException
{
- return this.cacheManager.getCache(cacheName);
+ this.cacheCheckFrequency = config.getChild("cacheCheckFrequency")
+ .getValueAsLong(DEFAULT_CACHE_CHECK_FREQUENCY);
+ this.cacheName = config.getChild("cacheName").getValue(DEFAULT_CACHE_NAME);
+ this.configFile = config.getChild("configurationFile").getValue(null);
}
/**
@@ -55,8 +106,30 @@ public class DefaultEHCacheService exten
*/
public void initialize() throws Exception
{
+ if (this.configFile == null)
+ {
+ this.cacheManager = new CacheManager();
+ this.cacheManager.addCache(this.cacheName);
+ }
+ else
+ {
+ this.cacheManager = new CacheManager(this.configFile);
+ }
+
+ this.cache = this.cacheManager.getCache(this.cacheName);
+
+ // Start housekeeping thread.
+ this.continueThread = true;
+ this.refreshing = new Thread(this);
- this.cacheManager = CacheManager.create();
+ // Indicate that this is a system thread. JVM will quit only when
+ // there are no more active user threads. Settings threads spawned
+ // internally by Turbine as daemons allows commandline applications
+ // using Turbine to terminate in an orderly manner.
+ this.refreshing.setDaemon(true);
+ this.refreshing.setName("EHCacheService Refreshing");
+ this.refreshing.start();
+
getLogger().debug("EHCacheService started!");
}
@@ -65,9 +138,200 @@ public class DefaultEHCacheService exten
*/
public void dispose()
{
+ this.continueThread = false;
+ this.refreshing.interrupt();
+
this.cacheManager.shutdown();
this.cacheManager = null;
+ this.cache = null;
getLogger().debug("EHCacheService stopped!");
+ }
+
+ /**
+ * @see org.apache.fulcrum.cache.GlobalCacheService#addObject(java.lang.String, org.apache.fulcrum.cache.CachedObject)
+ */
+ public void addObject(String id, CachedObject o)
+ {
+ Element cacheElement = new Element(id, o);
+
+ if (o instanceof RefreshableCachedObject)
+ {
+ cacheElement.setEternal(true);
+ }
+ else
+ {
+ cacheElement.setEternal(false);
+ cacheElement.setTimeToLive((int)(o.getExpires() + 500) / 1000);
+ }
+
+ cacheElement.setCreateTime();
+
+ this.cache.put(cacheElement);
+ }
+
+ /**
+ * @see org.apache.fulcrum.cache.GlobalCacheService#flushCache()
+ */
+ public void flushCache()
+ {
+ this.cache.removeAll();
+ }
+
+ /**
+ * @see org.apache.fulcrum.cache.GlobalCacheService#getCachedObjects()
+ */
+ public List getCachedObjects()
+ {
+ ArrayList values = new ArrayList();
+
+ for (Iterator i = getKeys().iterator(); i.hasNext();)
+ {
+ Element cachedElement = this.cache.get(i.next());
+
+ if (cachedElement != null)
+ {
+ values.add(cachedElement.getObjectValue());
+ }
+ }
+
+ return values;
+ }
+
+ /**
+ * @see org.apache.fulcrum.cache.GlobalCacheService#getCacheSize()
+ */
+ public int getCacheSize() throws IOException
+ {
+ return (int)this.cache.calculateInMemorySize();
+ }
+
+ /**
+ * @see org.apache.fulcrum.cache.GlobalCacheService#getKeys()
+ */
+ public List getKeys()
+ {
+ return this.cache.getKeysWithExpiryCheck();
+ }
+
+ /**
+ * @see org.apache.fulcrum.cache.GlobalCacheService#getNumberOfObjects()
+ */
+ public int getNumberOfObjects()
+ {
+ return getKeys().size();
+ }
+
+ /**
+ * @see org.apache.fulcrum.cache.GlobalCacheService#getObject(java.lang.String)
+ */
+ public CachedObject getObject(String id) throws ObjectExpiredException
+ {
+ Element cachedElement = this.cache.get(id);
+
+ if (cachedElement == null)
+ {
+ // Not in the cache.
+ throw new ObjectExpiredException();
+ }
+
+ CachedObject obj = (CachedObject)cachedElement.getObjectValue();
+
+ if (obj.isStale())
+ {
+ if (obj instanceof RefreshableCachedObject)
+ {
+ RefreshableCachedObject rco = (RefreshableCachedObject) obj;
+ if (rco.isUntouched())
+ {
+ // Do not refresh an object that has exceeded TimeToLive
+ removeObject(id);
+ throw new ObjectExpiredException();
+ }
+
+ // Refresh Object
+ rco.refresh();
+ if (rco.isStale())
+ {
+ // Object is Expired, remove it from cache.
+ removeObject(id);
+ throw new ObjectExpiredException();
+ }
+ }
+ else
+ {
+ // Expired.
+ removeObject(id);
+ throw new ObjectExpiredException();
+ }
+ }
+
+ if (obj instanceof RefreshableCachedObject)
+ {
+ // notify it that it's being accessed.
+ RefreshableCachedObject rco = (RefreshableCachedObject) obj;
+ rco.touch();
+ }
+
+ return obj;
+ }
+
+ /**
+ * @see org.apache.fulcrum.cache.GlobalCacheService#removeObject(java.lang.String)
+ */
+ public void removeObject(String id)
+ {
+ this.cache.remove(id);
+ }
+
+ /**
+ * Circle through the cache and refresh stale objects. Frequency is
+ * determined by the cacheCheckFrequency property.
+ */
+ public void run()
+ {
+ while (this.continueThread)
+ {
+ // Sleep for amount of time set in cacheCheckFrequency -
+ // default = 5 seconds.
+ try
+ {
+ Thread.sleep(this.cacheCheckFrequency);
+ }
+ catch (InterruptedException exc)
+ {
+ if (!this.continueThread)
+ {
+ return;
+ }
+ }
+ for (Iterator i = getKeys().iterator(); i.hasNext();)
+ {
+ String key = (String) i.next();
+
+ Element cachedElement = this.cache.get(key);
+
+ if (cachedElement == null)
+ {
+ this.cache.remove(key);
+ continue;
+ }
+
+ Object o = cachedElement.getObjectValue();
+
+ if (o instanceof RefreshableCachedObject)
+ {
+ RefreshableCachedObject rco = (RefreshableCachedObject) o;
+ if (rco.isUntouched())
+ {
+ this.cache.remove(key);
+ }
+ else if (rco.isStale())
+ {
+ rco.refresh();
+ }
+ }
+ }
+ }
}
}
Modified: turbine/fulcrum/trunk/cache/src/test/TestComponentConfig.xml
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/test/TestComponentConfig.xml?rev=927153&r1=927152&r2=927153&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/test/TestComponentConfig.xml (original)
+++ turbine/fulcrum/trunk/cache/src/test/TestComponentConfig.xml Wed Mar 24 18:55:08 2010
@@ -19,7 +19,11 @@
-->
<componentConfig>
<cache cacheInitialSize="20" cacheCheckFrequency="5"/>
- <ehcache/>
+ <ehcache>
+ <cacheCheckFrequency>5000</cacheCheckFrequency>
+ <cacheName>fulcrum</cacheName>
+ <!-- configurationFile>ehcache.xml</configurationFile -->
+ </ehcache>
<jcscache>
<cacheCheckFrequency>5000</cacheCheckFrequency>
<region>Cache_Test</region>
Modified: turbine/fulcrum/trunk/cache/src/test/TestRoleConfig.xml
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/test/TestRoleConfig.xml?rev=927153&r1=927152&r2=927153&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/test/TestRoleConfig.xml (original)
+++ turbine/fulcrum/trunk/cache/src/test/TestRoleConfig.xml Wed Mar 24 18:55:08 2010
@@ -22,12 +22,12 @@
<role
name="org.apache.fulcrum.cache.GlobalCacheService"
shorthand="cache"
- default-class="org.apache.fulcrum.cache.DefaultGlobalCacheService"/>
+ default-class="org.apache.fulcrum.cache.impl.DefaultGlobalCacheService"/>
<role
- name="org.apache.fulcrum.cache.EHCacheService"
+ name="org.apache.fulcrum.cache.GlobalCacheService_EH"
shorthand="ehcache"
- default-class="org.apache.fulcrum.cache.impl.DefaultEHCacheService"/>
+ default-class="org.apache.fulcrum.cache.impl.EHCacheService"/>
<role
name="org.apache.fulcrum.cache.GlobalCacheService_JCS"
Modified: turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java?rev=927153&r1=927152&r2=927153&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java (original)
+++ turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/CacheTest.java Wed Mar 24 18:55:08 2010
@@ -29,6 +29,7 @@ import java.util.List;
import org.apache.avalon.framework.component.ComponentException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
+import org.apache.fulcrum.cache.impl.DefaultGlobalCacheService;
import org.apache.fulcrum.testcontainer.BaseUnitTest;
/**
@@ -42,15 +43,15 @@ import org.apache.fulcrum.testcontainer.
public class CacheTest extends BaseUnitTest
{
- private GlobalCacheService globalCache = null;
+ protected GlobalCacheService globalCache = null;
- private static final String cacheKey = "CacheKey";
+ protected static final String cacheKey = "CacheKey";
- private static final String cacheKey_2 = "CacheKey_2";
+ protected static final String cacheKey_2 = "CacheKey_2";
public static final String SKIP_TESTS_KEY = "fulcrum.cache.skip.long.tests";
- private static final Log LOG = LogFactory.getLog(CacheTest.class);
+ protected static final Log LOG = LogFactory.getLog(CacheTest.class);
/**
* Defines the testcase name for JUnit.
@@ -63,13 +64,24 @@ public class CacheTest extends BaseUnitT
super(name);
}
+ /**
+ * Method to configure the role name of the service used
+ *
+ * @return the role name of the service to lookup
+ */
+ protected String getCacheRoleName()
+ {
+ return GlobalCacheService.ROLE;
+ }
+
protected void setUp() throws Exception
{
super.setUp();
+
try
{
this.globalCache = (GlobalCacheService) this
- .lookup(GlobalCacheService.ROLE);
+ .lookup(getCacheRoleName());
}
catch (ComponentException e)
{
@@ -95,7 +107,7 @@ public class CacheTest extends BaseUnitT
this.globalCache.addObject(cacheKey, cacheObject1);
// Get object from cache
retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrievea cached object 1", retrievedObject);
+ assertNotNull("Did not retrieve a cached object 1", retrievedObject);
assertSame("Did not retrieve a correct, expected cached object 1",
retrievedObject, cacheObject1);
// Remove object from cache
@@ -139,34 +151,34 @@ public class CacheTest extends BaseUnitT
assertNotNull("Failed to create a cachable object 1", cacheObject1);
this.globalCache.addObject(cacheKey, cacheObject1);
retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object 1", retrievedObject);
- assertEquals("Did not retrieved correct cached object", cacheObject1,
+ assertNotNull("Did not retrieve a cached object 1", retrievedObject);
+ assertEquals("Did not retrieve correct cached object", cacheObject1,
retrievedObject);
// Create and add Object #2
cacheObject2 = new CachedObject(testString);
assertNotNull("Failed to create a cachable object 2", cacheObject2);
this.globalCache.addObject(cacheKey_2, cacheObject2);
retrievedObject = this.globalCache.getObject(cacheKey_2);
- assertNotNull("Did not retrieved a cached object 2", retrievedObject);
- assertEquals("Did not retrieved correct cached object 2", cacheObject2,
+ assertNotNull("Did not retrieve a cached object 2", retrievedObject);
+ assertEquals("Did not retrieve correct cached object 2", cacheObject2,
retrievedObject);
// Get object #1
retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object 1. Attempt #2",
+ assertNotNull("Did not retrieve a cached object 1. Attempt #2",
retrievedObject);
- assertEquals("Did not retrieved correct cached object 1. Attempt #2",
+ assertEquals("Did not retrieve correct cached object 1. Attempt #2",
cacheObject1, retrievedObject);
// Get object #1
retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object 1. Attempt #3",
+ assertNotNull("Did not retrieve a cached object 1. Attempt #3",
retrievedObject);
- assertEquals("Did not retrieved correct cached object 1. Attempt #3",
+ assertEquals("Did not retrieve correct cached object 1. Attempt #3",
cacheObject1, retrievedObject);
// Get object #2
retrievedObject = this.globalCache.getObject(cacheKey_2);
- assertNotNull("Did not retrieved a cached object 2. Attempt #2",
+ assertNotNull("Did not retrieve a cached object 2. Attempt #2",
retrievedObject);
- assertEquals("Did not retrieved correct cached object 2 Attempt #2",
+ assertEquals("Did not retrieve correct cached object 2 Attempt #2",
cacheObject2, retrievedObject);
// Remove objects
this.globalCache.removeObject(cacheKey);
@@ -193,8 +205,8 @@ public class CacheTest extends BaseUnitT
try
{
retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object", retrievedObject);
- assertEquals("Did not retrieved correct cached object",
+ assertNotNull("Did not retrieve a cached object", retrievedObject);
+ assertEquals("Did not retrieve correct cached object",
cacheObject, retrievedObject);
}
catch (ObjectExpiredException e)
@@ -262,21 +274,28 @@ public class CacheTest extends BaseUnitT
*/
public void testObjectCount() throws Exception
{
- assertNotNull("Could not retrive cache service.", this.globalCache);
+ assertNotNull("Could not retrieve cache service.", this.globalCache);
+
+ long cacheRefresh = getCacheRefresh();
+
// Create and add Object that expires in 1.5 turbine Refresh
- long expireTime = getCacheRefresh() + getCacheRefresh() / 2;
+ long expireTime = cacheRefresh + cacheRefresh / 2;
+
CachedObject cacheObject = new CachedObject("This is a test",
expireTime);
assertNotNull("Failed to create a cachable object", cacheObject);
+
this.globalCache.addObject(cacheKey, cacheObject);
assertEquals("After adding 1 Object", 1, this.globalCache
.getNumberOfObjects());
+
// Wait until we're passed 1 refresh, but not half way.
- Thread.sleep(getCacheRefresh() + getCacheRefresh() / 3);
+ Thread.sleep(cacheRefresh + cacheRefresh / 3);
assertEquals("After one refresh", 1, this.globalCache
.getNumberOfObjects());
+
// Wait until we're passed 2 more refreshes
- Thread.sleep((getCacheRefresh() * 2) + getCacheRefresh() / 3);
+ Thread.sleep((cacheRefresh * 2) + cacheRefresh / 3);
assertEquals("After three refreshes", 0, this.globalCache
.getNumberOfObjects());
}
@@ -304,8 +323,8 @@ public class CacheTest extends BaseUnitT
try
{
retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object", retrievedObject);
- assertEquals("Did not retrieved correct cached object",
+ assertNotNull("Did not retrieve a cached object", retrievedObject);
+ assertEquals("Did not retrieve correct cached object",
cacheObject, retrievedObject);
}
catch (ObjectExpiredException e)
@@ -321,7 +340,7 @@ public class CacheTest extends BaseUnitT
{
retrievedObject = null;
retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object, after sleep",
+ assertNotNull("Did not retrieve a cached object, after sleep",
retrievedObject);
assertNotNull("Cached object has no contents, after sleep.",
((RefreshableCachedObject) retrievedObject).getContents());
@@ -347,7 +366,7 @@ public class CacheTest extends BaseUnitT
{
retrievedObject = null;
retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object, after sleep",
+ assertNotNull("Did not retrieve a cached object, after sleep",
retrievedObject);
assertNotNull("Cached object has no contents, after sleep.",
((RefreshableCachedObject) retrievedObject)
@@ -406,14 +425,14 @@ public class CacheTest extends BaseUnitT
assertEquals("Returned TimeToLive", getTestExpireTime(), cacheObject
.getTTL());
// Add object to Cache
- long addTime = System.currentTimeMillis();
this.globalCache.addObject(cacheKey, cacheObject);
+ long addTime = System.currentTimeMillis();
// Try to get un-expired object
try
{
retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object", retrievedObject);
- assertEquals("Did not retrieved correct cached object",
+ assertNotNull("Did not retrieve a cached object", retrievedObject);
+ assertEquals("Did not retrieve correct cached object",
cacheObject, retrievedObject);
}
catch (ObjectExpiredException e)
@@ -566,8 +585,16 @@ public class CacheTest extends BaseUnitT
*/
private long getCacheRefresh()
{
- return ((DefaultGlobalCacheService) this.globalCache)
- .getCacheCheckFrequency() * 1000;
+ try
+ {
+ DefaultGlobalCacheService cache =
+ (DefaultGlobalCacheService)this.lookup(GlobalCacheService.ROLE);
+ return cache.getCacheCheckFrequency() * 1000L;
+ }
+ catch (ComponentException e)
+ {
+ return 5000;
+ }
}
/**
Modified: turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/EHCacheTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/EHCacheTest.java?rev=927153&r1=927152&r2=927153&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/EHCacheTest.java (original)
+++ turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/EHCacheTest.java Wed Mar 24 18:55:08 2010
@@ -19,27 +19,15 @@
package org.apache.fulcrum.cache;
-import net.sf.ehcache.Cache;
-import net.sf.ehcache.Element;
-
-import org.apache.avalon.framework.component.ComponentException;
-import org.apache.fulcrum.testcontainer.BaseUnitTest;
/**
* EHCacheTest
*
- * @author <a href="epugh@opensourceconnections.com">Eric Pugh</a>
- * @version $Id: CacheTest.java 223198 2004-11-09 08:30:41Z epugh $
+ * @author <a href="tv@apache.org">Thomas Vandahl</a>
+ * @version $Id:$
*/
-public class EHCacheTest extends BaseUnitTest
+public class EHCacheTest extends CacheTest
{
-
- private EHCacheService ehCacheService = null;
-
- private static final String cacheKey = "CacheKey";
-
- private static final String cacheKey_2 = "CacheKey_2";
-
/**
* Defines the testcase name for JUnit.
*
@@ -51,144 +39,11 @@ public class EHCacheTest extends BaseUni
super(name);
}
- protected void setUp() throws Exception
- {
- super.setUp();
- try
- {
- this.ehCacheService = (EHCacheService) this
- .lookup(EHCacheService.ROLE);
- }
- catch (ComponentException e)
- {
- e.printStackTrace();
- fail(e.getMessage());
- }
- }
-
/**
- * Simple test that verify an object can be created and deleted.
- *
- * @throws Exception
+ * @see org.apache.fulcrum.cache.CacheTest#getCacheRoleName()
*/
- public void testSimpleAddGetCacheObject() throws Exception
+ protected String getCacheRoleName()
{
- Cache ehCache = new Cache("test", 1, true, false, 5, 2);
- this.ehCacheService.getCacheManager().addCache(ehCache);
- String testString = "This is a test";
- Object retrievedObject = null;
- Element cacheObject1 = null;
- // Create object
- cacheObject1 = new Element(cacheKey, testString);
- assertNotNull("Failed to create a cachable object 1", cacheObject1);
- // Add object to cache
- ehCache.put(cacheObject1);
- // Get object from cache
- retrievedObject = ehCache.get(cacheKey);
- assertNotNull("Did not retrieve a cached object 1", retrievedObject);
- assertSame("Did not retrieve a correct, expected cached object 1",
- retrievedObject, cacheObject1);
- // Remove object from cache
- ehCache.remove(cacheKey);
- // Verify object removed from cache
- retrievedObject = null;
- cacheObject1 = null;
-
- retrievedObject = ehCache.get(cacheKey);
- assertNull(
- "Retrieved the deleted cached object 1 and did not get expected ObjectExpiredException",
- retrievedObject);
-
- // Remove object from cache that does NOT exist in the cache
- ehCache.remove(cacheKey);
- }
-
- /**
- * Simple test that adds, retrieves, and deletes 2 object.
- *
- * @throws Exception
- */
- public void test2ObjectAddGetCachedObject() throws Exception
- {
- Cache ehCache = new Cache("test2", 1, true, false, 5, 2);
- this.ehCacheService.getCacheManager().addCache(ehCache);
- String testString = "This is a test";
- Object retrievedObject = null;
- Element cacheObject1 = null;
- Element cacheObject2 = null;
- // Create and add Object #1
- cacheObject1 = new Element(cacheKey, testString);
- assertNotNull("Failed to create a cachable object 1", cacheObject1);
- ehCache.put(cacheObject1);
- retrievedObject = ehCache.get(cacheKey);
- assertNotNull("Did not retrieved a cached object 1", retrievedObject);
- assertEquals("Did not retrieved correct cached object", cacheObject1,
- retrievedObject);
- // Create and add Object #2
- cacheObject2 = new Element(cacheKey_2, testString);
- assertNotNull("Failed to create a cachable object 2", cacheObject2);
- ehCache.put(cacheObject2);
- retrievedObject = ehCache.get(cacheKey_2);
- assertNotNull("Did not retrieved a cached object 2", retrievedObject);
- assertEquals("Did not retrieved correct cached object 2", cacheObject2,
- retrievedObject);
- // Get object #1
- retrievedObject = ehCache.get(cacheKey);
- assertNotNull("Did not retrieved a cached object 1. Attempt #2",
- retrievedObject);
- assertEquals("Did not retrieved correct cached object 1. Attempt #2",
- cacheObject1, retrievedObject);
- // Get object #1
- retrievedObject = ehCache.get(cacheKey);
- assertNotNull("Did not retrieved a cached object 1. Attempt #3",
- retrievedObject);
- assertEquals("Did not retrieved correct cached object 1. Attempt #3",
- cacheObject1, retrievedObject);
- // Get object #2
- retrievedObject = ehCache.get(cacheKey_2);
- assertNotNull("Did not retrieved a cached object 2. Attempt #2",
- retrievedObject);
- assertEquals("Did not retrieved correct cached object 2 Attempt #2",
- cacheObject2, retrievedObject);
- // Remove objects
- ehCache.remove(cacheKey);
- ehCache.remove(cacheKey_2);
- }
-
- /**
- * Verify that an object expiration when it now longer exists in cache.
- *
- * @throws Exception
- */
- public void testObjectExpiration() throws Exception
- {
- Cache ehCache = new Cache("expire", 1, true, false, 2, 2);
- this.ehCacheService.getCacheManager().addCache(ehCache);
- String testString = "This is a test";
- Object retrievedObject = null;
- Element cacheObject = null;
- // Create and add Object that expires in 1000 millis (1 second)
- cacheObject = new Element(cacheKey, testString);
- assertNotNull("Failed to create a cachable object", cacheObject);
- ehCache.put(cacheObject);
-
- // Try to get un-expired object
- retrievedObject = ehCache.get(cacheKey);
- assertNotNull("Did not retrieved a cached object", retrievedObject);
- assertEquals("Did not retrieved correct cached object", cacheObject,
- retrievedObject);
-
- // Sleep 3000 Millis (3 seconds)
- Thread.sleep(3000);
- // Try to get expired object
-
- retrievedObject = null;
- retrievedObject = ehCache.get(cacheKey);
- assertNull(
- "Retrieved the expired cached object and did not get expected ObjectExpiredException",
- retrievedObject);
-
- // Remove objects
- ehCache.remove(cacheKey);
+ return GlobalCacheService.ROLE + "_EH";
}
}
Modified: turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/JCSCacheTest.java
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/JCSCacheTest.java?rev=927153&r1=927152&r2=927153&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/JCSCacheTest.java (original)
+++ turbine/fulcrum/trunk/cache/src/test/org/apache/fulcrum/cache/JCSCacheTest.java Wed Mar 24 18:55:08 2010
@@ -20,15 +20,6 @@ package org.apache.fulcrum.cache;
*/
// Cactus and Junit imports
-import java.util.ConcurrentModificationException;
-import java.util.Date;
-import java.util.Iterator;
-import java.util.List;
-
-import org.apache.avalon.framework.component.ComponentException;
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
-import org.apache.fulcrum.testcontainer.BaseUnitTest;
/**
* JCSCacheTest
@@ -36,18 +27,8 @@ import org.apache.fulcrum.testcontainer.
* @author <a href="tv@apache.org">Thomas Vandahl</a>
* @version $Id:$
*/
-public class JCSCacheTest extends BaseUnitTest
+public class JCSCacheTest extends CacheTest
{
- private GlobalCacheService globalCache = null;
-
- private static final String cacheKey = "CacheKey";
-
- private static final String cacheKey_2 = "CacheKey_2";
-
- public static final String SKIP_TESTS_KEY = "fulcrum.cache.skip.long.tests";
-
- private static final Log LOG = LogFactory.getLog(JCSCacheTest.class);
-
/**
* Defines the testcase name for JUnit.
*
@@ -59,540 +40,11 @@ public class JCSCacheTest extends BaseUn
super(name);
}
- protected void setUp() throws Exception
- {
- super.setUp();
- try
- {
- this.globalCache = (GlobalCacheService) this
- .lookup(GlobalCacheService.ROLE + "_JCS");
- }
- catch (ComponentException e)
- {
- e.printStackTrace();
- fail(e.getMessage());
- }
- }
-
- /**
- * Simple test that verify an object can be created and deleted.
- *
- * @throws Exception
- */
- public void testSimpleAddGetCacheObject() throws Exception
- {
- String testString = "This is a test";
- Object retrievedObject = null;
- CachedObject cacheObject1 = null;
- // Create object
- cacheObject1 = new CachedObject(testString);
- assertNotNull("Failed to create a cachable object 1", cacheObject1);
- // Add object to cache
- this.globalCache.addObject(cacheKey, cacheObject1);
- // Get object from cache
- retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieve a cached object 1", retrievedObject);
- assertSame("Did not retrieve a correct, expected cached object 1",
- retrievedObject, cacheObject1);
- // Remove object from cache
- this.globalCache.removeObject(cacheKey);
- // Verify object removed from cache
- retrievedObject = null;
- cacheObject1 = null;
- try
- {
- retrievedObject = this.globalCache.getObject(cacheKey);
- assertNull(
- "Retrieved the deleted cached object 1 and did not get expected ObjectExpiredException",
- retrievedObject);
- assertNotNull(
- "Did not get expected ObjectExpiredException retrieving a deleted object",
- retrievedObject);
- }
- catch (ObjectExpiredException e)
- {
- assertNull(
- "Retrieved the deleted cached object 1, but caught expected ObjectExpiredException exception",
- retrievedObject);
- }
- // Remove object from cache that does NOT exist in the cache
- this.globalCache.removeObject(cacheKey);
- }
-
- /**
- * Simple test that adds, retrieves, and deletes 2 object.
- *
- * @throws Exception
- */
- public void test2ObjectAddGetCachedObject() throws Exception
- {
- String testString = "This is a test";
- Object retrievedObject = null;
- CachedObject cacheObject1 = null;
- CachedObject cacheObject2 = null;
- // Create and add Object #1
- cacheObject1 = new CachedObject(testString);
- assertNotNull("Failed to create a cachable object 1", cacheObject1);
- this.globalCache.addObject(cacheKey, cacheObject1);
- retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieve a cached object 1", retrievedObject);
- assertEquals("Did not retrieve correct cached object", cacheObject1,
- retrievedObject);
- // Create and add Object #2
- cacheObject2 = new CachedObject(testString);
- assertNotNull("Failed to create a cachable object 2", cacheObject2);
- this.globalCache.addObject(cacheKey_2, cacheObject2);
- retrievedObject = this.globalCache.getObject(cacheKey_2);
- assertNotNull("Did not retrieve a cached object 2", retrievedObject);
- assertEquals("Did not retrieve correct cached object 2", cacheObject2,
- retrievedObject);
- // Get object #1
- retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieve a cached object 1. Attempt #2",
- retrievedObject);
- assertEquals("Did not retrieve correct cached object 1. Attempt #2",
- cacheObject1, retrievedObject);
- // Get object #1
- retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieve a cached object 1. Attempt #3",
- retrievedObject);
- assertEquals("Did not retrieve correct cached object 1. Attempt #3",
- cacheObject1, retrievedObject);
- // Get object #2
- retrievedObject = this.globalCache.getObject(cacheKey_2);
- assertNotNull("Did not retrieve a cached object 2. Attempt #2",
- retrievedObject);
- assertEquals("Did not retrieve correct cached object 2 Attempt #2",
- cacheObject2, retrievedObject);
- // Remove objects
- this.globalCache.removeObject(cacheKey);
- this.globalCache.removeObject(cacheKey_2);
- }
-
- /**
- * Verify that an object will throw the ObjectExpiredException when it now
- * longer exists in cache.
- *
- * @throws Exception
- */
- public void testObjectExpiration() throws Exception
- {
- String testString = "This is a test";
- Object retrievedObject = null;
- CachedObject cacheObject = null;
- // Create and add Object that expires in 1000 millis (1 second)
- cacheObject = new CachedObject(testString, 1000);
- assertNotNull("Failed to create a cachable object", cacheObject);
- long addTime = System.currentTimeMillis();
- this.globalCache.addObject(cacheKey, cacheObject);
- // Try to get un-expired object
- try
- {
- retrievedObject = this.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);
- }
- // Sleep 1500 Millis (1.5 seconds)
- Thread.sleep(1500);
- // Try to get expired object
- try
- {
- retrievedObject = null;
- retrievedObject = this.globalCache.getObject(cacheKey);
- assertNull(
- "Retrieved the expired cached object and did not get expected ObjectExpiredException",
- retrievedObject);
- assertNotNull(
- "Did not get expected ObjectExpiredException retrieving an expired object",
- retrievedObject);
- }
- catch (ObjectExpiredException e)
- {
- assertNull(
- "Retrieved the expired cached object, but caught expected ObjectExpiredException exception",
- retrievedObject);
- }
- // Remove objects
- this.globalCache.removeObject(cacheKey);
- }
-
- /**
- * Verify the all object will be flushed from the cache.
- *
- * This test can take server minutes.
- *
- * @throws Exception
- */
- public void testCacheFlush() throws Exception
- {
- String testString = "This is a test";
- CachedObject cacheObject = null;
- // Create and add Object that expires in 1 turbine Refresh + 1 millis
- cacheObject = new CachedObject(testString, (getCacheRefresh() * 5) + 1);
- assertNotNull("Failed to create a cachable object", cacheObject);
- this.globalCache.addObject(cacheKey, cacheObject);
- // 1 Refresh
- Thread.sleep(getCacheRefresh() + 1);
- assertTrue("No object in cache before flush", (0 < this.globalCache
- .getNumberOfObjects()));
- // Flush Cache
- this.globalCache.flushCache();
- // Wait 15 seconds, 3 Refresh
- Thread.sleep((getCacheRefresh() * 2) + 1);
- assertEquals("After refresh", 0, this.globalCache.getNumberOfObjects());
- // Remove objects
- this.globalCache.removeObject(cacheKey);
- }
-
- /**
- * Verify the Cache count is correct.
- *
- * @throws Exception
- */
- public void testObjectCount() throws Exception
- {
- assertNotNull("Could not retrive cache service.", this.globalCache);
- // Create and add Object that expires in 1.5 turbine Refresh
- long expireTime = getCacheRefresh() + getCacheRefresh() / 2;
- CachedObject cacheObject = new CachedObject("This is a test",
- expireTime);
- assertNotNull("Failed to create a cachable object", cacheObject);
- this.globalCache.addObject("testObjectCount", cacheObject);
- assertEquals("After adding 1 Object", 1, this.globalCache
- .getNumberOfObjects());
- // Wait until we're passed 1 refresh, but not half way.
- Thread.sleep(getCacheRefresh() + getCacheRefresh() / 3);
- assertEquals("After one refresh", 1, this.globalCache
- .getNumberOfObjects());
- // Wait until we're passed 2 more refreshes
- Thread.sleep((getCacheRefresh() * 2) + getCacheRefresh() / 3);
- assertEquals("After three refreshes", 0, this.globalCache
- .getNumberOfObjects());
- }
-
- /**
- * Verfy a refreshable object will refreshed in the following cases: - The
- * object is retrieved via getObject an it is stale. - The object is
- * determied to be stale during a cache refresh
- *
- * This test can take serveral minutes.
- *
- * @throws Exception
- */
- public void testRefreshableObject() throws Exception
- {
- Object retrievedObject = null;
- RefreshableCachedObject cacheObject = null;
- // Create and add Object that expires in TEST_EXPIRETIME millis.
- cacheObject = new RefreshableCachedObject(new RefreshableObject(),
- getTestExpireTime());
- assertNotNull("Failed to create a cachable object", cacheObject);
- long addTime = System.currentTimeMillis();
- this.globalCache.addObject("refreshableObject", cacheObject);
- // Try to get un-expired object
- try
- {
- retrievedObject = this.globalCache.getObject("refreshableObject");
- 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);
- }
- // Wait 1 Turbine cache refresh + 1 second.
- Thread.sleep(getTestExpireTime() + 1000);
- // Try to get expired object
- try
- {
- retrievedObject = null;
- retrievedObject = this.globalCache.getObject("refreshableObject");
- 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);
- }
-
- // See if object will expires (testing every second for 100 seconds. It
- // should not!
- for (int i = 0; i < 100; i++)
- {
- Thread.sleep(1000); // Sleep 0.5 seconds
- // Try to get expired object
- try
- {
- retrievedObject = null;
- retrievedObject = this.globalCache
- .getObject("refreshableObject");
- 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);
- }
- }
- // Remove objects
- this.globalCache.removeObject(cacheKey);
- }
-
- /**
- * Verify a cached object will be delete after it has been untouched beyond
- * it's TimeToLive.
- *
- * This test can take serveral minutes.
- *
- * @throws Exception
- */
- public void testRefreshableTimeToLive() throws Exception
- {
- String skipTestsProperty = System.getProperty(SKIP_TESTS_KEY, "false");
- LOG.info("What is the value of the skipTestsProperty:"
- + skipTestsProperty);
- if (Boolean.valueOf(skipTestsProperty).booleanValue())
- {
- LOG.warn("Skipping testRefreshableTimeToLive test due to property "
- + SKIP_TESTS_KEY + " being true.");
- return;
- }
- else
- {
- LOG.warn("Running testRefreshableTimeToLive test due to property "
- + SKIP_TESTS_KEY + " being false.");
- }
-
- Object retrievedObject = null;
- RefreshableCachedObject cacheObject = null;
- // Create and add Object that expires in TEST_EXPIRETIME millis.
- cacheObject = new RefreshableCachedObject(new RefreshableObject(),
- getTestExpireTime());
- assertNotNull("Failed to create a cachable object", cacheObject);
- cacheObject.setTTL(getTestExpireTime());
- // Verify TimeToLive was set
- assertEquals("Returned TimeToLive", getTestExpireTime(), cacheObject
- .getTTL());
- // Add object to Cache
- long addTime = System.currentTimeMillis();
- this.globalCache.addObject(cacheKey, cacheObject);
- // Try to get un-expired object
- try
- {
- retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieved a cached object", retrievedObject);
- assertEquals("Did not retrieved correct cached object",
- cacheObject, retrievedObject);
- }
- catch (ObjectExpiredException e)
- {
- fail("Object expired early ( "
- + (System.currentTimeMillis() - addTime) + " millis)");
- }
- // Wait long enough to allow object to expire, but do not exceed TTL
- long timeout = getTestExpireTime() - 0;
- Thread.sleep(timeout);
- // Try to get expired object
- try
- {
- retrievedObject = null;
- retrievedObject = this.globalCache.getObject(cacheKey);
- assertNotNull("Did not retrieve a cached object, after sleep",
- retrievedObject);
- assertNotNull("Cached object has no contents, after sleep.",
- ((RefreshableCachedObject) retrievedObject).getContents());
- /*
- * @todo this is not working for some reason
- *
- * 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);
- }
- // Wait long enough to allow object to expire and exceed TTL
- Thread.sleep(getTestExpireTime() + 5000);
- // Try to get expired object
- try
- {
- retrievedObject = null;
- retrievedObject = this.globalCache.getObject(cacheKey);
- assertNull("Retrieved a cached object, after exceeding TimeToLive",
- retrievedObject);
- }
- catch (ObjectExpiredException e)
- {
- assertNull(
- "Retrieved the expired cached object, but caught expected ObjectExpiredException exception",
- retrievedObject);
- }
- }
-
/**
- * Test that we can get a list of the keys in the cache
+ * @see org.apache.fulcrum.cache.CacheTest#getCacheRoleName()
*/
- public void testCacheGetKeyList()
+ protected String getCacheRoleName()
{
- this.globalCache.flushCache();
- this.globalCache.addObject("date1", new CachedObject(new Date()));
- this.globalCache.addObject("date2", new CachedObject(new Date()));
- this.globalCache.addObject("date3", new CachedObject(new Date()));
- assertTrue("Did not get key list back.",
- (this.globalCache.getKeys() != null));
- List keys = this.globalCache.getKeys();
- for (Iterator itr = keys.iterator(); itr.hasNext();)
- {
- Object key = itr.next();
- assertTrue("Key was not an instance of String.",
- (key instanceof String));
- }
-
- }
-
- /**
- * Test that we can get a list of the keys in the cache
- */
- public void testCacheGetCachedObjects()
- {
- this.globalCache.flushCache();
- this.globalCache.addObject("date1", new CachedObject(new Date()));
- this.globalCache.addObject("date2", new CachedObject(new Date()));
- this.globalCache.addObject("date3", new CachedObject(new Date()));
- assertTrue("Did not get object list back.", (this.globalCache
- .getCachedObjects() != null));
- List objects = this.globalCache.getCachedObjects();
- for (Iterator itr = objects.iterator(); itr.hasNext();)
- {
- Object obj = itr.next();
- assertNotNull("Object was null.", obj);
- assertTrue("Object was not an instance of CachedObject",
- (obj instanceof CachedObject));
- }
- }
-
- /**
- * Test that the retrieved list is safe from
- * ConcurrentModificationException's being thrown if the cache is updated
- * while we are iterating over the List.
- */
- public void testCacheModification()
- {
- this.globalCache.flushCache();
- this.globalCache.addObject("date1", new CachedObject(new Date()));
- this.globalCache.addObject("date2", new CachedObject(new Date()));
- this.globalCache.addObject("date3", new CachedObject(new Date()));
- assertTrue("Did not get key list back.",
- (this.globalCache.getKeys() != null));
- List keys = this.globalCache.getKeys();
- try
- {
- for (Iterator itr = keys.iterator(); itr.hasNext();)
- {
- Object key = itr.next();
- this.globalCache.addObject("date4",
- new CachedObject(new Date()));
- }
- }
- catch (ConcurrentModificationException cme)
- {
- fail("Caught ConcurrentModificationException adding to cache.");
- }
- List objects = this.globalCache.getCachedObjects();
- try
- {
- for (Iterator itr = objects.iterator(); itr.hasNext();)
- {
- Object obj = itr.next();
- this.globalCache.addObject("date4",
- new CachedObject(new Date()));
- }
- }
- catch (ConcurrentModificationException cme)
- {
- fail("Caught ConcurrentModificationException adding to cache.");
- }
- }
-
- /**
- * Down cast the interface to the concreate object in order to grab the
- * cache check frequency.
- *
- * @return the refresh requency in milliseconds
- */
- private long getCacheRefresh()
- {
- return 5 * 1000;
- }
-
- /**
- * How long until it expires
- *
- * @return the cache refresh plus 1000.
- */
- private long getTestExpireTime()
- {
- return getCacheRefresh() + 1000;
- }
-
- /**
- * Simple object that can be refreshed
- */
- class RefreshableObject implements Refreshable
- {
- private int refreshCount = 0;
-
- /**
- * Increment the refresh counter
- */
- public void refresh()
- {
- this.refreshCount++;
- }
-
- /**
- * Reutrn the number of time this object has been refreshed
- *
- * @return Number of times refresh() has been called
- */
- public int getRefreshCount()
- {
- return this.refreshCount;
- }
+ return GlobalCacheService.ROLE + "_JCS";
}
}
Modified: turbine/fulcrum/trunk/cache/xdocs/changes.xml
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/xdocs/changes.xml?rev=927153&r1=927152&r2=927153&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/xdocs/changes.xml (original)
+++ turbine/fulcrum/trunk/cache/xdocs/changes.xml Wed Mar 24 18:55:08 2010
@@ -25,6 +25,12 @@
<body>
<release version="1.1.0" date="in Subversion">
+ <action type="update" dev="tv">
+ Refactored all implementations to be in the impl-package.
+ </action>
+ <action type="update" dev="tv">
+ Added a EHCache based implementation of the GlobalCacheService interface.
+ </action>
<action type="update" dev="sgoeschl" date="2010-03-22">
Pulling in commons-logging-1.1.1 to avoid the 1.1 version
which depends on an outdated Avalon framework
@@ -32,7 +38,7 @@
<action type="update" dev="sgoeschl" date="2010-03-18">
Updating the M2 build.
</action>
- <action type="update" dev="sgoeschl">
+ <action type="update" dev="sgoeschl">
Improved maven-1.1 build (default goal now in project.xml)
</action>
<action type="add" dev="sgoeschl">
Modified: turbine/fulcrum/trunk/cache/xdocs/index.xml
URL: http://svn.apache.org/viewvc/turbine/fulcrum/trunk/cache/xdocs/index.xml?rev=927153&r1=927152&r2=927153&view=diff
==============================================================================
--- turbine/fulcrum/trunk/cache/xdocs/index.xml (original)
+++ turbine/fulcrum/trunk/cache/xdocs/index.xml Wed Mar 24 18:55:08 2010
@@ -60,7 +60,7 @@
<role
name="org.apache.fulcrum.cache.GlobalCacheService"
shorthand="cache"
- default-class="org.apache.fulcrum.cache.DefaultGlobalCacheService"/>
+ default-class="org.apache.fulcrum.cache.impl.DefaultGlobalCacheService"/>
]]></source>
</subsection>
@@ -105,17 +105,64 @@
<subsection name="Role Configuration">
<source><![CDATA[
<role
- name="org.apache.fulcrum.cache.EHCacheService"
+ name="org.apache.fulcrum.cache.GlobalCacheService"
shorthand="ehcache"
- default-class="org.apache.fulcrum.cache.impl.DefaultEHCacheService"/>
+ default-class="org.apache.fulcrum.cache.impl.EHCacheService"/>
]]></source>
</subsection>
<subsection name="Component Configuration">
+ <table>
+ <tr>
+ <th>Item</th>
+ <th>Datatype</th>
+ <th>Cardinality</th>
+ <th>Description</th>
+ </tr>
+ <tr>
+ <td>cacheCheckFrequency</td>
+ <td>int</td>
+ <td>[0|1]</td>
+ <td>
+ The cache uses a background thread to check for expired objects.
+ This defines the time between two checks in milliseconds. The
+ default is 5000.
+ </td>
+ </tr>
+ <tr>
+ <td>cacheName</td>
+ <td>String</td>
+ <td>[0|1]</td>
+ <td>
+ The EHcache cache name to use for the cache. The default is
+ <code>fulcrum</code>.
+ </td>
+ </tr>
+ <tr>
+ <td>configurationFile</td>
+ <td>String</td>
+ <td>[0|1]</td>
+ <td>
+ The the location of the EHcache configuration file.
+ The default is to create a default cache withut settings.
+ </td>
+ </tr>
+ </table>
<p>
- There is nothing to configure for the component.
+ See <a href="http://jakarta.apache.org/jcs/">the JCS site</a> for more
+ information about configuring JCS.
</p>
</subsection>
+
+ <subsection name="Component Configuration Example">
+ <source><![CDATA[
+ <ehcache>
+ <cacheCheckFrequency>5000</cacheCheckFrequency>
+ <cacheName>fulcrum</cacheName>
+ <configurationFile>ehcache.xml</configurationFile>
+ </ehcache>
+ ]]></source>
+ </subsection>
</section>
<section name="JCSCacheService" id="JCSCacheService">
@@ -159,7 +206,7 @@
<td>String</td>
<td>[0|1]</td>
<td>
- The JCS cache region name to use for the cache The default is
+ The JCS cache region name to use for the cache. The default is
<code>fulcrum</code>.
JCS will store the objects in a group named <code>default_group</code>
in the given region.