Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 265   Methods: 15
NCLOC: 181   Classes: 4
 
 Source file Conditionals Statements Methods TOTAL
CallbackTest.java 83.3% 94.8% 100% 94.2%
coverage coverage
 1    package org.jboss.cache;
 2   
 3    import junit.framework.Test;
 4    import junit.framework.TestCase;
 5    import junit.framework.TestSuite;
 6    import org.jboss.cache.config.Configuration;
 7    import org.jboss.cache.lock.IsolationLevel;
 8    import org.jboss.cache.transaction.DummyTransactionManager;
 9   
 10    import javax.transaction.NotSupportedException;
 11    import javax.transaction.Transaction;
 12   
 13    /**
 14    * Tests whether modifications within callbacks (TreeCacheListener) are handled correctly
 15    *
 16    * @author Bela Ban
 17    * @version $Id: CallbackTest.java,v 1.15 2007/01/11 13:49:06 msurtani Exp $
 18    */
 19    public class CallbackTest extends TestCase
 20    {
 21    CacheImpl cache = null, cache2;
 22    Transaction tx = null;
 23    final Fqn FQN_A = Fqn.fromString("/a");
 24    final Fqn FQN_B = Fqn.fromString("/b");
 25    final String KEY = "key";
 26    final String VALUE = "value";
 27   
 28   
 29  5 protected void setUp() throws Exception
 30    {
 31  5 super.setUp();
 32    }
 33   
 34  5 protected void tearDown() throws Exception
 35    {
 36  5 super.tearDown();
 37  5 if (cache != null)
 38    {
 39  5 cache.stop();
 40  5 cache.destroy();
 41  5 cache = null;
 42    }
 43  5 if (tx != null)
 44    {
 45  2 tx.commit();
 46  2 tx = null;
 47    }
 48    }
 49   
 50   
 51  1 public void testLocalPutCallbackWithoutTransaction() throws Exception, NotSupportedException
 52    {
 53  1 cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
 54  1 cache.getNotifier().addCacheListener(new PutListener(cache));
 55   
 56  1 cache.put(FQN_A, null);
 57  1 assertTrue(cache.exists(FQN_A));
 58  1 assertTrue(cache.exists(FQN_B));//created by callback
 59  1 assertEquals(cache.getLockTable().size(), 0);
 60  1 System.out.println("cache locks:\n" + cache.printLockInfo());
 61  1 assertEquals(0, cache.getNumberOfLocksHeld());
 62    }
 63   
 64  1 public void testLocalGetCallbackSameFqnWithoutTransaction() throws Exception, NotSupportedException
 65    {
 66  1 cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
 67  1 cache.getNotifier().addCacheListener(new GetListener(cache, FQN_A));
 68   
 69  1 cache.put(FQN_A, null);
 70  1 assertTrue(cache.exists(FQN_A));
 71  1 assertEquals(cache.getLockTable().size(), 0);
 72  1 System.out.println("cache locks:\n" + cache.printLockInfo());
 73  1 assertEquals(0, cache.getNumberOfLocksHeld());
 74    }
 75   
 76  1 public void testLocalGetCallbackDifferentFqnWithoutTransaction() throws Exception, NotSupportedException
 77    {
 78  1 cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
 79  1 cache.put(FQN_B, null);
 80  1 cache.getNotifier().addCacheListener(new GetListener(cache, FQN_B));
 81   
 82  1 cache.put("/a", null);
 83  1 assertTrue(cache.exists(FQN_A));
 84  1 assertTrue(cache.exists(FQN_B));
 85  1 assertEquals(cache.getLockTable().size(), 0);
 86  1 System.out.println("cache locks:\n" + cache.printLockInfo());
 87  1 assertEquals(0, cache.getNumberOfLocksHeld());
 88    }
 89   
 90   
 91  1 public void testLocalCallbackWithTransaction() throws Exception, NotSupportedException
 92    {
 93  1 cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
 94  1 cache.getNotifier().addCacheListener(new PutListener(cache));
 95  1 tx = startTransaction();
 96  1 cache.put(FQN_A, null);
 97  1 tx.commit();
 98  1 assertTrue(cache.exists(FQN_A));
 99  1 assertEquals(0, cache.getNumberOfLocksHeld());
 100    }
 101   
 102   
 103  1 public void testLocalCallbackWithException() throws Exception, NotSupportedException
 104    {
 105  1 cache = createCache(Configuration.CacheMode.LOCAL, IsolationLevel.SERIALIZABLE);
 106  1 cache.getNotifier().addCacheListener(new ExceptionListener());
 107  1 tx = startTransaction();
 108  1 try
 109    {
 110  1 cache.put(FQN_A, null);
 111  0 tx.rollback();
 112    }
 113    catch (RuntimeException ex)
 114    {
 115  1 tx.rollback();
 116    }
 117  1 assertFalse(cache.exists(FQN_A));
 118  1 assertEquals(0, cache.getNumberOfLocksHeld());
 119    // assertFalse(cache.exists("/a")); -- fix: reason is that CreateIfNotExists interceptor is *before*
 120    // PessimisticLockInterceptor, which registers for TX completion, we don't even get that far
 121    }
 122   
 123    /*public void testSyncReplicationWith2Caches() throws Exception, NotSupportedException {
 124    cache=createCache(CacheImpl.REPL_SYNC, IsolationLevel.SERIALIZABLE);
 125    cache2=createCache(CacheImpl.REPL_SYNC, IsolationLevel.SERIALIZABLE);
 126    cache.setSyncCommitPhase(true);
 127    cache2.setSyncCommitPhase(true);
 128   
 129    assertEquals(2, cache.getMembers().size());
 130    System.out.println("view is correct: " + cache.getMembers() + " (2 members)");
 131    cache2.addTreeCacheListener(new PutListener(cache2));
 132   
 133    tx=startTransaction();
 134    cache.put("/a", null);
 135    tx.commit();
 136   
 137    // 1. cache: put("/a")
 138    // 2. tx.commit() in cache causes 2PC to cache2
 139    // 3. cache2 is updated with "/a"
 140    // 4. listener in cache2 is called, creates FQN
 141    // 5. cache2 replicates FQN over to cache as part of its own 2PC protocol
 142   
 143    assertTrue(cache.exists("/a")); // original modification to cache
 144    assertTrue(cache.exists(FQN)); // result of replication from cache
 145    assertTrue(cache2.exists("/a")); // result of replication from cache
 146    assertTrue(cache2.exists(FQN)); // created by listener, triggered by replication of "/a" from cache
 147    TransactionTable tx_table1, tx_table2;
 148    tx_table1=cache.getTransactionTable();
 149    tx_table2=cache2.getTransactionTable();
 150    System.out.println("tx_table1=" + tx_table1 + ", tx_table2=" + tx_table2);
 151    assertEquals(0, tx_table1.getNumLocalTransactions());
 152    assertEquals(0, tx_table2.getNumLocalTransactions());
 153    assertEquals(0, tx_table1.getNumGlobalTransactions());
 154    assertEquals(0, tx_table2.getNumGlobalTransactions());
 155    }*/
 156   
 157    /* public static void main(String[] args) {
 158    try {
 159    new CallbackTest().testSyncReplicationWith2Caches();
 160    }
 161    catch(Exception e) {
 162    e.printStackTrace();
 163    }
 164    }*/
 165   
 166   
 167  5 CacheImpl createCache(Configuration.CacheMode mode, IsolationLevel level) throws Exception
 168    {
 169  5 Configuration c = new Configuration();
 170  5 c.setCacheMode(mode);
 171  5 c.setIsolationLevel(level);
 172  5 c.setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
 173  5 return (CacheImpl) DefaultCacheFactory.getInstance().createCache(c);
 174    }
 175   
 176  2 Transaction startTransaction()
 177    {
 178  2 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 179  2 try
 180    {
 181  2 mgr.begin();
 182  2 return mgr.getTransaction();
 183    }
 184    catch (Throwable t)
 185    {
 186  0 return null;
 187    }
 188    }
 189   
 190   
 191    class ExceptionListener extends AbstractCacheListener
 192    {
 193  1 public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
 194    {
 195  1 if (pre) throw new RuntimeException("this will cause the TX to rollback");
 196    }
 197    }
 198   
 199   
 200    class GetListener extends AbstractCacheListener
 201    {
 202    CacheImpl c;
 203    Fqn my_fqn;
 204   
 205  2 public GetListener(CacheImpl c, Fqn my_fqn)
 206    {
 207  2 this.c = c;
 208  2 this.my_fqn = my_fqn;
 209    }
 210   
 211  4 public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
 212    {
 213  4 if (!pre)
 214    {
 215  2 try
 216    {
 217  2 Node n = c.get(this.my_fqn);
 218  2 assertNotNull(n);
 219    }
 220    catch (CacheException e)
 221    {
 222  0 fail("listener was unable to do a get(" + my_fqn + ") during callback: " + e);
 223    }
 224    }
 225    }
 226   
 227    }
 228   
 229    class PutListener extends AbstractCacheListener
 230    {
 231    CacheImpl c;
 232   
 233  2 public PutListener(CacheImpl c)
 234    {
 235  2 this.c = c;
 236    }
 237   
 238  8 public void nodeCreated(Fqn fqn, boolean pre, boolean isLocal)
 239    {
 240  8 if (!pre)
 241    {
 242  4 try
 243    {
 244  4 if (!c.exists(FQN_B))
 245    {
 246  2 System.out.println("PutListener: creating node " + FQN_B);
 247  2 c.put(FQN_B, KEY, VALUE);
 248  2 System.out.println("PutListener: created node " + FQN_B);
 249    }
 250    }
 251    catch (CacheException e)
 252    {
 253  0 fail("listener was unable to update cache during callback: " + e);
 254    }
 255    }
 256    }
 257   
 258    }
 259   
 260  1 public static Test suite()
 261    {
 262  1 return new TestSuite(CallbackTest.class);
 263    }
 264   
 265    }