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.CacheLoaderInterceptor; 12 import org.jboss.cache.interceptors.CacheStoreInterceptor; 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 CacheLoaderTest 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 testCacheLoaderMgmt() 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 CacheLoaderInterceptor loader = getCacheLoaderInterceptor(cache); 72 assertNotNull("CacheLoaderInterceptor not found.", loader); 73 CacheStoreInterceptor store = getCacheStoreInterceptor(cache); 74 assertNotNull("CacheStoreInterceptor not found.", store); 75 76 int miss = 0; 78 int load = 0; 79 int stores = 11; 80 assertEquals("CacheLoaderLoads count error: ", new Long (0), new Long (loader.getCacheLoaderLoads())); 81 assertEquals("CacheLoaderMisses count error: ", new Long (miss), new Long (loader.getCacheLoaderMisses())); 82 assertEquals("CacheLoaderStores count error: ", new Long (stores), new Long (store.getCacheLoaderStores())); 83 84 Fqn key = Fqn.fromString("Europe/Austria"); 86 assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL)); 87 assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA)); 88 89 load++; 91 assertEquals("CacheLoaderLoads count error: ", new Long (load), new Long (loader.getCacheLoaderLoads())); 92 assertEquals("CacheLoaderMisses count error: ", new Long (miss), new Long (loader.getCacheLoaderMisses())); 93 assertEquals("CacheLoaderStores count error: ", new Long (stores), new Long (store.getCacheLoaderStores())); 94 95 key = Fqn.fromString("Europe/Poland"); 97 assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL)); 98 99 miss++; 101 assertEquals("CacheLoaderLoads count error: ", new Long (1), new Long (loader.getCacheLoaderLoads())); 102 assertEquals("CacheLoaderMisses count error: ", new Long (miss), new Long (loader.getCacheLoaderMisses())); 103 assertEquals("CacheLoaderStores count error: ", new Long (stores), new Long (store.getCacheLoaderStores())); 104 105 key = Fqn.fromString("Europe/Austria"); 107 cache.evict(key); 108 assertFalse("Retrieval error: did not expect to find node " + key + " in cache", cache.exists(key)); 109 110 assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL)); 112 assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY)); 113 114 load++; 116 assertEquals("CacheLoaderLoads count error: ", new Long (load), new Long (loader.getCacheLoaderLoads())); 117 assertEquals("CacheLoaderMisses count error: ", new Long (miss), new Long (loader.getCacheLoaderMisses())); 118 assertEquals("CacheLoaderStores count error: ", new Long (stores), new Long (store.getCacheLoaderStores())); 119 120 cache.remove(key); 122 assertFalse("Retrieval error: did not expect to find node " + key + " in cache", cache.exists(key)); 123 assertFalse("Retrieval error: did not expect to find node " + key + " in loader", cl.exists(key)); 124 125 assertEquals("CacheLoaderLoads count error: ", new Long (load), new Long (loader.getCacheLoaderLoads())); 127 assertEquals("CacheLoaderMisses count error: ", new Long (miss), new Long (loader.getCacheLoaderMisses())); 128 assertEquals("CacheLoaderStores count error: ", new Long (stores), new Long (store.getCacheLoaderStores())); 129 130 assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL)); 132 assertNull("Retrieval error: did not expect to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY)); 133 134 miss += 2; 136 assertEquals("CacheLoaderLoads count error: ", new Long (load), new Long (loader.getCacheLoaderLoads())); 137 assertEquals("CacheLoaderMisses count error: ", new Long (miss), new Long (loader.getCacheLoaderMisses())); 138 assertEquals("CacheLoaderStores count error: ", new Long (stores), new Long (store.getCacheLoaderStores())); 139 140 stores++; 142 cache.put("Europe/Poland", new HashMap ()); 143 assertEquals("CacheLoaderLoads count error: ", new Long (load), new Long (loader.getCacheLoaderLoads())); 144 assertEquals("CacheLoaderMisses count error: ", new Long (miss), new Long (loader.getCacheLoaderMisses())); 145 assertEquals("CacheLoaderStores count error: ", new Long (stores), new Long (store.getCacheLoaderStores())); 146 147 stores += 2; 149 cache.put("Europe/Poland", CAPITAL, "Warsaw"); 150 cache.put("Europe/Poland", CURRENCY, "Zloty"); 151 assertEquals("CacheLoaderLoads count error: ", new Long (load), new Long (loader.getCacheLoaderLoads())); 152 assertEquals("CacheLoaderMisses count error: ", new Long (miss), new Long (loader.getCacheLoaderMisses())); 153 assertEquals("CacheLoaderStores count error: ", new Long (stores), new Long (store.getCacheLoaderStores())); 154 155 load++; 157 key = Fqn.fromString("Europe/Poland"); 158 cache.evict(key); 159 assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA)); 160 assertEquals("CacheLoaderLoads count error: ", new Long (load), new Long (loader.getCacheLoaderLoads())); 161 assertEquals("CacheLoaderMisses count error: ", new Long (miss), new Long (loader.getCacheLoaderMisses())); 162 assertEquals("CacheLoaderStores count error: ", new Long (stores), new Long (store.getCacheLoaderStores())); 163 164 loader.resetStatistics(); 166 store.resetStatistics(); 167 168 assertEquals("CacheLoaderLoads count error after reset: ", new Long (0), new Long (loader.getCacheLoaderLoads())); 170 assertEquals("CacheLoaderMisses count error after reset: ", new Long (0), new Long (loader.getCacheLoaderMisses())); 171 assertEquals("CacheLoaderStores count error after reset: ", new Long (0), new Long (store.getCacheLoaderStores())); 172 } 173 174 private void loadCache(CacheImpl cache) throws Exception 175 { 176 cache.put(EUROPE_NODE, new HashMap ()); 177 cache.put("Europe/Austria", new HashMap ()); 178 cache.put("Europe/Austria", CAPITAL, "Vienna"); 179 cache.put("Europe/Austria", CURRENCY, "Euro"); 180 cache.put("Europe/Austria", POPULATION, 8184691); 181 182 cache.put("Europe/England", new HashMap ()); 183 cache.put("Europe/England", CAPITAL, "London"); 184 cache.put("Europe/England", CURRENCY, "British Pound"); 185 cache.put("Europe/England", POPULATION, 60441457); 186 187 HashMap albania = new HashMap (4); 188 albania.put(CAPITAL, "Tirana"); 189 albania.put(CURRENCY, "Lek"); 190 albania.put(POPULATION, 3563112); 191 albania.put(AREA, 28748); 192 cache.put("Europe/Albania", albania); 193 194 HashMap hungary = new HashMap (4); 195 hungary.put(CAPITAL, "Budapest"); 196 hungary.put(CURRENCY, "Forint"); 197 hungary.put(POPULATION, 10006835); 198 hungary.put(AREA, 93030); 199 cache.put("Europe/Hungary", hungary); 200 201 } 202 203 private CacheImpl createCache() throws Exception 204 { 205 CacheImpl cache = new CacheImpl(); 206 cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL); 207 cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig("location=" + getTempDir())); 208 cache.getConfiguration().setExposeManagementStatistics(true); 209 cache.create(); 210 cache.start(); 211 return cache; 212 } 213 214 private CacheLoaderInterceptor getCacheLoaderInterceptor(CacheImpl cache) 215 { 216 List interceptors = cache.getInterceptors(); 217 if (interceptors.isEmpty()) 218 return null; 219 220 for (int i = 0; i < interceptors.size(); i++) 221 { 222 Object o = interceptors.get(i); 223 if (o instanceof CacheLoaderInterceptor) 224 return (CacheLoaderInterceptor) o; 225 } 226 return null; 227 } 228 229 private CacheStoreInterceptor getCacheStoreInterceptor(CacheImpl cache) 230 { 231 List interceptors = cache.getInterceptors(); 232 if (interceptors.isEmpty()) 233 return null; 234 235 for (int i = 0; i < interceptors.size(); i++) 236 { 237 Object o = interceptors.get(i); 238 if (o instanceof CacheStoreInterceptor) 239 return (CacheStoreInterceptor) o; 240 } 241 return null; 242 } 243 244 private CacheLoaderConfig getCacheLoaderConfig(String properties) throws Exception 245 { 246 String xml = "<config>\n" + 247 "<passivation>false</passivation>\n" + 248 "<preload></preload>\n" + 249 "<shared>false</shared>\n" + 250 "<cacheloader>\n" + 251 "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" + 252 "<properties>" + properties + "</properties>\n" + 253 "<async>false</async>\n" + 254 "<fetchPersistentState>false</fetchPersistentState>\n" + 255 "<ignoreModifications>false</ignoreModifications>\n" + 256 "</cacheloader>\n" + 257 "</config>"; 258 Element element = XmlHelper.stringToElement(xml); 259 return XmlConfigurationParser.parseCacheLoaderConfig(element); 260 } 261 262 private String getTempDir() 263 { 264 return System.getProperty("java.io.tempdir", "/tmp"); 265 } 266 267 public static Test suite() 268 { 269 return new TestSuite(CacheLoaderTest.class); 270 } 271 272 } 273 | Popular Tags |