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.withincode;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import junit.framework.TestCase;
14
15 import org.codehaus.aspectwerkz.joinpoint.CatchClauseRtti;
16 import org.codehaus.aspectwerkz.joinpoint.CatchClauseSignature;
17 import org.codehaus.aspectwerkz.joinpoint.ConstructorRtti;
18 import org.codehaus.aspectwerkz.joinpoint.ConstructorSignature;
19 import org.codehaus.aspectwerkz.joinpoint.EnclosingStaticJoinPoint;
20 import org.codehaus.aspectwerkz.joinpoint.FieldRtti;
21 import org.codehaus.aspectwerkz.joinpoint.FieldSignature;
22 import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
23 import org.codehaus.aspectwerkz.joinpoint.MethodRtti;
24 import org.codehaus.aspectwerkz.joinpoint.MethodSignature;
25 import org.codehaus.aspectwerkz.joinpoint.Rtti;
26 import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
27 import org.codehaus.aspectwerkz.joinpoint.impl.StaticInitializerSignatureImpl;
28 import org.codehaus.aspectwerkz.joinpoint.management.JoinPointType;
29
30
31 /***
32 * Test for withincode(clinit).
33 *
34 * @author <a href="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
35 */
36 public class WithincodeClinitTest extends TestCase {
37 private static List s_messages = new ArrayList();
38 private static List s_staticJoinPoints = new ArrayList();
39 private static List s_joinPoints = new ArrayList();
40
41 private static final String[] EXPECTED_MSGS = {
42 "beforeCtorCall",
43 "beforeWithincodeClinitCtorCall",
44 "beforeWithincodeClinitPatternCtorCall",
45 "afterReturningCtorCall",
46 "afterCtorCall",
47 "afterWithincodeClinitCtorCall",
48 "afterWithincodeClinitPatternCtorCall",
49 "beforeGetSet",
50 "afterReturningGetSet",
51 "afterGetSet",
52 "beforeGetSet",
53 "afterReturningGetSet",
54 "afterGetSet",
55 "beforeMethodCall",
56 "afterThrowingTypeMethodCall",
57 "afterThrowingMethodCall",
58 "afterFinallyMethodCall",
59 "beforeHandler"
60 };
61
62 private static final Class[] EXPECTED_SIGNATURES = new Class[] {
63 ConstructorSignature.class,
64 ConstructorSignature.class,
65 ConstructorSignature.class,
66 ConstructorSignature.class,
67 FieldSignature.class,
68 FieldSignature.class,
69 FieldSignature.class,
70 FieldSignature.class,
71 FieldSignature.class,
72 FieldSignature.class,
73 FieldSignature.class,
74 FieldSignature.class,
75 MethodSignature.class,
76 MethodSignature.class,
77 MethodSignature.class,
78 MethodSignature.class,
79 MethodSignature.class,
80 CatchClauseSignature.class,
81 CatchClauseSignature.class
82 };
83
84 private static final JoinPointType[] EXPECTED_JP_TYPES = new JoinPointType[] {
85 JoinPointType.CONSTRUCTOR_CALL,
86 JoinPointType.CONSTRUCTOR_CALL,
87 JoinPointType.CONSTRUCTOR_CALL,
88 JoinPointType.CONSTRUCTOR_CALL,
89 JoinPointType.FIELD_SET,
90 JoinPointType.FIELD_SET,
91 JoinPointType.FIELD_SET,
92 JoinPointType.FIELD_SET,
93 JoinPointType.FIELD_GET,
94 JoinPointType.FIELD_GET,
95 JoinPointType.FIELD_GET,
96 JoinPointType.FIELD_GET,
97 JoinPointType.METHOD_CALL,
98 JoinPointType.METHOD_CALL,
99 JoinPointType.METHOD_CALL,
100 JoinPointType.METHOD_CALL,
101 JoinPointType.METHOD_CALL,
102 JoinPointType.HANDLER,
103 JoinPointType.HANDLER
104 };
105
106 private static final Class ENCLOSING_SJP_CLASS = StaticInitializerSignatureImpl.class;
107 private static final JoinPointType ENCLOSING_SJP_TYPE = JoinPointType.STATIC_INITIALIZATION;
108 private static final String CALLER_CLASS_NAME = "test.withincode.Target";
109 private static final String CALLER_INSTANCE = "null";
110
111 private static final String[] CALLEE_CLASS_NAME = {
112 "test.withincode.Target$CtorCallTarget",
113 "test.withincode.Target$CtorCallTarget",
114 "test.withincode.Target$CtorCallTarget",
115 "test.withincode.Target$CtorCallTarget",
116 "test.withincode.Target",
117 "test.withincode.Target",
118 "test.withincode.Target",
119 "test.withincode.Target",
120 "test.withincode.Target",
121 "test.withincode.Target",
122 "test.withincode.Target",
123 "test.withincode.Target",
124 "test.withincode.Target",
125 "test.withincode.Target",
126 "test.withincode.Target",
127 "test.withincode.Target",
128 "test.withincode.Target",
129 "test.handler.HandlerTestBeforeException",
130 "test.handler.HandlerTestBeforeException"
131 };
132
133 private static final Class[] RTTI_CLASS = new Class[] {
134 ConstructorRtti.class,
135 ConstructorRtti.class,
136 ConstructorRtti.class,
137 ConstructorRtti.class,
138 FieldRtti.class,
139 FieldRtti.class,
140 FieldRtti.class,
141 FieldRtti.class,
142 FieldRtti.class,
143 FieldRtti.class,
144 FieldRtti.class,
145 FieldRtti.class,
146 MethodRtti.class,
147 MethodRtti.class,
148 MethodRtti.class,
149 MethodRtti.class,
150 MethodRtti.class,
151 CatchClauseRtti.class,
152 CatchClauseRtti.class
153 };
154
155 public void testWithincode() {
156 Class clazz = Target.class;
157
158 try {
159
160 Object fake = clazz.newInstance();
161 } catch (Exception e) {
162 fail(e.toString());
163 }
164
165 checkMessages();
166
167 checkStaticJoinPoints();
168
169 checkJoinPoints();
170
171 }
172
173 private void checkMessages() {
174 assertEquals("no of occured messages",
175 EXPECTED_MSGS.length,
176 s_messages.size()
177 );
178
179 for(int i = 0; i < EXPECTED_MSGS.length; i++) {
180 assertEquals("expected message: " + EXPECTED_MSGS[i],
181 EXPECTED_MSGS[i],
182 s_messages.get(i)
183 );
184 }
185 }
186
187 private void checkStaticJoinPoints() {
188 assertEquals("captured SJP signature",
189 EXPECTED_SIGNATURES.length,
190 s_staticJoinPoints.size()
191 );
192
193 for(int i = 0; i < EXPECTED_SIGNATURES.length; i++) {
194 StaticJoinPoint sjp = (StaticJoinPoint) s_staticJoinPoints.get(i);
195
196
197 assertTrue(
198 EXPECTED_SIGNATURES[i].isAssignableFrom(sjp.getSignature().getClass())
199 );
200
201 assertEquals(EXPECTED_JP_TYPES[i], sjp.getType());
202
203 EnclosingStaticJoinPoint esjp = sjp.getEnclosingStaticJoinPoint();
204
205 assertTrue(
206 ENCLOSING_SJP_CLASS.isAssignableFrom(esjp.getSignature().getClass())
207 );
208
209 assertEquals(ENCLOSING_SJP_TYPE,
210 esjp.getType()
211 );
212 }
213 }
214
215 private void checkJoinPoints() {
216 assertEquals("captured JP signature",
217 EXPECTED_SIGNATURES.length,
218 s_joinPoints.size()
219 );
220
221 for(int i = 0; i < s_joinPoints.size(); i++) {
222 JoinPoint jp = (JoinPoint) s_joinPoints.get(i);
223
224 assertEquals(CALLER_CLASS_NAME, jp.getCallerClass().getName());
225 assertEquals(CALLEE_CLASS_NAME[i], jp.getCalleeClass().getName());
226 assertEquals(CALLER_INSTANCE, String.valueOf(jp.getCaller()));
227 assertEquals(CALLER_INSTANCE, String.valueOf(jp.getThis()));
228
229 if(i < 4 || i > s_joinPoints.size() - 3) {
230 assertNotNull(jp.getCallee());
231 assertNotNull(jp.getTarget());
232 } else {
233 assertEquals(CALLER_INSTANCE, String.valueOf(jp.getCallee()));
234 assertEquals(CALLER_INSTANCE, String.valueOf(jp.getTarget()));
235 }
236
237 Rtti rtti = jp.getRtti();
238
239 assertTrue("expected " + RTTI_CLASS[i].getName() + " found " + rtti.getClass().getName(),
240 RTTI_CLASS[i].isAssignableFrom(rtti.getClass()));
241 }
242 }
243
244 public static void addMessage(final String msg) {
245 s_messages.add(msg);
246 }
247
248 public static void addSJP(StaticJoinPoint sjp) {
249 s_staticJoinPoints.add(sjp);
250 }
251
252 public static void addJP(JoinPoint jp) {
253 s_joinPoints.add(jp);
254 }
255
256 public static void main(String[] args) {
257 junit.textui.TestRunner.run(WithincodeClinitTest.class);
258 }
259
260 public static junit.framework.Test suite() {
261 return new junit.framework.TestSuite(WithincodeClinitTest.class);
262 }
263
264
265 }