(no commit message)
authorerik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 27 Mar 2006 13:48:16 +0000 (13:48 +0000)
committererik <erik@77661180-640e-0410-b3a8-9f9b13e6d0e0>
Mon, 27 Mar 2006 13:48:16 +0000 (13:48 +0000)
.classpath
support/build.xml
support/resources/test/org/wamblee/cache/ehcache.xml [new file with mode: 0644]
support/src/org/wamblee/cache/EhCache.java
support/test/org/wamblee/cache/CachedObjectTest.java [new file with mode: 0644]
support/test/org/wamblee/io/TestResource.java [new file with mode: 0644]

index 0de549e77369fea1cf84e9fced87d24c9a252b0a..c450054242b56559f32196b6e1e5d78d62a77eea 100644 (file)
@@ -40,5 +40,6 @@
        <classpathentry kind="lib" path="support/lib/external/commons-beanutils-1.7.0.jar"/>
        <classpathentry kind="lib" path="support/lib/external/commons-logging-1.0.2.jar"/>
        <classpathentry kind="lib" path="support/lib/external/jaxen-1.1-beta-4.jar"/>
+       <classpathentry kind="lib" path="support/lib/external/commons-collections-3.1.jar"/>
        <classpathentry kind="output" path="support/testbin"/>
 </classpath>
index ed49d7a9eb225bd9e471eb96aaad93ab65a5d885..7be2e619c8e36314f1b4e0804c2f8822e34abbba 100644 (file)
@@ -17,7 +17,7 @@
    &header;
        
        <target name="module.build.deps" 
-         depends="logging.d,commons-beanutils.d,dom4j.d,xerces.d,ehcache.d,spring.d">
+         depends="logging.d,commons-collections.d,commons-beanutils.d,dom4j.d,xerces.d,ehcache.d,spring.d">
        </target>
        
        <!-- Set libraries to use in addition for test, a library which 
