1 package org.jboss.cache.mgmt; 2 3 import junit.framework.TestCase; 4 import org.jboss.cache.CacheImpl; 5 import org.jboss.cache.Fqn; 6 import org.jboss.cache.config.CacheLoaderConfig; 7 import org.jboss.cache.config.Configuration; 8 import org.jboss.cache.factories.XmlConfigurationParser; 9 import org.jboss.cache.interceptors.CacheMgmtInterceptor; 10 import org.jboss.cache.jmx.CacheJmxWrapper; 11 import org.jboss.cache.jmx.JmxUtil; 12 import org.jboss.cache.loader.CacheLoader; 13 import org.jboss.cache.xml.XmlHelper; 14 import org.w3c.dom.Element ; 15 16 import javax.management.MBeanServer ; 17 import javax.management.MBeanServerFactory ; 18 import javax.management.Notification ; 19 import javax.management.NotificationListener ; 20 import javax.management.ObjectName ; 21 import java.util.HashMap ; 22 23 29 public class NotificationTest extends TestCase 30 { 31 private static final String MGMT_SERVICE = ",cache-interceptor=CacheMgmtInterceptor"; 32 private static final String CLUSTER_NAME = "NotificationTestCluster"; 33 34 private static final String CAPITAL = "capital"; 35 private static final String CURRENCY = "currency"; 36 private static final String POPULATION = "population"; 37 private static final String EUROPE_NODE = "Europe"; 38 39 private boolean m_cacheStarted; 40 private boolean m_cacheStopped; 41 private boolean m_nodeCreatedPre; 42 private boolean m_nodeCreatedPost; 43 private boolean m_nodeEvictedPre; 44 private boolean m_nodeEvictedPost; 45 private boolean m_nodeLoadedPre; 46 private boolean m_nodeLoadedPost; 47 private boolean m_nodeRemovedPre; 48 private boolean m_nodeRemovedPost; 49 private boolean m_nodeVisitedPre; 50 private boolean m_nodeVisitedPost; 51 private boolean m_viewChange; 52 private boolean m_nodeActivatedPre; 53 private boolean m_nodeActivatedPost; 54 private boolean m_nodeModifiedPre; 55 private boolean m_nodeModifiedPost; 56 private boolean m_nodePassivatedPre; 57 private boolean m_nodePassivatedPost; 58 59 private MBeanServer m_server; 60 61 CacheImpl cache = null; 62 boolean optimistic = false; 63 64 protected void setUp() throws Exception 65 { 66 super.setUp(); 67 m_server = MBeanServerFactory.createMBeanServer(); 68 cache = createCache(CLUSTER_NAME); 69 ObjectName mgmt = new ObjectName (JmxUtil.PREFIX + cache.getConfiguration().getClusterName()); 71 CacheJmxWrapper cacheMBean = new CacheJmxWrapper(cache); 72 73 m_server.registerMBean(cacheMBean, mgmt); 74 } 75 76 protected void tearDown() throws Exception 77 { 78 super.tearDown(); 79 if (cache != null) 80 { 81 cache.destroy(); 84 cache = null; 85 MBeanServerFactory.releaseMBeanServer(m_server); 87 } 88 } 89 90 public void testNotifications() throws Exception 91 { 92 assertNotNull("MBeanServer is null.", m_server); 93 assertNotNull("Cache is null.", cache); 94 95 MyListener listener = new MyListener(); 96 97 ObjectName mgmt = new ObjectName (JmxUtil.PREFIX + cache.getConfiguration().getClusterName() + MGMT_SERVICE); 98 m_server.addNotificationListener(mgmt, listener, null, null); 99 100 cache.start(); 103 104 HashMap albania = new HashMap (4); 106 albania.put(CAPITAL, "Tirana"); 107 albania.put(CURRENCY, "Lek"); 108 cache.put("Europe/Albania", albania); 109 110 cache.put("Europe/Albania", POPULATION, 3563112); 112 113 Fqn key = Fqn.fromString("Europe/Albania"); 115 assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY)); 116 117 cache.evict(key); 119 120 assertNotNull("Retrieval error: expected to retrieve " + CURRENCY + " for " + key, cache.get(key, CURRENCY)); 122 123 cache.remove(key); 125 126 CacheLoader cl = cache.getCacheLoader(); 128 cl.remove(Fqn.fromString(EUROPE_NODE)); 129 130 cache.stop(); 132 m_server.removeNotificationListener(mgmt, listener); 133 134 assertTrue("Expected CacheStarted notification", m_cacheStarted); 136 assertTrue("Expected CacheStopped notification", m_cacheStopped); 137 assertTrue("Expected NodeCreated notification", m_nodeCreatedPre); 138 assertTrue("Expected NodeCreated notification", m_nodeCreatedPost); 139 assertTrue("Expected NodeEvicted notification", m_nodeEvictedPre); 140 assertTrue("Expected NodeEvicted notification", m_nodeEvictedPost); 141 assertTrue("Expected NodeLoaded notification", m_nodeLoadedPre); 142 assertTrue("Expected NodeLoaded notification", m_nodeLoadedPost); 143 assertTrue("Expected NodeRemoved notification", m_nodeRemovedPre); 144 assertTrue("Expected NodeVisited notification", m_nodeVisitedPre); 145 assertTrue("Expected NodeVisited notification", m_nodeVisitedPost); 146 assertTrue("Expected NodeActivated notification", m_nodeActivatedPre); 147 assertTrue("Expected NodeActivated notification", m_nodeActivatedPost); 148 assertTrue("Expected NodeEvicted notification", m_nodeEvictedPre); 149 assertTrue("Expected NodeEvicted notification", m_nodeEvictedPost); 150 assertTrue("Expected NodeModified notification", m_nodeModifiedPre); 151 assertTrue("Expected NodeModified notification", m_nodeModifiedPost); 152 assertTrue("Expected NodePassivated notification", m_nodePassivatedPre); 153 assertTrue("Expected NodePassivated notification", m_nodePassivatedPost); 154 assertTrue("Expected NodeRemoved notification", m_nodeRemovedPre); 155 assertTrue("Expected NodeRemoved notification", m_nodeRemovedPost); 156 assertTrue("Expected ViewChange notification", m_viewChange); 157 } 158 159 private CacheImpl createCache(String clusterName) throws Exception 160 { 161 CacheImpl cache = new CacheImpl(); 162 cache.setConfiguration(new XmlConfigurationParser().parseFile("META-INF/replSync-service.xml")); 163 cache.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC); 164 cache.getConfiguration().setCacheLoaderConfig(getCacheLoaderConfig("location=" + getTempDir())); 165 cache.getConfiguration().setExposeManagementStatistics(true); 166 cache.getConfiguration().setClusterName(clusterName); 167 if (optimistic) 168 { 169 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup"); 170 cache.getConfiguration().setNodeLockingScheme("OPTIMISTIC"); 171 } 172 cache.create(); 173 return cache; 176 } 177 178 private String getTempDir() 179 { 180 return System.getProperty("java.io.tempdir", "/tmp"); 181 } 182 183 private boolean getPre(Object data) 184 { 185 assertNotNull("User data is null, should be Object[]", data); 186 assertTrue("User data is " + data.getClass().getName() + ", should be Object[]", data instanceof Object []); 187 188 Object [] parms = (Object []) data; 189 assertTrue("Parameter is " + parms[1].getClass().getName() + ", should be Boolean", parms[1] instanceof Boolean ); 190 return (Boolean ) parms[1]; 191 } 192 193 private CacheLoaderConfig getCacheLoaderConfig(String properties) throws Exception 194 { 195 String xml = "<config>\n" + 196 "<passivation>true</passivation>\n" + 197 "<preload></preload>\n" + 198 "<shared>true</shared>\n" + 199 "<cacheloader>\n" + 200 "<class>org.jboss.cache.loader.FileCacheLoader</class>\n" + 201 "<properties>" + properties + "</properties>\n" + 202 "<async>false</async>\n" + 203 "<fetchPersistentState>false</fetchPersistentState>\n" + 204 "<ignoreModifications>false</ignoreModifications>\n" + 205 "</cacheloader>\n" + 206 "</config>"; 207 Element element = XmlHelper.stringToElement(xml); 208 return XmlConfigurationParser.parseCacheLoaderConfig(element); 209 } 210 211 private class MyListener implements NotificationListener 212 { 213 public void handleNotification(Notification notification, Object handback) 214 { 215 String type = notification.getType(); 216 Object userData = notification.getUserData(); 217 218 if (type.equals(CacheMgmtInterceptor.NOTIF_CACHE_STARTED)) 219 { 220 m_cacheStarted = true; 221 } 222 else if (type.equals(CacheMgmtInterceptor.NOTIF_CACHE_STOPPED)) 223 { 224 m_cacheStopped = true; 225 } 226 else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_CREATED)) 227 { 228 if (getPre(userData)) 229 { 230 m_nodeCreatedPre = true; 231 } 232 else 233 { 234 m_nodeCreatedPost = true; 235 } 236 } 237 else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_EVICTED)) 238 { 239 if (getPre(userData)) 240 { 241 m_nodeEvictedPre = true; 242 } 243 else 244 { 245 m_nodeEvictedPost = true; 246 } 247 } 248 else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_LOADED)) 249 { 250 if (getPre(userData)) 251 { 252 m_nodeLoadedPre = true; 253 } 254 else 255 { 256 m_nodeLoadedPost = true; 257 } 258 } 259 else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_REMOVED)) 260 { 261 if (getPre(userData)) 262 { 263 m_nodeRemovedPre = true; 264 } 265 else 266 { 267 m_nodeRemovedPost = true; 268 } 269 } 270 else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_VISITED)) 271 { 272 if (getPre(userData)) 273 { 274 m_nodeVisitedPre = true; 275 } 276 else 277 { 278 m_nodeVisitedPost = true; 279 } 280 } 281 else if (type.equals(CacheMgmtInterceptor.NOTIF_VIEW_CHANGED)) 282 { 283 m_viewChange = true; 284 } 285 else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_ACTIVATED)) 286 { 287 if (getPre(userData)) 288 { 289 m_nodeActivatedPre = true; 290 } 291 else 292 { 293 m_nodeActivatedPost = true; 294 } 295 } 296 else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_MODIFIED)) 297 { 298 if (getPre(userData)) 299 { 300 m_nodeModifiedPre = true; 301 } 302 else 303 { 304 m_nodeModifiedPost = true; 305 } 306 } 307 else if (type.equals(CacheMgmtInterceptor.NOTIF_NODE_PASSIVATED)) 308 { 309 if (getPre(userData)) 310 { 311 m_nodePassivatedPre = true; 312 } 313 else 314 { 315 m_nodePassivatedPost = true; 316 } 317 } 318 } 319 } 320 321 } 322 | Popular Tags |