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.interceptors.CacheMgmtInterceptor; 10 11 import java.util.HashMap ; 12 import java.util.List ; 13 14 20 public class MgmtCoreTest extends TestCase 21 { 22 private static final String CAPITAL = "capital"; 23 private static final String CURRENCY = "currency"; 24 private static final String POPULATION = "population"; 25 private static final String AREA = "area"; 26 27 CacheImpl cache = null; 28 29 protected void setUp() throws Exception 30 { 31 super.setUp(); 32 cache = new CacheImpl(); 33 cache.getConfiguration().setCacheMode(Configuration.CacheMode.LOCAL); 34 cache.getConfiguration().setExposeManagementStatistics(true); 35 cache.create(); 36 cache.start(); 37 } 38 39 protected void tearDown() throws Exception 40 { 41 super.tearDown(); 42 if (cache != null) 43 { 44 cache.stop(); 45 cache.destroy(); 46 cache = null; 47 } 48 } 49 50 public void testCacheMgmt() throws Exception 51 { 52 assertNotNull("Cache is null.", cache); 53 54 loadCacheData(); 56 57 CacheMgmtInterceptor mgmt = getCacheMgmtInterceptor(); 61 assertNotNull("CacheMgmtInterceptor not found.", mgmt); 62 63 Fqn key = Fqn.fromString("Europe/Austria"); 65 assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL)); 66 assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY)); 67 assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION)); 68 key = Fqn.fromString("Europe/England"); 69 assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL)); 70 assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY)); 71 assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION)); 72 key = Fqn.fromString("Europe/France"); 73 assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL)); 74 assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY)); 75 assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION)); 76 key = Fqn.fromString("Europe/Germany"); 77 assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL)); 78 assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY)); 79 assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION)); 80 key = Fqn.fromString("Europe/Italy"); 81 assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL)); 82 assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY)); 83 assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION)); 84 key = Fqn.fromString("Europe/Switzerland"); 85 assertNotNull("Retrieval error: expected to retrieve " + CAPITAL + " for " + key, cache.get(key, CAPITAL)); 86 assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY)); 87 assertNotNull("Retrieval error: expected to retrieve " + POPULATION + " for " + key, cache.get(key, POPULATION)); 88 89 key = Fqn.fromString("Europe/Austria"); 91 assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA)); 92 key = Fqn.fromString("Europe/England"); 93 assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA)); 94 key = Fqn.fromString("Europe/France"); 95 assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA)); 96 key = Fqn.fromString("Europe/Germany"); 97 assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA)); 98 key = Fqn.fromString("Europe/Italy"); 99 assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA)); 100 key = Fqn.fromString("Europe/Switzerland"); 101 assertNull("Retrieval error: did not expect to retrieve " + AREA + " for " + key, cache.get(key, AREA)); 102 103 assertEquals("NumberOfNodes count error: ", new Integer (13), new Integer (mgmt.getNumberOfNodes())); 105 assertEquals("NumberOfAttributes count error: ", new Integer (40), new Integer (mgmt.getNumberOfAttributes())); 106 assertEquals("Stores count error: ", new Long (40), new Long (mgmt.getStores())); 107 assertEquals("Evictions count error: ", new Long (0), new Long (mgmt.getEvictions())); 108 assertEquals("Hits count error: ", new Long (18), new Long (mgmt.getHits())); 109 assertEquals("Misses count error: ", new Long (6), new Long (mgmt.getMisses())); 110 assertEquals("HitMissRatio error: ", 0.75, mgmt.getHitMissRatio()); 111 assertEquals("ReadWriteRatio error: ", 0.60, mgmt.getReadWriteRatio()); 112 113 cache.evict(Fqn.fromString("Europe/Czech Republic")); 115 cache.evict(Fqn.fromString("Europe/Poland")); 116 assertEquals("NumberOfNodes count error after evictions: ", new Integer (11), new Integer (mgmt.getNumberOfNodes())); 117 assertEquals("NumberOfAttributes count error after evictions: ", new Integer (34), new Integer (mgmt.getNumberOfAttributes())); 118 assertEquals("Stores count error: ", new Long (40), new Long (mgmt.getStores())); 119 assertEquals("Evictions count error: ", new Long (2), new Long (mgmt.getEvictions())); 120 121 Thread.sleep(1000); 123 long t1 = mgmt.getElapsedTime(); 124 if (t1 < 1) 125 fail("ElapsedTime should be greater than 0 seconds."); 126 t1 = mgmt.getTimeSinceReset(); 127 if (t1 < 1) 128 fail("TimeSinceReset should be greater than 0 seconds."); 129 Thread.sleep(1000); 130 131 mgmt.resetStatistics(); 133 134 t1 = mgmt.getElapsedTime(); 136 if (t1 < 2) 137 fail("ElapsedTime after reset should be greater than 1 second."); 138 t1 = mgmt.getTimeSinceReset(); 139 if (t1 > 1) fail("TimeSinceReset after reset should be less than 2 seconds."); 141 142 assertEquals("NumberOfNodes count error after reset: ", new Integer (11), new Integer (mgmt.getNumberOfNodes())); 144 assertEquals("NumberOfAttributes count error after reset: ", new Integer (34), new Integer (mgmt.getNumberOfAttributes())); 145 assertEquals("Stores count error after reset: ", new Long (0), new Long (mgmt.getStores())); 146 assertEquals("Evictions count error after reset: ", new Long (0), new Long (mgmt.getEvictions())); 147 assertEquals("Hits count error after reset: ", new Long (0), new Long (mgmt.getHits())); 148 assertEquals("Misses count error after reset: ", new Long (0), new Long (mgmt.getMisses())); 149 } 150 151 private void loadCacheData() throws Exception 152 { 153 cache.put("Europe", new HashMap ()); 154 cache.put("Europe/Austria", new HashMap ()); 155 cache.put("Europe/Czech Republic", new HashMap ()); 156 cache.put("Europe/England", new HashMap ()); 157 cache.put("Europe/France", new HashMap ()); 158 cache.put("Europe/Germany", new HashMap ()); 159 cache.put("Europe/Italy", new HashMap ()); 160 cache.put("Europe/Poland", new HashMap ()); 161 cache.put("Europe/Switzerland", new HashMap ()); 162 163 cache.put("Europe/Austria", CAPITAL, "Vienna"); 164 cache.put("Europe/Czech Republic", CAPITAL, "Prague"); 165 cache.put("Europe/England", CAPITAL, "London"); 166 cache.put("Europe/France", CAPITAL, "Paris"); 167 cache.put("Europe/Germany", CAPITAL, "Berlin"); 168 cache.put("Europe/Italy", CAPITAL, "Rome"); 169 cache.put("Europe/Poland", CAPITAL, "Warsaw"); 170 cache.put("Europe/Switzerland", CAPITAL, "Bern"); 171 172 cache.put("Europe/Austria", CURRENCY, "Euro"); 173 cache.put("Europe/Czech Republic", CURRENCY, "Czech Koruna"); 174 cache.put("Europe/England", CURRENCY, "British Pound"); 175 cache.put("Europe/France", CURRENCY, "Euro"); 176 cache.put("Europe/Germany", CURRENCY, "Euro"); 177 cache.put("Europe/Italy", CURRENCY, "Euro"); 178 cache.put("Europe/Poland", CURRENCY, "Zloty"); 179 cache.put("Europe/Switzerland", CURRENCY, "Swiss Franc"); 180 181 cache.put("Europe/Austria", POPULATION, 8184691); 182 cache.put("Europe/Czech Republic", POPULATION, 10241138); 183 cache.put("Europe/England", POPULATION, 60441457); 184 cache.put("Europe/France", POPULATION, 60656178); 185 cache.put("Europe/Germany", POPULATION, 82431390); 186 cache.put("Europe/Italy", POPULATION, 58103033); 187 cache.put("Europe/Poland", POPULATION, 38635144); 188 cache.put("Europe/Switzerland", POPULATION, 7489370); 189 190 HashMap albania = new HashMap (4); 191 albania.put(CAPITAL, "Tirana"); 192 albania.put(CURRENCY, "Lek"); 193 albania.put(POPULATION, 3563112); 194 albania.put(AREA, 28748); 195 cache.put("Europe/Albania", albania); 196 197 HashMap hungary = new HashMap (4); 198 hungary.put(CAPITAL, "Budapest"); 199 hungary.put(CURRENCY, "Forint"); 200 hungary.put(POPULATION, 10006835); 201 hungary.put(AREA, 93030); 202 cache.put("Europe/Hungary", hungary); 203 204 HashMap romania = new HashMap (4); 205 romania.put(CAPITAL, "Bucharest"); 206 romania.put(CURRENCY, "Leu"); 207 romania.put(POPULATION, 22329977); 208 romania.put(AREA, 237500); 209 cache.put("Europe/Romania", romania); 210 211 HashMap slovakia = new HashMap (4); 212 slovakia.put(CAPITAL, "Bratislava"); 213 slovakia.put(CURRENCY, "Slovak Koruna"); 214 slovakia.put(POPULATION, 5431363); 215 slovakia.put(AREA, 48845); 216 cache.put("Europe/Slovakia", slovakia); 217 218 } 219 220 private CacheMgmtInterceptor getCacheMgmtInterceptor() 221 { 222 List interceptors = cache.getInterceptors(); 223 if (interceptors.isEmpty()) 224 return null; 225 226 for (int i = 0; i < interceptors.size(); i++) 227 { 228 Object o = interceptors.get(i); 229 if (o instanceof CacheMgmtInterceptor) 230 return (CacheMgmtInterceptor) o; 231 } 232 return null; 233 } 234 235 public static Test suite() 236 { 237 return new TestSuite(MgmtCoreTest.class); 238 } 239 240 } 241 | Popular Tags |