Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 266   Methods: 16
NCLOC: 207   Classes: 3
 
 Source file Conditionals Statements Methods TOTAL
SyncCacheListenerTest.java 70% 97.1% 100% 95.4%
coverage coverage
 1    /*
 2    *
 3    * JBoss, the OpenSource J2EE webOS
 4    *
 5    * Distributable under LGPL license.
 6    * See terms of license at gnu.org.
 7    */
 8    package org.jboss.cache.replicated;
 9   
 10    import junit.framework.Test;
 11    import junit.framework.TestCase;
 12    import junit.framework.TestSuite;
 13    import org.apache.commons.logging.Log;
 14    import org.apache.commons.logging.LogFactory;
 15    import org.jboss.cache.CacheException;
 16    import org.jboss.cache.CacheImpl;
 17    import org.jboss.cache.DefaultCacheFactory;
 18    import org.jboss.cache.config.Configuration;
 19    import org.jboss.cache.lock.IsolationLevel;
 20    import org.jboss.cache.notifications.annotation.CacheListener;
 21    import org.jboss.cache.notifications.annotation.NodeModified;
 22    import org.jboss.cache.notifications.annotation.NodeRemoved;
 23    import org.jboss.cache.notifications.event.NodeEvent;
 24    import org.jboss.cache.transaction.DummyTransactionManager;
 25   
 26    import javax.naming.Context;
 27    import javax.transaction.NotSupportedException;
 28    import javax.transaction.SystemException;
 29    import javax.transaction.Transaction;
 30    import java.util.HashMap;
 31    import java.util.Map;
 32    import java.util.Set;
 33   
 34    /**
 35    * Test out the TreeCacheListener
 36    *
 37    * @version $Revision: 1.18 $
 38    */
 39    public class SyncCacheListenerTest extends TestCase
 40    {
 41    private CacheImpl cache1, cache2;
 42    private final static Log log_ = LogFactory.getLog(SyncCacheListenerTest.class);
 43    private String old_factory = null;
 44    private final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
 45   
 46  5 public SyncCacheListenerTest(String name)
 47    {
 48  5 super(name);
 49    }
 50   
 51  5 protected void setUp() throws Exception
 52    {
 53  5 System.out.println("*** starting setUp()");
 54  5 super.setUp();
 55  5 old_factory = System.getProperty(Context.INITIAL_CONTEXT_FACTORY);
 56  5 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
 57   
 58  5 initCaches();
 59  5 System.out.println("*** finished setUp()");
 60    }
 61   
 62  5 protected void tearDown() throws Exception
 63    {
 64  5 System.out.println("*** starting tearDown()");
 65  5 super.tearDown();
 66  5 DummyTransactionManager.destroy();
 67  5 destroyCaches();
 68  5 if (old_factory != null)
 69    {
 70  4 System.setProperty(Context.INITIAL_CONTEXT_FACTORY, old_factory);
 71  4 old_factory = null;
 72    }
 73  5 System.out.println("*** finished tearDown()");
 74    }
 75   
 76  2 private Transaction beginTransaction() throws SystemException, NotSupportedException
 77    {
 78  2 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 79  2 mgr.begin();
 80  2 return mgr.getTransaction();
 81    }
 82   
 83  5 private void initCaches() throws Exception
 84    {
 85  5 cache1 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 86  5 cache2 = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 87  5 cache1.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
 88  5 cache2.getConfiguration().setCacheMode(Configuration.CacheMode.REPL_SYNC);
 89  5 cache1.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
 90  5 cache2.getConfiguration().setIsolationLevel(IsolationLevel.SERIALIZABLE);
 91   
 92  5 cache1.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
 93  5 cache2.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.transaction.DummyTransactionManagerLookup");
 94    /*
 95    cache1.setTransactionManagerLookupClass("org.jboss.cache.transaction.GenericTransactionManagerLookup");
 96    cache2.setTransactionManagerLookupClass("org.jboss.cache.transaction.GenericTransactionManagerLookup");
 97    */
 98  5 cache1.getConfiguration().setLockAcquisitionTimeout(5000);
 99  5 cache2.getConfiguration().setLockAcquisitionTimeout(5000);
 100  5 cache1.start();
 101  5 cache2.start();
 102    }
 103   
 104  5 private void destroyCaches()
 105    {
 106  5 if (cache1 != null)
 107    {
 108  5 cache1.stop();
 109    }
 110  5 if (cache2 != null)
 111    {
 112  5 cache2.stop();
 113    }
 114    }
 115   
 116  1 public void testSyncTxRepl() throws Exception
 117    {
 118  1 Integer age;
 119  1 Transaction tx;
 120   
 121  1 tx = beginTransaction();
 122  1 Listener lis = new Listener();
 123  1 cache1.getNotifier().addCacheListener(lis);
 124  1 lis.put("/a/b/c", "age", 38);
 125  1 cache1.getTransactionManager().suspend();
 126  1 assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age"));
 127  1 cache1.getTransactionManager().resume(tx);
 128  1 tx.commit();
 129   
 130    // value on cache2 must be 38
 131  1 age = (Integer) cache2.get("/a/b/c", "age");
 132  1 assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
 133  1 assertTrue("\"age\" must be 38", age == 38);
 134    }
 135   
 136  1 public void testRemoteCacheListener() throws Exception
 137    {
 138  1 Integer age;
 139  1 RemoteListener lis = new RemoteListener();
 140  1 cache2.getNotifier().addCacheListener(lis);
 141  1 cache1.put("/a/b/c", "age", 38);
 142   
 143    // value on cache2 must be 38
 144  1 age = (Integer) cache2.get("/a/b/c", "age");
 145  1 assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
 146  1 assertTrue("\"age\" must be 38", age == 38);
 147  1 cache1.remove("/a/b/c", "age");
 148    }
 149   
 150   
 151  1 public void testSyncRepl() throws Exception
 152    {
 153  1 Integer age;
 154  1 Listener lis = new Listener();
 155  1 cache1.getNotifier().addCacheListener(lis);
 156  1 lis.put("/a/b/c", "age", 38);
 157   
 158    // value on cache2 must be 38
 159  1 age = (Integer) cache2.get("/a/b/c", "age");
 160  1 assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
 161  1 assertTrue("\"age\" must be 38", age == 38);
 162    }
 163   
 164  1 public void testSyncTxReplMap() throws Exception
 165    {
 166  1 Integer age;
 167  1 Transaction tx;
 168   
 169  1 tx = beginTransaction();
 170  1 Listener lis = new Listener();
 171  1 cache1.getNotifier().addCacheListener(lis);
 172  1 Map map = new HashMap();
 173  1 map.put("age", 38);
 174  1 map.put("name", "Ben");
 175  1 lis.put("/a/b/c", map);
 176  1 cache1.getTransactionManager().suspend();
 177  1 assertNull("age on cache2 must be null as the TX has not yet been committed", cache2.get("/a/b/c", "age"));
 178  1 cache1.getTransactionManager().resume(tx);
 179  1 tx.commit();
 180   
 181    // value on cache2 must be 38
 182  1 age = (Integer) cache2.get("/a/b/c", "age");
 183  1 assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
 184  1 assertTrue("\"age\" must be 38", age == 38);
 185    }
 186   
 187   
 188  1 public void testSyncReplMap() throws Exception
 189    {
 190  1 Integer age;
 191   
 192  1 Listener lis = new Listener();
 193  1 cache1.getNotifier().addCacheListener(lis);
 194  1 Map map = new HashMap();
 195  1 map.put("age", 38);
 196  1 map.put("name", "Ben");
 197  1 lis.put("/a/b/c", map);
 198   
 199    // value on cache2 must be 38
 200  1 age = (Integer) cache2.get("/a/b/c", "age");
 201  1 assertNotNull("\"age\" obtained from cache2 must be non-null ", age);
 202  1 assertTrue("\"age\" must be 38", age == 38);
 203    }
 204   
 205   
 206    @CacheListener
 207    public class Listener
 208    {
 209    Object key_ = null;
 210   
 211  2 public void put(String fqn, Object key, Object val)
 212    {
 213  2 key_ = key;
 214  2 cache1.put(fqn, key, val);
 215    }
 216   
 217  2 public void put(String fqn, Map map)
 218    {
 219  0 if (map.size() == 0) fail("put(): map size can't be 0");
 220  2 Set set = map.keySet();
 221  2 key_ = set.iterator().next();// take anyone
 222  2 cache1.put(fqn, map);
 223    }
 224   
 225  8 @NodeModified
 226    public void nodeModified(NodeEvent ne)
 227    {
 228  8 if (!ne.isPre())
 229    {
 230  4 log_.debug("nodeModified visited with fqn: " + ne.getFqn());
 231  4 try
 232    {
 233    // test out if we can get the read lock since there is a write lock going as well.
 234  4 cache1.get(ne.getFqn(), key_);
 235    }
 236    catch (CacheException e)
 237    {
 238  0 e.printStackTrace();//To change body of catch statement use File | Settings | File Templates.
 239  0 fail("nodeModified: test failed with exception: " + e);
 240    }
 241    }
 242    }
 243   
 244    }
 245   
 246    @CacheListener
 247    public class RemoteListener
 248    {
 249   
 250  4 @NodeRemoved
 251    @NodeModified
 252    public void callback(NodeEvent e)
 253    {
 254  4 System.out.println("Callback got event " + e);
 255  4 log_.debug("Callback got event " + e);
 256  4 assertFalse("node was removed on remote cache so isLocal should be false", e.isOriginLocal());
 257    }
 258    }
 259   
 260  1 public static Test suite()
 261    {
 262  1 return new TestSuite(SyncCacheListenerTest.class);
 263    }
 264   
 265   
 266    }