KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.lang.reflect.Method JavaDoc;
20 import java.util.LinkedList JavaDoc;
21 import java.util.List JavaDoc;
22
23 import junit.framework.TestCase;
24 import org.aopalliance.intercept.MethodInterceptor;
25 import org.aopalliance.intercept.MethodInvocation;
26
27 import org.springframework.beans.TestBean;
28
29 /**
30  * TODO: could refactor to be generic.
31  * @author Rod Johnson
32  * @since 14.03.2003
33  */

34 public class MethodInvocationTests extends TestCase {
35     
36     /*
37     public static MethodInvocation testInvocation(Object o, String methodName, Class[] args, Interceptor[] interceptors) throws Exception {
38         Method m = o.getClass().getMethod(methodName, args);
39         MethodInvocationImpl invocation = new MethodInvocationImpl(null, null, m.getDeclaringClass(),
40     m, null, interceptors, // list
41     new Attrib4jAttributeRegistry());
42     return invocation;
43     }*/

44
45     /*
46     public void testNullInterceptor() throws Exception {
47         Method m = Object.class.getMethod("hashCode", null);
48         Object proxy = new Object();
49         try {
50                 MethodInvocationImpl invocation = new MethodInvocationImpl(proxy, null, m.getDeclaringClass(), //?
51         m, null, null // could customize here
52     );
53             fail("Shouldn't be able to create methodInvocationImpl with null interceptors");
54         } catch (AopConfigException ex) {
55         }
56     }
57
58     public void testEmptyInterceptorList() throws Exception {
59         Method m = Object.class.getMethod("hashCode", null);
60         Object proxy = new Object();
61         try {
62                 MethodInvocationImpl invocation = new MethodInvocationImpl(proxy, null, m.getDeclaringClass(), //?
63         m, null, new LinkedList() // list
64     );
65             fail("Shouldn't be able to create methodInvocationImpl with no interceptors");
66         } catch (AopConfigException ex) {
67         }
68     }
69     */

70
71     public void testValidInvocation() throws Throwable JavaDoc {
72         Method JavaDoc m = Object JavaDoc.class.getMethod("hashCode", (Class JavaDoc[]) null);
73         Object JavaDoc proxy = new Object JavaDoc();
74         final Object JavaDoc returnValue = new Object JavaDoc();
75         List JavaDoc is = new LinkedList JavaDoc();
76         is.add(new MethodInterceptor() {
77             public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
78                 return returnValue;
79             }
80         });
81             ReflectiveMethodInvocation invocation = new ReflectiveMethodInvocation(proxy, null, //?
82
m, null, null, is // list
83
);
84         Object JavaDoc rv = invocation.proceed();
85         assertTrue("correct response", rv == returnValue);
86     }
87     
88     /**
89      * ToString on target can cause failure.
90      */

91     public void testToStringDoesntHitTarget() throws Throwable JavaDoc {
92         Object JavaDoc target = new TestBean() {
93             public String JavaDoc toString() {
94                 throw new UnsupportedOperationException JavaDoc("toString");
95             }
96         };
97         final Object JavaDoc returnValue = new Object JavaDoc();
98         List JavaDoc is = new LinkedList JavaDoc();
99
100         Method JavaDoc m = Object JavaDoc.class.getMethod("hashCode", (Class JavaDoc[]) null);
101         Object JavaDoc proxy = new Object JavaDoc();
102         ReflectiveMethodInvocation invocation =
103             new ReflectiveMethodInvocation(proxy, target, m, null, null, is);
104
105         // If it hits target, the test will fail with the UnsupportedOpException
106
// in the inner class above.
107
invocation.toString();
108     }
109
110 }
111
Popular Tags