Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 172   Methods: 11
NCLOC: 133   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
InMemoryTxUndoTest.java 25% 96.1% 90.9% 92.3%
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.rollback;
 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.aop.Advised;
 16    import org.jboss.aop.advice.Interceptor;
 17    import org.jboss.cache.pojo.PojoCache;
 18    import org.jboss.cache.pojo.PojoCacheFactory;
 19    import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor;
 20    import org.jboss.cache.pojo.test.Address;
 21    import org.jboss.cache.pojo.test.Person;
 22    import org.jboss.cache.transaction.DummyTransactionManager;
 23   
 24    import javax.transaction.TransactionManager;
 25    import java.util.ArrayList;
 26   
 27    /**
 28    * Additional basic tests
 29    *
 30    * @author Ben Wang
 31    */
 32   
 33    public class InMemoryTxUndoTest extends TestCase
 34    {
 35    Log log_ = LogFactory.getLog(InMemoryTxUndoTest.class);
 36    PojoCache cache_;
 37    TransactionManager tx_mgr;
 38   
 39  5 public InMemoryTxUndoTest(String name)
 40    {
 41  5 super(name);
 42    }
 43   
 44  5 protected void setUp() throws Exception
 45    {
 46  5 super.setUp();
 47  5 log_.info("setUp() ....");
 48  5 String configFile = "META-INF/local-service.xml";
 49  5 boolean toStart = false;
 50  5 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 51  5 cache_.start();
 52  5 tx_mgr = DummyTransactionManager.getInstance();
 53   
 54    }
 55   
 56  5 protected void tearDown() throws Exception
 57    {
 58  5 super.tearDown();
 59  5 cache_.stop();
 60    }
 61   
 62    // public void testDummy() {}
 63  1 private boolean hasCacheInterceptor(Object pojo)
 64    {
 65  1 Interceptor[] interceptors = ((Advised) pojo)._getInstanceAdvisor().getInterceptors();
 66  1 for (int i = 0; i < interceptors.length; i++)
 67    {
 68  0 if (interceptors[i] instanceof CacheFieldInterceptor)
 69  0 return true;
 70    }
 71  1 return false;
 72    }
 73   
 74  1 public void testSimpleTxWithRollback1() throws Exception
 75    {
 76  1 log_.info("testSimpleTxWithRollback1() ....");
 77  1 Person test = new Person();
 78  1 test.setName("Ben");
 79  1 test.setAge(10);
 80  1 tx_mgr.begin();
 81  1 cache_.attach("/a", test);
 82  1 test.setAge(20);
 83  1 tx_mgr.getTransaction().rollback();
 84   
 85  1 assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
 86  1 assertEquals("Should still be ", 10, test.getAge());
 87    }
 88   
 89  1 public void testSimpleTxWithRollback2() throws Exception
 90    {
 91  1 log_.info("testSimpleTxWithRollback2() ....");
 92  1 Person test = new Person();
 93  1 test.setName("Ben");
 94  1 test.setAge(10);
 95  1 cache_.attach("/a", test);
 96   
 97  1 tx_mgr.begin();
 98  1 test.setAge(20);
 99  1 tx_mgr.getTransaction().rollback();
 100  1 assertEquals("Should still be ", 10, test.getAge());
 101    }
 102   
 103  1 public void testSimpleTxWithRollback3() throws Exception
 104    {
 105  1 log_.info("testSimpleTxWithRollback3() ....");
 106  1 Person test = new Person();
 107  1 test.setName("Ben");
 108  1 test.setAge(10);
 109  1 cache_.attach("/a", test);
 110   
 111  1 Person test1 = new Person();
 112  1 test1.setName("Irin");
 113  1 test1.setAge(30);
 114  1 Address addr = new Address();
 115  1 addr.setCity("Taipei");
 116  1 test1.setAddress(addr);
 117  1 tx_mgr.begin();
 118  1 cache_.attach("/a", test1);
 119  1 tx_mgr.getTransaction().rollback();
 120   
 121  1 assertEquals("Should still be ", 10, test.getAge());
 122  1 assertNull("Address should be ", test.getAddress());
 123    }
 124   
 125  1 public void testSimpleTxWithRollback4() throws Exception
 126    {
 127  1 log_.info("testSimpleTxWithRollback4() ....");
 128  1 Person test = new Person();
 129  1 test.setName("Ben");
 130  1 test.setAge(10);
 131  1 cache_.attach("/a", test);
 132  1 Address addr = new Address();
 133  1 addr.setCity("Taipei");
 134  1 test.setAddress(addr);
 135   
 136  1 tx_mgr.begin();
 137  1 addr.setCity("Tainan");
 138  1 tx_mgr.getTransaction().rollback();
 139   
 140  1 assertEquals("Should still be ", "Taipei", test.getAddress().getCity());
 141    }
 142   
 143  1 public void testSimpleTxWithRollback5() throws Exception
 144    {
 145  1 log_.info("testSimpleTxWithRollback5() ....");
 146  1 Person test = new Person();
 147  1 test.setName("Ben");
 148  1 test.setAge(10);
 149  1 cache_.attach("/a", test);
 150   
 151  1 ArrayList lang = new ArrayList();
 152  1 lang.add("English");
 153  1 test.setLanguages(lang);
 154  1 tx_mgr.begin();
 155  1 lang.add("French");
 156  1 tx_mgr.getTransaction().rollback();
 157   
 158  1 assertEquals("Should still be ", 1, test.getLanguages().size());
 159    }
 160   
 161  1 public static Test suite() throws Exception
 162    {
 163  1 return new TestSuite(InMemoryTxUndoTest.class);
 164    }
 165   
 166   
 167  0 public static void main(String[] args) throws Exception
 168    {
 169  0 junit.textui.TestRunner.run(InMemoryTxUndoTest.suite());
 170    }
 171   
 172    }