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

Ben Wang bwang at jboss.com
Sat Jan 13 10:55:08 EST 2007


  User: bwang   
  Date: 07/01/13 10:55:07

  Added:       tests/functional/org/jboss/cache/pojo/rollback            
                        ReplicatedTxTest.java LocalUndoTest.java
                        ListUndoTest.java MapTxUndoTest.java
                        MapUndoTest.java SetUndoTest.java
                        ListTxUndoTest.java LocalTxUndoTest.java
                        LocalExceptionUndoTest.java SetTxUndoTest.java
                        InMemoryTxUndoTest.java
                        PojoCollectionRollbackTest.java
  Log:
  JBCACHE-922 Merged src-50 and tests-50 into src and tests, respectively.
  
  Revision  Changes    Path
  1.1      date: 2007/01/13 15:55:07;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/rollback/ReplicatedTxTest.java
  
  Index: ReplicatedTxTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.rollback;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.config.Configuration.CacheMode;
  import org.jboss.cache.factories.UnitTestCacheFactory;
  import org.jboss.cache.pojo.*;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.transaction.DummyTransactionManager;
  
  import javax.naming.Context;
  import javax.naming.NamingException;
  import javax.naming.InitialContext;
  import javax.transaction.UserTransaction;
  import javax.transaction.SystemException;
  import javax.transaction.NotSupportedException;
  import javax.transaction.Transaction;
  import javax.transaction.RollbackException;
  import java.util.Properties;
  import java.util.List;
  import java.util.ArrayList;
  
  /**
   */
  
  public class ReplicatedTxTest extends TestCase
  {
     Log log = LogFactory.getLog(org.jboss.cache.pojo.rollback.ReplicatedTxTest.class);
     PojoCache cache, cache1;
     final String FACTORY = "org.jboss.cache.transaction.DummyContextFactory";
     DummyTransactionManager tx_mgr;
     Throwable t1_ex, t2_ex;
     long start = 0;
  
  
     public ReplicatedTxTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log.info("setUp() ....");
        boolean toStart = false;
        cache = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache.start();
        cache1 = PojoCacheFactory.createCache(UnitTestCacheFactory.createConfiguration(CacheMode.REPL_SYNC), toStart);
        cache1.start();
  
        System.setProperty(Context.INITIAL_CONTEXT_FACTORY, FACTORY);
  
        tx_mgr = DummyTransactionManager.getInstance();
        t1_ex = t2_ex = null;
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache.stop();
        cache1.stop();
  
        DummyTransactionManager.destroy();
     }
  
  //   public void testDummy() {}
  
     UserTransaction getTransaction() throws SystemException, NotSupportedException, NamingException
     {
        Properties prop = new Properties();
        prop.put(Context.INITIAL_CONTEXT_FACTORY,
                "org.jboss.cache.transaction.DummyContextFactory");
        return (UserTransaction) new InitialContext(prop).lookup("UserTransaction");
     }
  
     private Person createPerson(String id, String name, int age)
     {
        Person p = new Person();
        p.setName(name);
        p.setAge(age);
        return p;
     }
  
     public void testSimple() throws Exception
     {
        log.info("testSimple() ....");
        UserTransaction tx = getTransaction();
        tx.begin();
        Person p = createPerson("/person/test1", "Harald Gliebe", 32);
        cache.attach("/person/test1", p);
  
        tx.commit();
        tx.begin();
        p.setName("Benoit");
        tx.commit();
        Person p1 = (Person) cache1.find("/person/test1");
        assertEquals("Benoit", p.getName());
        assertEquals("Benoit", p1.getName());
        tx.begin();
        p1.setAge(61);
        tx.commit();
        assertEquals(61, p.getAge());
        assertEquals(61, p1.getAge());
     }
  
     /**
      * Concurrent puts (whole POJO) from the same cache instance (different threads) with rollback.
      */
     public void testConcurrentPuts() throws Exception
     {
        Thread t1 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              try
              {
                 Person p = createPerson("/person/test6", "p6", 50);
                 List<String> lang = new ArrayList<String>();
                 lang.add("German");
                 p.setLanguages(lang);
                 UserTransaction tx = getTransaction();
                 tx.begin();
                 cache.attach("/person/test6", p);
                 TestingUtil.sleepThread(17000);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 t1_ex = ex;
              }
           }
        };
  
        Thread t2 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              UserTransaction tx = null;
              Person p = createPerson("/person/test6", "p6", 50);
              try
              {
                 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
                 List<String> lang = new ArrayList<String>();
                 lang.add("German");
                 p.setLanguages(lang);
                 tx = getTransaction();
                 tx.begin();
                 cache.attach("/person/test6", p);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 try
                 {
                    tx.rollback();
                 } catch (SystemException e)
                 {
                    e.printStackTrace();
                    t2_ex = e;
                 }
              }
  
              cache.attach("/person/test6", p);
  
           }
        };
  
        t1.start();
        t2.start();
  
        t1.join();
        t2.join();
  
        // t2 should rollback due to timeout while t2 should succeed
        if (t2_ex != null)
           fail("Thread1 failed: " + t2_ex);
        if (t1_ex != null)
           fail("Thread2 failed: " + t1_ex);
  
        int size = ((Person) cache.find("/person/test6")).getLanguages().size();
        assertEquals("number of languages", 1, size);
        size = ((Person) cache1.find("/person/test6")).getLanguages().size();
        assertEquals("number of languages", 1, size);
     }
  
     /**
      * Concurrent puts from the same cache instance (different threads) with rollback.
      */
     public void testConcurrentPuts1() throws Exception
     {
        Thread t1 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              try
              {
                 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
                 UserTransaction tx = getTransaction();
                 tx.begin();
                 lang.add("German");
                 TestingUtil.sleepThread(17000);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 t1_ex = ex;
              }
           }
        };
  
        Thread t2 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              UserTransaction tx = null;
              try
              {
                 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
                 List<String> lang = ((Person) cache1.find("/person/test6")).getLanguages();
                 tx = getTransaction();
                 tx.begin();
                 lang.add("English");
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 try
                 {
                    tx.rollback();
                 } catch (SystemException e)
                 {
                    e.printStackTrace();
                    t2_ex = e;
                 }
              }
           }
        };
  
        Person p = createPerson("/person/test6", "p6", 50);
        cache.attach("/person/test6", p);
        List<String> lang = new ArrayList<String>();
        lang.add("German");
        p.setLanguages(lang);
  
        t1.start();
        t2.start();
  
        t1.join();
        t2.join();
  
        // t2 should rollback due to timeout while t2 should succeed
        if (t2_ex != null)
           fail("Thread1 failed: " + t2_ex);
        if (t1_ex != null)
           fail("Thread2 failed: " + t1_ex);
  
        int size = ((Person) cache.find("/person/test6")).getLanguages().size();
        assertEquals("number of languages", 2, size);
        size = ((Person) cache1.find("/person/test6")).getLanguages().size();
        assertEquals("number of languages", 2, size);
     }
  
     /**
      * Concurrent puts from the different cache instances (different threads) with rollback.
      */
     public void testConcurrentPuts2() throws Exception
     {
        Thread t1 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              try
              {
                 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
                 UserTransaction tx = getTransaction();
                 tx.begin();
                 lang.add("German");
                 TestingUtil.sleepThread(17000);
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 t1_ex = ex;
              }
           }
        };
  
        Thread t2 = new Thread()
        {
           Transaction tx;
  
           public void run()
           {
              UserTransaction tx = null;
              try
              {
                 TestingUtil.sleepThread(1000); // give Thread1 time to createPerson
                 List<String> lang = ((Person) cache.find("/person/test6")).getLanguages();
                 tx = getTransaction();
                 tx.begin();
                 lang.add("English");
                 tx.commit();
              }
              catch (RollbackException rollback)
              {
                 ;
              }
              catch (Exception ex)
              {
                 try
                 {
                    tx.rollback();
                 } catch (SystemException e)
                 {
                    e.printStackTrace();
                    t2_ex = e;
                 }
              }
           }
        };
  
        Person p = createPerson("/person/test6", "p6", 50);
        cache.attach("/person/test6", p);
        List<String> lang = new ArrayList<String>();
        lang.add("German");
        p.setLanguages(lang);
  
        t1.start();
        t2.start();
  
        t1.join();
        t2.join();
  
        // t2 should rollback due to timeout while t2 should succeed
        if (t2_ex != null)
           fail("Thread1 failed: " + t2_ex);
        if (t1_ex != null)
           fail("Thread2 failed: " + t1_ex);
  
        int size = ((Person) cache.find("/person/test6")).getLanguages().size();
        assertEquals("number of languages", 2, size);
        size = ((Person) cache1.find("/person/test6")).getLanguages().size();
        assertEquals("number of languages", 2, size);
     }
  
     void log(String s)
     {
        long now;
        if (start == 0)
           start = System.currentTimeMillis();
        now = System.currentTimeMillis();
  
        System.out.println("[" + Thread.currentThread().getName() + "] [" + (now - start) + "] " + s);
     }
  
  
     public static Test suite() throws Exception
     {
        return new TestSuite(org.jboss.cache.pojo.rollback.ReplicatedTxTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(org.jboss.cache.pojo.rollback.ReplicatedTxTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:07;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/rollback/LocalUndoTest.java
  
  Index: LocalUndoTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.rollback;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor;
  import org.jboss.cache.pojo.interceptors.PojoFailedTxMockupInterceptor;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.aop.advice.Interceptor;
  import org.jboss.aop.Advised;
  
  import javax.transaction.TransactionManager;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class LocalUndoTest extends TestCase
  {
     Log log_ = LogFactory.getLog(LocalUndoTest.class);
     PojoCache cache_;
     TransactionManager tx_mgr;
  
     public LocalUndoTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
        tx_mgr = DummyTransactionManager.getInstance();
  
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     private void setTxRollback(boolean isTrue)
     {
        PojoFailedTxMockupInterceptor.TX_ROLLBACK = isTrue;
     }
  
     public void testSimpleTxWithRollback1() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
  
        setTxRollback(true);
        cache_.attach("/a", test);
        assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
     }
  
     private boolean hasCacheInterceptor(Object pojo)
     {
        Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
        for(int i=0; i < interceptors.length; i++)
        {
           if(interceptors[i] instanceof CacheFieldInterceptor)
              return true;
        }
        return false;
     }
  
     public void testSimpleTxWithRollback2() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("/a", test);
  
        setTxRollback(true);
        cache_.detach("/a");
  
        assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(LocalUndoTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(LocalUndoTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:07;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/rollback/ListUndoTest.java
  
  Index: ListUndoTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.rollback;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.interceptors.PojoFailedTxMockupInterceptor;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.aop.proxy.ClassProxy;
  
  import javax.transaction.TransactionManager;
  import java.util.ArrayList;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class ListUndoTest extends TestCase
  {
     Log log_ = LogFactory.getLog(ListUndoTest.class);
     PojoCache cache_;
     TransactionManager tx_mgr;
  
     public ListUndoTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
        tx_mgr = DummyTransactionManager.getInstance();
  
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     private void setTxRollback(boolean isTrue)
     {
        PojoFailedTxMockupInterceptor.TX_ROLLBACK = isTrue;
     }
  
     public void testSimple() throws Exception
     {
        ArrayList list = new ArrayList();
        list.add("test1");
  
        setTxRollback(true);
        cache_.attach("/a", list);
        assertFalse("Should not have cache interceptor ", isProxy(list));
  
        cache_.attach("/a", list);
     }
  
     public void testSimpleTxWithRollback1() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        ArrayList list = new ArrayList();
        list.add("English");
        test.setLanguages(list);
  
        setTxRollback(true);
        cache_.attach("/a", test);
        assertFalse("Should not have cache interceptor ", isProxy(test.getLanguages()));
  
        cache_.attach("/a", test);
     }
  
     private boolean isProxy(Object pojo)
     {
        if(pojo instanceof ClassProxy) return true;
        return false;
     }
  
     public void testSimpleTxWithRollback2() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        ArrayList list = new ArrayList();
        list.add("English");
        test.setLanguages(list);
  
        cache_.attach("/a", test);
  
        setTxRollback(true);
        cache_.detach("/a");
  
        assertTrue("Should still have cache interceptor ", isProxy(test.getLanguages()));
        cache_.detach("/a");
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ListUndoTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(ListUndoTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:07;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/rollback/MapTxUndoTest.java
  
  Index: MapTxUndoTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.rollback;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.aop.proxy.ClassProxy;
  
  import javax.transaction.TransactionManager;
  import java.util.HashMap;
  import java.util.Map;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class MapTxUndoTest extends TestCase
  {
     Log log_ = LogFactory.getLog(MapTxUndoTest.class);
     PojoCache cache_;
     TransactionManager tx_mgr;
  
     public MapTxUndoTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
        tx_mgr = DummyTransactionManager.getInstance();
  
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     public void testSimple() throws Exception
     {
        HashMap map = new HashMap();
        map.put("1", "test1");
  
        tx_mgr.begin();
        cache_.attach("/a", map);
        tx_mgr.getTransaction().rollback();
        assertFalse("Should not have cache interceptor ", isProxy(map));
  
        cache_.attach("/a", map);
     }
  
     public void testSimpleTxWithRollback1() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        HashMap map = new HashMap();
        map.put("1", "English");
        test.setHobbies(map);
  
        tx_mgr.begin();
        cache_.attach("/a", test);
        tx_mgr.getTransaction().rollback();
        assertFalse("Should not have cache interceptor ", isProxy(test.getHobbies()));
  
        cache_.attach("/a", test);
     }
  
     private boolean isProxy(Object pojo)
     {
        if(pojo instanceof ClassProxy) return true;
        return false;
     }
  
     public void testSimpleTxWithRollback2() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        HashMap map = new HashMap();
        map.put("1", "English");
        test.setHobbies(map);
  
        cache_.attach("/a", test);
  
        tx_mgr.begin();
        cache_.detach("/a");
        tx_mgr.getTransaction().rollback();
  
        assertTrue("Should still have cache interceptor ", isProxy(test.getHobbies()));
        cache_.detach("/a");
     }
  
     public void testSimpleTxWithRollback3() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        HashMap map = new HashMap();
        map.put("1", "English");
        test.setHobbies(map);
        tx_mgr.begin();
        cache_.attach("/a", test);
        cache_.detach("/a");
        tx_mgr.getTransaction().rollback();
  
        assertFalse("Should not have cache interceptor ", isProxy(test.getHobbies()));
     }
  
     /**
      * Contributed by Niztan Niv
      * @throws Exception
      */
     public void testNestedMapWithRollback() throws Exception
     {
         // 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
         Map indexMap = new HashMap();
         cache_.attach("objsIndex", indexMap);
         indexMap = (Map) cache_.find("objsIndex");
  
         // 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();
         indexMap.put(KEY, obj2);
         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);
     }
  
     public void testPlainMapRollback() throws Exception
     {
        // create cached data objects
        Map obj1 = new HashMap();
        obj1.put("id", "1");
        cache_.attach("objs/1", obj1);
        obj1 = (Map) cache_.find("objs/1");
  
        tx_mgr.begin();
        obj1.put("id", "newId");
        tx_mgr.rollback();
  
        assertEquals("1", obj1.get("id"));
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(MapTxUndoTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(MapTxUndoTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:07;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/rollback/MapUndoTest.java
  
  Index: MapUndoTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.rollback;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.interceptors.PojoFailedTxMockupInterceptor;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.aop.proxy.ClassProxy;
  
  import javax.transaction.TransactionManager;
  import java.util.HashMap;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class MapUndoTest extends TestCase
  {
     Log log_ = LogFactory.getLog(MapUndoTest.class);
     PojoCache cache_;
     TransactionManager tx_mgr;
  
     public MapUndoTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
        tx_mgr = DummyTransactionManager.getInstance();
  
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     private void setTxRollback(boolean isTrue)
     {
        PojoFailedTxMockupInterceptor.TX_ROLLBACK = isTrue;
     }
  
     public void testSimple() throws Exception
     {
        HashMap map = new HashMap();
        map.put("1", "test1");
  
        setTxRollback(true);
        cache_.attach("/a", map);
        assertFalse("Should not have cache interceptor ", isProxy(map));
  
        cache_.attach("/a", map);
     }
  
     public void testSimpleTxWithRollback1() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        HashMap map = new HashMap();
        map.put("1", "English");
        test.setHobbies(map);
  
        setTxRollback(true);
        cache_.attach("/a", test);
        assertFalse("Should not have cache interceptor ", isProxy(test.getHobbies()));
  
        cache_.attach("/a", test);
     }
  
     private boolean isProxy(Object pojo)
     {
        if(pojo instanceof ClassProxy) return true;
        return false;
     }
  
     public void testSimpleTxWithRollback2() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        HashMap map = new HashMap();
        map.put("1", "English");
        test.setHobbies(map);
  
        cache_.attach("/a", test);
  
        setTxRollback(true);
        cache_.detach("/a");
  
        assertTrue("Should still have cache interceptor ", isProxy(test.getHobbies()));
        cache_.detach("/a");
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(MapUndoTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(MapUndoTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:07;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/rollback/SetUndoTest.java
  
  Index: SetUndoTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.rollback;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.interceptors.PojoFailedTxMockupInterceptor;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.aop.proxy.ClassProxy;
  
  import javax.transaction.TransactionManager;
  import java.util.HashSet;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class SetUndoTest extends TestCase
  {
     Log log_ = LogFactory.getLog(SetUndoTest.class);
     PojoCache cache_;
     TransactionManager tx_mgr;
  
     public SetUndoTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
        tx_mgr = DummyTransactionManager.getInstance();
  
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     private void setTxRollback(boolean isTrue)
     {
        PojoFailedTxMockupInterceptor.TX_ROLLBACK = isTrue;
     }
  
     public void testSimple() throws Exception
     {
        HashSet set = new HashSet();
        set.add("test1");
  
        setTxRollback(true);
        cache_.attach("/a", set);
        assertFalse("Should not have cache interceptor ", isProxy(set));
  
        cache_.attach("/a", set);
     }
  
     public void testSimpleTxWithRollback1() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        HashSet set = new HashSet();
        set.add("English");
        test.setSkills(set);
  
        setTxRollback(true);
        cache_.attach("/a", test);
        assertFalse("Should not have cache interceptor ", isProxy(test.getSkills()));
  
        cache_.attach("/a", test);
     }
  
     private boolean isProxy(Object pojo)
     {
        if(pojo instanceof ClassProxy) return true;
        return false;
     }
  
     public void testSimpleTxWithRollback2() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        HashSet set = new HashSet();
        set.add("English");
        test.setSkills(set);
  
        cache_.attach("/a", test);
  
        setTxRollback(true);
        cache_.detach("/a");
  
        assertTrue("Should still have cache interceptor ", isProxy(test.getSkills()));
        cache_.detach("/a");
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(SetUndoTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(SetUndoTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:07;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/rollback/ListTxUndoTest.java
  
  Index: ListTxUndoTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.rollback;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.aop.proxy.ClassProxy;
  
  import javax.transaction.TransactionManager;
  import java.util.ArrayList;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class ListTxUndoTest extends TestCase
  {
     Log log_ = LogFactory.getLog(ListTxUndoTest.class);
     PojoCache cache_;
     TransactionManager tx_mgr;
  
     public ListTxUndoTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
        tx_mgr = DummyTransactionManager.getInstance();
  
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     public void testSimple() throws Exception
     {
        ArrayList list = new ArrayList();
        list.add("test1");
  
        tx_mgr.begin();
        cache_.attach("/a", list);
        tx_mgr.getTransaction().rollback();
        assertFalse("Should not have cache interceptor ", isProxy(list));
  
        cache_.attach("/a", list);
     }
  
     public void testSimpleTxWithRollback1() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        ArrayList list = new ArrayList();
        list.add("English");
        test.setLanguages(list);
  
        tx_mgr.begin();
        cache_.attach("/a", test);
        tx_mgr.getTransaction().rollback();
        assertFalse("Should not have cache interceptor ", isProxy(test.getLanguages()));
  
        cache_.attach("/a", test);
     }
  
     private boolean isProxy(Object pojo)
     {
        if(pojo instanceof ClassProxy) return true;
        return false;
     }
  
     public void testSimpleTxWithRollback2() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        ArrayList list = new ArrayList();
        list.add("English");
        test.setLanguages(list);
  
        cache_.attach("/a", test);
  
        tx_mgr.begin();
        cache_.detach("/a");
        tx_mgr.getTransaction().rollback();
  
        assertTrue("Should still have cache interceptor ", isProxy(test.getLanguages()));
        cache_.detach("/a");
     }
  
     public void testSimpleTxWithRollback3() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        ArrayList list = new ArrayList();
        list.add("English");
        test.setLanguages(list);
        tx_mgr.begin();
        cache_.attach("/a", test);
        cache_.detach("/a");
        tx_mgr.getTransaction().rollback();
  
        assertFalse("Should not have cache interceptor ", isProxy(test.getLanguages()));
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(ListTxUndoTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(ListTxUndoTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:07;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/rollback/LocalTxUndoTest.java
  
  Index: LocalTxUndoTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.rollback;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.aop.Advised;
  import org.jboss.aop.advice.Interceptor;
  
  import javax.transaction.TransactionManager;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class LocalTxUndoTest extends TestCase
  {
     Log log_ = LogFactory.getLog(LocalTxUndoTest.class);
     PojoCache cache_;
     TransactionManager tx_mgr;
  
     public LocalTxUndoTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
        tx_mgr = DummyTransactionManager.getInstance();
  
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     public void testSimpleTxWithRollback1() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
  
        tx_mgr.begin();
        cache_.attach("/a", test);
        tx_mgr.getTransaction().rollback();
        assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
  
        cache_.attach("/a", test);
     }
  
     private boolean hasCacheInterceptor(Object pojo)
     {
        Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
        for(int i=0; i < interceptors.length; i++)
        {
           if(interceptors[i] instanceof CacheFieldInterceptor)
              return true;
        }
        return false;
     }
  
     public void testSimpleTxWithRollback2() throws Exception
     {
        log_.info("testSimpleTxWithRollback2() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("/a", test);
  
        tx_mgr.begin();
        cache_.detach("/a");
        tx_mgr.getTransaction().rollback();
  
        assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
        cache_.detach("/a");
     }
  
     public void testSimpleTxWithRollback3() throws Exception
     {
        log_.info("testSimpleTxWithRollback3() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        tx_mgr.begin();
        cache_.attach("/a", test);
        cache_.detach("/a");
        tx_mgr.getTransaction().rollback();
  
        assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(LocalTxUndoTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(LocalTxUndoTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:07;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/rollback/LocalExceptionUndoTest.java
  
  Index: LocalExceptionUndoTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source.
   * Copyright 2006, Red Hat Middleware LLC, and individual contributors
   * as indicated by the @author tags. See the copyright.txt file in the
   * distribution for a full listing of individual contributors.
   *
   * This is free software; you can redistribute it and/or modify it
   * under the terms of the GNU Lesser General Public License as
   * published by the Free Software Foundation; either version 2.1 of
   * the License, or (at your option) any later version.
   *
   * This software is distributed in the hope that it will be useful,
   * but WITHOUT ANY WARRANTY; without even the implied warranty of
   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
   * Lesser General Public License for more details.
   *
   * You should have received a copy of the GNU Lesser General Public
   * License along with this software; if not, write to the Free
   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
   */
  
  package org.jboss.cache.pojo.rollback;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.InternalConstant;
  import org.jboss.cache.pojo.PojoCacheException;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.interceptors.PojoFailedTxMockupInterceptor;
  import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.cache.Fqn;
  import org.jboss.aop.advice.Interceptor;
  import org.jboss.aop.Advised;
  
  import javax.transaction.TransactionManager;
  import java.util.ArrayList;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class LocalExceptionUndoTest extends TestCase
  {
     Log log_ = LogFactory.getLog(LocalExceptionUndoTest.class);
     PojoCache cache_;
     TransactionManager tx_mgr;
     boolean isTrue = false;
  
     public LocalExceptionUndoTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.getCache().getConfiguration().setLockAcquisitionTimeout(500); // timeout to 500 ms
        cache_.start();
        tx_mgr = DummyTransactionManager.getInstance();
     }
  
     private void startLockThread() throws Exception
     {
        // Lock thread to lock underlying node.
        (new LockThread()).start();
        Thread.sleep(300);
     }
  
     private void stopLockThread() throws Exception
     {
        isTrue = true;
        Thread.sleep(200);
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
     public void testSimpleTxWithRollback1() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        ArrayList list = new ArrayList();
        list.add("English");
        test.setLanguages(list);
  
        startLockThread();
  
        try {
           cache_.attach("/a", test);
        } catch (PojoCacheException ex)
        {
  //         ex.printStackTrace();
        }
        assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
        assertNull("Should be null", cache_.find("/a"));
  
        stopLockThread();
        cache_.attach("/a", test);
     }
  
     public void testSimpleTxWithRollback2() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        ArrayList list = new ArrayList();
        list.add("English");
        test.setLanguages(list);
  
        try {
           cache_.attach("/a", test);
        } catch (PojoCacheException ex)
        {
  //         ex.printStackTrace();
        }
  
        startLockThread();
  
        try {
           cache_.detach("/a");
        } catch (PojoCacheException ex)
        {
  //         ex.printStackTrace();
        }
  
        stopLockThread();
        assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
        cache_.detach("/a");
        assertNull("Should be null", cache_.find("/a"));
     }
  
     private boolean hasCacheInterceptor(Object pojo)
     {
        Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
        for(int i=0; i < interceptors.length; i++)
        {
           if(interceptors[i] instanceof CacheFieldInterceptor)
              return true;
        }
        return false;
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(LocalExceptionUndoTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(suite());
     }
  
     public class LockThread extends Thread
     {
        public void run()
        {
           try
           {
              Fqn f = new Fqn(InternalConstant.JBOSS_INTERNAL_STRING);
              cache_.getCache().put(new Fqn(f, "123"), "key", "test");
              cache_.getCache().put(new Fqn("a"), "key", "test");
              tx_mgr.begin();
              cache_.getCache().put(new Fqn(f, "124"), "key", "test");
  
              while (!isTrue)
              {
                 sleep(100);
              }
              tx_mgr.commit();
           } catch (Exception ex)
           {
              ex.printStackTrace();
           }
  
        }
     }
  }
  
  
  
  1.1      date: 2007/01/13 15:55:07;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/rollback/SetTxUndoTest.java
  
  Index: SetTxUndoTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.rollback;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.aop.proxy.ClassProxy;
  
  import javax.transaction.TransactionManager;
  import java.util.HashSet;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class SetTxUndoTest extends TestCase
  {
     Log log_ = LogFactory.getLog(SetTxUndoTest.class);
     PojoCache cache_;
     TransactionManager tx_mgr;
  
     public SetTxUndoTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
        tx_mgr = DummyTransactionManager.getInstance();
  
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
  
     public void testSimple() throws Exception
     {
        HashSet set = new HashSet();
        set.add("test1");
  
        tx_mgr.begin();
        cache_.attach("/a", set);
        tx_mgr.getTransaction().rollback();
        assertFalse("Should not have cache interceptor ", isProxy(set));
  
        cache_.attach("/a", set);
     }
  
     public void testSimpleTxWithRollback1() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        HashSet set = new HashSet();
        set.add("English");
        test.setSkills(set);
  
        tx_mgr.begin();
        cache_.attach("/a", test);
        tx_mgr.getTransaction().rollback();
        assertFalse("Should not have cache interceptor ", isProxy(test.getSkills()));
  
        cache_.attach("/a", test);
     }
  
     private boolean isProxy(Object pojo)
     {
        if(pojo instanceof ClassProxy) return true;
        return false;
     }
  
     public void testSimpleTxWithRollback2() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        HashSet set = new HashSet();
        set.add("English");
        test.setSkills(set);
  
        cache_.attach("/a", test);
  
        tx_mgr.begin();
        cache_.detach("/a");
        tx_mgr.getTransaction().rollback();
  
        assertTrue("Should still have cache interceptor ", isProxy(test.getSkills()));
        cache_.detach("/a");
     }
  
     public void testSimpleTxWithRollback3() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        HashSet set = new HashSet();
        set.add("English");
        test.setSkills(set);
        tx_mgr.begin();
        cache_.attach("/a", test);
        cache_.detach("/a");
        tx_mgr.getTransaction().rollback();
  
        assertFalse("Should not have cache interceptor ", isProxy(test.getSkills()));
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(SetTxUndoTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(SetTxUndoTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:07;  author: bwang;  state: Exp;JBossCache/tests/functional/org/jboss/cache/pojo/rollback/InMemoryTxUndoTest.java
  
  Index: InMemoryTxUndoTest.java
  ===================================================================
  /*
   * JBoss, Home of Professional Open Source
   *
   * Distributable under LGPL license.
   * See terms of license at gnu.org.
   */
  
  package org.jboss.cache.pojo.rollback;
  
  import junit.framework.TestCase;
  import junit.framework.Test;
  import junit.framework.TestSuite;
  import org.apache.commons.logging.Log;
  import org.apache.commons.logging.LogFactory;
  import org.jboss.cache.pojo.PojoCache;
  import org.jboss.cache.pojo.PojoCacheFactory;
  import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor;
  import org.jboss.cache.pojo.test.Person;
  import org.jboss.cache.pojo.test.Address;
  import org.jboss.cache.transaction.DummyTransactionManager;
  import org.jboss.aop.advice.Interceptor;
  import org.jboss.aop.Advised;
  
  import javax.transaction.TransactionManager;
  import java.util.ArrayList;
  
  /**
   * Additional basic tests
   *
   * @author Ben Wang
   */
  
  public class InMemoryTxUndoTest extends TestCase
  {
     Log log_ = LogFactory.getLog(InMemoryTxUndoTest.class);
     PojoCache cache_;
     TransactionManager tx_mgr;
  
     public InMemoryTxUndoTest(String name)
     {
        super(name);
     }
  
     protected void setUp() throws Exception
     {
        super.setUp();
        log_.info("setUp() ....");
        String configFile = "META-INF/local-service.xml";
        boolean toStart = false;
        cache_ = PojoCacheFactory.createCache(configFile, toStart);
        cache_.start();
        tx_mgr = DummyTransactionManager.getInstance();
  
     }
  
     protected void tearDown() throws Exception
     {
        super.tearDown();
        cache_.stop();
     }
  
  //   public void testDummy() {}
     private boolean hasCacheInterceptor(Object pojo)
     {
        Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
        for(int i=0; i < interceptors.length; i++)
        {
           if(interceptors[i] instanceof CacheFieldInterceptor)
              return true;
        }
        return false;
     }
  
     public void testSimpleTxWithRollback1() throws Exception
     {
        log_.info("testSimpleTxWithRollback1() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        tx_mgr.begin();
        cache_.attach("/a", test);
        test.setAge(20);
        tx_mgr.getTransaction().rollback();
  
        assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
        assertEquals("Should still be ", 10, test.getAge());
     }
  
     public void testSimpleTxWithRollback2() throws Exception
     {
        log_.info("testSimpleTxWithRollback2() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("/a", test);
  
        tx_mgr.begin();
        test.setAge(20);
        tx_mgr.getTransaction().rollback();
        assertEquals("Should still be ", 10, test.getAge());
     }
  
     public void testSimpleTxWithRollback3() throws Exception
     {
        log_.info("testSimpleTxWithRollback3() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("/a", test);
  
        Person test1 = new Person();
        test1.setName("Irin");
        test1.setAge(30);
        Address addr = new Address();
        addr.setCity("Taipei");
        test1.setAddress(addr);
        tx_mgr.begin();
        cache_.attach("/a", test1);
        tx_mgr.getTransaction().rollback();
  
        assertEquals("Should still be ", 10, test.getAge());
        assertNull("Address should be ", test.getAddress());
     }
  
     public void testSimpleTxWithRollback4() throws Exception
     {
        log_.info("testSimpleTxWithRollback4() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("/a", test);
        Address addr = new Address();
        addr.setCity("Taipei");
        test.setAddress(addr);
  
        tx_mgr.begin();
        addr.setCity("Tainan");
        tx_mgr.getTransaction().rollback();
  
        assertEquals("Should still be ", "Taipei", test.getAddress().getCity());
     }
  
     public void testSimpleTxWithRollback5() throws Exception
     {
        log_.info("testSimpleTxWithRollback5() ....");
        Person test = new Person();
        test.setName("Ben");
        test.setAge(10);
        cache_.attach("/a", test);
  
        ArrayList lang = new ArrayList();
        lang.add("English");
        test.setLanguages(lang);
        tx_mgr.begin();
        lang.add("French");
        tx_mgr.getTransaction().rollback();
  
        assertEquals("Should still be ", 1, test.getLanguages().size());
     }
  
     public static Test suite() throws Exception
     {
        return new TestSuite(InMemoryTxUndoTest.class);
     }
  
  
     public static void main(String[] args) throws Exception
     {
        junit.textui.TestRunner.run(InMemoryTxUndoTest.suite());
     }
  
  }
  
  
  
  1.1      date: 2007/01/13 15:55:07;  author: bwang;  state: Exp;JBossCache/tests/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.createCache(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