From 0ce24cdf21bf20c6db2ee8361d80e7d817611e9c Mon Sep 17 00:00:00 2001 From: Erik Brakkee Date: Mon, 27 Mar 2006 13:48:16 +0000 Subject: [PATCH] --- .classpath | 1 + support/build.xml | 2 +- .../test/org/wamblee/cache/ehcache.xml | 64 +++++++ support/src/org/wamblee/cache/EhCache.java | 2 +- .../org/wamblee/cache/CachedObjectTest.java | 174 ++++++++++++++++++ support/test/org/wamblee/io/TestResource.java | 50 +++++ 6 files changed, 291 insertions(+), 2 deletions(-) create mode 100644 support/resources/test/org/wamblee/cache/ehcache.xml create mode 100644 support/test/org/wamblee/cache/CachedObjectTest.java create mode 100644 support/test/org/wamblee/io/TestResource.java diff --git a/.classpath b/.classpath index 0de549e7..c4500542 100644 --- a/.classpath +++ b/.classpath @@ -40,5 +40,6 @@ + diff --git a/support/build.xml b/support/build.xml index ed49d7a9..7be2e619 100644 --- a/support/build.xml +++ b/support/build.xml @@ -17,7 +17,7 @@ &header; + depends="logging.d,commons-collections.d,commons-beanutils.d,dom4j.d,xerces.d,ehcache.d,spring.d"> + + + + + + + + + + diff --git a/support/src/org/wamblee/cache/EhCache.java b/support/src/org/wamblee/cache/EhCache.java index b870f325..ca11ab3f 100644 --- a/support/src/org/wamblee/cache/EhCache.java +++ b/support/src/org/wamblee/cache/EhCache.java @@ -61,7 +61,7 @@ public class EhCache _computation; + private int _ncomputations; + + /* (non-Javadoc) + * @see junit.framework.TestCase#setUp() + */ + @Override + protected void setUp() throws Exception { + super.setUp(); + _computation = new CachedObject.Computation() { + public Integer getObject(Integer aObjectKey) { + _ncomputations++; + return compute(aObjectKey); + }; + }; + _ncomputations = 0; + } + + private int compute(int aValue) { + return aValue + 10; + } + + private CachedObject createCached(Cache aCache) { + return new CachedObject(aCache, OBJECT_KEY, _computation); + } + + /** + * Verifies that upon first use, the cached object uses the computation to + * retrieve the object. + * + */ + public void testComputation() { + CachedObject cached = createCached(new ZeroCache()); + int value = cached.get(); + assertEquals(compute(OBJECT_KEY), value); + assertEquals(1, _ncomputations); + } + + public void testInvalidateCache() { + CachedObject 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 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 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 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 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 index 00000000..f4d407d6 --- /dev/null +++ b/support/test/org/wamblee/io/TestResource.java @@ -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); + } + +} -- 2.31.1