Clover coverage report -
Coverage timestamp: Thu Jul 5 2007 20:02:32 EDT
file stats: LOC: 204   Methods: 11
NCLOC: 148   Classes: 2
 
 Source file Conditionals Statements Methods TOTAL
LocalExceptionUndoTest.java 66.7% 92.1% 90.9% 90%
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.Test;
 26    import junit.framework.TestCase;
 27    import junit.framework.TestSuite;
 28    import org.apache.commons.logging.Log;
 29    import org.apache.commons.logging.LogFactory;
 30    import org.jboss.aop.Advised;
 31    import org.jboss.aop.advice.Interceptor;
 32    import org.jboss.cache.Fqn;
 33    import org.jboss.cache.pojo.PojoCache;
 34    import org.jboss.cache.pojo.PojoCacheException;
 35    import org.jboss.cache.pojo.PojoCacheFactory;
 36    import org.jboss.cache.pojo.impl.InternalConstant;
 37    import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor;
 38    import org.jboss.cache.pojo.test.Person;
 39    import org.jboss.cache.transaction.DummyTransactionManager;
 40   
 41    import javax.transaction.TransactionManager;
 42    import java.util.ArrayList;
 43   
 44    /**
 45    * Additional basic tests
 46    *
 47    * @author Ben Wang
 48    */
 49   
 50    public class LocalExceptionUndoTest extends TestCase
 51    {
 52    Log log_ = LogFactory.getLog(LocalExceptionUndoTest.class);
 53    PojoCache cache_;
 54    TransactionManager tx_mgr;
 55    boolean isTrue = false;
 56   
 57  2 public LocalExceptionUndoTest(String name)
 58    {
 59  2 super(name);
 60    }
 61   
 62  2 protected void setUp() throws Exception
 63    {
 64  2 super.setUp();
 65  2 log_.info("setUp() ....");
 66  2 String configFile = "META-INF/local-service.xml";
 67  2 boolean toStart = false;
 68  2 cache_ = PojoCacheFactory.createCache(configFile, toStart);
 69  2 cache_.getCache().getConfiguration().setLockAcquisitionTimeout(500); // timeout to 500 ms
 70  2 cache_.getCache().getConfiguration().setLockParentForChildInsertRemove(true); // test written to expect this
 71  2 cache_.start();
 72  2 tx_mgr = DummyTransactionManager.getInstance();
 73    }
 74   
 75  2 private void startLockThread() throws Exception
 76    {
 77    // Lock thread to lock underlying node.
 78  2 (new LockThread()).start();
 79  2 Thread.sleep(300);
 80    }
 81   
 82  1 private void stopLockThread() throws Exception
 83    {
 84  1 isTrue = true;
 85  1 Thread.sleep(200);
 86    }
 87   
 88  2 protected void tearDown() throws Exception
 89    {
 90  2 super.tearDown();
 91  2 cache_.stop();
 92    }
 93   
 94  1 public void testSimpleTxWithRollback1() throws Exception
 95    {
 96  1 log_.info("testSimpleTxWithRollback1() ....");
 97  1 Person test = new Person();
 98  1 test.setName("Ben");
 99  1 test.setAge(10);
 100  1 ArrayList list = new ArrayList();
 101  1 list.add("English");
 102  1 test.setLanguages(list);
 103   
 104  1 startLockThread();
 105   
 106  1 try
 107    {
 108  1 cache_.attach("/a", test);
 109    }
 110    catch (PojoCacheException ex)
 111    {
 112    // ex.printStackTrace();
 113    }
 114  1 assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test));
 115  0 assertNull("Should be null", cache_.find("/a"));
 116   
 117  0 stopLockThread();
 118  0 cache_.attach("/a", test);
 119    }
 120   
 121  1 public void testSimpleTxWithRollback2() throws Exception
 122    {
 123  1 log_.info("testSimpleTxWithRollback1() ....");
 124  1 Person test = new Person();
 125  1 test.setName("Ben");
 126  1 test.setAge(10);
 127  1 ArrayList list = new ArrayList();
 128  1 list.add("English");
 129  1 test.setLanguages(list);
 130   
 131  1 try
 132    {
 133  1 cache_.attach("/a", test);
 134    }
 135    catch (PojoCacheException ex)
 136    {
 137    // ex.printStackTrace();
 138    }
 139   
 140  1 startLockThread();
 141   
 142  1 try
 143    {
 144  1 cache_.detach("/a");
 145    }
 146    catch (PojoCacheException ex)
 147    {
 148    // ex.printStackTrace();
 149    }
 150   
 151  1 stopLockThread();
 152  1 assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test));
 153  1 cache_.detach("/a");
 154  1 assertNull("Should be null", cache_.find("/a"));
 155    }
 156   
 157  2 private boolean hasCacheInterceptor(Object pojo)
 158    {
 159  2 Interceptor[] interceptors = ((Advised) pojo)._getInstanceAdvisor().getInterceptors();
 160  2 for (int i = 0; i < interceptors.length; i++)
 161    {
 162  2 if (interceptors[i] instanceof CacheFieldInterceptor)
 163  2 return true;
 164    }
 165  0 return false;
 166    }
 167   
 168  1 public static Test suite() throws Exception
 169    {
 170  1 return new TestSuite(LocalExceptionUndoTest.class);
 171    }
 172   
 173   
 174  0 public static void main(String[] args) throws Exception
 175    {
 176  0 junit.textui.TestRunner.run(suite());
 177    }
 178   
 179    public class LockThread extends Thread
 180    {
 181  2 public void run()
 182    {
 183  2 try
 184    {
 185  2 Fqn f = new Fqn(InternalConstant.JBOSS_INTERNAL_STRING);
 186  2 cache_.getCache().put(new Fqn(f, "123"), "key", "test");
 187  2 cache_.getCache().put(new Fqn("a"), "key", "test");
 188  2 tx_mgr.begin();
 189  2 cache_.getCache().put(new Fqn(f, "124"), "key", "test");
 190   
 191  1 while (!isTrue)
 192    {
 193  10 sleep(100);
 194    }
 195  1 tx_mgr.commit();
 196    }
 197    catch (Exception ex)
 198    {
 199  1 ex.printStackTrace();
 200    }
 201   
 202    }
 203    }
 204    }