Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 205   Methods: 12
NCLOC: 143   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
MapTxUndoTest.java 100% 98.9% 91.7% 98%
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.proxy.ClassProxy;
 16    import org.jboss.cache.pojo.PojoCache;
 17    import org.jboss.cache.pojo.PojoCacheFactory;
 18    import org.jboss.cache.pojo.test.Person;
 19    import org.jboss.cache.transaction.DummyTransactionManager;
 20   
 21    import javax.transaction.TransactionManager;
 22    import java.util.HashMap;
 23    import java.util.Map;
 24   
 25    /**
 26    * Additional basic tests
 27    *
 28    * @author Ben Wang
 29    */
 30   
 31    public class MapTxUndoTest extends TestCase
 32    {
 33    Log log_ = LogFactory.getLog(MapTxUndoTest.class);
 34    PojoCache cache_;
 35    TransactionManager tx_mgr;
 36   
 37  6 public MapTxUndoTest(String name)
 38    {
 39  6 super(name);
 40    }
 41   
 42  6 protected void setUp() throws Exception
 43    {
 44  6 super.setUp();
 45  6 log_.info("setUp() ....");
 46  6 String configFile = "META-INF/local-service.xml";
 47  6 boolean toStart = false;
 48  6 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 49  6 cache_.start();
 50  6 tx_mgr = DummyTransactionManager.getInstance();
 51   
 52    }
 53   
 54  6 protected void tearDown() throws Exception
 55    {
 56  6 super.tearDown();
 57  6 cache_.stop();
 58    }
 59   
 60    // public void testDummy() {}
 61   
 62  1 public void testSimple() throws Exception
 63    {
 64  1 HashMap map = new HashMap();
 65  1 map.put("1", "test1");
 66   
 67  1 tx_mgr.begin();
 68  1 cache_.attach("/a", map);
 69  1 tx_mgr.getTransaction().rollback();
 70  1 assertFalse("Should not have cache interceptor ", isProxy(map));
 71   
 72  1 cache_.attach("/a", map);
 73    }
 74   
 75  1 public void testSimpleTxWithRollback1() throws Exception
 76    {
 77  1 log_.info("testSimpleTxWithRollback1() ....");
 78  1 Person test = new Person();
 79  1 test.setName("Ben");
 80  1 test.setAge(10);
 81  1 HashMap map = new HashMap();
 82  1 map.put("1", "English");
 83  1 test.setHobbies(map);
 84   
 85  1 tx_mgr.begin();
 86  1 cache_.attach("/a", test);
 87  1 tx_mgr.getTransaction().rollback();
 88  1 assertFalse("Should not have cache interceptor ", isProxy(test.getHobbies()));
 89   
 90  1 cache_.attach("/a", test);
 91    }
 92   
 93  4 private boolean isProxy(Object pojo)
 94    {
 95  1 if (pojo instanceof ClassProxy) return true;
 96  3 return false;
 97    }
 98   
 99  1 public void testSimpleTxWithRollback2() throws Exception
 100    {
 101  1 log_.info("testSimpleTxWithRollback1() ....");
 102  1 Person test = new Person();
 103  1 test.setName("Ben");
 104  1 test.setAge(10);
 105  1 HashMap map = new HashMap();
 106  1 map.put("1", "English");
 107  1 test.setHobbies(map);
 108   
 109  1 cache_.attach("/a", test);
 110   
 111  1 tx_mgr.begin();
 112  1 cache_.detach("/a");
 113  1 tx_mgr.getTransaction().rollback();
 114   
 115  1 assertTrue("Should still have cache interceptor ", isProxy(test.getHobbies()));
 116  1 cache_.detach("/a");
 117    }
 118   
 119  1 public void testSimpleTxWithRollback3() throws Exception
 120    {
 121  1 log_.info("testSimpleTxWithRollback1() ....");
 122  1 Person test = new Person();
 123  1 test.setName("Ben");
 124  1 test.setAge(10);
 125  1 HashMap map = new HashMap();
 126  1 map.put("1", "English");
 127  1 test.setHobbies(map);
 128  1 tx_mgr.begin();
 129  1 cache_.attach("/a", test);
 130  1 cache_.detach("/a");
 131  1 tx_mgr.getTransaction().rollback();
 132   
 133  1 assertFalse("Should not have cache interceptor ", isProxy(test.getHobbies()));
 134    }
 135   
 136    /**
 137    * Contributed by Niztan Niv
 138    *
 139    * @throws Exception
 140    */
 141  1 public void testNestedMapWithRollback() throws Exception
 142    {
 143    // create cached data objects
 144  1 Map obj1 = new HashMap();
 145  1 obj1.put("id", "1");
 146  1 cache_.attach("objs/1", obj1);
 147  1 obj1 = (Map) cache_.find("objs/1");
 148   
 149  1 Map obj2 = new HashMap();
 150  1 obj2.put("id", "2");
 151  1 cache_.attach("objs/2", obj2);
 152  1 obj2 = (Map) cache_.find("objs/2");
 153   
 154    // create cached collection of data objects
 155  1 Map indexMap = new HashMap();
 156  1 cache_.attach("objsIndex", indexMap);
 157  1 indexMap = (Map) cache_.find("objsIndex");
 158   
 159    // initialize collection by adding a data object
 160  1 final String KEY = "KEY";
 161  1 indexMap.put(KEY, obj1);
 162   
 163  1 Object beforeModify = indexMap.get(KEY);
 164  1 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get("id"));
 165   
 166    // modify the collection by replacing the first data object with the second
 167    // and then roll-back the transaction
 168  1 tx_mgr.begin();
 169  1 indexMap.put(KEY, obj2);
 170  1 tx_mgr.rollback();
 171   
 172  1 Object afterRollback = indexMap.get(KEY);
 173  1 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get("id"));
 174   
 175    // check if state of collection was restored
 176  1 assertEquals(beforeModify, afterRollback);
 177    }
 178   
 179  1 public void testPlainMapRollback() throws Exception
 180    {
 181    // create cached data objects
 182  1 Map obj1 = new HashMap();
 183  1 obj1.put("id", "1");
 184  1 cache_.attach("objs/1", obj1);
 185  1 obj1 = (Map) cache_.find("objs/1");
 186   
 187  1 tx_mgr.begin();
 188  1 obj1.put("id", "newId");
 189  1 tx_mgr.rollback();
 190   
 191  1 assertEquals("1", obj1.get("id"));
 192    }
 193   
 194  1 public static Test suite() throws Exception
 195    {
 196  1 return new TestSuite(MapTxUndoTest.class);
 197    }
 198   
 199   
 200  0 public static void main(String[] args) throws Exception
 201    {
 202  0 junit.textui.TestRunner.run(MapTxUndoTest.suite());
 203    }
 204   
 205    }