X-Git-Url: http://wamblee.org/gitweb/?a=blobdiff_plain;f=support%2Fgeneral%2Fsrc%2Ftest%2Fjava%2Forg%2Fwamblee%2Fcache%2FCachedObjectTest.java;h=630770019b904e2977fcc610f6acaca390485f25;hb=8de36ff0206c996baf3ee4adc3e2293b12ff5f39;hp=70d1b7af9733a6cc52769404d04b525f28f4131c;hpb=0d8d8f24656e585ee75558cfd6a4c661f8f14985;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 index 70d1b7af..63077001 100644 --- a/support/general/src/test/java/org/wamblee/cache/CachedObjectTest.java +++ b/support/general/src/test/java/org/wamblee/cache/CachedObjectTest.java @@ -1,175 +1,180 @@ /* * 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; +import java.io.IOException; + /** - * Cached object test. - * + * 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; - private static final int OBJECT_KEY = 10; - - private CachedObject.Computation computation; - private int ncomputations; - - /* (non-Javadoc) + /* + * (non-Javadoc) + * * @see junit.framework.TestCase#setUp() */ @Override protected void setUp() throws Exception { super.setUp(); - computation = new CachedObject.Computation() { + computation = new CachedObject.Computation() { public Integer getObject(Integer aObjectKey) { ncomputations++; + return compute(aObjectKey); }; }; - ncomputations = 0; + ncomputations = 0; } - - private int compute(int aValue) { + + private int compute(int aValue) { return aValue + 10; } - - private CachedObject createCached(Cache aCache) { - return new CachedObject(aCache, OBJECT_KEY, computation); + + 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. - * + * Verifies that upon first use, the cached object uses the computation to + * retrieve the object. + * */ - public void testComputation() { - CachedObject cached = createCached(new ZeroCache()); + public void testComputation() { + CachedObject cached = createCached(new ZeroCache()); int value = cached.get(); assertEquals(compute(OBJECT_KEY), value); - assertEquals(1, ncomputations); + assertEquals(1, ncomputations); } - - public void testInvalidateCache() { - CachedObject cached = createCached(new ForeverCache()); + + public void testInvalidateCache() { + CachedObject cached = createCached(new ForeverCache()); int value = cached.get(); assertEquals(compute(OBJECT_KEY), value); assertEquals(1, ncomputations); - cached.invalidate(); + 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"); + Cache cache = new EhCache( + new TestResource(CachedObjectTest.class, EHCACHE_CONFIG), "test"); CachedObject cached = createCached(cache); - - assertTrue( cache == cached.getCache()); + + 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(); + // 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(); + + // Cache expiry. + TimingUtils.sleep(6000); + value = cached.get(); assertEquals(compute(OBJECT_KEY), value); assertEquals(2, ncomputations); - + // Should still be cached now. - value = cached.get(); + value = cached.get(); assertEquals(compute(OBJECT_KEY), value); assertEquals(2, ncomputations); - + // explicit invalidation. - cached.invalidate(); - value = cached.get(); + 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"); + Cache cache = new EhCache( + new TestResource(CachedObjectTest.class, EHCACHE_CONFIG), + "undefined"); CachedObject cached = createCached(cache); - - assertTrue( cache == cached.getCache()); + + 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(); + // 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(); + + // Cache expiry. + TimingUtils.sleep(6000); + value = cached.get(); assertEquals(compute(OBJECT_KEY), value); assertEquals(2, ncomputations); - + // Should still be cached now. - value = cached.get(); + value = cached.get(); assertEquals(compute(OBJECT_KEY), value); assertEquals(2, ncomputations); - } - - - public void testBehaviorForeverCache() { - CachedObject cached = createCached(new ForeverCache()); + + 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(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()); + + 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(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(); + + cached.invalidate(); + value = cached.get(); assertEquals(compute(OBJECT_KEY), value); assertEquals(101, ncomputations); }