Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 131   Methods: 9
NCLOC: 96   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
TxUndoTest.java 75% 95.7% 88.9% 93.2%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source
 3    *
 4    * Distributable under LGPL license.
 5    * See terms of license at gnu.org.
 6    */
 7   
 8    package org.jboss.cache.pojo;
 9   
 10    import junit.framework.TestCase;
 11    import junit.framework.Test;
 12    import junit.framework.TestSuite;
 13    import org.apache.commons.logging.Log;
 14    import org.apache.commons.logging.LogFactory;
 15    import org.jboss.cache.pojo.test.Person;
 16    import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor;
 17    import org.jboss.cache.transaction.DummyTransactionManager;
 18    import org.jboss.aop.Advised;
 19    import org.jboss.aop.advice.Interceptor;
 20   
 21    import javax.transaction.TransactionManager;
 22   
 23    /**
 24    * Additional basic tests
 25    *
 26    * @author Ben Wang
 27    */
 28   
 29    public class TxUndoTest extends TestCase
 30    {
 31    Log log_ = LogFactory.getLog(TxUndoTest.class);
 32    PojoCache cache_;
 33    TransactionManager tx_mgr;
 34   
 35  6 public TxUndoTest(String name)
 36    {
 37  6 super(name);
 38    }
 39   
 40  6 protected void setUp() throws Exception
 41    {
 42  6 super.setUp();
 43  6 log_.info("setUp() ....");
 44  6 String configFile = "META-INF/local-service.xml";
 45  6 boolean toStart = false;
 46  6 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 47  6 cache_.start();
 48  6 tx_mgr = DummyTransactionManager.getInstance();
 49   
 50    }
 51   
 52  6 protected void tearDown() throws Exception
 53    {
 54  6 super.tearDown();
 55  6 cache_.stop();
 56    }
 57   
 58    // public void testDummy() {}
 59   
 60  2 public void testSimpleTxWithRollback1() throws Exception
 61    {
 62  2 log_.info("testSimpleTxWithRollback1() ....");
 63  2 Person test = new Person();
 64  2 test.setName("Ben");
 65  2 test.setAge(10);
 66   
 67  2 tx_mgr.begin();
 68  2 cache_.attach("/a", test);
 69  2 tx_mgr.getTransaction().rollback();
 70  2 assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
 71    }
 72   
 73  6 private boolean hasCacheInterceptor(Object pojo)
 74    {
 75  6 Interceptor[] interceptors = null;
 76  6 try {
 77  6 interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
 78    } catch (Exception ex)
 79    {
 80  0 return false;
 81    }
 82   
 83  6 for(int i=0; i < interceptors.length; i++)
 84    {
 85  2 if(interceptors[i] instanceof CacheFieldInterceptor)
 86  2 return true;
 87    }
 88  4 return false;
 89    }
 90   
 91  2 public void testSimpleTxWithRollback2() throws Exception
 92    {
 93  2 log_.info("testSimpleTxWithRollback1() ....");
 94  2 Person test = new Person();
 95  2 test.setName("Ben");
 96  2 test.setAge(10);
 97  2 cache_.attach("/a", test);
 98   
 99  2 tx_mgr.begin();
 100  2 cache_.detach("/a");
 101  2 tx_mgr.getTransaction().rollback();
 102   
 103  2 assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
 104    }
 105   
 106  2 public void testSimpleTxWithRollback3() throws Exception
 107    {
 108  2 log_.info("testSimpleTxWithRollback1() ....");
 109  2 Person test = new Person();
 110  2 test.setName("Ben");
 111  2 test.setAge(10);
 112  2 tx_mgr.begin();
 113  2 cache_.attach("/a", test);
 114  2 cache_.detach("/a");
 115  2 tx_mgr.getTransaction().rollback();
 116   
 117  2 assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
 118    }
 119   
 120  2 public static Test suite() throws Exception
 121    {
 122  2 return new TestSuite(TxUndoTest.class);
 123    }
 124   
 125   
 126  0 public static void main(String[] args) throws Exception
 127    {
 128  0 junit.textui.TestRunner.run(TxUndoTest.suite());
 129    }
 130   
 131    }