1 |
| package org.jboss.cache.mgmt; |
2 |
| |
3 |
| import junit.framework.Test; |
4 |
| import junit.framework.TestCase; |
5 |
| import junit.framework.TestSuite; |
6 |
| import org.jboss.cache.CacheImpl; |
7 |
| import org.jboss.cache.DefaultCacheFactory; |
8 |
| import org.jboss.cache.Fqn; |
9 |
| import org.jboss.cache.config.CacheLoaderConfig; |
10 |
| import org.jboss.cache.config.Configuration; |
11 |
| import org.jboss.cache.factories.XmlConfigurationParser; |
12 |
| import org.jboss.cache.interceptors.ActivationInterceptor; |
13 |
| import org.jboss.cache.interceptors.PassivationInterceptor; |
14 |
| import org.jboss.cache.loader.CacheLoader; |
15 |
| import org.jboss.cache.xml.XmlHelper; |
16 |
| import org.w3c.dom.Element; |
17 |
| |
18 |
| import java.util.HashMap; |
19 |
| import java.util.List; |
20 |
| |
21 |
| |
22 |
| |
23 |
| |
24 |
| |
25 |
| |
26 |
| |
27 |
| public class PassivationTest extends TestCase |
28 |
| { |
29 |
| private static final String CAPITAL = "capital"; |
30 |
| private static final String CURRENCY = "currency"; |
31 |
| private static final String POPULATION = "population"; |
32 |
| private static final String AREA = "area"; |
33 |
| private static final String EUROPE_NODE = "Europe"; |
34 |
| |
35 |
| CacheImpl cache = null; |
36 |
| |
37 |
1
| protected void setUp() throws Exception
|
38 |
| { |
39 |
1
| super.setUp();
|
40 |
1
| cache = createCache();
|
41 |
| } |
42 |
| |
43 |
1
| protected void tearDown() throws Exception
|
44 |
| { |
45 |
1
| super.tearDown();
|
46 |
1
| if (cache != null)
|
47 |
| { |
48 |
1
| CacheLoader cl = cache.getCacheLoader();
|
49 |
1
| cl.remove(Fqn.fromString(EUROPE_NODE));
|
50 |
1
| cache.stop();
|
51 |
1
| cache.destroy();
|
52 |
1
| cache = null;
|
53 |
| } |
54 |
| } |
55 |
| |
56 |
1
| public void testPassivationMgmt() throws Exception
|
57 |
| { |
58 |
1
| assertNotNull("Cache is null.", cache);
|
59 |
| |
60 |
| |
61 |
| |
62 |
1
| CacheLoader cl = cache.getCacheLoader();
|
63 |
1
| assertNotNull("CacheLoader is null.", cl);
|
64 |
1
| cl.remove(Fqn.fromString(EUROPE_NODE));
|
65 |
| |
66 |
| |
67 |
1
| loadCache(cache);
|
68 |
| |
69 |
| |
70 |
| |
71 |
| |
72 |
1
| ActivationInterceptor act = getActivationInterceptor(cache);
|
73 |
1
| assertNotNull("ActivationInterceptor not found.", act);
|
74 |
1
| PassivationInterceptor pass = getPassivationInterceptor(cache);
|
75 |
1
| assertNotNull("PassivationInterceptor not found.", pass);
|
76 |
| |
77 |
1
| System.out.println("count of misses " + act.getCacheLoaderMisses());
|
78 |
1
| int miss = 2;
|
79 |
| |
80 |
| |
81 |
| |
82 |
| |
83 |
1
| assertEquals("CacheLoaderLoads count error: ", new Long(0), new Long(act.getCacheLoaderLoads()));
|
84 |
1
| assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
|
85 |
1
| assertEquals("Activations count error: ", new Long(0), new Long(act.getActivations()));
|
86 |
1
| assertEquals("Passivations count error: ", new Long(0), new Long(pass.getPassivations()));
|
87 |
| |
88 |
| |
89 |
1
| Fqn key = Fqn.fromString("Europe/Austria");
|
90 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
|
91 |
1
| assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
|
92 |
| |
93 |
| |
94 |
1
| assertEquals("CacheLoaderLoads count error: ", new Long(0), new Long(act.getCacheLoaderLoads()));
|
95 |
1
| assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
|
96 |
1
| assertEquals("Activations count error: ", new Long(0), new Long(act.getActivations()));
|
97 |
1
| assertEquals("Passivations count error: ", new Long(0), new Long(pass.getPassivations()));
|
98 |
| |
99 |
| |
100 |
1
| key = Fqn.fromString("Europe/Poland");
|
101 |
1
| assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
|
102 |
| |
103 |
| |
104 |
1
| miss++;
|
105 |
1
| assertEquals("CacheLoaderLoads count error: ", new Long(0), new Long(act.getCacheLoaderLoads()));
|
106 |
1
| assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
|
107 |
1
| assertEquals("Activations count error: ", new Long(0), new Long(act.getActivations()));
|
108 |
1
| assertEquals("Passivations count error: ", new Long(0), new Long(pass.getPassivations()));
|
109 |
| |
110 |
| |
111 |
1
| key = Fqn.fromString("Europe/Austria");
|
112 |
1
| cache.evict(key);
|
113 |
1
| assertFalse("Retrieval error: did not expect to find node " + key + " in cache", cache.exists(key));
|
114 |
| |
115 |
| |
116 |
1
| assertEquals("Passivations count error: ", new Long(1), new Long(pass.getPassivations()));
|
117 |
| |
118 |
| |
119 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
|
120 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
|
121 |
| |
122 |
| |
123 |
1
| assertEquals("CacheLoaderLoads count error: ", new Long(1), new Long(act.getCacheLoaderLoads()));
|
124 |
1
| assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
|
125 |
1
| assertEquals("Activations count error: ", new Long(1), new Long(act.getActivations()));
|
126 |
1
| assertEquals("Passivations count error: ", new Long(1), new Long(pass.getPassivations()));
|
127 |
| |
128 |
| |
129 |
1
| cache.remove(key);
|
130 |
1
| assertFalse("Retrieval error: did not expect to find node " + key + " in cache", cache.exists(key));
|
131 |
1
| assertFalse("Retrieval error: did not expect to find node " + key + " in loader", cl.exists(key));
|
132 |
| |
133 |
| |
134 |
1
| assertEquals("CacheLoaderLoads count error: ", new Long(1), new Long(act.getCacheLoaderLoads()));
|
135 |
1
| assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
|
136 |
1
| assertEquals("Activations count error: ", new Long(1), new Long(act.getActivations()));
|
137 |
1
| assertEquals("Passivations count error: ", new Long(1), new Long(pass.getPassivations()));
|
138 |
| |
139 |
| |
140 |
1
| assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
|
141 |
1
| assertNull("Retrieval error: did not expect to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
|
142 |
| |
143 |
| |
144 |
1
| miss += 2;
|
145 |
1
| assertEquals("CacheLoaderLoads count error: ", new Long(1), new Long(act.getCacheLoaderLoads()));
|
146 |
1
| assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
|
147 |
1
| assertEquals("Activations count error: ", new Long(1), new Long(act.getActivations()));
|
148 |
1
| assertEquals("Passivations count error: ", new Long(1), new Long(pass.getPassivations()));
|
149 |
| |
150 |
| |
151 |
1
| miss++;
|
152 |
1
| cache.put("Europe/Poland", new HashMap());
|
153 |
1
| cache.put("Europe/Poland", CAPITAL, "Warsaw");
|
154 |
1
| cache.put("Europe/Poland", CURRENCY, "Zloty");
|
155 |
1
| assertEquals("CacheLoaderLoads count error: ", new Long(1), new Long(act.getCacheLoaderLoads()));
|
156 |
1
| assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
|
157 |
1
| assertEquals("Activations count error: ", new Long(1), new Long(act.getActivations()));
|
158 |
1
| assertEquals("Passivations count error: ", new Long(1), new Long(pass.getPassivations()));
|
159 |
| |
160 |
| |
161 |
1
| key = Fqn.fromString("Europe/Poland");
|
162 |
1
| cache.evict(key);
|
163 |
1
| assertEquals("CacheLoaderLoads count error: ", new Long(1), new Long(act.getCacheLoaderLoads()));
|
164 |
1
| assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
|
165 |
1
| assertEquals("Activations count error: ", new Long(1), new Long(act.getActivations()));
|
166 |
1
| assertEquals("Passivations count error: ", new Long(2), new Long(pass.getPassivations()));
|
167 |
| |
168 |
| |
169 |
1
| assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
|
170 |
1
| assertEquals("CacheLoaderLoads count error: ", new Long(2), new Long(act.getCacheLoaderLoads()));
|
171 |
1
| assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
|
172 |
1
| assertEquals("Activations count error: ", new Long(2), new Long(act.getActivations()));
|
173 |
1
| assertEquals("Passivations count error: ", new Long(2), new Long(pass.getPassivations()));
|
174 |
| |
175 |
| |
176 |
1
| cache.evict(key);
|
177 |
1
| assertEquals("CacheLoaderLoads count error: ", new Long(2), new Long(act.getCacheLoaderLoads()));
|
178 |
1
| assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
|
179 |
1
| assertEquals("Activations count error: ", new Long(2), new Long(act.getActivations()));
|
180 |
1
| assertEquals("Passivations count error: ", new Long(3), new Long(pass.getPassivations()));
|
181 |
| |
182 |
| |
183 |
1
| assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
|
184 |
1
| assertEquals("CacheLoaderLoads count error: ", new Long(3), new Long(act.getCacheLoaderLoads()));
|
185 |
1
| assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(act.getCacheLoaderMisses()));
|
186 |
1
| assertEquals("Activations count error: ", new Long(3), new Long(act.getActivations()));
|
187 |
1
| assertEquals("Passivations count error: ", new Long(3), new Long(pass.getPassivations()));
|
188 |
| |
189 |
| |
190 |
1
| act.resetStatistics();
|
191 |
1
| pass.resetStatistics();
|
192 |
| |
193 |
| |
194 |
1
| assertEquals("CacheLoaderLoads count error after reset: ", new Long(0), new Long(act.getCacheLoaderLoads()));
|
195 |
1
| assertEquals("CacheLoaderMisses count error after reset: ", new Long(0), new Long(act.getCacheLoaderMisses()));
|
196 |
1
| assertEquals("Activations count error: ", new Long(0), new Long(act.getActivations()));
|
197 |
1
| assertEquals("Passivations count error: ", new Long(0), new Long(pass.getPassivations()));
|
198 |
| } |
199 |
| |
200 |
1
| private void loadCache(CacheImpl cache) throws Exception
|
201 |
| { |
202 |
1
| cache.put(EUROPE_NODE, new HashMap());
|
203 |
1
| cache.put("Europe/Austria", new HashMap());
|
204 |
1
| cache.put("Europe/Austria", CAPITAL, "Vienna");
|
205 |
1
| cache.put("Europe/Austria", CURRENCY, "Euro");
|
206 |
1
| cache.put("Europe/Austria", POPULATION, 8184691);
|
207 |
| |
208 |
1
| cache.put("Europe/England", new HashMap());
|
209 |
1
| cache.put("Europe/England", CAPITAL, "London");
|
210 |
1
| cache.put("Europe/England", CURRENCY, "British Pound");
|
211 |
1
| cache.put("Europe/England", POPULATION, 60441457);
|
212 |
| |
213 |
1
| HashMap albania = new HashMap(4);
|
214 |
1
| albania.put(CAPITAL, "Tirana");
|
215 |
1
| albania.put(CURRENCY, "Lek");
|
216 |
1
| albania.put(POPULATION, 3563112);
|
217 |
1
| albania.put(AREA, 28748);
|
218 |
1
| cache.put("Europe/Albania", albania);
|
219 |
| |
220 |
1
| HashMap hungary = new HashMap(4);
|
221 |
1
| hungary.put(CAPITAL, "Budapest");
|
222 |
1
| hungary.put(CURRENCY, "Forint");
|
223 |
1
| hungary.put(POPULATION, 10006835);
|
224 |
1
| hungary.put(AREA, 93030);
|
225 |
1
| cache.put("Europe/Hungary", hungary);
|
226 |
| } |
227 |
| |
228 |
1
| private CacheImpl createCache() throws Exception
|
229 |
| { |
230 |
1
| CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
|
231 |
1
| Configuration c = new Configuration();
|
232 |
1
| c.setCacheMode("LOCAL");
|
233 |
1
| c.setCacheLoaderConfig(getCacheLoaderConfig("location=" + getTempDir()));
|
234 |
1
| c.setExposeManagementStatistics(true);
|
235 |
1
| cache.setConfiguration(c);
|
236 |
1
| cache.create();
|
237 |
1
| cache.start();
|
238 |
1
| return cache;
|
239 |
| } |
240 |
| |
241 |
1
| private ActivationInterceptor getActivationInterceptor(CacheImpl cache)
|
242 |
| { |
243 |
1
| List interceptors = cache.getInterceptors();
|
244 |
1
| if (interceptors.isEmpty())
|
245 |
| { |
246 |
0
| return null;
|
247 |
| } |
248 |
| |
249 |
7
| for (int i = 0; i < interceptors.size(); i++)
|
250 |
| { |
251 |
7
| Object o = interceptors.get(i);
|
252 |
7
| if (o instanceof ActivationInterceptor)
|
253 |
| { |
254 |
1
| return (ActivationInterceptor) o;
|
255 |
| } |
256 |
| } |
257 |
0
| return null;
|
258 |
| } |
259 |
| |
260 |
1
| private PassivationInterceptor getPassivationInterceptor(CacheImpl cache)
|
261 |
| { |
262 |
1
| List interceptors = cache.getInterceptors();
|
263 |
1
| if (interceptors.isEmpty())
|
264 |
| { |
265 |
0
| return null;
|
266 |
| } |
267 |
| |
268 |
5
| for (int i = 0; i < interceptors.size(); i++)
|
269 |
| { |
270 |
5
| Object o = interceptors.get(i);
|
271 |
5
| if (o instanceof PassivationInterceptor)
|
272 |
| { |
273 |
1
| return (PassivationInterceptor) o;
|
274 |
| } |
275 |
| } |
276 |
0
| return null;
|
277 |
| } |
278 |
| |
279 |
1
| private CacheLoaderConfig getCacheLoaderConfig(String properties) throws Exception
|
280 |
| { |
281 |
1
| String xml = "<config>\n" +
|
282 |
| "<passivation>true</passivation>\n" + |
283 |
| "<preload></preload>\n" + |
284 |
| "<shared>false</shared>\n" + |
285 |
| "<cacheloader>\n" + |
286 |
| "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" + |
287 |
| "<properties>" + properties + "</properties>\n" + |
288 |
| "<async>false</async>\n" + |
289 |
| "<fetchPersistentState>false</fetchPersistentState>\n" + |
290 |
| "<ignoreModifications>false</ignoreModifications>\n" + |
291 |
| "</cacheloader>\n" + |
292 |
| "</config>"; |
293 |
1
| Element element = XmlHelper.stringToElement(xml);
|
294 |
1
| return XmlConfigurationParser.parseCacheLoaderConfig(element);
|
295 |
| } |
296 |
| |
297 |
1
| private String getTempDir()
|
298 |
| { |
299 |
1
| return System.getProperty("java.io.tempdir", "/tmp");
|
300 |
| } |
301 |
| |
302 |
1
| public static Test suite()
|
303 |
| { |
304 |
1
| return new TestSuite(PassivationTest.class);
|
305 |
| } |
306 |
| |
307 |
| } |