diff --git a/support/resources/test/org/wamblee/cache/ehcache.xml b/support/resources/test/org/wamblee/cache/ehcache.xml
new file mode 100644 (file)
index 0000000..694184e
--- /dev/null
@@ -0,0 +1,64 @@
+<ehcache>
+
+    <!-- Sets the path to the directory where cache .data files are created.
+
+         If the path is a Java System Property it is replaced by
+         its value in the running VM.
+
+         The following properties are translated:
+         user.home - User's home directory
+         user.dir - User's current working directory
+         java.io.tmpdir - Default temp file path -->
+    <diskStore path="java.io.tmpdir"/>
+
+
+    <!--Default Cache configuration. These will applied to caches programmatically created through
+        the CacheManager.
+
+        The following attributes are required:
+
+        maxElementsInMemory            - Sets the maximum number of objects that will be created in memory
+        eternal                        - Sets whether elements are eternal. If eternal,  timeouts are ignored and the
+                                         element is never expired.
+        overflowToDisk                 - Sets whether elements can overflow to disk when the in-memory cache
+                                         has reached the maxInMemory limit.
+
+        The following attributes are optional:
+        timeToIdleSeconds              - Sets the time to idle for an element before it expires.
+                                         i.e. The maximum amount of time between accesses before an element expires
+                                         Is only used if the element is not eternal.
+                                         Optional attribute. A value of 0 means that an Element can idle for infinity.
+                                         The default value is 0.
+        timeToLiveSeconds              - Sets the time to live for an element before it expires.
+                                         i.e. The maximum time between creation time and when an element expires.
+                                         Is only used if the element is not eternal.
+                                         Optional attribute. A value of 0 means that and Element can live for infinity.
+                                         The default value is 0.
+        diskPersistent                 - Whether the disk store persists between restarts of the Virtual Machine.
+                                         The default value is false.
+        diskExpiryThreadIntervalSeconds- The number of seconds between runs of the disk expiry thread. The default value
+                                         is 120 seconds.
+        -->
+
+    <defaultCache
+        maxElementsInMemory="10000"
+        eternal="false"
+        overflowToDisk="false"
+        timeToIdleSeconds="5"
+        timeToLiveSeconds="5"
+        diskPersistent="false"
+        diskExpiryThreadIntervalSeconds="120"
+        />
+        
+    <cache
+        name="test"
+        maxElementsInMemory="10000"
+        eternal="false"
+        overflowToDisk="false"
+        timeToIdleSeconds="5"
+        timeToLiveSeconds="5"
+        diskPersistent="false"
+        diskExpiryThreadIntervalSeconds="120"
+    />
+    
+</ehcache>
index b870f3256bdff154fed39c3fee534479a427ffab..ca11ab3f72c71f13aa92341afb6f5ba13fd064e2 100644 (file)
@@ -61,7 +61,7 @@ public class EhCache<KeyType extends Serializable, ValueType extends Serializabl
             throws IOException, CacheException {
         InputStream is = aResource.getInputStream();
         try {
-            _manager = CacheManager.create();
+            _manager = CacheManager.create(is);
             _cache = _manager.getCache(aCacheName);
             if (_cache == null) {
                 LOGGER.warn("Creating cache '" + aCacheName
diff --git a/support/test/org/wamblee/cache/CachedObjectTest.java b/support/test/org/wamblee/cache/CachedObjectTest.java
new file mode 100644 (file)
index 0000000..23706f3
--- /dev/null
@@ -0,0 +1,174 @@
+/*
+ * Copyright 2005 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+
+package org.wamblee.cache;
+
+import java.io.IOException;
+
+import junit.framework.TestCase;
+import net.sf.ehcache.CacheException;
+
+import org.wamblee.io.TestResource;
+import org.wamblee.test.TimingUtils;
+
+/**
+ * Cached object test. 
+ */
+public class CachedObjectTest extends TestCase {
+    
+    /**
+     * 
+     */
+    private static final String EHCACHE_CONFIG = "ehcache.xml";
+
+    private static final int OBJECT_KEY = 10; 
+    
+    private CachedObject.Computation<Integer,Integer> _computation;
+    private int _ncomputations; 
+    
+    /* (non-Javadoc)
+     * @see junit.framework.TestCase#setUp()
+     */
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        _computation = new CachedObject.Computation<Integer,Integer>() { 
+            public Integer getObject(Integer aObjectKey) {
+                _ncomputations++;
+                return compute(aObjectKey);
+            };
+        };
+        _ncomputations = 0; 
+    }
+    
+    private int compute(int aValue) { 
+        return aValue + 10;
+    }
+    
+    private CachedObject<Integer, Integer> createCached(Cache aCache) { 
+        return new CachedObject<Integer, Integer>(aCache, OBJECT_KEY, _computation);
+    }
+
+    /**
+     * Verifies that upon first use, the cached object uses the computation to 
+     * retrieve the object.  
+     *
+     */
+    public void testComputation() { 
+        CachedObject<Integer, Integer> cached = createCached(new ZeroCache());
+        int value = cached.get();
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(1, _ncomputations); 
+    }
+    
+    public void testInvalidateCache() { 
+        CachedObject<Integer, Integer> cached = createCached(new ForeverCache());
+        int value = cached.get();
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(1, _ncomputations);
+        cached.invalidate(); 
+        value = cached.get();
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(2, _ncomputations);
+    }
+    
+    public void testBehaviorEhCache() throws CacheException, IOException {
+        Cache cache = new EhCache(new TestResource(CachedObjectTest.class, EHCACHE_CONFIG), "test");
+        CachedObject<Integer, Integer> cached = createCached(cache);
+        
+        assertTrue( cache == cached.getCache());
+        int value = cached.get();
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(1, _ncomputations);
+        // The value must still be cached. 
+        value = cached.get(); 
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(1, _ncomputations);
+        
+        // Cache expiry. 
+        TimingUtils.sleep(6000); 
+        value = cached.get(); 
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(2, _ncomputations);
+        
+        // Should still be cached now.
+        value = cached.get(); 
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(2, _ncomputations);
+        
+        // explicit invalidation.
+        cached.invalidate(); 
+        value = cached.get(); 
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(3, _ncomputations);
+        
+    }
+    
+    public void testBehaviorEhCacheDefault() throws CacheException, IOException {
+        Cache cache = new EhCache(new TestResource(CachedObjectTest.class, EHCACHE_CONFIG), "undefined");
+        CachedObject<Integer, Integer> cached = createCached(cache);
+        
+        assertTrue( cache == cached.getCache());
+        int value = cached.get();
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(1, _ncomputations);
+        // The value must still be cached. 
+        value = cached.get(); 
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(1, _ncomputations);
+        
+        // Cache expiry. 
+        TimingUtils.sleep(6000); 
+        value = cached.get(); 
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(2, _ncomputations);
+        
+        // Should still be cached now.
+        value = cached.get(); 
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(2, _ncomputations);
+        
+    }
+    
+    
+    public void testBehaviorForeverCache() { 
+        CachedObject<Integer, Integer> cached = createCached(new ForeverCache());
+        int value = cached.get();
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(1, _ncomputations); 
+        for (int ncomp = 2; ncomp <= 100; ncomp++) { 
+            value = cached.get(); 
+            assertEquals(compute(OBJECT_KEY), value);
+            assertEquals(1, _ncomputations);
+        }
+    }
+    
+    public void testBehaviorZeroCache() { 
+        CachedObject<Integer, Integer> cached = createCached(new ZeroCache());
+        int value = cached.get();
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(1, _ncomputations); 
+        for (int ncomp = 2; ncomp <= 100; ncomp++) { 
+            value = cached.get(); 
+            assertEquals(compute(OBJECT_KEY), value);
+            assertEquals(ncomp, _ncomputations);
+        }
+        cached.invalidate(); 
+        value = cached.get(); 
+        assertEquals(compute(OBJECT_KEY), value);
+        assertEquals(101, _ncomputations);
+    }
+}
diff --git a/support/test/org/wamblee/io/TestResource.java b/support/test/org/wamblee/io/TestResource.java
new file mode 100644 (file)
index 0000000..f4d407d
--- /dev/null
@@ -0,0 +1,50 @@
+/*
+ * Copyright 2005 the original author or authors.
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */ 
+
+package org.wamblee.io;
+
+import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.wamblee.test.FileSystemUtils;
+
+/**
+ * Test resource for locating resources in the classpath.
+ */
+public class TestResource extends FileResource {
+   
+    /**
+     * Test class name. 
+     * @param aTestClass Test class. 
+     * @param aName Name of the file to look for. 
+     */
+    public TestResource(Class aTestClass, String aName) {
+        super(getFile(aTestClass, aName)); 
+    }
+    
+    /**
+     * Computes the file path of the file to look for. 
+     * @param aClass Test class name. 
+     * @param aName Name of the file. 
+     * @return File. 
+     */
+    private static File getFile(Class aClass, String aName) { 
+        File dir = FileSystemUtils.getTestInputDir(aClass);
+        return new File(dir, aName);
+    }
+
+}