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.
16 package org.wamblee.cache;
18 import junit.framework.TestCase;
20 import net.sf.ehcache.CacheException;
22 import org.wamblee.io.TestResource;
24 import org.wamblee.test.TimingUtils;
26 import java.io.IOException;
32 * @author Erik Brakkee
34 public class CachedObjectTest extends TestCase {
38 private static final String EHCACHE_CONFIG = "ehcache.xml";
39 private static final int OBJECT_KEY = 10;
40 private CachedObject.Computation<Integer, Integer> computation;
41 private int ncomputations;
44 * @see junit.framework.TestCase#setUp()
47 protected void setUp() throws Exception {
49 computation = new CachedObject.Computation<Integer, Integer>() {
50 public Integer getObject(Integer aObjectKey) {
53 return compute(aObjectKey);
60 private int compute(int aValue) {
64 private CachedObject<Integer, Integer> createCached(
65 Cache<Integer, Integer> aCache) {
66 return new CachedObject<Integer, Integer>(aCache, OBJECT_KEY,
71 * Verifies that upon first use, the cached object uses the computation to
72 * retrieve the object.
75 public void testComputation() {
76 CachedObject<Integer, Integer> cached = createCached(new ZeroCache<Integer, Integer>());
77 int value = cached.get();
78 assertEquals(compute(OBJECT_KEY), value);
79 assertEquals(1, ncomputations);
82 public void testInvalidateCache() {
83 CachedObject<Integer, Integer> cached = createCached(new ForeverCache<Integer, Integer>());
84 int value = cached.get();
85 assertEquals(compute(OBJECT_KEY), value);
86 assertEquals(1, ncomputations);
89 assertEquals(compute(OBJECT_KEY), value);
90 assertEquals(2, ncomputations);
93 public void testBehaviorEhCache() throws CacheException, IOException {
94 Cache<Integer, Integer> cache = new EhCache<Integer, Integer>(new TestResource(
95 CachedObjectTest.class, EHCACHE_CONFIG), "test");
96 CachedObject<Integer, Integer> cached = createCached(cache);
98 assertTrue(cache == cached.getCache());
100 int value = cached.get();
101 assertEquals(compute(OBJECT_KEY), value);
102 assertEquals(1, ncomputations);
103 // The value must still be cached.
104 value = cached.get();
105 assertEquals(compute(OBJECT_KEY), value);
106 assertEquals(1, ncomputations);
109 TimingUtils.sleep(6000);
110 value = cached.get();
111 assertEquals(compute(OBJECT_KEY), value);
112 assertEquals(2, ncomputations);
114 // Should still be cached now.
115 value = cached.get();
116 assertEquals(compute(OBJECT_KEY), value);
117 assertEquals(2, ncomputations);
119 // explicit invalidation.
121 value = cached.get();
122 assertEquals(compute(OBJECT_KEY), value);
123 assertEquals(3, ncomputations);
126 public void testBehaviorEhCacheDefault() throws CacheException, IOException {
127 Cache<Integer, Integer> cache = new EhCache<Integer, Integer>(new TestResource(
128 CachedObjectTest.class, EHCACHE_CONFIG), "undefined");
129 CachedObject<Integer, Integer> cached = createCached(cache);
131 assertTrue(cache == cached.getCache());
133 int value = cached.get();
134 assertEquals(compute(OBJECT_KEY), value);
135 assertEquals(1, ncomputations);
136 // The value must still be cached.
137 value = cached.get();
138 assertEquals(compute(OBJECT_KEY), value);
139 assertEquals(1, ncomputations);
142 TimingUtils.sleep(6000);
143 value = cached.get();
144 assertEquals(compute(OBJECT_KEY), value);
145 assertEquals(2, ncomputations);
147 // Should still be cached now.
148 value = cached.get();
149 assertEquals(compute(OBJECT_KEY), value);
150 assertEquals(2, ncomputations);
153 public void testBehaviorForeverCache() {
154 CachedObject<Integer, Integer> cached = createCached(new ForeverCache<Integer, Integer>());
155 int value = cached.get();
156 assertEquals(compute(OBJECT_KEY), value);
157 assertEquals(1, ncomputations);
159 for (int ncomp = 2; ncomp <= 100; ncomp++) {
160 value = cached.get();
161 assertEquals(compute(OBJECT_KEY), value);
162 assertEquals(1, ncomputations);
166 public void testBehaviorZeroCache() {
167 CachedObject<Integer, Integer> cached = createCached(new ZeroCache<Integer, Integer>());
168 int value = cached.get();
169 assertEquals(compute(OBJECT_KEY), value);
170 assertEquals(1, ncomputations);
172 for (int ncomp = 2; ncomp <= 100; ncomp++) {
173 value = cached.get();
174 assertEquals(compute(OBJECT_KEY), value);
175 assertEquals(ncomp, ncomputations);
179 value = cached.get();
180 assertEquals(compute(OBJECT_KEY), value);
181 assertEquals(101, ncomputations);