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

Ben Wang bwang at jboss.com
Fri Dec 29 23:07:54 EST 2006


  User: bwang   
  Date: 06/12/29 23:07:54

  Added:       tests-50/functional/org/jboss/cache/pojo/rollback 
                        LocalExceptionUndoTest.java
  Log:
  Created to test lock time out exception for PojoCache rollback.
  
  Revision  Changes    Path
  1.1      date: 2006/12/30 04:07:54;  author: bwang;  state: Exp;JBossCache/tests-50/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();
           }
  
        }
     }
  }
  
  
  



More information about the jboss-cvs-commits mailing list