Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 281   Methods: 10
NCLOC: 211   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
CacheLoaderTest.java 64.3% 96.9% 100% 94.2%
coverage coverage
 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.CacheLoaderInterceptor;
 13    import org.jboss.cache.interceptors.CacheStoreInterceptor;
 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    * Simple functional tests for CacheLoaderInterceptor and CacheStoreInterceptor statistics
 23    *
 24    * @author Jerry Gauthier
 25    * @version $Id: CacheLoaderTest.java,v 1.12 2007/01/11 13:49:05 msurtani Exp $
 26    */
 27    public class CacheLoaderTest 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 testCacheLoaderMgmt() throws Exception
 57    {
 58  1 assertNotNull("Cache is null.", cache);
 59   
 60    // note -it's essential to flush any existing test data from the cache loader
 61    // otherwise the loads and misses counts will be incorrect
 62  1 CacheLoader cl = cache.getCacheLoader();
 63  1 assertNotNull("CacheLoader is null.", cl);
 64  1 cl.remove(Fqn.fromString(EUROPE_NODE));
 65   
 66    // populate cache with test data
 67  1 loadCache(cache);
 68   
 69    // Note: because these tests are normally executed without a server, the interceptor
 70    // MBeans are usually not available for use in the tests. Consequently it's necessary
 71    // to obtain references to the interceptors and work with them directly.
 72  1 CacheLoaderInterceptor loader = getCacheLoaderInterceptor(cache);
 73  1 assertNotNull("CacheLoaderInterceptor not found.", loader);
 74  1 CacheStoreInterceptor store = getCacheStoreInterceptor(cache);
 75  1 assertNotNull("CacheStoreInterceptor not found.", store);
 76   
 77    // verify cache loader statistics for entries loaded into cache
 78  1 int miss = 0;
 79  1 int load = 0;
 80  1 int stores = 11;
 81  1 assertEquals("CacheLoaderLoads count error: ", new Long(0), new Long(loader.getCacheLoaderLoads()));
 82  1 assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(loader.getCacheLoaderMisses()));
 83  1 assertEquals("CacheLoaderStores count error: ", new Long(stores), new Long(store.getCacheLoaderStores()));
 84   
 85    // now try retrieving a valid attribute and an invalid attribute
 86  1 Fqn key = Fqn.fromString("Europe/Austria");
 87  1 assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
 88  1 assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
 89   
 90    // verify statistics after retrieving entries - misses should still be same since nodes were already loaded
 91  1 load++;
 92  1 assertEquals("CacheLoaderLoads count error: ", new Long(load), new Long(loader.getCacheLoaderLoads()));
 93  1 assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(loader.getCacheLoaderMisses()));
 94  1 assertEquals("CacheLoaderStores count error: ", new Long(stores), new Long(store.getCacheLoaderStores()));
 95   
 96    // now try retrieving an attribute for an invalid node
 97  1 key = Fqn.fromString("Europe/Poland");
 98  1 assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
 99   
 100    // verify statistics for after retrieving entries - misses should now be +1 after attempt to load Poland
 101  1 miss++;
 102  1 assertEquals("CacheLoaderLoads count error: ", new Long(1), new Long(loader.getCacheLoaderLoads()));
 103  1 assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(loader.getCacheLoaderMisses()));
 104  1 assertEquals("CacheLoaderStores count error: ", new Long(stores), new Long(store.getCacheLoaderStores()));
 105   
 106    // now evict Austria and confirm that it's no longer in cache
 107  1 key = Fqn.fromString("Europe/Austria");
 108  1 cache.evict(key);
 109  1 assertFalse("Retrieval error: did not expect to find node " + key + " in cache", cache.exists(key));
 110   
 111    // now try retrieving its attributes again - first retrieval should trigger a cache load
 112  1 assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
 113  1 assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
 114   
 115    // verify statistics after retrieving evicted entry - loads should now be +1
 116  1 load++;
 117  1 assertEquals("CacheLoaderLoads count error: ", new Long(load), new Long(loader.getCacheLoaderLoads()));
 118  1 assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(loader.getCacheLoaderMisses()));
 119  1 assertEquals("CacheLoaderStores count error: ", new Long(stores), new Long(store.getCacheLoaderStores()));
 120   
 121    // now remove Austria and confirm that it's not in cache or loader
 122  1 cache.remove(key);
 123  1 assertFalse("Retrieval error: did not expect to find node " + key + " in cache", cache.exists(key));
 124  1 assertFalse("Retrieval error: did not expect to find node " + key + " in loader", cl.exists(key));
 125   
 126    // verify statistics after removing entry - should be unchanged
 127  1 assertEquals("CacheLoaderLoads count error: ", new Long(load), new Long(loader.getCacheLoaderLoads()));
 128  1 assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(loader.getCacheLoaderMisses()));
 129  1 assertEquals("CacheLoaderStores count error: ", new Long(stores), new Long(store.getCacheLoaderStores()));
 130   
 131    // now try retrieving attributes again - each attempt should fail and cause a miss since node is now removed
 132  1 assertNull("Retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL));
 133  1 assertNull("Retrieval error: did not expect to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY));
 134   
 135    // verify statistics after trying to retrieve removed node's attributes - should be two more misses
 136  1 miss += 2;
 137  1 assertEquals("CacheLoaderLoads count error: ", new Long(load), new Long(loader.getCacheLoaderLoads()));
 138  1 assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(loader.getCacheLoaderMisses()));
 139  1 assertEquals("CacheLoaderStores count error: ", new Long(stores), new Long(store.getCacheLoaderStores()));
 140   
 141    // add a new node - this should cause a store
 142  1 stores++;
 143  1 cache.put("Europe/Poland", new HashMap());
 144  1 assertEquals("CacheLoaderLoads count error: ", new Long(load), new Long(loader.getCacheLoaderLoads()));
 145  1 assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(loader.getCacheLoaderMisses()));
 146  1 assertEquals("CacheLoaderStores count error: ", new Long(stores), new Long(store.getCacheLoaderStores()));
 147   
 148    // add two attributes - this should cause two stores
 149  1 stores += 2;
 150  1 cache.put("Europe/Poland", CAPITAL, "Warsaw");
 151  1 cache.put("Europe/Poland", CURRENCY, "Zloty");
 152  1 assertEquals("CacheLoaderLoads count error: ", new Long(load), new Long(loader.getCacheLoaderLoads()));
 153  1 assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(loader.getCacheLoaderMisses()));
 154  1 assertEquals("CacheLoaderStores count error: ", new Long(stores), new Long(store.getCacheLoaderStores()));
 155   
 156    // evict Poland and then try to retrieve an invalid attribute - this will cause a load as the node is restored
 157  1 load++;
 158  1 key = Fqn.fromString("Europe/Poland");
 159  1 cache.evict(key);
 160  1 assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA));
 161  1 assertEquals("CacheLoaderLoads count error: ", new Long(load), new Long(loader.getCacheLoaderLoads()));
 162  1 assertEquals("CacheLoaderMisses count error: ", new Long(miss), new Long(loader.getCacheLoaderMisses()));
 163  1 assertEquals("CacheLoaderStores count error: ", new Long(stores), new Long(store.getCacheLoaderStores()));
 164   
 165    // reset statistics
 166  1 loader.resetStatistics();
 167  1 store.resetStatistics();
 168   
 169    // check the statistics again
 170  1 assertEquals("CacheLoaderLoads count error after reset: ", new Long(0), new Long(loader.getCacheLoaderLoads()));
 171  1 assertEquals("CacheLoaderMisses count error after reset: ", new Long(0), new Long(loader.getCacheLoaderMisses()));
 172  1 assertEquals("CacheLoaderStores count error after reset: ", new Long(0), new Long(store.getCacheLoaderStores()));
 173    }
 174   
 175  1 private void loadCache(CacheImpl cache) throws Exception
 176    {
 177  1 cache.put(EUROPE_NODE, new HashMap());
 178  1 cache.put("Europe/Austria", new HashMap());
 179  1 cache.put("Europe/Austria", CAPITAL, "Vienna");
 180  1 cache.put("Europe/Austria", CURRENCY, "Euro");
 181  1 cache.put("Europe/Austria", POPULATION, 8184691);
 182   
 183  1 cache.put("Europe/England", new HashMap());
 184  1 cache.put("Europe/England", CAPITAL, "London");
 185  1 cache.put("Europe/England", CURRENCY, "British Pound");
 186  1 cache.put("Europe/England", POPULATION, 60441457);
 187   
 188  1 HashMap albania = new HashMap(4);
 189  1 albania.put(CAPITAL, "Tirana");
 190  1 albania.put(CURRENCY, "Lek");
 191  1 albania.put(POPULATION, 3563112);
 192  1 albania.put(AREA, 28748);
 193  1 cache.put("Europe/Albania", albania);
 194   
 195  1 HashMap hungary = new HashMap(4);
 196  1 hungary.put(CAPITAL, "Budapest");
 197  1 hungary.put(CURRENCY, "Forint");
 198  1 hungary.put(POPULATION, 10006835);
 199  1 hungary.put(AREA, 93030);
 200  1 cache.put("Europe/Hungary", hungary);
 201   
 202    }
 203   
 204  1 private CacheImpl createCache() throws Exception
 205    {
 206  1 CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 207  1 cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL);
 208  1 cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig("location=" + getTempDir()));
 209  1 cache.getConfiguration().setExposeManagementStatistics(true);
 210  1 cache.create();
 211  1 cache.start();
 212  1 return cache;
 213    }
 214   
 215  1 private CacheLoaderInterceptor getCacheLoaderInterceptor(CacheImpl cache)
 216    {
 217  1 List interceptors = cache.getInterceptors();
 218  1 if (interceptors.isEmpty())
 219    {
 220  0 return null;
 221    }
 222   
 223  7 for (int i = 0; i < interceptors.size(); i++)
 224    {
 225  7 Object o = interceptors.get(i);
 226  7 if (o instanceof CacheLoaderInterceptor)
 227    {
 228  1 return (CacheLoaderInterceptor) o;
 229    }
 230    }
 231  0 return null;
 232    }
 233   
 234  1 private CacheStoreInterceptor getCacheStoreInterceptor(CacheImpl cache)
 235    {
 236  1 List interceptors = cache.getInterceptors();
 237  1 if (interceptors.isEmpty())
 238    {
 239  0 return null;
 240    }
 241   
 242  5 for (int i = 0; i < interceptors.size(); i++)
 243    {
 244  5 Object o = interceptors.get(i);
 245  5 if (o instanceof CacheStoreInterceptor)
 246    {
 247  1 return (CacheStoreInterceptor) o;
 248    }
 249    }
 250  0 return null;
 251    }
 252   
 253  1 private CacheLoaderConfig getCacheLoaderConfig(String properties) throws Exception
 254    {
 255  1 String xml = "<config>\n" +
 256    "<passivation>false</passivation>\n" +
 257    "<preload></preload>\n" +
 258    "<shared>false</shared>\n" +
 259    "<cacheloader>\n" +
 260    "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" +
 261    "<properties>" + properties + "</properties>\n" +
 262    "<async>false</async>\n" +
 263    "<fetchPersistentState>false</fetchPersistentState>\n" +
 264    "<ignoreModifications>false</ignoreModifications>\n" +
 265    "</cacheloader>\n" +
 266    "</config>";
 267  1 Element element = XmlHelper.stringToElement(xml);
 268  1 return XmlConfigurationParser.parseCacheLoaderConfig(element);
 269    }
 270   
 271  1 private String getTempDir()
 272    {
 273  1 return System.getProperty("java.io.tempdir", "/tmp");
 274    }
 275   
 276  1 public static Test suite()
 277    {
 278  1 return new TestSuite(CacheLoaderTest.class);
 279    }
 280   
 281    }