[jboss-cvs] JBossCache/tests-50/functional/org/jboss/cache/pojo/rollback ...

Ben Wang bwang at jboss.com
Tue Oct 10 06:50:34 EDT 2006


  User: bwang   
  Date: 06/10/10 06:50:34

  Added:       tests-50/functional/org/jboss/cache/pojo/rollback 
                        PojoCollectionRollbackTest.java
  Log:
  First cut for JBCACHE-763 transaction rollback problem for collection.
  
  Revision  Changes    Path
  1.1      date: 2006/10/10 10:50:34;  author: bwang;  state: Exp;JBossCache/tests-50/functional/org/jboss/cache/pojo/rollback/PojoCollectionRollbackTest.java
  
  Index: PojoCollectionRollbackTest.java
  ===================================================================
  /*****************************************
   *                                       *
   *  JBoss Portal: The OpenSource Portal  *
   *                                       *
   *   Distributable under LGPL license.   *
   *   See terms of license at gnu.org.    *
   *                                       *
   *****************************************/
  package org.jboss.cache.pojo.rollback;
  
  import junit.framework.TestCase;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.transaction.DummyTransactionManager;
  
  import javax.transaction.TransactionManager;
  import java.util.ArrayList;
  import java.util.HashMap;
  import java.util.List;
  import java.util.Map;
  
  /**
   * @author
   */
  public class PojoCollectionRollbackTest extends TestCase
  {
     private static final String ID = "id";
     private static final String CONTAINER_FQN = "/objsIndex";
     TransactionManager tx_mgr;
  
     public PojoCollectionRollbackTest(String s)
     {
        super(s);
     }
  
     private PojoCache cache_;
  
     protected void setUp() throws Exception
     {
        super.setUp();
     }
  
     private void startTest() throws Exception
     {
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createInstance(configFile, toStart);
        cache_.start();
        tx_mgr = DummyTransactionManager.getInstance();
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     public void testNestedMapAndIndexWithModifyRollback() throws Exception
     {
        System.out.println("testNestedMapAndIndexWithModifyRollback");
        startTest();
  
        // create cached data objects
        Map obj1 = new HashMap();
        obj1.put(ID, "1");
        cache_.attach("/objs/1", obj1);
        obj1 = (Map) cache_.find("/objs/1");
  
        // create cached collection of data objects
        // initialize collection by adding a data object
        Map indexMap = null;
        final String KEY = "KEY";
        Object beforeModify;
        Object afterRollback;
  
        indexMap = new HashMap();
        cache_.attach(CONTAINER_FQN, indexMap);
        indexMap = (Map) cache_.find(CONTAINER_FQN);
        indexMap.put(KEY, obj1);
  
        beforeModify = indexMap.get(KEY);
        Object idBeforeModify = ((Map) beforeModify).get(ID);
  
        System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get(ID));
  
        // modify the collection by replacing the first data object with the second
        // and then roll-back the transaction
        tx_mgr.begin();
        obj1.put(ID, "newID");
        indexMap.remove(KEY);
        tx_mgr.rollback();
  
        indexMap = (Map) cache_.find(CONTAINER_FQN);
        afterRollback = indexMap.get(KEY);
        System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get(ID));
  
        // check if state of collection was restored
        assertEquals(beforeModify, afterRollback);
        assertTrue(beforeModify == afterRollback);
        assertEquals(1, indexMap.size());
  
        assertEquals("1", obj1.get(ID));
        Object idAfterRollback = ((Map) afterRollback).get(ID);
        System.out.println("idBeforeModify: " + idBeforeModify + " idAfterRollback: " + idAfterRollback);
        assertEquals(idBeforeModify, idAfterRollback);
     }
  
     public void testNestedMapWithModifyRollback() throws Exception
     {
        System.out.println("testNestedMapWithModifyRollback");
        startTest();
  
        // create cached data objects
        Map obj1 = new HashMap();
        obj1.put(ID, "1");
        cache_.attach("/objs/1", obj1);
        obj1 = (Map) cache_.find("/objs/1");
  
        Map obj2 = new HashMap();
        obj2.put(ID, "2");
        cache_.attach("/objs/2", obj2);
        obj2 = (Map) cache_.find("/objs/2");
  
        // create cached collection of data objects
        // initialize collection by adding a data object
        Map indexMap = null;
        final String KEY = "KEY";
        Object beforeModify;
        Object afterRollback;
  
        indexMap = new HashMap();
        cache_.attach(CONTAINER_FQN, indexMap);
        indexMap = (Map) cache_.find(CONTAINER_FQN);
        indexMap.put(KEY, obj1);
        beforeModify = indexMap.get(KEY);
  
        System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get(ID));
  
        // modify the collection by replacing the first data object with the second
        // and then roll-back the transaction
        tx_mgr.begin();
        Object removedByModify = indexMap.put(KEY, obj2);
        System.out.println("removedByModify: " + removedByModify + ", data object id: " + ((Map) removedByModify).get(ID));
        assertEquals(removedByModify, beforeModify);
        tx_mgr.rollback();
  
        indexMap = (Map) cache_.find(CONTAINER_FQN);
        afterRollback = indexMap.get(KEY);
        System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get(ID));
  
        // check if state of collection was restored
        assertEquals(beforeModify, afterRollback);
        assertEquals(1, indexMap.size());
        // check that map entry can now be modified
        indexMap.put(KEY, obj2);
        assertEquals(obj2, indexMap.get(KEY));
     }
  
     public void testNestedMapWithRemoveRollback() throws Exception
     {
        System.out.println("testNestedMapWithRemoveRollback");
        startTest();
  
        // create cache_d data objects
        Map obj1 = new HashMap();
        obj1.put(ID, "1");
        cache_.attach("/objs/1", obj1);
        obj1 = (Map) cache_.find("/objs/1");
  
        Map obj2 = new HashMap();
        obj2.put(ID, "2");
        cache_.attach("/objs/2", obj2);
        obj2 = (Map) cache_.find("/objs/2");
  
        // create cached collection of data objects
        Map indexMap = new HashMap();
        cache_.attach(CONTAINER_FQN, indexMap);
        indexMap = (Map) cache_.find(CONTAINER_FQN);
  
        // initialize collection by adding a data object
        final String KEY = "KEY";
        indexMap.put(KEY, obj1);
  
        Object beforeModify = indexMap.get(KEY);
        System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get(ID));
  
        // modify the collection by replacing the first data object with the second
        // and then roll-back the transaction
        tx_mgr.begin();
        Object removedByRemove = indexMap.remove(KEY);
        System.out.println("removedByRemove: " + removedByRemove + ", data object id: " + ((Map) removedByRemove).get(ID));
        assertEquals(beforeModify, removedByRemove);
        tx_mgr.rollback();
  
        Object afterRollback = indexMap.get(KEY);
        System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get(ID));
  
        // check if state of collection was restored
        assertEquals(beforeModify, afterRollback);
        assertEquals(1, indexMap.size());
  
        // check that map entry can now be modified
        indexMap.put(KEY, obj2);
        assertEquals(obj2, indexMap.get(KEY));
     }
  
     public void testNestedListWithModifyAddRollback() throws Exception
     {
        System.out.println("testNestedListWithModifyAddRollback");
        startTest();
  
        // create cached data objects
        Map obj1 = new HashMap();
        obj1.put(ID, "1");
        cache_.attach("/objs/1", obj1);
        obj1 = (Map) cache_.find("/objs/1");
  
        Map obj2 = new HashMap();
        obj2.put(ID, "2");
        cache_.attach("/objs/2", obj2);
        obj2 = (Map) cache_.find("/objs/2");
        assertFalse(obj1.equals(obj2));
  
        // create cached collection of data objects
        List indexList = new ArrayList();
        cache_.attach(CONTAINER_FQN, indexList);
        indexList = (List) cache_.find(CONTAINER_FQN);
  
        // initialize collection by adding a data object
        indexList.add(obj1);
  
        Object beforeModify = indexList.get(0);
        System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get(ID));
        int objIndex1, objIndex2;
  
        // modify the collection by replacing the first data object with the second
        // and then roll-back the transaction
        tx_mgr.begin();
        indexList.add(obj2);
        objIndex1 = indexList.indexOf(obj1);
        objIndex2 = indexList.indexOf(obj2);
        tx_mgr.rollback();
  
        // before rollback - object set
        assertFalse(obj1.equals(obj2));
        assertEquals(0, objIndex1);
        assertEquals(1, objIndex2);
  
        Object afterRollback = indexList.get(0);
        System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get(ID));
  
        // check if state of collection was restored
        assertEquals(beforeModify, afterRollback);
        assertEquals(1, indexList.size());
  
        // check that list entry can now be modified
        indexList.set(0, obj2);
        assertEquals(obj2, indexList.get(0));
     }
  
     public void testNestedListWithModifySetRollback() throws Exception
     {
        System.out.println("testNestedListWithModifySetRollback");
        startTest();
  
        // create cached data objects
        Map obj1 = new HashMap();
        obj1.put(ID, "1");
        cache_.attach("/objs/1", obj1);
        obj1 = (Map) cache_.find("/objs/1");
  
        Map obj2 = new HashMap();
        obj2.put(ID, "2");
        cache_.attach("/objs/2", obj2);
        obj2 = (Map) cache_.find("/objs/2");
  
        // create cached collection of data objects
        List indexList = new ArrayList();
        cache_.attach(CONTAINER_FQN, indexList);
        indexList = (List) cache_.find(CONTAINER_FQN);
  
        // initialize collection by adding a data object
        indexList.add(obj1);
  
        Object beforeModify = indexList.get(0);
        System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get(ID));
        int objIndex;
  
        // modify the collection by replacing the first data object with the second
        // and then roll-back the transaction
        tx_mgr.begin();
        Object removedBySet = indexList.set(0, obj2);
        System.out.println("removedBySet: " + removedBySet + ", data object id: " + ((Map) removedBySet).get(ID));
        assertEquals(beforeModify, removedBySet);
        objIndex = indexList.indexOf(obj2);
        tx_mgr.rollback();
  
        // before rollback - object set
        assertEquals(0, objIndex);
  
        Object afterRollback = indexList.get(0);
        System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get(ID));
  
        // check if state of collection was restored
        assertEquals(beforeModify, afterRollback);
        assertEquals(1, indexList.size());
  
        // check that list entry can now be modified
        indexList.set(0, obj2);
        assertEquals(obj2, indexList.get(0));
     }
  
     public void testNestedListWithRemoveRollback() throws Exception
     {
        System.out.println("testNestedListWithRemoveRollback");
        startTest();
  
        // create cached data objects
        Map obj1 = new HashMap();
        obj1.put(ID, "1");
        cache_.attach("/objs/1", obj1);
        obj1 = (Map) cache_.find("/objs/1");
  
        Map obj2 = new HashMap();
        obj2.put(ID, "2");
        cache_.attach("/objs/2", obj2);
        obj2 = (Map) cache_.find("/objs/2");
  
        // create cached collection of data objects
        List indexList = new ArrayList();
        cache_.attach(CONTAINER_FQN, indexList);
        indexList = (List) cache_.find(CONTAINER_FQN);
  
        // initialize collection by adding a data object
        indexList.add(obj1);
  
        Object beforeModify = indexList.get(0);
        System.out.println("beforeModify: " + beforeModify + ", data object id: " + ((Map) beforeModify).get(ID));
        int objIndex;
  
        // modify the collection by replacing the first data object with the second
        // and then roll-back the transaction
        tx_mgr.begin();
        indexList.remove(obj1);
        objIndex = indexList.indexOf(obj1);
        tx_mgr.rollback();
  
        // before rollback - object removed
        assertEquals(-1, objIndex);
  
        Object afterRollback = indexList.get(0);
        System.out.println("afterRollback: " + afterRollback + ", data object id: " + ((Map) afterRollback).get(ID));
  
        // check if state of collection was restored
        assertEquals(beforeModify, afterRollback);
        assertEquals(1, indexList.size());
  
        // check that list entry can now be modified
        indexList.set(0, obj2);
        assertEquals(obj2, indexList.get(0));
     }
  
  
  }
  
  
  



More information about the jboss-cvs-commits mailing list