KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > aop > framework > adapter > ThrowsAdviceInterceptorTests


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.adapter;
18
19 import java.lang.reflect.Method JavaDoc;
20 import java.rmi.RemoteException JavaDoc;
21
22 import javax.servlet.ServletException JavaDoc;
23 import javax.transaction.TransactionRolledbackException JavaDoc;
24
25 import junit.framework.TestCase;
26 import org.aopalliance.intercept.MethodInvocation;
27 import org.easymock.MockControl;
28
29 import org.springframework.aop.ThrowsAdvice;
30 import org.springframework.aop.framework.MethodCounter;
31
32 /**
33  * @author Rod Johnson
34  */

35 public class ThrowsAdviceInterceptorTests extends TestCase {
36
37     public void testNoHandlerMethods() {
38         Object JavaDoc o = new Object JavaDoc();
39         try {
40             new ThrowsAdviceInterceptor(o);
41             fail("Should require one handler method at least");
42         }
43         catch (IllegalArgumentException JavaDoc ex) {
44             // Ok
45
}
46     }
47     
48     public void testNotInvoked() throws Throwable JavaDoc {
49         MyThrowsHandler th = new MyThrowsHandler();
50         ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
51         Object JavaDoc ret = new Object JavaDoc();
52         MockControl mc = MockControl.createControl(MethodInvocation.class);
53         MethodInvocation mi = (MethodInvocation) mc.getMock();
54         mi.proceed();
55         mc.setReturnValue(ret, 1);
56         mc.replay();
57         assertEquals(ret, ti.invoke(mi));
58         assertEquals(0, th.getCalls());
59         mc.verify();
60     }
61     
62     public void testNoHandlerMethodForThrowable() throws Throwable JavaDoc {
63         MyThrowsHandler th = new MyThrowsHandler();
64         ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
65         assertEquals(2, ti.getHandlerMethodCount());
66         Exception JavaDoc ex = new Exception JavaDoc();
67         MockControl mc = MockControl.createControl(MethodInvocation.class);
68         MethodInvocation mi = (MethodInvocation) mc.getMock();
69         mi.proceed();
70         mc.setThrowable(ex);
71         mc.replay();
72         try {
73             ti.invoke(mi);
74             fail();
75         }
76         catch (Exception JavaDoc caught) {
77             assertEquals(ex, caught);
78         }
79         assertEquals(0, th.getCalls());
80         mc.verify();
81     }
82     
83     public void testCorrectHandlerUsed() throws Throwable JavaDoc {
84         MyThrowsHandler th = new MyThrowsHandler();
85         ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
86         ServletException JavaDoc ex = new ServletException JavaDoc();
87         MockControl mc = MockControl.createControl(MethodInvocation.class);
88         MethodInvocation mi = (MethodInvocation) mc.getMock();
89         mi.getMethod();
90         mc.setReturnValue(Object JavaDoc.class.getMethod("hashCode", (Class JavaDoc[]) null), 1);
91         mi.getArguments();
92         mc.setReturnValue(null);
93         mi.getThis();
94         mc.setReturnValue(new Object JavaDoc());
95         mi.proceed();
96         mc.setThrowable(ex);
97         mc.replay();
98         try {
99             ti.invoke(mi);
100             fail();
101         }
102         catch (Exception JavaDoc caught) {
103             assertEquals(ex, caught);
104         }
105         assertEquals(1, th.getCalls());
106         assertEquals(1, th.getCalls("servletException"));
107         mc.verify();
108     }
109     
110     public void testCorrectHandlerUsedForSubclass() throws Throwable JavaDoc {
111         MyThrowsHandler th = new MyThrowsHandler();
112         ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
113         // Extends RemoteException
114
TransactionRolledbackException JavaDoc ex = new TransactionRolledbackException JavaDoc();
115         MockControl mc = MockControl.createControl(MethodInvocation.class);
116         MethodInvocation mi = (MethodInvocation) mc.getMock();
117         mi.proceed();
118         mc.setThrowable(ex);
119         mc.replay();
120         try {
121             ti.invoke(mi);
122             fail();
123         }
124         catch (Exception JavaDoc caught) {
125             assertEquals(ex, caught);
126         }
127         assertEquals(1, th.getCalls());
128         assertEquals(1, th.getCalls("remoteException"));
129         mc.verify();
130     }
131     
132     public void testHandlerMethodThrowsException() throws Throwable JavaDoc {
133         final Throwable JavaDoc t = new Throwable JavaDoc();
134         MyThrowsHandler th = new MyThrowsHandler() {
135             public void afterThrowing(RemoteException JavaDoc ex) throws Throwable JavaDoc {
136                 super.afterThrowing(ex);
137                 throw t;
138             }
139         };
140         ThrowsAdviceInterceptor ti = new ThrowsAdviceInterceptor(th);
141         // Extends RemoteException
142
TransactionRolledbackException JavaDoc ex = new TransactionRolledbackException JavaDoc();
143         MockControl mc = MockControl.createControl(MethodInvocation.class);
144         MethodInvocation mi = (MethodInvocation) mc.getMock();
145         mi.proceed();
146         mc.setThrowable(ex);
147         mc.replay();
148         try {
149             ti.invoke(mi);
150             fail();
151         }
152         catch (Throwable JavaDoc caught) {
153             assertEquals(t, caught);
154         }
155         assertEquals(1, th.getCalls());
156         assertEquals(1, th.getCalls("remoteException"));
157         mc.verify();
158     }
159     
160     public static class MyThrowsHandler extends MethodCounter implements ThrowsAdvice {
161         // Full method signature
162
public void afterThrowing(Method JavaDoc m, Object JavaDoc[] args, Object JavaDoc target, ServletException JavaDoc ex) {
163             count("servletException");
164          }
165         public void afterThrowing(RemoteException JavaDoc ex) throws Throwable JavaDoc {
166             count("remoteException");
167          }
168         
169         /** Not valid, wrong number of arguments */
170         public void afterThrowing(Method JavaDoc m, Exception JavaDoc ex) throws Throwable JavaDoc {
171             throw new UnsupportedOperationException JavaDoc("Shouldn't be called");
172          }
173     }
174     
175     public interface IEcho {
176         int echoException(int i, Throwable JavaDoc t) throws Throwable JavaDoc;
177         int getA();
178         void setA(int a);
179     }
180     
181     public static class Echo implements IEcho {
182         private int a;
183         
184         public int echoException(int i, Throwable JavaDoc t) throws Throwable JavaDoc {
185             if (t != null)
186                 throw t;
187             return i;
188         }
189         public void setA(int a) {
190             this.a = a;
191         }
192         public int getA() {
193             return a;
194         }
195     }
196
197 }
198
Popular Tags