LocalTxUndoTest.java :  » JBoss » JBossCache » org » jboss » cache » pojo » rollback » Java Open Source

Java Open Source » JBoss » JBossCache 
JBossCache » org » jboss » cache » pojo » rollback » 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.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jboss.aop.Advised;
import org.jboss.aop.advice.Interceptor;
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.transaction.DummyTransactionManager;

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());
   }

}
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.