1 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 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 public LocalExceptionUndoTest(String name) 59 { 60 super(name); 61 } 62 63 protected void setUp() throws Exception 64 { 65 super.setUp(); 66 log_.info("setUp() ...."); 67 String configFile = "META-INF/local-service.xml"; 68 boolean toStart = false; 69 cache_ = PojoCacheFactory.createCache(configFile, toStart); 70 cache_.getCache().getConfiguration().setLockAcquisitionTimeout(500); cache_.start(); 72 tx_mgr = DummyTransactionManager.getInstance(); 73 } 74 75 private void startLockThread() throws Exception 76 { 77 (new LockThread()).start(); 79 Thread.sleep(300); 80 } 81 82 private void stopLockThread() throws Exception 83 { 84 isTrue = true; 85 Thread.sleep(200); 86 } 87 88 protected void tearDown() throws Exception 89 { 90 super.tearDown(); 91 cache_.stop(); 92 } 93 94 public void testSimpleTxWithRollback1() throws Exception 95 { 96 log_.info("testSimpleTxWithRollback1() ...."); 97 Person test = new Person(); 98 test.setName("Ben"); 99 test.setAge(10); 100 ArrayList list = new ArrayList (); 101 list.add("English"); 102 test.setLanguages(list); 103 104 startLockThread(); 105 106 try { 107 cache_.attach("/a", test); 108 } catch (PojoCacheException ex) 109 { 110 } 112 assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test)); 113 assertNull("Should be null", cache_.find("/a")); 114 115 stopLockThread(); 116 cache_.attach("/a", test); 117 } 118 119 public void testSimpleTxWithRollback2() throws Exception 120 { 121 log_.info("testSimpleTxWithRollback1() ...."); 122 Person test = new Person(); 123 test.setName("Ben"); 124 test.setAge(10); 125 ArrayList list = new ArrayList (); 126 list.add("English"); 127 test.setLanguages(list); 128 129 try { 130 cache_.attach("/a", test); 131 } catch (PojoCacheException ex) 132 { 133 } 135 136 startLockThread(); 137 138 try { 139 cache_.detach("/a"); 140 } catch (PojoCacheException ex) 141 { 142 } 144 145 stopLockThread(); 146 assertTrue("Should still have cache interceptor ", hasCacheInterceptor(test)); 147 cache_.detach("/a"); 148 assertNull("Should be null", cache_.find("/a")); 149 } 150 151 private boolean hasCacheInterceptor(Object pojo) 152 { 153 Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors(); 154 for(int i=0; i < interceptors.length; i++) 155 { 156 if(interceptors[i] instanceof CacheFieldInterceptor) 157 return true; 158 } 159 return false; 160 } 161 162 public static Test suite() throws Exception 163 { 164 return new TestSuite(LocalExceptionUndoTest.class); 165 } 166 167 168 public static void main(String [] args) throws Exception 169 { 170 junit.textui.TestRunner.run(suite()); 171 } 172 173 public class LockThread extends Thread 174 { 175 public void run() 176 { 177 try 178 { 179 Fqn f = new Fqn(InternalConstant.JBOSS_INTERNAL_STRING); 180 cache_.getCache().put(new Fqn(f, "123"), "key", "test"); 181 cache_.getCache().put(new Fqn("a"), "key", "test"); 182 tx_mgr.begin(); 183 cache_.getCache().put(new Fqn(f, "124"), "key", "test"); 184 185 while (!isTrue) 186 { 187 sleep(100); 188 } 189 tx_mgr.commit(); 190 } catch (Exception ex) 191 { 192 ex.printStackTrace(); 193 } 194 195 } 196 } 197 } 198 | Popular Tags |