001: /**************************************************************************************
002: * Copyright (c) Jonas BonŽr, Alexandre Vasseur. All rights reserved. *
003: * http://aspectwerkz.codehaus.org *
004: * ---------------------------------------------------------------------------------- *
005: * The software in this package is published under the terms of the LGPL license *
006: * a copy of which has been included with this distribution in the license.txt file. *
007: **************************************************************************************/package test.intercept.execution;
008:
009: import junit.framework.TestCase;
010: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
011: import org.codehaus.aspectwerkz.intercept.BeforeAdvice;
012: import org.codehaus.aspectwerkz.intercept.Advisable;
013: import org.codehaus.aspectwerkz.intercept.AroundAdvice;
014: import org.codehaus.aspectwerkz.intercept.AfterAdvice;
015: import org.codehaus.aspectwerkz.intercept.AfterReturningAdvice;
016: import org.codehaus.aspectwerkz.intercept.AfterThrowingAdvice;
017:
018: /**
019: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
020: */
021: public class InterceptTest extends TestCase {
022: private static String LOG = "";
023:
024: public static void log(String msg) {
025: LOG += msg;
026: }
027:
028: public void testIsAdvisable() {
029: assertTrue(this instanceof Advisable);
030: }
031:
032: public void testAddAround() {
033: LOG = "";
034: adviseWithAround();
035: assertEquals("adviseWithAround ", LOG);
036:
037: ((Advisable) this )
038: .aw_addAdvice(
039: "execution(* test.intercept.execution.InterceptTest.adviseWithAround(..))",
040: new AroundAdvice() {
041: public Object invoke(JoinPoint jp)
042: throws Throwable {
043: InterceptTest
044: .log("around1_pre_execution ");
045: Object result = jp.copy().proceed();
046: InterceptTest
047: .log("around1_post_execution ");
048: return result;
049: }
050: });
051:
052: LOG = "";
053: adviseWithAround();
054: assertEquals(
055: "around1_pre_execution adviseWithAround around1_post_execution ",
056: LOG);
057: }
058:
059: public void testAddAndRemoveAround() {
060: LOG = "";
061: adviseWithAround2();
062: assertEquals("adviseWithAround2 ", LOG);
063:
064: final AroundAdvice advice = new AroundAdvice() {
065: public Object invoke(JoinPoint jp) throws Throwable {
066: InterceptTest.log("around1_pre_execution ");
067: Object result = jp.proceed();
068: InterceptTest.log("around1_post_execution ");
069: return result;
070: }
071: };
072: ((Advisable) this )
073: .aw_addAdvice(
074: "execution(* test.intercept.execution.InterceptTest.adviseWithAround2(..))",
075: advice);
076:
077: LOG = "";
078: adviseWithAround2();
079: assertEquals(
080: "around1_pre_execution adviseWithAround2 around1_post_execution ",
081: LOG);
082:
083: ((Advisable) this )
084: .aw_removeAdvice(
085: "execution(* test.intercept.execution.InterceptTest.adviseWithAround2(..))",
086: advice.getClass());
087:
088: LOG = "";
089: adviseWithAround2();
090: assertEquals("adviseWithAround2 ", LOG);
091: }
092:
093: public void testAddAroundStack() {
094: LOG = "";
095: adviseWithAroundStack();
096: assertEquals("adviseWithAroundStack ", LOG);
097:
098: ((Advisable) this )
099: .aw_addAdvice(
100: "execution(* test.intercept.execution.InterceptTest.adviseWithAroundStack(..))",
101: new AroundAdvice() {
102: public Object invoke(JoinPoint jp)
103: throws Throwable {
104: InterceptTest
105: .log("around2_pre_execution ");
106: Object result = jp.proceed();
107: InterceptTest
108: .log("around2_post_execution ");
109: return result;
110: }
111: });
112:
113: LOG = "";
114: adviseWithAroundStack();
115: assertEquals(
116: "around2_pre_execution adviseWithAroundStack around2_post_execution ",
117: LOG);
118:
119: ((Advisable) this )
120: .aw_addAdvice(
121: "execution(* test.intercept.execution.InterceptTest.adviseWithAroundStack(..))",
122: new AroundAdvice() {
123: public Object invoke(JoinPoint jp)
124: throws Throwable {
125: InterceptTest
126: .log("around3_pre_execution ");
127: Object result = jp.proceed();
128: InterceptTest
129: .log("around3_post_execution ");
130: return result;
131: }
132: });
133:
134: LOG = "";
135: adviseWithAroundStack();
136: assertEquals(
137: "around2_pre_execution around3_pre_execution adviseWithAroundStack around3_post_execution around2_post_execution ",
138: LOG);
139: }
140:
141: public void testAddBefore() {
142: LOG = "";
143: adviseWithBefore();
144: assertEquals("adviseWithBefore ", LOG);
145:
146: ((Advisable) this )
147: .aw_addAdvice(
148: "execution(* test.intercept.execution.InterceptTest.adviseWithBefore(..))",
149: new BeforeAdvice() {
150: public void invoke(JoinPoint jp)
151: throws Throwable {
152: InterceptTest.log("before ");
153: }
154: });
155:
156: LOG = "";
157: adviseWithBefore();
158: assertEquals("before adviseWithBefore ", LOG);
159: }
160:
161: public void testAddAfter() {
162: LOG = "";
163: adviseWithAfter();
164: assertEquals("adviseWithAfter ", LOG);
165:
166: ((Advisable) this )
167: .aw_addAdvice(
168: "execution(* test.intercept.execution.InterceptTest.adviseWithAfter(..))",
169: new AfterAdvice() {
170: public void invoke(JoinPoint jp)
171: throws Throwable {
172: InterceptTest.log("afterFinally ");
173: }
174: });
175:
176: LOG = "";
177: adviseWithAfter();
178: assertEquals("adviseWithAfter afterFinally ", LOG);
179: }
180:
181: public void testAddAfterReturning() {
182: LOG = "";
183: adviseWithAfterReturning();
184: assertEquals("adviseWithAfterReturning ", LOG);
185:
186: ((Advisable) this )
187: .aw_addAdvice(
188: "execution(* test.intercept.execution.InterceptTest.adviseWithAfterReturning(..))",
189: new AfterReturningAdvice() {
190: public void invoke(JoinPoint jp,
191: Object returnValue)
192: throws Throwable {
193: InterceptTest.log("afterReturning ");
194: InterceptTest.log((String) returnValue);
195: InterceptTest.log(" ");
196: }
197: });
198:
199: LOG = "";
200: adviseWithAfterReturning();
201: assertEquals(
202: "adviseWithAfterReturning afterReturning returnValue ",
203: LOG);
204: }
205:
206: public void testAddAfterReturningPrimitive() {
207: LOG = "";
208: adviseWithAfterReturningPrimitive();
209: assertEquals("adviseWithAfterReturningPrimitive ", LOG);
210:
211: ((Advisable) this )
212: .aw_addAdvice(
213: "execution(* test.intercept.execution.InterceptTest.adviseWithAfterReturningPrimitive(..))",
214: new AfterReturningAdvice() {
215: public void invoke(JoinPoint jp,
216: Object returnValue)
217: throws Throwable {
218: InterceptTest.log("afterReturning ");
219: InterceptTest
220: .log(((Integer) returnValue)
221: .toString());
222: InterceptTest.log(" ");
223: }
224: });
225:
226: LOG = "";
227: adviseWithAfterReturningPrimitive();
228: assertEquals(
229: "adviseWithAfterReturningPrimitive afterReturning -1 ",
230: LOG);
231: }
232:
233: public void testAddAfterThrowing() {
234: LOG = "";
235: try {
236: adviseWithAfterThrowing();
237: } catch (RuntimeException e) {
238: }
239: assertEquals("adviseWithAfterThrowing ", LOG);
240:
241: ((Advisable) this )
242: .aw_addAdvice(
243: "execution(* test.intercept.execution.InterceptTest.adviseWithAfterThrowing(..))",
244: new AfterThrowingAdvice() {
245: public void invoke(JoinPoint jp,
246: Throwable exception)
247: throws Throwable {
248: InterceptTest.log("afterThrowing ");
249: InterceptTest.log(exception
250: .getMessage());
251: InterceptTest.log(" ");
252: }
253: });
254:
255: LOG = "";
256: try {
257: adviseWithAfterThrowing();
258: } catch (RuntimeException e) {
259: }
260: assertEquals("adviseWithAfterThrowing afterThrowing noop ", LOG);
261: }
262:
263: public void testAddAfterAndAfterThrowing() {
264: LOG = "";
265: try {
266: addAfterAndAfterThrowing();
267: } catch (RuntimeException e) {
268: }
269: assertEquals("addAfterAndAfterThrowing ", LOG);
270:
271: ((Advisable) this )
272: .aw_addAdvice(
273: "execution(* test.intercept.execution.InterceptTest.addAfterAndAfterThrowing(..))",
274: new AfterAdvice() {
275: public void invoke(JoinPoint jp)
276: throws Throwable {
277: InterceptTest.log("after ");
278: }
279: });
280: ((Advisable) this )
281: .aw_addAdvice(
282: "execution(* test.intercept.execution.InterceptTest.addAfterAndAfterThrowing(..))",
283: new AfterThrowingAdvice() {
284: public void invoke(JoinPoint jp,
285: Throwable exception)
286: throws Throwable {
287: InterceptTest.log("afterThrowing ");
288: InterceptTest.log(exception
289: .getMessage());
290: InterceptTest.log(" ");
291: }
292: });
293:
294: LOG = "";
295: try {
296: addAfterAndAfterThrowing();
297: } catch (RuntimeException e) {
298: }
299: assertEquals(
300: "addAfterAndAfterThrowing afterThrowing noop after ",
301: LOG);
302: }
303:
304: public static void main(String[] args) {
305: junit.textui.TestRunner.run(suite());
306: }
307:
308: public static junit.framework.Test suite() {
309: return new junit.framework.TestSuite(InterceptTest.class);
310: }
311:
312: public void adviseWithAround() {
313: log("adviseWithAround ");
314: }
315:
316: public void adviseWithAround2() {
317: log("adviseWithAround2 ");
318: }
319:
320: public void adviseWithAroundStack() {
321: log("adviseWithAroundStack ");
322: }
323:
324: public void adviseWithBefore() {
325: log("adviseWithBefore ");
326: }
327:
328: public void adviseWithAfter() {
329: log("adviseWithAfter ");
330: }
331:
332: public Object adviseWithAfterReturning() {
333: log("adviseWithAfterReturning ");
334: return "returnValue";
335: }
336:
337: public int adviseWithAfterReturningPrimitive() {
338: log("adviseWithAfterReturningPrimitive ");
339: return -1;
340: }
341:
342: public void adviseWithAfterThrowing() {
343: log("adviseWithAfterThrowing ");
344: throw new RuntimeException("noop");
345: }
346:
347: public void addAfterAndAfterThrowing() {
348: log("addAfterAndAfterThrowing ");
349: throw new RuntimeException("noop");
350: }
351: }
|