Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 107   Methods: 7
NCLOC: 76   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TransactionTest.java 75% 100% 100% 97.9%
coverage coverage
 1    package org.jboss.cache.invocationcontext;
 2   
 3    import junit.framework.TestCase;
 4    import org.jboss.cache.CacheSPI;
 5    import org.jboss.cache.DefaultCacheFactory;
 6    import org.jboss.cache.Fqn;
 7   
 8    import javax.transaction.TransactionManager;
 9    import java.util.Map;
 10   
 11    /**
 12    * A test to ensure the transactional context is properly set up in the IC
 13    *
 14    * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
 15    */
 16    public class TransactionTest extends TestCase
 17    {
 18    private CacheSPI cache;
 19    private TransactionManager tm;
 20   
 21  4 protected void setUp()
 22    {
 23  4 cache = (CacheSPI) DefaultCacheFactory.getInstance().createCache("META-INF/local-tx-service.xml");
 24  4 tm = cache.getTransactionManager();
 25    }
 26   
 27  4 protected void tearDown()
 28    {
 29  4 if (cache != null)
 30    {
 31  4 cache.stop();
 32  4 cache = null;
 33    }
 34    }
 35   
 36  1 public void testTxExistenceAfterWrite() throws Exception
 37    {
 38    // make sure we have a running transaction.
 39  1 tm.begin();
 40   
 41  1 assertNull("Tx should not have been set up yet", cache.getInvocationContext().getTransaction());
 42  1 assertNull("Gtx should not have been set up yet", cache.getInvocationContext().getGlobalTransaction());
 43   
 44    // now make a WRITE call into the cache (should start a tx)
 45  1 cache.getRoot().put("k", "v");
 46  1 Map data = cache.getRoot().getData();
 47  1 assertEquals("Data map should not empty", 1, data.size());
 48   
 49    // but now we should have a local tx registered in the invocation context
 50  1 assertNotNull("Tx should have been set up by now", cache.getInvocationContext().getTransaction());
 51  1 assertEquals("The same current transaction should be associated with this invocation ctx.", tm.getTransaction(), cache.getInvocationContext().getTransaction());
 52  1 assertNotNull("Gtx should have been set up by now", cache.getInvocationContext().getGlobalTransaction());
 53   
 54  1 tm.commit();
 55    }
 56   
 57  1 public void testTxExistenceAfterRead() throws Exception
 58    {
 59    // make sure we have a running transaction.
 60  1 tm.begin();
 61   
 62  1 assertNull("Tx should not have been set up yet", cache.getInvocationContext().getTransaction());
 63  1 assertNull("Gtx should not have been set up yet", cache.getInvocationContext().getGlobalTransaction());
 64   
 65    // now make a WRITE call into the cache (should start a tx)
 66  1 Object value = cache.get(Fqn.ROOT, "k");
 67  1 assertNull("Value should be null", value);
 68   
 69    // but now we should have a local tx registered in the invocation context
 70  1 assertNotNull("Tx should have been set up by now", cache.getInvocationContext().getTransaction());
 71  1 assertEquals("The same current transaction should be associated with this invocation ctx.", tm.getTransaction(), cache.getInvocationContext().getTransaction());
 72  1 assertNotNull("Gtx should have been set up by now", cache.getInvocationContext().getGlobalTransaction());
 73   
 74  1 tm.commit();
 75    }
 76   
 77  1 public void testScrubbingAfterCommit() throws Exception
 78    {
 79  1 doScrubbingTest(true);
 80    }
 81   
 82  1 public void testScrubbingAfterRollback() throws Exception
 83    {
 84  1 doScrubbingTest(false);
 85    }
 86   
 87  2 private void doScrubbingTest(boolean commit) throws Exception
 88    {
 89  2 tm.begin();
 90  2 cache.getRoot().put("key", "value");
 91  2 assertNotNull("Tx should have been set up by now", cache.getInvocationContext().getTransaction());
 92  2 assertEquals("The same current transaction should be associated with this invocation ctx.", tm.getTransaction(), cache.getInvocationContext().getTransaction());
 93  2 assertNotNull("Gtx should have been set up by now", cache.getInvocationContext().getGlobalTransaction());
 94   
 95  2 if (commit)
 96    {
 97  1 tm.commit();
 98    }
 99    else
 100    {
 101  1 tm.rollback();
 102    }
 103   
 104  2 assertNull("Tx should have been scrubbed", cache.getInvocationContext().getTransaction());
 105  2 assertNull("Gtx should have been scrubbed", cache.getInvocationContext().getGlobalTransaction());
 106    }
 107    }