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.Configuration; 9 import org.jboss.cache.factories.XmlConfigurationParser; 10 import org.jboss.cache.interceptors.InvalidationInterceptor; 11 12 import java.util.HashMap ; 13 import java.util.List ; 14 15 21 public class InvalidationTest extends TestCase 22 { 23 private static final String CLUSTER_NAME = "InvalidationTestCluster"; 24 private static final String CAPITAL = "capital"; 25 private static final String CURRENCY = "currency"; 26 private static final String POPULATION = "population"; 27 private static final String AREA = "area"; 28 29 CacheImpl cache1 = null; 30 CacheImpl cache2 = null; 31 32 protected void setUp() throws Exception 33 { 34 super.setUp(); 35 cache1 = createCache(CLUSTER_NAME); 36 cache2 = createCache(CLUSTER_NAME); 37 } 38 39 protected void tearDown() throws Exception 40 { 41 super.tearDown(); 42 if (cache1 != null) 43 { 44 cache1.stop(); 45 cache1.destroy(); 46 cache1 = null; 47 } 48 if (cache2 != null) 49 { 50 cache2.stop(); 51 cache2.destroy(); 52 cache2 = null; 53 } 54 } 55 56 public void testInvalidationMgmt() throws Exception 57 { 58 assertNotNull("Cache1 is null.", cache1); 59 assertNotNull("Cache2 is null.", cache2); 60 61 loadCache1(cache1); 63 64 Fqn key = Fqn.fromString("Europe/Austria"); 66 assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL)); 67 assertNull("Cache2 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL)); 68 key = Fqn.fromString("Europe/Albania"); 69 assertNotNull("Cache1 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL)); 70 assertNull("Cache2 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL)); 71 72 loadCache2(cache2); 74 75 key = Fqn.fromString("Europe/Austria"); 77 assertNull("Cache1 retrieval error: did not expect to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL)); 78 assertNotNull("Cache2 retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache2.get(key, CAPITAL)); 79 80 key = Fqn.fromString("Europe/Albania"); 82 assertNotNull("Cache1 retrieval error after unrelated eviction: expected to retrieve " + CAPITAL + " for " + key, cache1.get(key, CAPITAL)); 83 84 InvalidationInterceptor mgmt1 = getInvalidationInterceptor(cache1); 88 assertNotNull("Cache1 InvalidationInterceptor not found.", mgmt1); 89 90 InvalidationInterceptor mgmt2 = getInvalidationInterceptor(cache2); 91 assertNotNull("Cache2 InvalidationInterceptor not found.", mgmt2); 92 93 assertTrue("Cache1 not configured to use MBeans", cache1.getConfiguration().getExposeManagementStatistics()); 94 assertTrue("Cache2 not configured to use MBeans", cache2.getConfiguration().getExposeManagementStatistics()); 95 assertTrue("InvalidationInterceptor on Cache1 not set up to use statistics!", mgmt1.getStatisticsEnabled()); 96 assertTrue("InvalidationInterceptor on Cache2 not set up to use statistics!", mgmt2.getStatisticsEnabled()); 97 98 assertEquals("Cache1 Invalidations count error: ", new Long (6), new Long (mgmt1.getInvalidations())); 100 assertEquals("Cache2 Invalidations count error: ", new Long (10), new Long (mgmt2.getInvalidations())); 101 102 mgmt1.resetStatistics(); 104 mgmt2.resetStatistics(); 105 106 assertEquals("Cache1 Invalidations count error after reset: ", new Long (0), new Long (mgmt1.getInvalidations())); 108 assertEquals("Cache2 Invalidations count error after reset: ", new Long (0), new Long (mgmt2.getInvalidations())); 109 110 } 111 112 private void loadCache1(CacheImpl cache) throws Exception 113 { 114 cache.put("Europe", new HashMap ()); 115 cache.put("Europe/Austria", new HashMap ()); 116 cache.put("Europe/Austria", CAPITAL, "Vienna"); 117 cache.put("Europe/Austria", CURRENCY, "Euro"); 118 cache.put("Europe/Austria", POPULATION, 8184691); 119 120 HashMap albania = new HashMap (4); 121 albania.put(CAPITAL, "Tirana"); 122 albania.put(CURRENCY, "Lek"); 123 albania.put(POPULATION, 3563112); 124 albania.put(AREA, 28748); 125 cache.put("Europe/Albania", albania); 126 127 } 128 129 private void loadCache2(CacheImpl cache) throws Exception 130 { 131 cache.put("Europe", new HashMap ()); 132 cache.put("Europe/Austria", new HashMap ()); 133 cache.put("Europe/Austria", CAPITAL, "Vienna"); 134 cache.put("Europe/Austria", CURRENCY, "Euro"); 135 cache.put("Europe/Austria", POPULATION, 8184691); 136 137 cache.put("Europe/Romania", new HashMap ()); 138 cache.put("Europe/Romania", CAPITAL, "Bucharest"); 139 cache.put("Europe/Romania", CURRENCY, "Leu"); 140 cache.put("Europe/Romania", POPULATION, 22329977); 141 cache.put("Europe/Romania", AREA, 237500); 142 143 } 144 145 private CacheImpl createCache(String clusterName) throws Exception 146 { 147 CacheImpl cache = new CacheImpl(); 148 XmlConfigurationParser parser = new XmlConfigurationParser(); 149 Configuration c = parser.parseFile("META-INF/invalidationSync-service.xml"); 150 cache.setConfiguration(c); 151 c.setCacheMode(Configuration.CacheMode.INVALIDATION_SYNC); 152 c.setExposeManagementStatistics(true); 153 c.setClusterName(clusterName); 154 cache.create(); 155 cache.start(); 156 return cache; 157 } 158 159 private InvalidationInterceptor getInvalidationInterceptor(CacheImpl cache) 160 { 161 List interceptors = cache.getInterceptors(); 162 if (interceptors.isEmpty()) 163 return null; 164 165 for (int i = 0; i < interceptors.size(); i++) 166 { 167 Object o = interceptors.get(i); 168 if (o instanceof InvalidationInterceptor) 169 return (InvalidationInterceptor) o; 170 } 171 return null; 172 } 173 174 public static Test suite() 175 { 176 return new TestSuite(InvalidationTest.class); 177 } 178 179 } 180 | Popular Tags |