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.get;
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:
016: /**
017: * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
018: */
019: public class InterceptTest extends TestCase {
020: private static String LOG = "";
021:
022: public static void log(String msg) {
023: LOG += msg;
024: }
025:
026: public void testIsAdvisable() {
027: assertTrue(this instanceof Advisable);
028: }
029:
030: public void testAddAround() {
031: LOG = "";
032: int tmp1 = adviseWithAround;
033: assertEquals("", LOG);
034:
035: ((Advisable) this )
036: .aw_addAdvice(
037: "get(* test.intercept.get.InterceptTest.adviseWithAround)",
038: new AroundAdvice() {
039: public Object invoke(JoinPoint jp)
040: throws Throwable {
041: InterceptTest.log("around1_pre ");
042: Object result = jp.proceed();
043: InterceptTest.log("around1_post ");
044: return result;
045: }
046: });
047:
048: LOG = "";
049: int tmp2 = adviseWithAround;
050: assertEquals("around1_pre around1_post ", LOG);
051: }
052:
053: public void testAddAndRemoveAround() {
054: LOG = "";
055: String tmp1 = adviseWithAround2;
056: assertEquals("", LOG);
057:
058: ((Advisable) this )
059: .aw_addAdvice(
060: "get(* test.intercept.get.InterceptTest.adviseWithAround2)",
061: new AroundAdvice() {
062: public Object invoke(JoinPoint jp)
063: throws Throwable {
064: InterceptTest.log("around1_pre ");
065: Object result = jp.proceed();
066: InterceptTest.log("around1_post ");
067: return result;
068: }
069: });
070:
071: LOG = "";
072: String tmp2 = adviseWithAround2;
073: assertEquals("around1_pre around1_post ", LOG);
074:
075: ((Advisable) this )
076: .aw_removeAdvice(
077: "get(* test.intercept.get.InterceptTest.adviseWithAround2)",
078: AroundAdvice.class);
079:
080: LOG = "";
081: String tmp3 = adviseWithAround2;
082: assertEquals("", LOG);
083: }
084:
085: public void testAddAroundStack() {
086: LOG = "";
087: int tmp1 = adviseWithAroundStack;
088: assertEquals("", LOG);
089:
090: ((Advisable) this )
091: .aw_addAdvice(
092: "get(* test.intercept.get.InterceptTest.adviseWithAroundStack)",
093: new AroundAdvice() {
094: public Object invoke(JoinPoint jp)
095: throws Throwable {
096: InterceptTest.log("around2_pre ");
097: Object result = jp.proceed();
098: InterceptTest.log("around2_post ");
099: return result;
100: }
101: });
102:
103: LOG = "";
104: int tmp2 = adviseWithAroundStack;
105: assertEquals("around2_pre around2_post ", LOG);
106:
107: ((Advisable) this )
108: .aw_addAdvice(
109: "get(* test.intercept.get.InterceptTest.adviseWithAroundStack)",
110: new AroundAdvice() {
111: public Object invoke(JoinPoint jp)
112: throws Throwable {
113: InterceptTest.log("around3_pre ");
114: Object result = jp.proceed();
115: InterceptTest.log("around3_post ");
116: return result;
117: }
118: });
119:
120: LOG = "";
121: int tmp3 = adviseWithAroundStack;
122: assertEquals(
123: "around2_pre around3_pre around3_post around2_post ",
124: LOG);
125: }
126:
127: public void testAddBefore() {
128: LOG = "";
129: Object tmp1 = adviseWithBefore;
130: assertEquals("", LOG);
131:
132: ((Advisable) this )
133: .aw_addAdvice(
134: "get(* test.intercept.get.InterceptTest.adviseWithBefore)",
135: new BeforeAdvice() {
136: public void invoke(JoinPoint jp)
137: throws Throwable {
138: InterceptTest.log("before ");
139: }
140: });
141:
142: LOG = "";
143: Object tmp2 = adviseWithBefore;
144: assertEquals("before ", LOG);
145: }
146:
147: public void testAddAfter() {
148: LOG = "";
149: boolean tmp1 = adviseWithAfter;
150: assertEquals("", LOG);
151:
152: ((Advisable) this )
153: .aw_addAdvice(
154: "get(* test.intercept.get.InterceptTest.adviseWithAfter)",
155: new AfterAdvice() {
156: public void invoke(JoinPoint jp)
157: throws Throwable {
158: InterceptTest.log("afterFinally ");
159: }
160: });
161:
162: LOG = "";
163: boolean tmp2 = adviseWithAfter;
164: assertEquals("afterFinally ", LOG);
165: }
166:
167: public static void main(String[] args) {
168: junit.textui.TestRunner.run(suite());
169: }
170:
171: public static junit.framework.Test suite() {
172: return new junit.framework.TestSuite(InterceptTest.class);
173: }
174:
175: int adviseWithAround = -1;
176:
177: String adviseWithAround2 = "lala";
178:
179: int adviseWithAroundStack = 135;
180:
181: Object adviseWithBefore = new Boolean(true);
182:
183: boolean adviseWithAfter = false;
184: }
|