2 * Copyright 2005-2010 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;
31 * @author Erik Brakkee
33 public class CachedObjectTest extends TestCase {
34 private static final String EHCACHE_CONFIG = "ehcache.xml";
35 private static final int OBJECT_KEY = 10;
36 private CachedObject.Computation<Integer, Integer> computation;
37 private int ncomputations;
42 * @see junit.framework.TestCase#setUp()
45 protected void setUp() throws Exception {
47 computation = new CachedObject.Computation<Integer, Integer>() {
48 public Integer getObject(Integer aObjectKey) {
51 return compute(aObjectKey);
57 private int compute(int aValue) {
61 private CachedObject<Integer, Integer> createCached(
62 Cache<Integer, Integer> aCache) {
63 return new CachedObject<Integer, Integer>(aCache, OBJECT_KEY,
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>(
92 new TestResource(CachedObjectTest.class, EHCACHE_CONFIG), "test");
93 CachedObject<Integer, Integer> cached = createCached(cache);
95 assertTrue(cache == cached.getCache());
97 int value = cached.get();
98 assertEquals(compute(OBJECT_KEY), value);
99 assertEquals(1, ncomputations);
100 // The value must still be cached.
101 value = cached.get();
102 assertEquals(compute(OBJECT_KEY), value);
103 assertEquals(1, ncomputations);
106 TimingUtils.sleep(6000);
107 value = cached.get();
108 assertEquals(compute(OBJECT_KEY), value);
109 assertEquals(2, ncomputations);
111 // Should still be cached now.
112 value = cached.get();
113 assertEquals(compute(OBJECT_KEY), value);
114 assertEquals(2, ncomputations);
116 // explicit invalidation.
118 value = cached.get();
119 assertEquals(compute(OBJECT_KEY), value);
120 assertEquals(3, ncomputations);
123 public void testBehaviorEhCacheDefault() throws CacheException, IOException {
124 Cache<Integer, Integer> cache = new EhCache<Integer, Integer>(
125 new TestResource(CachedObjectTest.class, EHCACHE_CONFIG),
127 CachedObject<Integer, Integer> cached = createCached(cache);
129 assertTrue(cache == cached.getCache());
131 int value = cached.get();
132 assertEquals(compute(OBJECT_KEY), value);
133 assertEquals(1, ncomputations);
134 // The value must still be cached.
135 value = cached.get();
136 assertEquals(compute(OBJECT_KEY), value);
137 assertEquals(1, ncomputations);
140 TimingUtils.sleep(6000);
141 value = cached.get();
142 assertEquals(compute(OBJECT_KEY), value);
143 assertEquals(2, ncomputations);
145 // Should still be cached now.
146 value = cached.get();
147 assertEquals(compute(OBJECT_KEY), value);
148 assertEquals(2, ncomputations);
151 public void testBehaviorForeverCache() {
152 CachedObject<Integer, Integer> cached = createCached(new ForeverCache<Integer, Integer>());
153 int value = cached.get();
154 assertEquals(compute(OBJECT_KEY), value);
155 assertEquals(1, ncomputations);
157 for (int ncomp = 2; ncomp <= 100; ncomp++) {
158 value = cached.get();
159 assertEquals(compute(OBJECT_KEY), value);
160 assertEquals(1, ncomputations);
164 public void testBehaviorZeroCache() {
165 CachedObject<Integer, Integer> cached = createCached(new ZeroCache<Integer, Integer>());
166 int value = cached.get();
167 assertEquals(compute(OBJECT_KEY), value);
168 assertEquals(1, ncomputations);
170 for (int ncomp = 2; ncomp <= 100; ncomp++) {
171 value = cached.get();
172 assertEquals(compute(OBJECT_KEY), value);
173 assertEquals(ncomp, ncomputations);
177 value = cached.get();
178 assertEquals(compute(OBJECT_KEY), value);
179 assertEquals(101, ncomputations);