KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > JdkDynamicProxyTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 /**
30  * @author Rod Johnson
31  * @author Juergen Hoeller
32  * @since 13.03.2003
33  */

34 public class JdkDynamicProxyTests extends AbstractAopProxyTests {
35
36     protected Object JavaDoc createProxy(AdvisedSupport as) {
37         assertFalse("Not forcible CGLIB", as.isProxyTargetClass());
38         Object JavaDoc 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             // Ok
55
}
56     }
57
58     public void testProxyIsJustInterface() throws Throwable JavaDoc {
59         TestBean raw = new TestBean();
60         raw.setAge(32);
61         AdvisedSupport pc = new AdvisedSupport(new Class JavaDoc[] {ITestBean.class});
62         pc.setTarget(raw);
63         JdkDynamicAopProxy aop = new JdkDynamicAopProxy(pc);
64
65         Object JavaDoc proxy = aop.getProxy();
66         assertTrue(proxy instanceof ITestBean);
67         assertTrue(!(proxy instanceof TestBean));
68     }
69
70     public void testInterceptorIsInvokedWithNoTarget() throws Throwable JavaDoc {
71         // Test return value
72
int age = 25;
73         MockControl miControl = MockControl.createControl(MethodInterceptor.class);
74         MethodInterceptor mi = (MethodInterceptor) miControl.getMock();
75
76         AdvisedSupport pc = new AdvisedSupport(new Class JavaDoc[] { ITestBean.class });
77         pc.addAdvice(mi);
78         AopProxy aop = createAopProxy(pc);
79
80         // Really would like to permit null arg:can't get exact mi
81
mi.invoke(null);
82         //mi.invoke(new MethodInvocationImpl(aop, null, ITestBean.class,
83
// ITestBean.class.getMethod("getAge", null),
84
// null, l, r));
85
//miControl.
86
//miControl.setReturnValue(new Integer(age));
87
// Have disabled strong argument checking
88
miControl.setDefaultReturnValue(new Integer JavaDoc(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 JavaDoc {
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 JavaDoc[] { ITestBean.class, IOther.class });
106         pc.addAdvice(ExposeInvocationInterceptor.INSTANCE);
107         TrapTargetInterceptor tii = new TrapTargetInterceptor() {
108             public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
109                 // Assert that target matches BEFORE invocation returns
110
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         // Not safe to trap invocation
121
//assertTrue(tii.invocation == target.invocation);
122

123         //assertTrue(target.invocation.getProxy() == tb);
124

125         // ((IOther) tb).absquatulate();
126
//MethodInvocation minv = tii.invocation;
127
//assertTrue("invoked on iother, not " + minv.getMethod().getDeclaringClass(), minv.getMethod().getDeclaringClass() == IOther.class);
128
//assertTrue(target.invocation == tii.invocation);
129
}
130
131 }
132
Popular Tags