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