Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 169   Methods: 10
NCLOC: 120   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PrepareTxTest.java - 94.9% 90% 94.2%
coverage coverage
 1    package org.jboss.cache.transaction;
 2   
 3    import junit.framework.Test;
 4    import junit.framework.TestCase;
 5    import junit.framework.TestSuite;
 6    import org.jboss.cache.CacheException;
 7    import org.jboss.cache.CacheImpl;
 8    import org.jboss.cache.DefaultCacheFactory;
 9    import org.jboss.cache.TransactionTable;
 10   
 11    import javax.transaction.NotSupportedException;
 12    import javax.transaction.Synchronization;
 13    import javax.transaction.Transaction;
 14   
 15    /**
 16    * Created by IntelliJ IDEA.
 17    * User: bela
 18    * Date: Jun 9, 2004
 19    * Time: 9:05:19 AM
 20    */
 21    public class PrepareTxTest extends TestCase
 22    {
 23    CacheImpl cache;
 24   
 25  2 protected void setUp() throws Exception
 26    {
 27  2 super.setUp();
 28  2 cache = (CacheImpl) DefaultCacheFactory.getInstance().createCache(false);
 29  2 cache.getConfiguration().setCacheMode("local");
 30  2 cache.getConfiguration().setTransactionManagerLookupClass("org.jboss.cache.DummyTransactionManagerLookup");
 31  2 cache.create();
 32  2 cache.start();
 33    }
 34   
 35  2 protected void tearDown() throws Exception
 36    {
 37  2 super.tearDown();
 38  2 cache.stop();
 39  2 cache.destroy();
 40    }
 41   
 42   
 43    /**
 44    * Tests cache modification <em>inside</em> the afterCompletion() callback. Reproduces a bug fixed in
 45    * connection with JBossCache being used as Hibernate's second level cache
 46    *
 47    * @throws Exception
 48    * @throws NotSupportedException
 49    */
 50  1 public void testCacheModificationInBeforeCompletionPhase() throws Exception, NotSupportedException
 51    {
 52  1 int numLocks = 0;
 53  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 54  1 mgr.begin();
 55  1 Transaction tx = mgr.getTransaction();
 56   
 57    // this will cause the cache to register with TransactionManager for TX completion callbacks
 58  1 cache.put("/one/two/three", "key1", "val1");
 59  1 System.out.println("before commit:\n" + cache.printLockInfo());
 60  1 numLocks = cache.getNumberOfLocksHeld();
 61  1 assertEquals(4, numLocks);
 62   
 63    // we register *second*
 64  1 tx.registerSynchronization(new Synchronization()
 65    {
 66   
 67  1 public void beforeCompletion()
 68    {
 69  1 try
 70    {
 71  1 cache.put("/a/b/c", null);
 72  1 System.out.println("before commit:\n" + cache.printLockInfo());
 73    }
 74    catch (CacheException e)
 75    {
 76  0 e.printStackTrace();
 77    }
 78    }
 79   
 80  1 public void afterCompletion(int status)
 81    {
 82    }
 83    });
 84   
 85  1 tx.commit();
 86  1 System.out.println("after commit:\n" + cache.printLockInfo());
 87  1 numLocks = cache.getNumberOfLocksHeld();
 88  1 assertEquals(0, numLocks);
 89   
 90  1 int num_local_txs, num_global_txs;
 91  1 TransactionTable tx_table = cache.getTransactionTable();
 92  1 num_local_txs = tx_table.getNumLocalTransactions();
 93  1 num_global_txs = tx_table.getNumGlobalTransactions();
 94  1 System.out.println("Number of Transactions: " + num_local_txs + "\nNumber of GlobalTransactions: " +
 95    num_global_txs + "\nTransactionTable:\n " + tx_table.toString(true));
 96  1 assertEquals(num_local_txs, num_global_txs);
 97  1 assertEquals(0, num_local_txs);
 98    }
 99   
 100   
 101    /**
 102    * Tests cache modification <em>inside</em> the afterCompletion() callback. Reproduces a bug fixed in
 103    * connection with JBossCache being used as Hibernate's second level cache
 104    *
 105    * @throws Exception
 106    * @throws NotSupportedException
 107    */
 108  1 public void testCacheModificationInAfterCompletionPhase() throws Exception, NotSupportedException
 109    {
 110  1 int numLocks = 0;
 111  1 DummyTransactionManager mgr = DummyTransactionManager.getInstance();
 112  1 mgr.begin();
 113  1 Transaction tx = mgr.getTransaction();
 114   
 115    // this will cause the cache to register with TransactionManager for TX completion callbacks
 116  1 cache.put("/one/two/three", "key1", "val1");
 117  1 System.out.println("before commit:\n" + cache.printLockInfo());
 118  1 numLocks = cache.getNumberOfLocksHeld();
 119  1 assertEquals(4, numLocks);
 120   
 121    // we register *second*
 122  1 tx.registerSynchronization(new Synchronization()
 123    {
 124   
 125  1 public void beforeCompletion()
 126    {
 127    }
 128   
 129  1 public void afterCompletion(int status)
 130    {
 131  1 try
 132    {
 133  1 cache.put("/a/b/c", null);
 134  1 System.out.println("before commit:\n" + cache.printLockInfo());
 135    }
 136    catch (CacheException e)
 137    {
 138  0 e.printStackTrace();
 139    }
 140    }
 141    });
 142   
 143  1 tx.commit();
 144  1 System.out.println("after commit:\n" + cache.printLockInfo());
 145  1 numLocks = cache.getNumberOfLocksHeld();
 146  1 assertEquals(0, numLocks);
 147   
 148  1 int num_local_txs, num_global_txs;
 149  1 TransactionTable tx_table = cache.getTransactionTable();
 150  1 num_local_txs = tx_table.getNumLocalTransactions();
 151  1 num_global_txs = tx_table.getNumGlobalTransactions();
 152  1 System.out.println("Number of Transactions: " + num_local_txs + "\nNumber of GlobalTransactions: " +
 153    num_global_txs + "\nTransactionTable:\n " + tx_table.toString(true));
 154  1 assertEquals(num_local_txs, num_global_txs);
 155  1 assertEquals(0, num_local_txs);
 156    }
 157   
 158   
 159  1 public static Test suite()
 160    {
 161  1 return new TestSuite(PrepareTxTest.class);
 162    }
 163   
 164  0 public static void main(String[] args)
 165    {
 166  0 junit.textui.TestRunner.run(suite());
 167    }
 168   
 169    }