KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > intercept > call > 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.call;
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         Callee callee = new Callee();
35
36         LOG = "";
37         callee.adviseWithAround();
38         assertEquals("adviseWithAround ", LOG);
39
40         ((Advisable) this).aw_addAdvice(
41                 "call(* test.intercept.call.Callee.adviseWithAround(..))",
42                 new AroundAdvice() {
43                     public Object JavaDoc invoke(JoinPoint jp) throws Throwable JavaDoc {
44                         InterceptTest.log("around1_pre_call ");
45                         Object JavaDoc result = jp.proceed();
46                         InterceptTest.log("around1_post_call ");
47                         return result;
48                     }
49                 }
50         );
51
52         LOG = "";
53         callee.adviseWithAround();
54         assertEquals("around1_pre_call adviseWithAround around1_post_call ", LOG);
55     }
56
57
58     public void testAddAndRemoveAround() {
59         Callee callee = new Callee();
60
61         LOG = "";
62         callee.adviseWithAround2();
63         assertEquals("adviseWithAround2 ", LOG);
64
65         ((Advisable) this).aw_addAdvice(
66                 "call(* test.intercept.call.Callee.adviseWithAround2(..))",
67                 new AroundAdvice() {
68                     public Object JavaDoc invoke(JoinPoint jp) throws Throwable JavaDoc {
69                         InterceptTest.log("around1_pre_call ");
70                         Object JavaDoc result = jp.proceed();
71                         InterceptTest.log("around1_post_call ");
72                         return result;
73                     }
74                 }
75         );
76
77         LOG = "";
78         callee.adviseWithAround2();
79         assertEquals("around1_pre_call adviseWithAround2 around1_post_call ", LOG);
80
81         ((Advisable) this).aw_removeAdvice("call(* test.intercept.call.Callee.adviseWithAround2(..))", AroundAdvice.class);
82
83         LOG = "";
84         callee.adviseWithAround2();
85         assertEquals("adviseWithAround2 ", LOG);
86     }
87
88     public void testAddAroundStack() {
89         Callee callee = new Callee();
90
91         LOG = "";
92         callee.adviseWithAroundStack();
93         assertEquals("adviseWithAroundStack ", LOG);
94
95         ((Advisable) this).aw_addAdvice(
96                 "call(* test.intercept.call.Callee.adviseWithAroundStack(..))",
97                 new AroundAdvice() {
98                     public Object JavaDoc invoke(JoinPoint jp) throws Throwable JavaDoc {
99                         InterceptTest.log("around2_pre_call ");
100                         Object JavaDoc result = jp.proceed();
101                         InterceptTest.log("around2_post_call ");
102                         return result;
103                     }
104                 }
105         );
106
107         LOG = "";
108         callee.adviseWithAroundStack();
109         assertEquals("around2_pre_call adviseWithAroundStack around2_post_call ", LOG);
110
111         ((Advisable) this).aw_addAdvice(
112                 "call(* test.intercept.call.Callee.adviseWithAroundStack(..))",
113                 new AroundAdvice() {
114                     public Object JavaDoc invoke(JoinPoint jp) throws Throwable JavaDoc {
115                         InterceptTest.log("around3_pre_call ");
116                         Object JavaDoc result = jp.proceed();
117                         InterceptTest.log("around3_post_call ");
118                         return result;
119                     }
120                 }
121         );
122
123         LOG = "";
124         callee.adviseWithAroundStack();
125         assertEquals(
126                 "around2_pre_call around3_pre_call adviseWithAroundStack around3_post_call around2_post_call ", LOG
127         );
128     }
129
130     public void testAddBefore() {
131         Callee callee = new Callee();
132
133         LOG = "";
134         callee.adviseWithBefore();
135         assertEquals("adviseWithBefore ", LOG);
136
137         ((Advisable) this).aw_addAdvice(
138                 "call(* test.intercept.call.Callee.adviseWithBefore(..))",
139                 new BeforeAdvice() {
140                     public void invoke(JoinPoint jp) throws Throwable JavaDoc {
141                         InterceptTest.log("before ");
142                     }
143                 }
144         );
145
146         LOG = "";
147         callee.adviseWithBefore();
148         assertEquals("before adviseWithBefore ", LOG);
149     }
150
151     public void testAddAfter() {
152         Callee callee = new Callee();
153
154         LOG = "";
155         callee.adviseWithAfter();
156         assertEquals("adviseWithAfter ", LOG);
157
158         ((Advisable) this).aw_addAdvice(
159                 "call(* test.intercept.call.Callee.adviseWithAfter(..))",
160                 new AfterAdvice() {
161                     public void invoke(JoinPoint jp) throws Throwable JavaDoc {
162                         InterceptTest.log("afterFinally ");
163                     }
164                 }
165         );
166
167         LOG = "";
168         callee.adviseWithAfter();
169         assertEquals("adviseWithAfter afterFinally ", LOG);
170     }
171
172     public void testAddAfterReturning() {
173         Callee callee = new Callee();
174
175         LOG = "";
176         callee.adviseWithAfterReturning();
177         assertEquals("adviseWithAfterReturning ", LOG);
178
179         ((Advisable) this).aw_addAdvice(
180                 "call(* test.intercept.call.Callee.adviseWithAfterReturning(..))",
181                 new AfterReturningAdvice() {
182                     public void invoke(JoinPoint jp, Object JavaDoc returnValue) throws Throwable JavaDoc {
183                         InterceptTest.log("afterReturning ");
184                         InterceptTest.log((String JavaDoc) returnValue);
185                         InterceptTest.log(" ");
186                     }
187                 }
188         );
189
190         LOG = "";
191         callee.adviseWithAfterReturning();
192         assertEquals("adviseWithAfterReturning afterReturning returnValue ", LOG);
193     }
194
195     public void testAddAfterThrowing() {
196         Callee callee = new Callee();
197
198         LOG = "";
199         try {
200             callee.adviseWithAfterThrowing();
201         } catch (RuntimeException JavaDoc e) {
202         }
203         assertEquals("adviseWithAfterThrowing ", LOG);
204
205         ((Advisable) this).aw_addAdvice(
206                 "call(* test.intercept.call.Callee.adviseWithAfterThrowing(..))",
207                 new AfterThrowingAdvice() {
208                     public void invoke(JoinPoint jp, Throwable JavaDoc exception) throws Throwable JavaDoc {
209                         InterceptTest.log("afterThrowing ");
210                         InterceptTest.log(exception.getMessage());
211                         InterceptTest.log(" ");
212                     }
213                 }
214         );
215
216         LOG = "";
217         try {
218             callee.adviseWithAfterThrowing();
219         } catch (RuntimeException JavaDoc e) {
220         }
221         assertEquals("adviseWithAfterThrowing afterThrowing noop ", LOG);
222     }
223
224     public static void main(String JavaDoc[] args) {
225         junit.textui.TestRunner.run(suite());
226     }
227
228     public static junit.framework.Test suite() {
229         return new junit.framework.TestSuite(InterceptTest.class);
230     }
231 }
232
Popular Tags