KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > intercept > execution > InterceptTest


1 /**************************************************************************************
2  * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
3  * http://aspectwerkz.codehaus.org *
4  * ---------------------------------------------------------------------------------- *
5  * The software in this package is published under the terms of the LGPL license *
6  * a copy of which has been included with this distribution in the license.txt file. *
7  **************************************************************************************/

8 package test.intercept.execution;
9
10 import junit.framework.TestCase;
11 import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
12 import org.codehaus.aspectwerkz.intercept.BeforeAdvice;
13 import org.codehaus.aspectwerkz.intercept.Advisable;
14 import org.codehaus.aspectwerkz.intercept.AroundAdvice;
15 import org.codehaus.aspectwerkz.intercept.AfterAdvice;
16 import org.codehaus.aspectwerkz.intercept.AfterReturningAdvice;
17 import org.codehaus.aspectwerkz.intercept.AfterThrowingAdvice;
18
19 /**
20  * @author <a HREF="mailto:jboner@codehaus.org">Jonas BonŽr </a>
21  */

22 public class InterceptTest extends TestCase {
23     private static String JavaDoc LOG = "";
24
25     public static void log(String JavaDoc msg) {
26         LOG += msg;
27     }
28
29     public void testIsAdvisable() {
30         assertTrue(this instanceof Advisable);
31     }
32
33     public void testAddAround() {
34         LOG = "";
35         adviseWithAround();
36         assertEquals("adviseWithAround ", LOG);
37
38         ((Advisable) this).aw_addAdvice(
39                 "execution(* test.intercept.execution.InterceptTest.adviseWithAround(..))",
40                 new AroundAdvice() {
41                     public Object JavaDoc invoke(JoinPoint jp) throws Throwable JavaDoc {
42                         InterceptTest.log("around1_pre_execution ");
43                         Object JavaDoc result = jp.copy().proceed();
44                         InterceptTest.log("around1_post_execution ");
45                         return result;
46                     }
47                 }
48         );
49
50         LOG = "";
51         adviseWithAround();
52         assertEquals("around1_pre_execution adviseWithAround around1_post_execution ", LOG);
53     }
54
55
56     public void testAddAndRemoveAround() {
57         LOG = "";
58         adviseWithAround2();
59         assertEquals("adviseWithAround2 ", LOG);
60
61         final AroundAdvice advice = new AroundAdvice() {
62             public Object JavaDoc invoke(JoinPoint jp) throws Throwable JavaDoc {
63                 InterceptTest.log("around1_pre_execution ");
64                 Object JavaDoc result = jp.proceed();
65                 InterceptTest.log("around1_post_execution ");
66                 return result;
67             }
68         };
69         ((Advisable) this).aw_addAdvice(
70                 "execution(* test.intercept.execution.InterceptTest.adviseWithAround2(..))",
71                 advice
72         );
73
74         LOG = "";
75         adviseWithAround2();
76         assertEquals("around1_pre_execution adviseWithAround2 around1_post_execution ", LOG);
77
78         ((Advisable) this).aw_removeAdvice(
79                 "execution(* test.intercept.execution.InterceptTest.adviseWithAround2(..))",
80                 advice.getClass()
81         );
82
83         LOG = "";
84         adviseWithAround2();
85         assertEquals("adviseWithAround2 ", LOG);
86     }
87
88     public void testAddAroundStack() {
89         LOG = "";
90         adviseWithAroundStack();
91         assertEquals("adviseWithAroundStack ", LOG);
92
93         ((Advisable) this).aw_addAdvice(
94                 "execution(* test.intercept.execution.InterceptTest.adviseWithAroundStack(..))",
95                 new AroundAdvice() {
96                     public Object JavaDoc invoke(JoinPoint jp) throws Throwable JavaDoc {
97                         InterceptTest.log("around2_pre_execution ");
98                         Object JavaDoc result = jp.proceed();
99                         InterceptTest.log("around2_post_execution ");
100                         return result;
101                     }
102                 }
103         );
104
105         LOG = "";
106         adviseWithAroundStack();
107         assertEquals("around2_pre_execution adviseWithAroundStack around2_post_execution ", LOG);
108
109         ((Advisable) this).aw_addAdvice(
110                 "execution(* test.intercept.execution.InterceptTest.adviseWithAroundStack(..))",
111                 new AroundAdvice() {
112                     public Object JavaDoc invoke(JoinPoint jp) throws Throwable JavaDoc {
113                         InterceptTest.log("around3_pre_execution ");
114                         Object JavaDoc result = jp.proceed();
115                         InterceptTest.log("around3_post_execution ");
116                         return result;
117                     }
118                 }
119         );
120
121         LOG = "";
122         adviseWithAroundStack();
123         assertEquals(
124                 "around2_pre_execution around3_pre_execution adviseWithAroundStack around3_post_execution around2_post_execution ",
125                 LOG
126         );
127     }
128
129     public void testAddBefore() {
130         LOG = "";
131         adviseWithBefore();
132         assertEquals("adviseWithBefore ", LOG);
133
134         ((Advisable) this).aw_addAdvice(
135                 "execution(* test.intercept.execution.InterceptTest.adviseWithBefore(..))",
136                 new BeforeAdvice() {
137                     public void invoke(JoinPoint jp) throws Throwable JavaDoc {
138                         InterceptTest.log("before ");
139                     }
140                 }
141         );
142
143         LOG = "";
144         adviseWithBefore();
145         assertEquals("before adviseWithBefore ", LOG);
146     }
147
148     public void testAddAfter() {
149         LOG = "";
150         adviseWithAfter();
151         assertEquals("adviseWithAfter ", LOG);
152
153         ((Advisable) this).aw_addAdvice(
154                 "execution(* test.intercept.execution.InterceptTest.adviseWithAfter(..))",
155                 new AfterAdvice() {
156                     public void invoke(JoinPoint jp) throws Throwable JavaDoc {
157                         InterceptTest.log("afterFinally ");
158                     }
159                 }
160         );
161
162         LOG = "";
163         adviseWithAfter();
164         assertEquals("adviseWithAfter afterFinally ", LOG);
165     }
166
167     public void testAddAfterReturning() {
168         LOG = "";
169         adviseWithAfterReturning();
170         assertEquals("adviseWithAfterReturning ", LOG);
171
172         ((Advisable) this).aw_addAdvice(
173                 "execution(* test.intercept.execution.InterceptTest.adviseWithAfterReturning(..))",
174                 new AfterReturningAdvice() {
175                     public void invoke(JoinPoint jp, Object JavaDoc returnValue) throws Throwable JavaDoc {
176                         InterceptTest.log("afterReturning ");
177                         InterceptTest.log((String JavaDoc) returnValue);
178                         InterceptTest.log(" ");
179                     }
180                 }
181         );
182
183         LOG = "";
184         adviseWithAfterReturning();
185         assertEquals("adviseWithAfterReturning afterReturning returnValue ", LOG);
186     }
187
188     public void testAddAfterReturningPrimitive() {
189         LOG = "";
190         adviseWithAfterReturningPrimitive();
191         assertEquals("adviseWithAfterReturningPrimitive ", LOG);
192
193         ((Advisable) this).aw_addAdvice(
194                 "execution(* test.intercept.execution.InterceptTest.adviseWithAfterReturningPrimitive(..))",
195                 new AfterReturningAdvice() {
196                     public void invoke(JoinPoint jp, Object JavaDoc returnValue) throws Throwable JavaDoc {
197                         InterceptTest.log("afterReturning ");
198                         InterceptTest.log(((Integer JavaDoc) returnValue).toString());
199                         InterceptTest.log(" ");
200                     }
201                 }
202         );
203
204         LOG = "";
205         adviseWithAfterReturningPrimitive();
206         assertEquals("adviseWithAfterReturningPrimitive afterReturning -1 ", LOG);
207     }
208
209     public void testAddAfterThrowing() {
210         LOG = "";
211         try {
212             adviseWithAfterThrowing();
213         } catch (RuntimeException JavaDoc e) {
214         }
215         assertEquals("adviseWithAfterThrowing ", LOG);
216
217         ((Advisable) this).aw_addAdvice(
218                 "execution(* test.intercept.execution.InterceptTest.adviseWithAfterThrowing(..))",
219                 new AfterThrowingAdvice() {
220                     public void invoke(JoinPoint jp, Throwable JavaDoc exception) throws Throwable JavaDoc {
221                         InterceptTest.log("afterThrowing ");
222                         InterceptTest.log(exception.getMessage());
223                         InterceptTest.log(" ");
224                     }
225                 }
226         );
227
228         LOG = "";
229         try {
230             adviseWithAfterThrowing();
231         } catch (RuntimeException JavaDoc e) {
232         }
233         assertEquals("adviseWithAfterThrowing afterThrowing noop ", LOG);
234     }
235
236     public void testAddAfterAndAfterThrowing() {
237         LOG = "";
238         try {
239             addAfterAndAfterThrowing();
240         } catch (RuntimeException JavaDoc e) {
241         }
242         assertEquals("addAfterAndAfterThrowing ", LOG);
243
244         ((Advisable) this).aw_addAdvice(
245                 "execution(* test.intercept.execution.InterceptTest.addAfterAndAfterThrowing(..))",
246                 new AfterAdvice() {
247                     public void invoke(JoinPoint jp) throws Throwable JavaDoc {
248                         InterceptTest.log("after ");
249                     }
250                 }
251         );
252         ((Advisable) this).aw_addAdvice(
253                 "execution(* test.intercept.execution.InterceptTest.addAfterAndAfterThrowing(..))",
254                 new AfterThrowingAdvice() {
255                     public void invoke(JoinPoint jp, Throwable JavaDoc exception) throws Throwable JavaDoc {
256                         InterceptTest.log("afterThrowing ");
257                         InterceptTest.log(exception.getMessage());
258                         InterceptTest.log(" ");
259                     }
260                 }
261         );
262
263         LOG = "";
264         try {
265             addAfterAndAfterThrowing();
266         } catch (RuntimeException JavaDoc e) {
267         }
268         assertEquals("addAfterAndAfterThrowing afterThrowing noop after ", LOG);
269     }
270
271     public static void main(String JavaDoc[] args) {
272         junit.textui.TestRunner.run(suite());
273     }
274
275     public static junit.framework.Test suite() {
276         return new junit.framework.TestSuite(InterceptTest.class);
277     }
278
279     public void adviseWithAround() {
280         log("adviseWithAround ");
281     }
282
283     public void adviseWithAround2() {
284         log("adviseWithAround2 ");
285     }
286
287     public void adviseWithAroundStack() {
288         log("adviseWithAroundStack ");
289     }
290
291     public void adviseWithBefore() {
292         log("adviseWithBefore ");
293     }
294
295     public void adviseWithAfter() {
296         log("adviseWithAfter ");
297     }
298
299     public Object JavaDoc adviseWithAfterReturning() {
300         log("adviseWithAfterReturning ");
301         return "returnValue";
302     }
303
304     public int adviseWithAfterReturningPrimitive() {
305         log("adviseWithAfterReturningPrimitive ");
306         return -1;
307     }
308
309     public void adviseWithAfterThrowing() {
310         log("adviseWithAfterThrowing ");
311         throw new RuntimeException JavaDoc("noop");
312     }
313
314     public void addAfterAndAfterThrowing() {
315         log("addAfterAndAfterThrowing ");
316         throw new RuntimeException JavaDoc("noop");
317     }
318 }
319
Popular Tags