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.set;
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
17 /***
18 * @author <a href="mailto:jboner@codehaus.org">Jonas BonŽr </a>
19 */
20 public class InterceptTest extends TestCase {
21 private static String LOG = "";
22
23 public static void log(String msg) {
24 LOG += msg;
25 }
26
27 public void testIsAdvisable() {
28 assertTrue(this instanceof Advisable);
29 }
30
31 public void testAddAround() {
32 LOG = "";
33 adviseWithAround = 1;
34 assertEquals("", LOG);
35
36 ((Advisable) this).aw_addAdvice(
37 "set(* test.intercept.set.InterceptTest.adviseWithAround)",
38 new AroundAdvice() {
39 public Object invoke(JoinPoint jp) throws Throwable {
40 InterceptTest.log("around1_pre ");
41 Object result = jp.proceed();
42 InterceptTest.log("around1_post ");
43 return result;
44 }
45 }
46 );
47
48 LOG = "";
49 adviseWithAround = 1;
50 assertEquals("around1_pre around1_post ", LOG);
51 }
52
53
54 public void testAddAndRemoveAround() {
55 LOG = "";
56 adviseWithAround2 = "test";
57 assertEquals("", LOG);
58
59 ((Advisable) this).aw_addAdvice(
60 "set(* test.intercept.set.InterceptTest.adviseWithAround2)",
61 new AroundAdvice() {
62 public Object invoke(JoinPoint jp) throws Throwable {
63 InterceptTest.log("around1_pre ");
64 Object result = jp.proceed();
65 InterceptTest.log("around1_post ");
66 return result;
67 }
68 }
69 );
70
71 LOG = "";
72 adviseWithAround2 = "test";
73 assertEquals("around1_pre around1_post ", LOG);
74
75 ((Advisable) this).aw_removeAdvice("set(* test.intercept.set.InterceptTest.adviseWithAround2)", AroundAdvice.class);
76
77 LOG = "";
78 adviseWithAround2 = "test";
79 assertEquals("", LOG);
80 }
81
82 public void testAddAroundStack() {
83 LOG = "";
84 adviseWithAroundStack = 2;
85 assertEquals("", LOG);
86
87 ((Advisable) this).aw_addAdvice(
88 "set(* test.intercept.set.InterceptTest.adviseWithAroundStack)",
89 new AroundAdvice() {
90 public Object invoke(JoinPoint jp) throws Throwable {
91 InterceptTest.log("around2_pre ");
92 Object result = jp.proceed();
93 InterceptTest.log("around2_post ");
94 return result;
95 }
96 }
97 );
98
99 LOG = "";
100 adviseWithAroundStack = 8;
101 assertEquals("around2_pre around2_post ", LOG);
102
103 ((Advisable) this).aw_addAdvice(
104 "set(* test.intercept.set.InterceptTest.adviseWithAroundStack)",
105 new AroundAdvice() {
106 public Object invoke(JoinPoint jp) throws Throwable {
107 InterceptTest.log("around3_pre ");
108 Object result = jp.proceed();
109 InterceptTest.log("around3_post ");
110 return result;
111 }
112 }
113 );
114
115 LOG = "";
116 adviseWithAroundStack = 4;
117 assertEquals("around2_pre around3_pre around3_post around2_post ", LOG);
118 }
119
120 public void testAddBefore() {
121 LOG = "";
122 adviseWithBefore = new Object();
123 assertEquals("", LOG);
124
125 ((Advisable) this).aw_addAdvice(
126 "set(* test.intercept.set.InterceptTest.adviseWithBefore)",
127 new BeforeAdvice() {
128 public void invoke(JoinPoint jp) throws Throwable {
129 InterceptTest.log("before ");
130 }
131 }
132 );
133
134 LOG = "";
135 adviseWithBefore = new Integer(1);
136 assertEquals("before ", LOG);
137 }
138
139 public void testAddAfter() {
140 LOG = "";
141 adviseWithAfter = false;
142 assertEquals("", LOG);
143
144 ((Advisable) this).aw_addAdvice(
145 "set(* test.intercept.set.InterceptTest.adviseWithAfter)",
146 new AfterAdvice() {
147 public void invoke(JoinPoint jp) throws Throwable {
148 InterceptTest.log("afterFinally ");
149 }
150 }
151 );
152
153 LOG = "";
154 adviseWithAfter = true;
155 assertEquals("afterFinally ", LOG);
156 }
157
158 public static void main(String[] args) {
159 junit.textui.TestRunner.run(suite());
160 }
161
162 public static junit.framework.Test suite() {
163 return new junit.framework.TestSuite(InterceptTest.class);
164 }
165
166 int adviseWithAround;
167
168 String adviseWithAround2;
169
170 int adviseWithAroundStack;
171
172 Object adviseWithBefore;
173
174 boolean adviseWithAfter;
175 }