X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;ds=sidebyside;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fcache%2FCachedObjectTest.java;fp=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fcache%2FCachedObjectTest.java;h=da6e1be6575a98cfb1652b933382126136bc505e;hb=32a62ca2c752e33a7873ac868a7a1f289caedcd4;hp=0000000000000000000000000000000000000000;hpb=d2bdf4e813c6a3964958c87b2ce56eaadf8a1f0a;p=utils diff --git a/support/general/src/test/java/org/wamblee/cache/CachedObjectTest.java b/support/general/src/test/java/org/wamblee/cache/CachedObjectTest.java new file mode 100644 index 00000000..da6e1be6 --- /dev/null +++ b/support/general/src/test/java/org/wamblee/cache/CachedObjectTest.java @@ -0,0 +1,176 @@ +/* + * 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. + * + * @author Erik Brakkee + */ +public class CachedObjectTest extends TestCase { + + /** + * + */ + private static final String EHCACHE_CONFIG = "ehcache.xml"; + + private static final int OBJECT_KEY = 10; + + private CachedObject.Computation _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); + } +}