1 16 17 package org.springframework.aop.framework.autoproxy.metadata; 18 19 import java.io.IOException ; 20 21 import javax.servlet.ServletException ; 22 23 import junit.framework.TestCase; 24 25 import org.springframework.aop.framework.Advised; 26 import org.springframework.aop.support.AopUtils; 27 import org.springframework.aop.target.AbstractPoolingTargetSource; 28 import org.springframework.aop.target.PrototypeTargetSource; 29 import org.springframework.aop.target.ThreadLocalTargetSource; 30 import org.springframework.beans.ITestBean; 31 import org.springframework.beans.TestBean; 32 import org.springframework.beans.factory.BeanFactory; 33 import org.springframework.transaction.CountingTxManager; 34 35 43 public abstract class AbstractMetadataAutoProxyTests extends TestCase { 44 45 private static final String TXMANAGER_BEAN_NAME = "es.txManager"; 46 47 50 protected abstract BeanFactory getBeanFactory() throws IOException ; 51 52 55 public void testNoProxy() throws Exception { 56 BeanFactory bf = getBeanFactory(); 57 TestBean tb = (TestBean) bf.getBean("noProxy"); 58 assertTrue(tb.getClass().getName().equals(TestBean.class.getName())); 60 } 61 62 public void testTxIsProxied() throws Exception { 63 BeanFactory bf = getBeanFactory(); 64 TxClass txClass = (TxClass) bf.getBean("txClass"); 65 assertTrue(AopUtils.isAopProxy(txClass)); 66 } 67 68 public void testIntroductionIsProxied() throws Exception { 69 BeanFactory bf = getBeanFactory(); 70 Object modifiable = bf.getBean("modifiable1"); 71 assertTrue(AopUtils.isAopProxy(modifiable)); 72 } 73 74 77 public void testDefaultTransactionAttributeOnMethod() throws Exception { 78 BeanFactory bf = getBeanFactory(); 79 TxClass txClass = (TxClass) bf.getBean("txClass"); 80 81 CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME); 82 83 assertEquals(0, txMan.commits); 84 int count = txClass.defaultTxAttribute(); 85 assertEquals("Return value was correct", 1, count); 86 assertEquals("Transaction counts match", 1, txMan.commits); 87 } 88 89 90 94 public void testRollbackRulesOnMethodCauseRollback() throws Exception { 95 BeanFactory bf = getBeanFactory(); 96 TxClass txClass = (TxClass) bf.getBean("txClass"); 97 assertTrue(AopUtils.isAopProxy(txClass)); 98 99 CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME); 100 101 assertEquals(0, txMan.commits); 102 txClass.echoException(null); 103 assertEquals("Transaction counts match", 1, txMan.commits); 104 105 assertEquals(0, txMan.rollbacks); 106 Exception ex = new Exception (); 107 try { 108 txClass.echoException(ex); 109 } 110 catch (Exception actual) { 111 assertEquals(ex, actual); 112 } 113 assertEquals("Transaction counts match", 1, txMan.rollbacks); 114 } 115 116 public void testRollbackRulesOnMethodPreventRollback() throws Exception { 117 BeanFactory bf = getBeanFactory(); 118 TxClass txClass = (TxClass) bf.getBean("txClass"); 119 120 CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME); 121 122 assertEquals(0, txMan.commits); 123 try { 125 txClass.echoException(new ServletException ()); 126 } 127 catch (ServletException ex) { 128 129 } 130 assertEquals("Transaction counts match", 1, txMan.commits); 131 } 132 133 public void testProgrammaticRollback() throws Exception { 134 BeanFactory bf = getBeanFactory(); 135 136 CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME); 137 138 TxClassWithClassAttribute txClass = (TxClassWithClassAttribute) bf.getBean("txClassWithClassAttribute"); 139 assertTrue(AopUtils.isCglibProxy(txClass)); 141 assertEquals(0, txMan.commits); 142 txClass.rollbackOnly(false); 143 assertEquals("Transaction counts match", 1, txMan.commits); 144 assertEquals(0, txMan.rollbacks); 145 txClass.rollbackOnly(true); 146 assertEquals(1, txMan.rollbacks); 147 } 148 149 150 public void testTransactionAttributeForMethodInheritedFromClass() throws Exception { 151 BeanFactory bf = getBeanFactory(); 152 TxClassWithClassAttribute txClass = (TxClassWithClassAttribute) bf.getBean("txClassWithClassAttribute"); 153 154 CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME); 155 156 assertEquals(0, txMan.commits); 157 int ret = txClass.inheritClassTxAttribute(25); 158 assertEquals("Return value was correct", 25, ret); 159 assertEquals("Transaction counts match when inherited from class", 1, txMan.commits); 160 161 try { 163 txClass.echoException(new Exception ()); 164 } 165 catch (Exception ex) { 166 167 } 168 assertEquals("Transaction rollbacks correct", 1, txMan.rollbacks); 169 } 170 171 174 public void testNoAutoProxying() throws Exception { 175 BeanFactory bf = getBeanFactory(); 176 ITestBean test = (ITestBean) bf.getBean("rawTest"); 177 assertFalse(AopUtils.isAopProxy(test)); 178 } 179 180 public void testAutoPrototype() throws Exception { 181 BeanFactory bf = getBeanFactory(); 182 ITestBean test = (ITestBean) bf.getBean("protoTest"); 183 assertTrue(AopUtils.isAopProxy(test)); 184 Advised advised = (Advised) test; 185 assertTrue(advised.getTargetSource() instanceof PrototypeTargetSource ); 186 ITestBean test2 = (ITestBean) bf.getBean("protoTest"); 187 assertFalse(test == test2); 188 } 189 190 public void testAutoThreadLocal() throws Exception { 191 final BeanFactory bf = getBeanFactory(); 192 final ITestBean test = (ITestBean) bf.getBean("threadLocalTest"); 193 assertTrue(AopUtils.isAopProxy(test)); 194 Advised advised = (Advised) test; 195 assertTrue(advised.getTargetSource() instanceof ThreadLocalTargetSource ); 196 String nameForMainThread = "tom"; 197 test.setName(nameForMainThread); 198 assertEquals(nameForMainThread, test.getName()); 199 Runnable r = new Runnable () { 201 public void run() { 202 ITestBean myTest = (ITestBean) bf.getBean("threadLocalTest"); 204 assertNull(myTest.getName()); 205 String myName = "Fred"; 206 myTest.setName(myName); 207 assertEquals(myName, myTest.getName()); 208 } 210 }; 211 Thread t = new Thread (r); 212 t.start(); 213 t.join(); 214 assertEquals(nameForMainThread, test.getName()); 216 } 217 218 221 public void testAutoPooling() throws Exception { 222 BeanFactory bf = getBeanFactory(); 223 TxClassWithClassAttribute txClass = (TxClassWithClassAttribute) bf.getBean("txClassWithClassAttribute"); 224 Advised advised = (Advised) txClass; 225 assertTrue(advised.getTargetSource() instanceof AbstractPoolingTargetSource ); 226 AbstractPoolingTargetSource ts = (AbstractPoolingTargetSource) advised.getTargetSource(); 227 assertEquals(10, ts.getMaxSize()); 229 txClass.rollbackOnly(false); 231 } 232 233 234 240 public void testIntroductionViaPrototype() throws Exception { 241 BeanFactory bf = getBeanFactory(); 242 243 ITestBean modifiable1 = (ITestBean) bf.getBean("modifiable1"); 244 ITestBean modifiable2 = (ITestBean) bf.getBean("modifiable2"); 245 246 248 Modifiable mod1 = (Modifiable) modifiable1; 250 Modifiable mod2 = (Modifiable) modifiable2; 251 252 assertFalse(mod1.isModified()); 253 assertFalse(mod2.isModified()); 254 255 int newAge = 33; 256 modifiable1.setAge(newAge); 257 assertTrue(mod1.isModified()); 258 assertFalse("Instances of prototype introduction don't seem distinct", mod2.isModified()); 260 mod1.acceptChanges(); 261 assertFalse(mod1.isModified()); 262 assertEquals(modifiable1.getAge(), newAge); 263 assertFalse(mod1.isModified()); 264 } 265 266 } 267 | Popular Tags |