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 public class CachedObjectTest extends TestCase {
35 private static final String EHCACHE_CONFIG = "ehcache.xml";
37 private static final int OBJECT_KEY = 10;
39 private CachedObject.Computation<Integer,Integer> _computation;
40 private int _ncomputations;
43 * @see junit.framework.TestCase#setUp()
46 protected void setUp() throws Exception {
48 _computation = new CachedObject.Computation<Integer,Integer>() {
49 public Integer getObject(Integer aObjectKey) {
51 return compute(aObjectKey);
57 private int compute(int aValue) {
61 private CachedObject<Integer, Integer> createCached(Cache<Integer,Integer> aCache) {
62 return new CachedObject<Integer, Integer>(aCache, OBJECT_KEY, _computation);
66 * Verifies that upon first use, the cached object uses the computation to
67 * retrieve the object.
70 public void testComputation() {
71 CachedObject<Integer, Integer> cached = createCached(new ZeroCache<Integer,Integer>());
72 int value = cached.get();
73 assertEquals(compute(OBJECT_KEY), value);
74 assertEquals(1, _ncomputations);
77 public void testInvalidateCache() {
78 CachedObject<Integer, Integer> cached = createCached(new ForeverCache<Integer,Integer>());
79 int value = cached.get();
80 assertEquals(compute(OBJECT_KEY), value);
81 assertEquals(1, _ncomputations);
84 assertEquals(compute(OBJECT_KEY), value);
85 assertEquals(2, _ncomputations);
88 public void testBehaviorEhCache() throws CacheException, IOException {
89 Cache<Integer,Integer> cache = new EhCache<Integer,Integer>(new TestResource(CachedObjectTest.class, EHCACHE_CONFIG), "test");
90 CachedObject<Integer, Integer> cached = createCached(cache);
92 assertTrue( cache == cached.getCache());
93 int value = cached.get();
94 assertEquals(compute(OBJECT_KEY), value);
95 assertEquals(1, _ncomputations);
96 // The value must still be cached.
98 assertEquals(compute(OBJECT_KEY), value);
99 assertEquals(1, _ncomputations);
102 TimingUtils.sleep(6000);
103 value = cached.get();
104 assertEquals(compute(OBJECT_KEY), value);
105 assertEquals(2, _ncomputations);
107 // Should still be cached now.
108 value = cached.get();
109 assertEquals(compute(OBJECT_KEY), value);
110 assertEquals(2, _ncomputations);
112 // explicit invalidation.
114 value = cached.get();
115 assertEquals(compute(OBJECT_KEY), value);
116 assertEquals(3, _ncomputations);
120 public void testBehaviorEhCacheDefault() throws CacheException, IOException {
121 Cache<Integer,Integer> cache = new EhCache<Integer,Integer>(new TestResource(CachedObjectTest.class, EHCACHE_CONFIG), "undefined");
122 CachedObject<Integer, Integer> cached = createCached(cache);
124 assertTrue( cache == cached.getCache());
125 int value = cached.get();
126 assertEquals(compute(OBJECT_KEY), value);
127 assertEquals(1, _ncomputations);
128 // The value must still be cached.
129 value = cached.get();
130 assertEquals(compute(OBJECT_KEY), value);
131 assertEquals(1, _ncomputations);
134 TimingUtils.sleep(6000);
135 value = cached.get();
136 assertEquals(compute(OBJECT_KEY), value);
137 assertEquals(2, _ncomputations);
139 // Should still be cached now.
140 value = cached.get();
141 assertEquals(compute(OBJECT_KEY), value);
142 assertEquals(2, _ncomputations);
147 public void testBehaviorForeverCache() {
148 CachedObject<Integer, Integer> cached = createCached(new ForeverCache<Integer,Integer>());
149 int value = cached.get();
150 assertEquals(compute(OBJECT_KEY), value);
151 assertEquals(1, _ncomputations);
152 for (int ncomp = 2; ncomp <= 100; ncomp++) {
153 value = cached.get();
154 assertEquals(compute(OBJECT_KEY), value);
155 assertEquals(1, _ncomputations);
159 public void testBehaviorZeroCache() {
160 CachedObject<Integer, Integer> cached = createCached(new ZeroCache<Integer,Integer>());
161 int value = cached.get();
162 assertEquals(compute(OBJECT_KEY), value);
163 assertEquals(1, _ncomputations);
164 for (int ncomp = 2; ncomp <= 100; ncomp++) {
165 value = cached.get();
166 assertEquals(compute(OBJECT_KEY), value);
167 assertEquals(ncomp, _ncomputations);
170 value = cached.get();
171 assertEquals(compute(OBJECT_KEY), value);
172 assertEquals(101, _ncomputations);