1 16 17 package org.springframework.aop.framework; 18 19 import org.aopalliance.intercept.MethodInterceptor; 20 import org.aopalliance.intercept.MethodInvocation; 21 import org.easymock.MockControl; 22 23 import org.springframework.aop.interceptor.ExposeInvocationInterceptor; 24 import org.springframework.aop.support.AopUtils; 25 import org.springframework.beans.IOther; 26 import org.springframework.beans.ITestBean; 27 import org.springframework.beans.TestBean; 28 29 34 public class JdkDynamicProxyTests extends AbstractAopProxyTests { 35 36 protected Object createProxy(AdvisedSupport as) { 37 assertFalse("Not forcible CGLIB", as.isProxyTargetClass()); 38 Object proxy = as.createAopProxy().getProxy(); 39 assertTrue("Should be a JDK proxy: " + proxy.getClass(), AopUtils.isJdkDynamicProxy(proxy)); 40 return proxy; 41 } 42 43 protected AopProxy createAopProxy(AdvisedSupport as) { 44 return new JdkDynamicAopProxy(as); 45 } 46 47 public void testNullConfig() { 48 try { 49 JdkDynamicAopProxy aop = new JdkDynamicAopProxy(null); 50 aop.getProxy(); 51 fail("Shouldn't allow null interceptors"); 52 } 53 catch (AopConfigException ex) { 54 } 56 } 57 58 public void testProxyIsJustInterface() throws Throwable { 59 TestBean raw = new TestBean(); 60 raw.setAge(32); 61 AdvisedSupport pc = new AdvisedSupport(new Class [] {ITestBean.class}); 62 pc.setTarget(raw); 63 JdkDynamicAopProxy aop = new JdkDynamicAopProxy(pc); 64 65 Object proxy = aop.getProxy(); 66 assertTrue(proxy instanceof ITestBean); 67 assertTrue(!(proxy instanceof TestBean)); 68 } 69 70 public void testInterceptorIsInvokedWithNoTarget() throws Throwable { 71 int age = 25; 73 MockControl miControl = MockControl.createControl(MethodInterceptor.class); 74 MethodInterceptor mi = (MethodInterceptor) miControl.getMock(); 75 76 AdvisedSupport pc = new AdvisedSupport(new Class [] { ITestBean.class }); 77 pc.addAdvice(mi); 78 AopProxy aop = createAopProxy(pc); 79 80 mi.invoke(null); 82 miControl.setDefaultReturnValue(new Integer (age)); 89 miControl.replay(); 90 91 ITestBean tb = (ITestBean) aop.getProxy(); 92 assertTrue("correct return value", tb.getAge() == age); 93 miControl.verify(); 94 } 95 96 public void testTargetCanGetInvocationWithPrivateClass() throws Throwable { 97 final ExposedInvocationTestBean expectedTarget = new ExposedInvocationTestBean() { 98 protected void assertions(MethodInvocation invocation) { 99 assertTrue(invocation.getThis() == this); 100 assertTrue("Invocation should be on ITestBean: " + invocation.getMethod(), 101 invocation.getMethod().getDeclaringClass() == ITestBean.class); 102 } 103 }; 104 105 AdvisedSupport pc = new AdvisedSupport(new Class [] { ITestBean.class, IOther.class }); 106 pc.addAdvice(ExposeInvocationInterceptor.INSTANCE); 107 TrapTargetInterceptor tii = new TrapTargetInterceptor() { 108 public Object invoke(MethodInvocation invocation) throws Throwable { 109 assertEquals("Target is correct", expectedTarget, invocation.getThis()); 111 return super.invoke(invocation); 112 } 113 }; 114 pc.addAdvice(tii); 115 pc.setTarget(expectedTarget); 116 AopProxy aop = createAopProxy(pc); 117 118 ITestBean tb = (ITestBean) aop.getProxy(); 119 tb.getName(); 120 123 125 } 130 131 } 132 | Popular Tags |