2 * Copyright 2005 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.wamblee.cache;
19 import java.io.IOException;
21 import junit.framework.TestCase;
22 import net.sf.ehcache.CacheException;
24 import org.wamblee.io.TestResource;
25 import org.wamblee.test.TimingUtils;
30 * @author Erik Brakkee
32 public class CachedObjectTest extends TestCase {
37 private static final String EHCACHE_CONFIG = "ehcache.xml";
39 private static final int OBJECT_KEY = 10;
41 private CachedObject.Computation<Integer,Integer> _computation;
42 private int _ncomputations;
45 * @see junit.framework.TestCase#setUp()
48 protected void setUp() throws Exception {
50 _computation = new CachedObject.Computation<Integer,Integer>() {
51 public Integer getObject(Integer aObjectKey) {
53 return compute(aObjectKey);
59 private int compute(int aValue) {
63 private CachedObject<Integer, Integer> createCached(Cache<Integer,Integer> aCache) {
64 return new CachedObject<Integer, Integer>(aCache, OBJECT_KEY, _computation);
68 * Verifies that upon first use, the cached object uses the computation to
69 * retrieve the object.
72 public void testComputation() {
73 CachedObject<Integer, Integer> cached = createCached(new ZeroCache<Integer,Integer>());
74 int value = cached.get();
75 assertEquals(compute(OBJECT_KEY), value);
76 assertEquals(1, _ncomputations);
79 public void testInvalidateCache() {
80 CachedObject<Integer, Integer> cached = createCached(new ForeverCache<Integer,Integer>());
81 int value = cached.get();
82 assertEquals(compute(OBJECT_KEY), value);
83 assertEquals(1, _ncomputations);
86 assertEquals(compute(OBJECT_KEY), value);
87 assertEquals(2, _ncomputations);
90 public void testBehaviorEhCache() throws CacheException, IOException {
91 Cache<Integer,Integer> cache = new EhCache<Integer,Integer>(new TestResource(CachedObjectTest.class, EHCACHE_CONFIG), "test");
92 CachedObject<Integer, Integer> cached = createCached(cache);
94 assertTrue( cache == cached.getCache());
95 int value = cached.get();
96 assertEquals(compute(OBJECT_KEY), value);
97 assertEquals(1, _ncomputations);
98 // The value must still be cached.
100 assertEquals(compute(OBJECT_KEY), value);
101 assertEquals(1, _ncomputations);
104 TimingUtils.sleep(6000);
105 value = cached.get();
106 assertEquals(compute(OBJECT_KEY), value);
107 assertEquals(2, _ncomputations);
109 // Should still be cached now.
110 value = cached.get();
111 assertEquals(compute(OBJECT_KEY), value);
112 assertEquals(2, _ncomputations);
114 // explicit invalidation.
116 value = cached.get();
117 assertEquals(compute(OBJECT_KEY), value);
118 assertEquals(3, _ncomputations);
122 public void testBehaviorEhCacheDefault() throws CacheException, IOException {
123 Cache<Integer,Integer> cache = new EhCache<Integer,Integer>(new TestResource(CachedObjectTest.class, EHCACHE_CONFIG), "undefined");
124 CachedObject<Integer, Integer> cached = createCached(cache);
126 assertTrue( cache == cached.getCache());
127 int value = cached.get();
128 assertEquals(compute(OBJECT_KEY), value);
129 assertEquals(1, _ncomputations);
130 // The value must still be cached.
131 value = cached.get();
132 assertEquals(compute(OBJECT_KEY), value);
133 assertEquals(1, _ncomputations);
136 TimingUtils.sleep(6000);
137 value = cached.get();
138 assertEquals(compute(OBJECT_KEY), value);
139 assertEquals(2, _ncomputations);
141 // Should still be cached now.
142 value = cached.get();
143 assertEquals(compute(OBJECT_KEY), value);
144 assertEquals(2, _ncomputations);
149 public void testBehaviorForeverCache() {
150 CachedObject<Integer, Integer> cached = createCached(new ForeverCache<Integer,Integer>());
151 int value = cached.get();
152 assertEquals(compute(OBJECT_KEY), value);
153 assertEquals(1, _ncomputations);
154 for (int ncomp = 2; ncomp <= 100; ncomp++) {
155 value = cached.get();
156 assertEquals(compute(OBJECT_KEY), value);
157 assertEquals(1, _ncomputations);
161 public void testBehaviorZeroCache() {
162 CachedObject<Integer, Integer> cached = createCached(new ZeroCache<Integer,Integer>());
163 int value = cached.get();
164 assertEquals(compute(OBJECT_KEY), value);
165 assertEquals(1, _ncomputations);
166 for (int ncomp = 2; ncomp <= 100; ncomp++) {
167 value = cached.get();
168 assertEquals(compute(OBJECT_KEY), value);
169 assertEquals(ncomp, _ncomputations);
172 value = cached.get();
173 assertEquals(compute(OBJECT_KEY), value);
174 assertEquals(101, _ncomputations);