source code formatting.
[utils] / support / general / src / test / java / org / wamblee / cache / CachedObjectTest.java
1 /*
2  * Copyright 2005 the original author or authors.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.wamblee.cache;
17
18 import junit.framework.TestCase;
19
20 import net.sf.ehcache.CacheException;
21
22 import org.wamblee.io.TestResource;
23
24 import org.wamblee.test.TimingUtils;
25
26 import java.io.IOException;
27
28
29 /**
30  * Cached object test.
31  *
32  * @author Erik Brakkee
33  */
34 public class CachedObjectTest extends TestCase {
35     /**
36      *
37      */
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;
42
43     /* (non-Javadoc)
44      * @see junit.framework.TestCase#setUp()
45      */
46     @Override
47     protected void setUp() throws Exception {
48         super.setUp();
49         computation = new CachedObject.Computation<Integer, Integer>() {
50                     public Integer getObject(Integer aObjectKey) {
51                         ncomputations++;
52
53                         return compute(aObjectKey);
54                     }
55                     ;
56                 };
57         ncomputations = 0;
58     }
59
60     private int compute(int aValue) {
61         return aValue + 10;
62     }
63
64     private CachedObject<Integer, Integer> createCached(
65         Cache<Integer, Integer> aCache) {
66         return new CachedObject<Integer, Integer>(aCache, OBJECT_KEY,
67             computation);
68     }
69
70     /**
71      * Verifies that upon first use, the cached object uses the computation to
72      * retrieve the object.
73      *
74      */
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);
80     }
81
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);
87         cached.invalidate();
88         value = cached.get();
89         assertEquals(compute(OBJECT_KEY), value);
90         assertEquals(2, ncomputations);
91     }
92
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);
97
98         assertTrue(cache == cached.getCache());
99
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);
107
108         // Cache expiry. 
109         TimingUtils.sleep(6000);
110         value = cached.get();
111         assertEquals(compute(OBJECT_KEY), value);
112         assertEquals(2, ncomputations);
113
114         // Should still be cached now.
115         value = cached.get();
116         assertEquals(compute(OBJECT_KEY), value);
117         assertEquals(2, ncomputations);
118
119         // explicit invalidation.
120         cached.invalidate();
121         value = cached.get();
122         assertEquals(compute(OBJECT_KEY), value);
123         assertEquals(3, ncomputations);
124     }
125
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);
130
131         assertTrue(cache == cached.getCache());
132
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);
140
141         // Cache expiry. 
142         TimingUtils.sleep(6000);
143         value = cached.get();
144         assertEquals(compute(OBJECT_KEY), value);
145         assertEquals(2, ncomputations);
146
147         // Should still be cached now.
148         value = cached.get();
149         assertEquals(compute(OBJECT_KEY), value);
150         assertEquals(2, ncomputations);
151     }
152
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);
158
159         for (int ncomp = 2; ncomp <= 100; ncomp++) {
160             value = cached.get();
161             assertEquals(compute(OBJECT_KEY), value);
162             assertEquals(1, ncomputations);
163         }
164     }
165
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);
171
172         for (int ncomp = 2; ncomp <= 100; ncomp++) {
173             value = cached.get();
174             assertEquals(compute(OBJECT_KEY), value);
175             assertEquals(ncomp, ncomputations);
176         }
177
178         cached.invalidate();
179         value = cached.get();
180         assertEquals(compute(OBJECT_KEY), value);
181         assertEquals(101, ncomputations);
182     }
183 }