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