1 16 17 package org.springframework.aop.framework.autoproxy; 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.framework.Lockable; 27 import org.springframework.aop.framework.MethodCounter; 28 import org.springframework.aop.interceptor.NopInterceptor; 29 import org.springframework.aop.support.AopUtils; 30 import org.springframework.aop.target.CommonsPoolTargetSource; 31 import org.springframework.aop.target.LazyInitTargetSource; 32 import org.springframework.aop.target.PrototypeTargetSource; 33 import org.springframework.aop.target.ThreadLocalTargetSource; 34 import org.springframework.beans.ITestBean; 35 import org.springframework.beans.factory.BeanFactory; 36 import org.springframework.context.support.ClassPathXmlApplicationContext; 37 import org.springframework.transaction.CountingTxManager; 38 39 44 public class AdvisorAutoProxyCreatorTests extends TestCase { 45 46 private static final String ADVISOR_APC_BEAN_NAME = "aapc"; 47 48 private static final String TXMANAGER_BEAN_NAME = "txManager"; 49 50 53 protected BeanFactory getBeanFactory() throws IOException { 54 return new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/advisorAutoProxyCreator.xml"); 55 } 56 57 public void testDefaultExclusionPrefix() throws Exception { 58 DefaultAdvisorAutoProxyCreator aapc = (DefaultAdvisorAutoProxyCreator) getBeanFactory().getBean(ADVISOR_APC_BEAN_NAME); 59 assertEquals(ADVISOR_APC_BEAN_NAME + DefaultAdvisorAutoProxyCreator.SEPARATOR, aapc.getAdvisorBeanNamePrefix()); 60 assertFalse(aapc.isUsePrefix()); 61 } 62 63 66 public void testNoProxy() throws Exception { 67 BeanFactory bf = getBeanFactory(); 68 Object o = bf.getBean("noSetters"); 69 assertFalse(AopUtils.isAopProxy(o)); 70 } 71 72 public void testTxIsProxied() throws Exception { 73 BeanFactory bf = getBeanFactory(); 74 ITestBean test = (ITestBean) bf.getBean("test"); 75 assertTrue(AopUtils.isAopProxy(test)); 76 } 77 78 public void testRegexpApplied() throws Exception { 79 BeanFactory bf = getBeanFactory(); 80 ITestBean test = (ITestBean) bf.getBean("test"); 81 MethodCounter counter = (MethodCounter) bf.getBean("countingAdvice"); 82 assertEquals(0, counter.getCalls()); 83 test.getName(); 84 assertEquals(1, counter.getCalls()); 85 } 86 87 92 public void testCommonInterceptorAndAdvisor() throws Exception { 93 BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/advisorAutoProxyCreatorWithCommonInterceptors.xml"); 94 ITestBean test1 = (ITestBean) bf.getBean("test1"); 95 assertTrue(AopUtils.isAopProxy(test1)); 96 97 Lockable lockable1 = (Lockable) test1; 98 NopInterceptor nop = (NopInterceptor) bf.getBean("nopInterceptor"); 99 assertEquals(0, nop.getCount()); 100 101 ITestBean test2 = (ITestBean) bf.getBean("test2"); 102 Lockable lockable2 = (Lockable) test2; 103 104 assertFalse(lockable1.locked()); 106 assertFalse(lockable2.locked()); 107 assertEquals(2, nop.getCount()); 111 lockable1.lock(); 112 assertTrue(lockable1.locked()); 113 assertFalse(lockable2.locked()); 114 assertEquals(5, nop.getCount()); 115 } 116 117 121 public void testCustomTargetSourceNoMatch() throws Exception { 122 BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/customTargetSource.xml"); 123 ITestBean test = (ITestBean) bf.getBean("test"); 124 assertFalse(AopUtils.isAopProxy(test)); 125 assertEquals("Rod", test.getName()); 126 assertEquals("Kerry", test.getSpouse().getName()); 127 } 128 129 public void testCustomPoolingTargetSource() throws Exception { 130 BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/customTargetSource.xml"); 131 ITestBean test = (ITestBean) bf.getBean("poolingTest"); 132 assertTrue(AopUtils.isAopProxy(test)); 133 Advised advised = (Advised) test; 134 assertTrue(advised.getTargetSource() instanceof CommonsPoolTargetSource); 135 assertEquals("Rod", test.getName()); 136 assertEquals("Kerry", test.getSpouse().getName()); 138 } 139 140 public void testCustomPrototypeTargetSource() throws Exception { 141 CountingTestBean.count = 0; 142 BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/customTargetSource.xml"); 143 ITestBean test = (ITestBean) bf.getBean("prototypeTest"); 144 assertTrue(AopUtils.isAopProxy(test)); 145 Advised advised = (Advised) test; 146 assertTrue(advised.getTargetSource() instanceof PrototypeTargetSource); 147 assertEquals("Rod", test.getName()); 148 assertEquals("Kerry", test.getSpouse().getName()); 150 assertEquals("Only 2 CountingTestBeans instantiated", 2, CountingTestBean.count); 151 CountingTestBean.count = 0; 152 } 153 154 public void testLazyInitTargetSource() throws Exception { 155 CountingTestBean.count = 0; 156 BeanFactory bf = new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/customTargetSource.xml"); 157 ITestBean test = (ITestBean) bf.getBean("lazyInitTest"); 158 assertTrue(AopUtils.isAopProxy(test)); 159 Advised advised = (Advised) test; 160 assertTrue(advised.getTargetSource() instanceof LazyInitTargetSource); 161 assertEquals("No CountingTestBean instantiated yet", 0, CountingTestBean.count); 162 assertEquals("Rod", test.getName()); 163 assertEquals("Kerry", test.getSpouse().getName()); 164 assertEquals("Only 1 CountingTestBean instantiated", 1, CountingTestBean.count); 165 CountingTestBean.count = 0; 166 } 167 168 public void testQuickTargetSourceCreator() throws Exception { 169 ClassPathXmlApplicationContext bf = 170 new ClassPathXmlApplicationContext("/org/springframework/aop/framework/autoproxy/quickTargetSource.xml"); 171 ITestBean test = (ITestBean) bf.getBean("test"); 172 assertFalse(AopUtils.isAopProxy(test)); 173 assertEquals("Rod", test.getName()); 174 assertEquals("Kerry", test.getSpouse().getName()); 176 177 test = (ITestBean) bf.getBean(":test"); 179 assertTrue(AopUtils.isAopProxy(test)); 180 Advised advised = (Advised) test; 181 assertTrue(advised.getTargetSource() instanceof CommonsPoolTargetSource); 182 assertEquals("Rod", test.getName()); 183 assertEquals("Kerry", test.getSpouse().getName()); 185 186 test = (ITestBean) bf.getBean("%test"); 188 assertTrue(AopUtils.isAopProxy(test)); 189 advised = (Advised) test; 190 assertTrue(advised.getTargetSource() instanceof ThreadLocalTargetSource); 191 assertEquals("Rod", test.getName()); 192 assertEquals("Kerry", test.getSpouse().getName()); 194 195 test = (ITestBean) bf.getBean("!test"); 197 assertTrue(AopUtils.isAopProxy(test)); 198 advised = (Advised) test; 199 assertTrue(advised.getTargetSource() instanceof PrototypeTargetSource); 200 assertEquals("Rod", test.getName()); 201 assertEquals("Kerry", test.getSpouse().getName()); 203 204 205 ITestBean test2 = (ITestBean) bf.getBean("!test"); 206 assertFalse("Prototypes cannot be the same object", test == test2); 207 assertEquals("Rod", test2.getName()); 208 assertEquals("Kerry", test2.getSpouse().getName()); 209 bf.close(); 210 } 211 212 221 222 public void testTransactionAttributeOnMethod() throws Exception { 223 BeanFactory bf = getBeanFactory(); 224 ITestBean test = (ITestBean) bf.getBean("test"); 225 226 CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME); 227 OrderedTxCheckAdvisor txc = (OrderedTxCheckAdvisor) bf.getBean("orderedBeforeTransaction"); 228 assertEquals(0, txc.getCountingBeforeAdvice().getCalls()); 229 230 assertEquals(0, txMan.commits); 231 assertEquals("Initial value was correct", 4, test.getAge()); 232 int newAge = 5; 233 test.setAge(newAge); 234 assertEquals(1, txc.getCountingBeforeAdvice().getCalls()); 235 236 assertEquals("New value set correctly", newAge, test.getAge()); 237 assertEquals("Transaction counts match", 1, txMan.commits); 238 } 239 240 243 public void testRollbackRulesOnMethodCauseRollback() throws Exception { 244 BeanFactory bf = getBeanFactory(); 245 Rollback rb = (Rollback) bf.getBean("rollback"); 246 247 CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME); 248 OrderedTxCheckAdvisor txc = (OrderedTxCheckAdvisor) bf.getBean("orderedBeforeTransaction"); 249 assertEquals(0, txc.getCountingBeforeAdvice().getCalls()); 250 251 assertEquals(0, txMan.commits); 252 rb.echoException(null); 253 assertEquals(0, txc.getCountingBeforeAdvice().getCalls()); 255 assertEquals("Transaction counts match", 1, txMan.commits); 256 257 assertEquals(0, txMan.rollbacks); 258 Exception ex = new Exception (); 259 try { 260 rb.echoException(ex); 261 } 262 catch (Exception actual) { 263 assertEquals(ex, actual); 264 } 265 assertEquals("Transaction counts match", 1, txMan.rollbacks); 266 } 267 268 public void testRollbackRulesOnMethodPreventRollback() throws Exception { 269 BeanFactory bf = getBeanFactory(); 270 Rollback rb = (Rollback) bf.getBean("rollback"); 271 272 CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME); 273 274 assertEquals(0, txMan.commits); 275 try { 277 rb.echoException(new ServletException ()); 278 } 279 catch (ServletException ex) { 280 281 } 282 assertEquals("Transaction counts match", 1, txMan.commits); 283 } 284 285 public void testProgrammaticRollback() throws Exception { 286 BeanFactory bf = getBeanFactory(); 287 288 assertTrue(bf.getBean(TXMANAGER_BEAN_NAME) instanceof CountingTxManager); 289 CountingTxManager txMan = (CountingTxManager) bf.getBean(TXMANAGER_BEAN_NAME); 290 291 Rollback rb = (Rollback) bf.getBean("rollback"); 292 assertEquals(0, txMan.commits); 293 rb.rollbackOnly(false); 294 assertEquals("Transaction counts match", 1, txMan.commits); 295 assertEquals(0, txMan.rollbacks); 296 rb.rollbackOnly(true); 298 assertEquals(1, txMan.rollbacks); 299 } 300 301 302 308 337 338 } 339 | Popular Tags |