1 7 8 package org.jboss.cache.pojo.rollback; 9 10 import junit.framework.TestCase; 11 import junit.framework.Test; 12 import junit.framework.TestSuite; 13 import org.apache.commons.logging.Log; 14 import org.apache.commons.logging.LogFactory; 15 import org.jboss.cache.pojo.PojoCache; 16 import org.jboss.cache.pojo.PojoCacheFactory; 17 import org.jboss.cache.pojo.interceptors.dynamic.CacheFieldInterceptor; 18 import org.jboss.cache.pojo.test.Person; 19 import org.jboss.cache.pojo.test.Address; 20 import org.jboss.cache.transaction.DummyTransactionManager; 21 import org.jboss.aop.advice.Interceptor; 22 import org.jboss.aop.Advised; 23 24 import javax.transaction.TransactionManager ; 25 import java.util.ArrayList ; 26 27 32 33 public class InMemoryTxUndoTest extends TestCase 34 { 35 Log log_ = LogFactory.getLog(InMemoryTxUndoTest.class); 36 PojoCache cache_; 37 TransactionManager tx_mgr; 38 39 public InMemoryTxUndoTest(String name) 40 { 41 super(name); 42 } 43 44 protected void setUp() throws Exception 45 { 46 super.setUp(); 47 log_.info("setUp() ...."); 48 String configFile = "META-INF/local-service.xml"; 49 boolean toStart = false; 50 cache_ = PojoCacheFactory.createCache(configFile, toStart); 51 cache_.start(); 52 tx_mgr = DummyTransactionManager.getInstance(); 53 54 } 55 56 protected void tearDown() throws Exception 57 { 58 super.tearDown(); 59 cache_.stop(); 60 } 61 62 private boolean hasCacheInterceptor(Object pojo) 64 { 65 Interceptor[] interceptors = ((Advised)pojo)._getInstanceAdvisor().getInterceptors(); 66 for(int i=0; i < interceptors.length; i++) 67 { 68 if(interceptors[i] instanceof CacheFieldInterceptor) 69 return true; 70 } 71 return false; 72 } 73 74 public void testSimpleTxWithRollback1() throws Exception 75 { 76 log_.info("testSimpleTxWithRollback1() ...."); 77 Person test = new Person(); 78 test.setName("Ben"); 79 test.setAge(10); 80 tx_mgr.begin(); 81 cache_.attach("/a", test); 82 test.setAge(20); 83 tx_mgr.getTransaction().rollback(); 84 85 assertFalse("Should not have cache interceptor ", hasCacheInterceptor(test)); 86 assertEquals("Should still be ", 10, test.getAge()); 87 } 88 89 public void testSimpleTxWithRollback2() throws Exception 90 { 91 log_.info("testSimpleTxWithRollback2() ...."); 92 Person test = new Person(); 93 test.setName("Ben"); 94 test.setAge(10); 95 cache_.attach("/a", test); 96 97 tx_mgr.begin(); 98 test.setAge(20); 99 tx_mgr.getTransaction().rollback(); 100 assertEquals("Should still be ", 10, test.getAge()); 101 } 102 103 public void testSimpleTxWithRollback3() throws Exception 104 { 105 log_.info("testSimpleTxWithRollback3() ...."); 106 Person test = new Person(); 107 test.setName("Ben"); 108 test.setAge(10); 109 cache_.attach("/a", test); 110 111 Person test1 = new Person(); 112 test1.setName("Irin"); 113 test1.setAge(30); 114 Address addr = new Address(); 115 addr.setCity("Taipei"); 116 test1.setAddress(addr); 117 tx_mgr.begin(); 118 cache_.attach("/a", test1); 119 tx_mgr.getTransaction().rollback(); 120 121 assertEquals("Should still be ", 10, test.getAge()); 122 assertNull("Address should be ", test.getAddress()); 123 } 124 125 public void testSimpleTxWithRollback4() throws Exception 126 { 127 log_.info("testSimpleTxWithRollback4() ...."); 128 Person test = new Person(); 129 test.setName("Ben"); 130 test.setAge(10); 131 cache_.attach("/a", test); 132 Address addr = new Address(); 133 addr.setCity("Taipei"); 134 test.setAddress(addr); 135 136 tx_mgr.begin(); 137 addr.setCity("Tainan"); 138 tx_mgr.getTransaction().rollback(); 139 140 assertEquals("Should still be ", "Taipei", test.getAddress().getCity()); 141 } 142 143 public void testSimpleTxWithRollback5() throws Exception 144 { 145 log_.info("testSimpleTxWithRollback5() ...."); 146 Person test = new Person(); 147 test.setName("Ben"); 148 test.setAge(10); 149 cache_.attach("/a", test); 150 151 ArrayList lang = new ArrayList (); 152 lang.add("English"); 153 test.setLanguages(lang); 154 tx_mgr.begin(); 155 lang.add("French"); 156 tx_mgr.getTransaction().rollback(); 157 158 assertEquals("Should still be ", 1, test.getLanguages().size()); 159 } 160 161 public static Test suite() throws Exception 162 { 163 return new TestSuite(InMemoryTxUndoTest.class); 164 } 165 166 167 public static void main(String [] args) throws Exception 168 { 169 junit.textui.TestRunner.run(InMemoryTxUndoTest.suite()); 170 } 171 172 } 173 | Popular Tags |