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