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