1 16 17 package org.springframework.aop.framework; 18 19 import org.aopalliance.intercept.MethodInterceptor; 20 import org.aopalliance.intercept.MethodInvocation; 21 import org.springframework.aop.interceptor.ExposeInvocationInterceptor; 22 import org.springframework.aop.interceptor.NopInterceptor; 23 import org.springframework.aop.support.AopUtils; 24 import org.springframework.aop.target.HotSwappableTargetSource; 25 import org.springframework.beans.ITestBean; 26 import org.springframework.beans.TestBean; 27 28 35 public class OptimizedCglibProxyTests extends CglibProxyTests { 36 37 protected Object createProxy(AdvisedSupport as) { 38 as.setProxyTargetClass(true); 39 as.setOptimize(true); 40 Object proxy = as.createAopProxy().getProxy(); 41 assertTrue(AopUtils.isCglibProxy(proxy)); 42 return proxy; 43 } 44 45 protected AopProxy createAopProxy(AdvisedSupport as) { 46 as.setProxyTargetClass(true); 47 as.setOptimize(true); 48 return new Cglib2AopProxy(as); 49 } 50 51 protected boolean requiresTarget() { 52 return true; 53 } 54 55 60 public void testStaticMethodPointcut() throws Throwable { 61 TestBean tb = new TestBean(); 62 ProxyFactory pc = new ProxyFactory(new Class [] { ITestBean.class }); 63 NopInterceptor di = new NopInterceptor(); 64 TestStaticPointcutAdvice sp = new TestStaticPointcutAdvice(di, "getAge"); 65 pc.addAdvisor(sp); 66 pc.setTarget(tb); 67 ITestBean it = (ITestBean) createProxy(pc); 68 assertEquals(di.getCount(), 0); 69 int age = it.getAge(); 70 assertEquals(di.getCount(), 1); 71 it.setAge(11); 72 } 75 76 public void testTargetCanGetInvocationEvenIfNoAdviceChain() throws Throwable { 77 } 79 80 84 public void testDeclaredException() throws Throwable { 85 final Exception expectedException = new Exception (); 86 MethodInterceptor mi = new MethodInterceptor() { 88 public Object invoke(MethodInvocation invocation) throws Throwable { 89 throw expectedException; 90 } 91 }; 92 AdvisedSupport pc = new AdvisedSupport(new Class [] { ITestBean.class }); 93 pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); 94 pc.addAdvice(mi); 95 96 pc.setTarget(new Object ()); 98 AopProxy aop = createAopProxy(pc); 99 100 try { 101 ITestBean tb = (ITestBean) aop.getProxy(); 102 tb.exceptional(expectedException); 104 fail("Should have thrown exception raised by interceptor"); 105 } 106 catch (Exception thrown) { 107 assertEquals("exception matches", expectedException, thrown); 108 } 109 } 110 111 115 public void testDynamicMethodPointcutThatAppliesStaticallyOnlyToSetters() throws Throwable { 116 TestBean tb = new TestBean(); 117 ProxyFactory pc = new ProxyFactory(new Class [] { ITestBean.class }); 118 TestDynamicPointcutAdvice dp = new TestDynamicPointcutForSettersOnly(new NopInterceptor(), "Age"); 120 pc.addAdvisor(dp); 121 pc.setTarget(tb); 122 ITestBean proxy = (ITestBean) createProxy(pc); 123 assertEquals(dp.count, 0); 124 int age = proxy.getAge(); 125 assertEquals(0, dp.count); 127 proxy.setAge(11); 128 assertEquals(11, proxy.getAge()); 129 assertEquals(dp.count, 1); 130 proxy.setName("joe"); 132 assertEquals(dp.count, 1); 133 } 134 135 139 public void testExistingProxyChangesTarget() throws Throwable { 140 TestBean tb1 = new TestBean(); 141 tb1.setAge(33); 142 143 TestBean tb2 = new TestBean(); 144 tb2.setAge(26); 145 146 ProxyFactory pc = new ProxyFactory(tb1); 147 NopInterceptor nop = new NopInterceptor(); 148 pc.addAdvice(nop); 149 ITestBean proxy = (ITestBean) createProxy(pc); 150 assertEquals(nop.getCount(), 0); 151 assertEquals(tb1.getAge(), proxy.getAge()); 152 assertEquals(nop.getCount(), 1); 153 try { 155 pc.setTarget(tb2); 156 fail("Shouldn't allow changing of target with CGLIB optimization"); 157 } 158 catch (AopConfigException ex) { 159 160 } 161 162 try { 163 pc.setTargetSource(new HotSwappableTargetSource(tb2)); 164 } 165 catch (AopConfigException ex) { 166 167 } 168 169 assertEquals(tb1.getAge(), proxy.getAge()); 171 assertEquals(nop.getCount(), 2); 172 } 173 174 179 203 204 205 } 206 | Popular Tags |