Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 364   Methods: 10
NCLOC: 237   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
PojoCollectionRollbackTest.java - 100% 100% 100%
coverage
 1    /*****************************************
 2    * *
 3    * JBoss Portal: The OpenSource Portal *
 4    * *
 5    * Distributable under LGPL license. *
 6    * See terms of license at gnu.org. *
 7    * *
 8    *****************************************/
 9    package org.jboss.cache.pojo.rollback;
 10   
 11    import junit.framework.TestCase;
 12    import org.jboss.cache.pojo.PojoCache;
 13    import org.jboss.cache.pojo.PojoCacheFactory;
 14    import org.jboss.cache.transaction.DummyTransactionManager;
 15   
 16    import javax.transaction.TransactionManager;
 17    import java.util.ArrayList;
 18    import java.util.HashMap;
 19    import java.util.List;
 20    import java.util.Map;
 21   
 22    /**
 23    * @author
 24    */
 25    public class PojoCollectionRollbackTest extends TestCase
 26    {
 27    private static final String ID = "id";
 28    private static final String CONTAINER_FQN = "/objsIndex";
 29    TransactionManager tx_mgr;
 30   
 31  12 public PojoCollectionRollbackTest(String s)
 32    {
 33  12 super(s);
 34    }
 35   
 36    private PojoCache cache_;
 37   
 38  12 protected void setUp() throws Exception
 39    {
 40  12 super.setUp();
 41    }
 42   
 43  12 private void startTest() throws Exception
 44    {
 45  12 String configFile = "META-INF/local-service.xml";
 46  12 boolean toStart = false;
 47  12 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 48  12 cache_.start();
 49  12 tx_mgr = DummyTransactionManager.getInstance();
 50    }
 51   
 52  12 protected void tearDown() throws Exception
 53    {
 54  12 super.tearDown();
 55  12 cache_.stop();
 56    }
 57   
 58  2 public void testNestedMapAndIndexWithModifyRollback() throws Exception
 59    {
 60  2 System.out.println("testNestedMapAndIndexWithModifyRollback");
 61  2 startTest();
 62   
 63    // create cached data objects
 64  2 Map obj1 = new HashMap();
 65  2 obj1.put(ID, "1");
 66  2 cache_.attach("/objs/1", obj1);
 67  2 obj1 = (Map) cache_.find("/objs/1");
 68   
 69    // create cached collection of data objects
 70    // initialize collection by adding a data object
 71  2 Map indexMap = null;
 72  2 final String KEY = "KEY";
 73  2 Object beforeModify;
 74  2 Object afterRollback;
 75   
 76  2 indexMap = new HashMap();
 77  2 cache_.attach(CONTAINER_FQN, indexMap);
 78  2 indexMap = (Map) cache_.find(CONTAINER_FQN);
 79  2 indexMap.put(KEY, obj1);
 80   
 81  2 beforeModify = indexMap.get(KEY);
 82  2 Object idBeforeModify = ((Map) beforeModify).get(ID);
 83   
 84  2 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get(ID));
 85   
 86    // modify the collection by replacing the first data object with the second
 87    // and then roll-back the transaction
 88  2 tx_mgr.begin();
 89  2 obj1.put(ID, "newID");
 90  2 indexMap.remove(KEY);
 91  2 tx_mgr.rollback();
 92   
 93  2 indexMap = (Map) cache_.find(CONTAINER_FQN);
 94  2 afterRollback = indexMap.get(KEY);
 95  2 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get(ID));
 96   
 97    // check if state of collection was restored
 98  2 assertEquals(beforeModify, afterRollback);
 99  2 assertTrue(beforeModify == afterRollback);
 100  2 assertEquals(1, indexMap.size());
 101   
 102  2 assertEquals("1", obj1.get(ID));
 103  2 Object idAfterRollback = ((Map) afterRollback).get(ID);
 104  2 System.out.println("idBeforeModify: " + idBeforeModify + " idAfterRollback: " + idAfterRollback);
 105  2 assertEquals(idBeforeModify, idAfterRollback);
 106    }
 107   
 108  2 public void testNestedMapWithModifyRollback() throws Exception
 109    {
 110  2 System.out.println("testNestedMapWithModifyRollback");
 111  2 startTest();
 112   
 113    // create cached data objects
 114  2 Map obj1 = new HashMap();
 115  2 obj1.put(ID, "1");
 116  2 cache_.attach("/objs/1", obj1);
 117  2 obj1 = (Map) cache_.find("/objs/1");
 118   
 119  2 Map obj2 = new HashMap();
 120  2 obj2.put(ID, "2");
 121  2 cache_.attach("/objs/2", obj2);
 122  2 obj2 = (Map) cache_.find("/objs/2");
 123   
 124    // create cached collection of data objects
 125    // initialize collection by adding a data object
 126  2 Map indexMap = null;
 127  2 final String KEY = "KEY";
 128  2 Object beforeModify;
 129  2 Object afterRollback;
 130   
 131  2 indexMap = new HashMap();
 132  2 cache_.attach(CONTAINER_FQN, indexMap);
 133  2 indexMap = (Map) cache_.find(CONTAINER_FQN);
 134  2 indexMap.put(KEY, obj1);
 135  2 beforeModify = indexMap.get(KEY);
 136   
 137  2 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get(ID));
 138   
 139    // modify the collection by replacing the first data object with the second
 140    // and then roll-back the transaction
 141  2 tx_mgr.begin();
 142  2 Object removedByModify = indexMap.put(KEY, obj2);
 143  2 System.out.println("removedByModify: " + removedByModify + ", data object id: " + ((Map) removedByModify).get(ID));
 144  2 assertEquals(removedByModify, beforeModify);
 145  2 tx_mgr.rollback();
 146   
 147  2 indexMap = (Map) cache_.find(CONTAINER_FQN);
 148  2 afterRollback = indexMap.get(KEY);
 149  2 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get(ID));
 150   
 151    // check if state of collection was restored
 152  2 assertEquals(beforeModify, afterRollback);
 153  2 assertEquals(1, indexMap.size());
 154    // check that map entry can now be modified
 155  2 indexMap.put(KEY, obj2);
 156  2 assertEquals(obj2, indexMap.get(KEY));
 157    }
 158   
 159  2 public void testNestedMapWithRemoveRollback() throws Exception
 160    {
 161  2 System.out.println("testNestedMapWithRemoveRollback");
 162  2 startTest();
 163   
 164    // create cache_d data objects
 165  2 Map obj1 = new HashMap();
 166  2 obj1.put(ID, "1");
 167  2 cache_.attach("/objs/1", obj1);
 168  2 obj1 = (Map) cache_.find("/objs/1");
 169   
 170  2 Map obj2 = new HashMap();
 171  2 obj2.put(ID, "2");
 172  2 cache_.attach("/objs/2", obj2);
 173  2 obj2 = (Map) cache_.find("/objs/2");
 174   
 175    // create cached collection of data objects
 176  2 Map indexMap = new HashMap();
 177  2 cache_.attach(CONTAINER_FQN, indexMap);
 178  2 indexMap = (Map) cache_.find(CONTAINER_FQN);
 179   
 180    // initialize collection by adding a data object
 181  2 final String KEY = "KEY";
 182  2 indexMap.put(KEY, obj1);
 183   
 184  2 Object beforeModify = indexMap.get(KEY);
 185  2 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get(ID));
 186   
 187    // modify the collection by replacing the first data object with the second
 188    // and then roll-back the transaction
 189  2 tx_mgr.begin();
 190  2 Object removedByRemove = indexMap.remove(KEY);
 191  2 System.out.println("removedByRemove: " + removedByRemove + ", data object id: " + ((Map) removedByRemove).get(ID));
 192  2 assertEquals(beforeModify, removedByRemove);
 193  2 tx_mgr.rollback();
 194   
 195  2 Object afterRollback = indexMap.get(KEY);
 196  2 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get(ID));
 197   
 198    // check if state of collection was restored
 199  2 assertEquals(beforeModify, afterRollback);
 200  2 assertEquals(1, indexMap.size());
 201   
 202    // check that map entry can now be modified
 203  2 indexMap.put(KEY, obj2);
 204  2 assertEquals(obj2, indexMap.get(KEY));
 205    }
 206   
 207  2 public void testNestedListWithModifyAddRollback() throws Exception
 208    {
 209  2 System.out.println("testNestedListWithModifyAddRollback");
 210  2 startTest();
 211   
 212    // create cached data objects
 213  2 Map obj1 = new HashMap();
 214  2 obj1.put(ID, "1");
 215  2 cache_.attach("/objs/1", obj1);
 216  2 obj1 = (Map) cache_.find("/objs/1");
 217   
 218  2 Map obj2 = new HashMap();
 219  2 obj2.put(ID, "2");
 220  2 cache_.attach("/objs/2", obj2);
 221  2 obj2 = (Map) cache_.find("/objs/2");
 222  2 assertFalse(obj1.equals(obj2));
 223   
 224    // create cached collection of data objects
 225  2 List indexList = new ArrayList();
 226  2 cache_.attach(CONTAINER_FQN, indexList);
 227  2 indexList = (List) cache_.find(CONTAINER_FQN);
 228   
 229    // initialize collection by adding a data object
 230  2 indexList.add(obj1);
 231   
 232  2 Object beforeModify = indexList.get(0);
 233  2 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get(ID));
 234  2 int objIndex1, objIndex2;
 235   
 236    // modify the collection by replacing the first data object with the second
 237    // and then roll-back the transaction
 238  2 tx_mgr.begin();
 239  2 indexList.add(obj2);
 240  2 objIndex1 = indexList.indexOf(obj1);
 241  2 objIndex2 = indexList.indexOf(obj2);
 242  2 tx_mgr.rollback();
 243   
 244    // before rollback - object set
 245  2 assertFalse(obj1.equals(obj2));
 246  2 assertEquals(0, objIndex1);
 247  2 assertEquals(1, objIndex2);
 248   
 249  2 Object afterRollback = indexList.get(0);
 250  2 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get(ID));
 251   
 252    // check if state of collection was restored
 253  2 assertEquals(beforeModify, afterRollback);
 254  2 assertEquals(1, indexList.size());
 255   
 256    // check that list entry can now be modified
 257  2 indexList.set(0, obj2);
 258  2 assertEquals(obj2, indexList.get(0));
 259    }
 260   
 261  2 public void testNestedListWithModifySetRollback() throws Exception
 262    {
 263  2 System.out.println("testNestedListWithModifySetRollback");
 264  2 startTest();
 265   
 266    // create cached data objects
 267  2 Map obj1 = new HashMap();
 268  2 obj1.put(ID, "1");
 269  2 cache_.attach("/objs/1", obj1);
 270  2 obj1 = (Map) cache_.find("/objs/1");
 271   
 272  2 Map obj2 = new HashMap();
 273  2 obj2.put(ID, "2");
 274  2 cache_.attach("/objs/2", obj2);
 275  2 obj2 = (Map) cache_.find("/objs/2");
 276   
 277    // create cached collection of data objects
 278  2 List indexList = new ArrayList();
 279  2 cache_.attach(CONTAINER_FQN, indexList);
 280  2 indexList = (List) cache_.find(CONTAINER_FQN);
 281   
 282    // initialize collection by adding a data object
 283  2 indexList.add(obj1);
 284   
 285  2 Object beforeModify = indexList.get(0);
 286  2 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get(ID));
 287  2 int objIndex;
 288   
 289    // modify the collection by replacing the first data object with the second
 290    // and then roll-back the transaction
 291  2 tx_mgr.begin();
 292  2 Object removedBySet = indexList.set(0, obj2);
 293  2 System.out.println("removedBySet: " + removedBySet + ", data object id: " + ((Map) removedBySet).get(ID));
 294  2 assertEquals(beforeModify, removedBySet);
 295  2 objIndex = indexList.indexOf(obj2);
 296  2 tx_mgr.rollback();
 297   
 298    // before rollback - object set
 299  2 assertEquals(0, objIndex);
 300   
 301  2 Object afterRollback = indexList.get(0);
 302  2 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get(ID));
 303   
 304    // check if state of collection was restored
 305  2 assertEquals(beforeModify, afterRollback);
 306  2 assertEquals(1, indexList.size());
 307   
 308    // check that list entry can now be modified
 309  2 indexList.set(0, obj2);
 310  2 assertEquals(obj2, indexList.get(0));
 311    }
 312   
 313  2 public void testNestedListWithRemoveRollback() throws Exception
 314    {
 315  2 System.out.println("testNestedListWithRemoveRollback");
 316  2 startTest();
 317   
 318    // create cached data objects
 319  2 Map obj1 = new HashMap();
 320  2 obj1.put(ID, "1");
 321  2 cache_.attach("/objs/1", obj1);
 322  2 obj1 = (Map) cache_.find("/objs/1");
 323   
 324  2 Map obj2 = new HashMap();
 325  2 obj2.put(ID, "2");
 326  2 cache_.attach("/objs/2", obj2);
 327  2 obj2 = (Map) cache_.find("/objs/2");
 328   
 329    // create cached collection of data objects
 330  2 List indexList = new ArrayList();
 331  2 cache_.attach(CONTAINER_FQN, indexList);
 332  2 indexList = (List) cache_.find(CONTAINER_FQN);
 333   
 334    // initialize collection by adding a data object
 335  2 indexList.add(obj1);
 336   
 337  2 Object beforeModify = indexList.get(0);
 338  2 System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get(ID));
 339  2 int objIndex;
 340   
 341    // modify the collection by replacing the first data object with the second
 342    // and then roll-back the transaction
 343  2 tx_mgr.begin();
 344  2 indexList.remove(obj1);
 345  2 objIndex = indexList.indexOf(obj1);
 346  2 tx_mgr.rollback();
 347   
 348    // before rollback - object removed
 349  2 assertEquals(-1, objIndex);
 350   
 351  2 Object afterRollback = indexList.get(0);
 352  2 System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get(ID));
 353   
 354    // check if state of collection was restored
 355  2 assertEquals(beforeModify, afterRollback);
 356  2 assertEquals(1, indexList.size());
 357   
 358    // check that list entry can now be modified
 359  2 indexList.set(0, obj2);
 360  2 assertEquals(obj2, indexList.get(0));
 361    }
 362   
 363   
 364    }