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.call;
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: Callee callee = new Callee();
034:
035: LOG = "";
036: callee.adviseWithAround();
037: assertEquals("adviseWithAround ", LOG);
038:
039: ((Advisable) this )
040: .aw_addAdvice(
041: "call(* test.intercept.call.Callee.adviseWithAround(..))",
042: new AroundAdvice() {
043: public Object invoke(JoinPoint jp)
044: throws Throwable {
045: InterceptTest.log("around1_pre_call ");
046: Object result = jp.proceed();
047: InterceptTest.log("around1_post_call ");
048: return result;
049: }
050: });
051:
052: LOG = "";
053: callee.adviseWithAround();
054: assertEquals(
055: "around1_pre_call adviseWithAround around1_post_call ",
056: LOG);
057: }
058:
059: public void testAddAndRemoveAround() {
060: Callee callee = new Callee();
061:
062: LOG = "";
063: callee.adviseWithAround2();
064: assertEquals("adviseWithAround2 ", LOG);
065:
066: ((Advisable) this )
067: .aw_addAdvice(
068: "call(* test.intercept.call.Callee.adviseWithAround2(..))",
069: new AroundAdvice() {
070: public Object invoke(JoinPoint jp)
071: throws Throwable {
072: InterceptTest.log("around1_pre_call ");
073: Object result = jp.proceed();
074: InterceptTest.log("around1_post_call ");
075: return result;
076: }
077: });
078:
079: LOG = "";
080: callee.adviseWithAround2();
081: assertEquals(
082: "around1_pre_call adviseWithAround2 around1_post_call ",
083: LOG);
084:
085: ((Advisable) this )
086: .aw_removeAdvice(
087: "call(* test.intercept.call.Callee.adviseWithAround2(..))",
088: AroundAdvice.class);
089:
090: LOG = "";
091: callee.adviseWithAround2();
092: assertEquals("adviseWithAround2 ", LOG);
093: }
094:
095: public void testAddAroundStack() {
096: Callee callee = new Callee();
097:
098: LOG = "";
099: callee.adviseWithAroundStack();
100: assertEquals("adviseWithAroundStack ", LOG);
101:
102: ((Advisable) this )
103: .aw_addAdvice(
104: "call(* test.intercept.call.Callee.adviseWithAroundStack(..))",
105: new AroundAdvice() {
106: public Object invoke(JoinPoint jp)
107: throws Throwable {
108: InterceptTest.log("around2_pre_call ");
109: Object result = jp.proceed();
110: InterceptTest.log("around2_post_call ");
111: return result;
112: }
113: });
114:
115: LOG = "";
116: callee.adviseWithAroundStack();
117: assertEquals(
118: "around2_pre_call adviseWithAroundStack around2_post_call ",
119: LOG);
120:
121: ((Advisable) this )
122: .aw_addAdvice(
123: "call(* test.intercept.call.Callee.adviseWithAroundStack(..))",
124: new AroundAdvice() {
125: public Object invoke(JoinPoint jp)
126: throws Throwable {
127: InterceptTest.log("around3_pre_call ");
128: Object result = jp.proceed();
129: InterceptTest.log("around3_post_call ");
130: return result;
131: }
132: });
133:
134: LOG = "";
135: callee.adviseWithAroundStack();
136: assertEquals(
137: "around2_pre_call around3_pre_call adviseWithAroundStack around3_post_call around2_post_call ",
138: LOG);
139: }
140:
141: public void testAddBefore() {
142: Callee callee = new Callee();
143:
144: LOG = "";
145: callee.adviseWithBefore();
146: assertEquals("adviseWithBefore ", LOG);
147:
148: ((Advisable) this )
149: .aw_addAdvice(
150: "call(* test.intercept.call.Callee.adviseWithBefore(..))",
151: new BeforeAdvice() {
152: public void invoke(JoinPoint jp)
153: throws Throwable {
154: InterceptTest.log("before ");
155: }
156: });
157:
158: LOG = "";
159: callee.adviseWithBefore();
160: assertEquals("before adviseWithBefore ", LOG);
161: }
162:
163: public void testAddAfter() {
164: Callee callee = new Callee();
165:
166: LOG = "";
167: callee.adviseWithAfter();
168: assertEquals("adviseWithAfter ", LOG);
169:
170: ((Advisable) this )
171: .aw_addAdvice(
172: "call(* test.intercept.call.Callee.adviseWithAfter(..))",
173: new AfterAdvice() {
174: public void invoke(JoinPoint jp)
175: throws Throwable {
176: InterceptTest.log("afterFinally ");
177: }
178: });
179:
180: LOG = "";
181: callee.adviseWithAfter();
182: assertEquals("adviseWithAfter afterFinally ", LOG);
183: }
184:
185: public void testAddAfterReturning() {
186: Callee callee = new Callee();
187:
188: LOG = "";
189: callee.adviseWithAfterReturning();
190: assertEquals("adviseWithAfterReturning ", LOG);
191:
192: ((Advisable) this )
193: .aw_addAdvice(
194: "call(* test.intercept.call.Callee.adviseWithAfterReturning(..))",
195: new AfterReturningAdvice() {
196: public void invoke(JoinPoint jp,
197: Object returnValue)
198: throws Throwable {
199: InterceptTest.log("afterReturning ");
200: InterceptTest.log((String) returnValue);
201: InterceptTest.log(" ");
202: }
203: });
204:
205: LOG = "";
206: callee.adviseWithAfterReturning();
207: assertEquals(
208: "adviseWithAfterReturning afterReturning returnValue ",
209: LOG);
210: }
211:
212: public void testAddAfterThrowing() {
213: Callee callee = new Callee();
214:
215: LOG = "";
216: try {
217: callee.adviseWithAfterThrowing();
218: } catch (RuntimeException e) {
219: }
220: assertEquals("adviseWithAfterThrowing ", LOG);
221:
222: ((Advisable) this )
223: .aw_addAdvice(
224: "call(* test.intercept.call.Callee.adviseWithAfterThrowing(..))",
225: new AfterThrowingAdvice() {
226: public void invoke(JoinPoint jp,
227: Throwable exception)
228: throws Throwable {
229: InterceptTest.log("afterThrowing ");
230: InterceptTest.log(exception
231: .getMessage());
232: InterceptTest.log(" ");
233: }
234: });
235:
236: LOG = "";
237: try {
238: callee.adviseWithAfterThrowing();
239: } catch (RuntimeException e) {
240: }
241: assertEquals("adviseWithAfterThrowing afterThrowing noop ", LOG);
242: }
243:
244: public static void main(String[] args) {
245: junit.textui.TestRunner.run(suite());
246: }
247:
248: public static junit.framework.Test suite() {
249: return new junit.framework.TestSuite(InterceptTest.class);
250: }
251: }
|