Removed DOCUMENT ME comments that were generated and applied source code
[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  * Cached object test.
30  * 
31  * @author Erik Brakkee
32  */
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;
38
39     /*
40      * (non-Javadoc)
41      * 
42      * @see junit.framework.TestCase#setUp()
43      */
44     @Override
45     protected void setUp() throws Exception {
46         super.setUp();
47         computation = new CachedObject.Computation<Integer, Integer>() {
48             public Integer getObject(Integer aObjectKey) {
49                 ncomputations++;
50
51                 return compute(aObjectKey);
52             };
53         };
54         ncomputations = 0;
55     }
56
57     private int compute(int aValue) {
58         return aValue + 10;
59     }
60
61     private CachedObject<Integer, Integer> createCached(
62         Cache<Integer, Integer> aCache) {
63         return new CachedObject<Integer, Integer>(aCache, OBJECT_KEY,
64             computation);
65     }
66
67     /**
68      * Verifies that upon first use, the cached object uses the computation to
69      * retrieve the object.
70      * 
71      */
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);
77     }
78
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);
84         cached.invalidate();
85         value = cached.get();
86         assertEquals(compute(OBJECT_KEY), value);
87         assertEquals(2, ncomputations);
88     }
89
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);
94
95         assertTrue(cache == cached.getCache());
96
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);
104
105         // Cache expiry.
106         TimingUtils.sleep(6000);
107         value = cached.get();
108         assertEquals(compute(OBJECT_KEY), value);
109         assertEquals(2, ncomputations);
110
111         // Should still be cached now.
112         value = cached.get();
113         assertEquals(compute(OBJECT_KEY), value);
114         assertEquals(2, ncomputations);
115
116         // explicit invalidation.
117         cached.invalidate();
118         value = cached.get();
119         assertEquals(compute(OBJECT_KEY), value);
120         assertEquals(3, ncomputations);
121     }
122
123     public void testBehaviorEhCacheDefault() throws CacheException, IOException {
124         Cache<Integer, Integer> cache = new EhCache<Integer, Integer>(
125             new TestResource(CachedObjectTest.class, EHCACHE_CONFIG),
126             "undefined");
127         CachedObject<Integer, Integer> cached = createCached(cache);
128
129         assertTrue(cache == cached.getCache());
130
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);
138
139         // Cache expiry.
140         TimingUtils.sleep(6000);
141         value = cached.get();
142         assertEquals(compute(OBJECT_KEY), value);
143         assertEquals(2, ncomputations);
144
145         // Should still be cached now.
146         value = cached.get();
147         assertEquals(compute(OBJECT_KEY), value);
148         assertEquals(2, ncomputations);
149     }
150
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);
156
157         for (int ncomp = 2; ncomp <= 100; ncomp++) {
158             value = cached.get();
159             assertEquals(compute(OBJECT_KEY), value);
160             assertEquals(1, ncomputations);
161         }
162     }
163
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);
169
170         for (int ncomp = 2; ncomp <= 100; ncomp++) {
171             value = cached.get();
172             assertEquals(compute(OBJECT_KEY), value);
173             assertEquals(ncomp, ncomputations);
174         }
175
176         cached.invalidate();
177         value = cached.get();
178         assertEquals(compute(OBJECT_KEY), value);
179         assertEquals(101, ncomputations);
180     }
181 }