(no commit message)
[utils] / support / src / test / java / 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 public class CachedObjectTest extends TestCase {
31     
32     /**
33      * 
34      */
35     private static final String EHCACHE_CONFIG = "ehcache.xml";
36
37     private static final int OBJECT_KEY = 10; 
38     
39     private CachedObject.Computation<Integer,Integer> _computation;
40     private int _ncomputations; 
41     
42     /* (non-Javadoc)
43      * @see junit.framework.TestCase#setUp()
44      */
45     @Override
46     protected void setUp() throws Exception {
47         super.setUp();
48         _computation = new CachedObject.Computation<Integer,Integer>() { 
49             public Integer getObject(Integer aObjectKey) {
50                 _ncomputations++;
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(Cache<Integer,Integer> aCache) { 
62         return new CachedObject<Integer, Integer>(aCache, OBJECT_KEY, _computation);
63     }
64
65     /**
66      * Verifies that upon first use, the cached object uses the computation to 
67      * retrieve the object.  
68      *
69      */
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); 
75     }
76     
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);
82         cached.invalidate(); 
83         value = cached.get();
84         assertEquals(compute(OBJECT_KEY), value);
85         assertEquals(2, _ncomputations);
86     }
87     
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);
91         
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. 
97         value = cached.get(); 
98         assertEquals(compute(OBJECT_KEY), value);
99         assertEquals(1, _ncomputations);
100         
101         // Cache expiry. 
102         TimingUtils.sleep(6000); 
103         value = cached.get(); 
104         assertEquals(compute(OBJECT_KEY), value);
105         assertEquals(2, _ncomputations);
106         
107         // Should still be cached now.
108         value = cached.get(); 
109         assertEquals(compute(OBJECT_KEY), value);
110         assertEquals(2, _ncomputations);
111         
112         // explicit invalidation.
113         cached.invalidate(); 
114         value = cached.get(); 
115         assertEquals(compute(OBJECT_KEY), value);
116         assertEquals(3, _ncomputations);
117         
118     }
119     
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);
123         
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);
132         
133         // Cache expiry. 
134         TimingUtils.sleep(6000); 
135         value = cached.get(); 
136         assertEquals(compute(OBJECT_KEY), value);
137         assertEquals(2, _ncomputations);
138         
139         // Should still be cached now.
140         value = cached.get(); 
141         assertEquals(compute(OBJECT_KEY), value);
142         assertEquals(2, _ncomputations);
143         
144     }
145     
146     
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);
156         }
157     }
158     
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);
168         }
169         cached.invalidate(); 
170         value = cached.get(); 
171         assertEquals(compute(OBJECT_KEY), value);
172         assertEquals(101, _ncomputations);
173     }
174 }