70d1b7af9733a6cc52769404d04b525f28f4131c
[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
17 package org.wamblee.cache;
18
19 import java.io.IOException;
20
21 import junit.framework.TestCase;
22 import net.sf.ehcache.CacheException;
23
24 import org.wamblee.io.TestResource;
25 import org.wamblee.test.TimingUtils;
26
27 /**
28  * Cached object test. 
29  *
30  * @author Erik Brakkee
31  */
32 public class CachedObjectTest extends TestCase {
33     
34     /**
35      * 
36      */
37     private static final String EHCACHE_CONFIG = "ehcache.xml";
38
39     private static final int OBJECT_KEY = 10; 
40     
41     private CachedObject.Computation<Integer,Integer> computation;
42     private int ncomputations; 
43     
44     /* (non-Javadoc)
45      * @see junit.framework.TestCase#setUp()
46      */
47     @Override
48     protected void setUp() throws Exception {
49         super.setUp();
50         computation = new CachedObject.Computation<Integer,Integer>() { 
51             public Integer getObject(Integer aObjectKey) {
52                 ncomputations++;
53                 return compute(aObjectKey);
54             };
55         };
56         ncomputations = 0; 
57     }
58     
59     private int compute(int aValue) { 
60         return aValue + 10;
61     }
62     
63     private CachedObject<Integer, Integer> createCached(Cache<Integer,Integer> aCache) { 
64         return new CachedObject<Integer, Integer>(aCache, OBJECT_KEY, 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>(new TestResource(CachedObjectTest.class, EHCACHE_CONFIG), "test");
92         CachedObject<Integer, Integer> cached = createCached(cache);
93         
94         assertTrue( cache == cached.getCache());
95         int value = cached.get();
96         assertEquals(compute(OBJECT_KEY), value);
97         assertEquals(1, ncomputations);
98         // The value must still be cached. 
99         value = cached.get(); 
100         assertEquals(compute(OBJECT_KEY), value);
101         assertEquals(1, ncomputations);
102         
103         // Cache expiry. 
104         TimingUtils.sleep(6000); 
105         value = cached.get(); 
106         assertEquals(compute(OBJECT_KEY), value);
107         assertEquals(2, ncomputations);
108         
109         // Should still be cached now.
110         value = cached.get(); 
111         assertEquals(compute(OBJECT_KEY), value);
112         assertEquals(2, ncomputations);
113         
114         // explicit invalidation.
115         cached.invalidate(); 
116         value = cached.get(); 
117         assertEquals(compute(OBJECT_KEY), value);
118         assertEquals(3, ncomputations);
119         
120     }
121     
122     public void testBehaviorEhCacheDefault() throws CacheException, IOException {
123         Cache<Integer,Integer> cache = new EhCache<Integer,Integer>(new TestResource(CachedObjectTest.class, EHCACHE_CONFIG), "undefined");
124         CachedObject<Integer, Integer> cached = createCached(cache);
125         
126         assertTrue( cache == cached.getCache());
127         int value = cached.get();
128         assertEquals(compute(OBJECT_KEY), value);
129         assertEquals(1, ncomputations);
130         // The value must still be cached. 
131         value = cached.get(); 
132         assertEquals(compute(OBJECT_KEY), value);
133         assertEquals(1, ncomputations);
134         
135         // Cache expiry. 
136         TimingUtils.sleep(6000); 
137         value = cached.get(); 
138         assertEquals(compute(OBJECT_KEY), value);
139         assertEquals(2, ncomputations);
140         
141         // Should still be cached now.
142         value = cached.get(); 
143         assertEquals(compute(OBJECT_KEY), value);
144         assertEquals(2, ncomputations);
145         
146     }
147     
148     
149     public void testBehaviorForeverCache() { 
150         CachedObject<Integer, Integer> cached = createCached(new ForeverCache<Integer,Integer>());
151         int value = cached.get();
152         assertEquals(compute(OBJECT_KEY), value);
153         assertEquals(1, ncomputations); 
154         for (int ncomp = 2; ncomp <= 100; ncomp++) { 
155             value = cached.get(); 
156             assertEquals(compute(OBJECT_KEY), value);
157             assertEquals(1, ncomputations);
158         }
159     }
160     
161     public void testBehaviorZeroCache() { 
162         CachedObject<Integer, Integer> cached = createCached(new ZeroCache<Integer,Integer>());
163         int value = cached.get();
164         assertEquals(compute(OBJECT_KEY), value);
165         assertEquals(1, ncomputations); 
166         for (int ncomp = 2; ncomp <= 100; ncomp++) { 
167             value = cached.get(); 
168             assertEquals(compute(OBJECT_KEY), value);
169             assertEquals(ncomp, ncomputations);
170         }
171         cached.invalidate(); 
172         value = cached.get(); 
173         assertEquals(compute(OBJECT_KEY), value);
174         assertEquals(101, ncomputations);
175     }
176 }