Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 184   Methods: 8
NCLOC: 137   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
InvalidationTest.java 60% 97.5% 100% 93.9%
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.Configuration;
 10    import org.jboss.cache.config.Configuration.CacheMode;
 11    import org.jboss.cache.factories.UnitTestCacheFactory;
 12    import org.jboss.cache.interceptors.InvalidationInterceptor;
 13   
 14    import java.util.HashMap;
 15    import java.util.List;
 16   
 17    /**
 18    * Simple functional tests for InvalidationInterceptor statistics
 19    *
 20    * @author Jerry Gauthier
 21    * @version $Id: InvalidationTest.java,v 1.12 2007/01/11 13:49:05 msurtani Exp $
 22    */
 23    public class InvalidationTest extends TestCase
 24    {
 25    private static final String CLUSTER_NAME = "InvalidationTestCluster";
 26    private static final String CAPITAL = "capital";
 27    private static final String CURRENCY = "currency";
 28    private static final String POPULATION = "population";
 29    private static final String AREA = "area";
 30   
 31    CacheImpl cache1 = null;
 32    CacheImpl cache2 = null;
 33   
 34  1 protected void setUp() throws Exception
 35    {
 36  1 super.setUp();
 37  1 cache1 = createCache(CLUSTER_NAME);
 38  1 cache2 = createCache(CLUSTER_NAME);
 39    }
 40   
 41  1 protected void tearDown() throws Exception
 42    {
 43  1 super.tearDown();
 44  1 if (cache1 != null)
 45    {
 46  1 cache1.stop();
 47  1 cache1.destroy();
 48  1 cache1 = null;
 49    }
 50  1 if (cache2 != null)
 51    {
 52  1 cache2.stop();
 53  1 cache2.destroy();
 54  1 cache2 = null;
 55    }
 56    }
 57   
 58  1 public void testInvalidationMgmt() throws Exception
 59    {
 60  1 assertNotNull("Cache1 is null.", cache1);
 61  1 assertNotNull("Cache2 is null.", cache2);
 62   
 63    // populate cache1 with test data
 64  1 loadCache1(cache1);
 65   
 66    // confirm that data is in cache1 and not in cache2
 67  1 Fqn key = Fqn.fromString("Europe/Austria");
 68  1 assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
 69  1 assertNull("Cache2 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
 70  1 key = Fqn.fromString("Europe/Albania");
 71  1 assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
 72  1 assertNull("Cache2 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
 73   
 74    // populate cache2 with test data - this will invalidate Austria
 75  1 loadCache2(cache2);
 76   
 77    // confirm that Austria is now in cache2 and not in cache1
 78  1 key = Fqn.fromString("Europe/Austria");
 79  1 assertNull("Cache1 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
 80  1 assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL));
 81   
 82    // confirm that Albania is still in cache1
 83  1 key = Fqn.fromString("Europe/Albania");
 84  1 assertNotNull("Cache1 retrieval error after unrelated eviction: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL));
 85   
 86    // Note: because these tests are normally executed without a server, the interceptor
 87    // MBeans are usually not available for use in the tests. Consequently it's necessary
 88    // to obtain a reference to the interceptor and work with it directly.
 89  1 InvalidationInterceptor mgmt1 = getInvalidationInterceptor(cache1);
 90  1 assertNotNull("Cache1 InvalidationInterceptor not found.", mgmt1);
 91   
 92  1 InvalidationInterceptor mgmt2 = getInvalidationInterceptor(cache2);
 93  1 assertNotNull("Cache2 InvalidationInterceptor not found.", mgmt2);
 94   
 95  1 assertTrue("Cache1 not configured to use MBeans", cache1.getConfiguration().getExposeManagementStatistics());
 96  1 assertTrue("Cache2 not configured to use MBeans", cache2.getConfiguration().getExposeManagementStatistics());
 97  1 assertTrue("InvalidationInterceptor on Cache1 not set up to use statistics!", mgmt1.getStatisticsEnabled());
 98  1 assertTrue("InvalidationInterceptor on Cache2 not set up to use statistics!", mgmt2.getStatisticsEnabled());
 99   
 100    // verify basic statistics for entries loaded into cache
 101  1 assertEquals("Cache1 Invalidations count error: ", new Long(6), new Long(mgmt1.getInvalidations()));
 102  1 assertEquals("Cache2 Invalidations count error: ", new Long(10), new Long(mgmt2.getInvalidations()));
 103   
 104    // reset statistics
 105  1 mgmt1.resetStatistics();
 106  1 mgmt2.resetStatistics();
 107   
 108    // check the statistics again
 109  1 assertEquals("Cache1 Invalidations count error after reset: ", new Long(0), new Long(mgmt1.getInvalidations()));
 110  1 assertEquals("Cache2 Invalidations count error after reset: ", new Long(0), new Long(mgmt2.getInvalidations()));
 111   
 112    }
 113   
 114  1 private void loadCache1(CacheImpl cache) throws Exception
 115    {
 116  1 cache.put("Europe", new HashMap());
 117  1 cache.put("Europe/Austria", new HashMap());
 118  1 cache.put("Europe/Austria", CAPITAL, "Vienna");
 119  1 cache.put("Europe/Austria", CURRENCY, "Euro");
 120  1 cache.put("Europe/Austria", POPULATION, 8184691);
 121   
 122  1 HashMap albania = new HashMap(4);
 123  1 albania.put(CAPITAL, "Tirana");
 124  1 albania.put(CURRENCY, "Lek");
 125  1 albania.put(POPULATION, 3563112);
 126  1 albania.put(AREA, 28748);
 127  1 cache.put("Europe/Albania", albania);
 128   
 129    }
 130   
 131  1 private void loadCache2(CacheImpl cache) throws Exception
 132    {
 133  1 cache.put("Europe", new HashMap());
 134  1 cache.put("Europe/Austria", new HashMap());
 135  1 cache.put("Europe/Austria", CAPITAL, "Vienna");
 136  1 cache.put("Europe/Austria", CURRENCY, "Euro");
 137  1 cache.put("Europe/Austria", POPULATION, 8184691);
 138   
 139  1 cache.put("Europe/Romania", new HashMap());
 140  1 cache.put("Europe/Romania", CAPITAL, "Bucharest");
 141  1 cache.put("Europe/Romania", CURRENCY, "Leu");
 142  1 cache.put("Europe/Romania", POPULATION, 22329977);
 143  1 cache.put("Europe/Romania", AREA, 237500);
 144   
 145    }
 146   
 147  2 private CacheImpl createCache(String clusterName) throws Exception
 148    {
 149  2 CacheImpl cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 150  2 Configuration c = UnitTestCacheFactory.createConfiguration(CacheMode.INVALIDATION_SYNC);
 151  2 cache.setConfiguration(c);
 152  2 c.setCacheMode(Configuration.CacheMode.INVALIDATION_SYNC);
 153  2 c.setExposeManagementStatistics(true);
 154  2 c.setClusterName(clusterName);
 155  2 cache.create();
 156  2 cache.start();
 157  2 return cache;
 158    }
 159   
 160  2 private InvalidationInterceptor getInvalidationInterceptor(CacheImpl cache)
 161    {
 162  2 List interceptors = cache.getInterceptors();
 163  2 if (interceptors.isEmpty())
 164    {
 165  0 return null;
 166    }
 167   
 168  10 for (int i = 0; i < interceptors.size(); i++)
 169    {
 170  10 Object o = interceptors.get(i);
 171  10 if (o instanceof InvalidationInterceptor)
 172    {
 173  2 return (InvalidationInterceptor) o;
 174    }
 175    }
 176  0 return null;
 177    }
 178   
 179  1 public static Test suite()
 180    {
 181  1 return new TestSuite(InvalidationTest.class);
 182    }
 183   
 184    }