Clover coverage report -
Coverage timestamp: Wed Jan 31 2007 15:38:53 EST
file stats: LOC: 198   Methods: 11
NCLOC: 142   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
LocalExceptionUndoTest.java 83.3% 96.8% 90.9% 95%
coverage coverage
 1    /*
 2    * JBoss, Home of Professional Open Source.
 3    * Copyright 2006, Red Hat Middleware LLC, and individual contributors
 4    * as indicated by the @author tags. See the copyright.txt file in the
 5    * distribution for a full listing of individual contributors.
 6    *
 7    * This is free software; you can redistribute it and/or modify it
 8    * under the terms of the GNU Lesser General Public License as
 9    * published by the Free Software Foundation; either version 2.1 of
 10    * the License, or (at your option) any later version.
 11    *
 12    * This software is distributed in the hope that it will be useful,
 13    * but WITHOUT ANY WARRANTY; without even the implied warranty of
 14    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 15    * Lesser General Public License for more details.
 16    *
 17    * You should have received a copy of the GNU Lesser General Public
 18    * License along with this software; if not, write to the Free
 19    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 20    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 21    */
 22   
 23    package org.jboss.cache.pojo.rollback;
 24   
 25    import junit.framework.TestCase;
 26    import junit.framework.Test;
 27    import junit.framework.TestSuite;
 28    import org.apache.commons.logging.Log;
 29    import org.apache.commons.logging.LogFactory;
 30    import org.jboss.cache.pojo.PojoCache;
 31    import org.jboss.cache.pojo.PojoCacheFactory;
 32    import org.jboss.cache.pojo.InternalConstant;
 33    import org.jboss.cache.pojo.PojoCacheException;
 34    import org.jboss.cache.pojo.test.Person;
 35    import org.jboss.cache.pojo.interceptors.PojoFailedTxMockupInterceptor;
 36    import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor;
 37    import org.jboss.cache.transaction.DummyTransactionManager;
 38    import org.jboss.cache.Fqn;
 39    import org.jboss.aop.advice.Interceptor;
 40    import org.jboss.aop.Advised;
 41   
 42    import javax.transaction.TransactionManager;
 43    import java.util.ArrayList;
 44   
 45    /**
 46    * Additional basic tests
 47    *
 48    * @author Ben Wang
 49    */
 50   
 51    public class LocalExceptionUndoTest extends TestCase
 52    {
 53    Log log_ = LogFactory.getLog(LocalExceptionUndoTest.class);
 54    PojoCache cache_;
 55    TransactionManager tx_mgr;
 56    boolean isTrue = false;
 57   
 58  4 public LocalExceptionUndoTest(String name)
 59    {
 60  4 super(name);
 61    }
 62   
 63  4 protected void setUp() throws Exception
 64    {
 65  4 super.setUp();
 66  4 log_.info("setUp() ....");
 67  4 String configFile = "META-INF/local-service.xml";
 68  4 boolean toStart = false;
 69  4 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 70  4 cache_.getCache().getConfiguration().setLockAcquisitionTimeout(500); // timeout to 500 ms
 71  4 cache_.getCache().getConfiguration().setLockParentForChildInsertRemove(true); // test written to expect this
 72  4 cache_.start();
 73  4 tx_mgr = DummyTransactionManager.getInstance();
 74    }
 75   
 76  4 private void startLockThread() throws Exception
 77    {
 78    // Lock thread to lock underlying node.
 79  4 (new LockThread()).start();
 80  4 Thread.sleep(300);
 81    }
 82   
 83  4 private void stopLockThread() throws Exception
 84    {
 85  4 isTrue = true;
 86  4 Thread.sleep(200);
 87    }
 88   
 89  4 protected void tearDown() throws Exception
 90    {
 91  4 super.tearDown();
 92  4 cache_.stop();
 93    }
 94   
 95  2 public void testSimpleTxWithRollback1() throws Exception
 96    {
 97  2 log_.info("testSimpleTxWithRollback1() ....");
 98  2 Person test = new Person();
 99  2 test.setName("Ben");
 100  2 test.setAge(10);
 101  2 ArrayList list = new ArrayList();
 102  2 list.add("English");
 103  2 test.setLanguages(list);
 104   
 105  2 startLockThread();
 106   
 107  2 try {
 108  2 cache_.attach("/a", test);
 109    } catch (PojoCacheException ex)
 110    {
 111    // ex.printStackTrace();
 112    }
 113  2 assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
 114  2 assertNull("Should be null", cache_.find("/a"));
 115   
 116  2 stopLockThread();
 117  2 cache_.attach("/a", test);
 118    }
 119   
 120  2 public void testSimpleTxWithRollback2() throws Exception
 121    {
 122  2 log_.info("testSimpleTxWithRollback1() ....");
 123  2 Person test = new Person();
 124  2 test.setName("Ben");
 125  2 test.setAge(10);
 126  2 ArrayList list = new ArrayList();
 127  2 list.add("English");
 128  2 test.setLanguages(list);
 129   
 130  2 try {
 131  2 cache_.attach("/a", test);
 132    } catch (PojoCacheException ex)
 133    {
 134    // ex.printStackTrace();
 135    }
 136   
 137  2 startLockThread();
 138   
 139  2 try {
 140  2 cache_.detach("/a");
 141    } catch (PojoCacheException ex)
 142    {
 143    // ex.printStackTrace();
 144    }
 145   
 146  2 stopLockThread();
 147  2 assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
 148  2 cache_.detach("/a");
 149  2 assertNull("Should be null", cache_.find("/a"));
 150    }
 151   
 152  4 private boolean hasCacheInterceptor(Object pojo)
 153    {
 154  4 Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors();
 155  4 for(int i=0; i < interceptors.length; i++)
 156    {
 157  2 if(interceptors[i] instanceof CacheFieldInterceptor)
 158  2 return true;
 159    }
 160  2 return false;
 161    }
 162   
 163  2 public static Test suite() throws Exception
 164    {
 165  2 return new TestSuite(LocalExceptionUndoTest.class);
 166    }
 167   
 168   
 169  0 public static void main(String[] args) throws Exception
 170    {
 171  0 junit.textui.TestRunner.run(suite());
 172    }
 173   
 174    public class LockThread extends Thread
 175    {
 176  4 public void run()
 177    {
 178  4 try
 179    {
 180  4 Fqn f = new Fqn(InternalConstant.JBOSS_INTERNAL_STRING);
 181  4 cache_.getCache().put(new Fqn(f, "123"), "key", "test");
 182  4 cache_.getCache().put(new Fqn("a"), "key", "test");
 183  4 tx_mgr.begin();
 184  4 cache_.getCache().put(new Fqn(f, "124"), "key", "test");
 185   
 186  4 while (!isTrue)
 187    {
 188  40 sleep(100);
 189    }
 190  4 tx_mgr.commit();
 191    } catch (Exception ex)
 192    {
 193  0 ex.printStackTrace();
 194    }
 195   
 196    }
 197    }
 198    }