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.staticinitialization;
9
10 import java.util.ArrayList;
11 import java.util.List;
12
13 import junit.framework.TestCase;
14
15 import org.codehaus.aspectwerkz.joinpoint.EnclosingStaticJoinPoint;
16 import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
17 import org.codehaus.aspectwerkz.joinpoint.Rtti;
18 import org.codehaus.aspectwerkz.joinpoint.Signature;
19 import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
20 import org.codehaus.aspectwerkz.joinpoint.impl.StaticInitializationRttiImpl;
21 import org.codehaus.aspectwerkz.joinpoint.impl.StaticInitializerSignatureImpl;
22 import org.codehaus.aspectwerkz.joinpoint.management.JoinPointType;
23 import test.CallerSideAdviceTest;
24
25
26 /***
27 * Test for staticinitialization pointcuts.
28 *
29 * @author <a href="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
30 */
31 public class StaticInitializationTest extends TestCase {
32 public static final String[] BEFORE_EXPECTED_MESSAGES = {
33 "beforeStaticinitialization",
34 "aroundStaticinitializationSJP",
35 "aroundStaticinitializationJP"
36 };
37
38 public static final String[] AFTER_EXPECTED_MESSAGES = {
39 "afterReturningStaticinitialization",
40 "afterStaticinititalization"
41 };
42
43 public static final String CLINIT_EXECUTION_MESSAGE = "<clinit>.execution";
44
45 public static List s_messages = new ArrayList();
46 public static List s_staticJoinPoints = new ArrayList();
47 public static List s_joinPoints = new ArrayList();
48
49 public void testStaticInitializer() throws ClassNotFoundException {
50 Class reflectClazz = Class.forName("test.staticinitialization.ClinitTarget");
51 try {
52
53 reflectClazz.newInstance();
54 } catch (Exception e) {
55 fail(e.toString());
56 }
57
58 checkMessages();
59
60 checkStaticJoinPoints(reflectClazz, s_staticJoinPoints);
61 checkStaticJoinPoints(reflectClazz, s_joinPoints);
62
63 checkJoinPoints(reflectClazz);
64 }
65
66 private void checkMessages() {
67 int messages = 3 * (BEFORE_EXPECTED_MESSAGES.length
68 + AFTER_EXPECTED_MESSAGES.length) + 1;
69
70 assertEquals("logged messages should match",
71 messages,
72 s_messages.size());
73
74 for(int i = 0; i < BEFORE_EXPECTED_MESSAGES.length; i++) {
75 for(int j = 0; j < 3; j++) {
76 assertEquals(BEFORE_EXPECTED_MESSAGES[i],
77 s_messages.get(i * 3 + j));
78 }
79 }
80
81 int lastBeforeIndex = 3 * BEFORE_EXPECTED_MESSAGES.length;
82
83 assertEquals("clinit was expected to execute",
84 CLINIT_EXECUTION_MESSAGE,
85 s_messages.get(lastBeforeIndex));
86
87 lastBeforeIndex++;
88
89 for(int i = 0; i < AFTER_EXPECTED_MESSAGES.length; i++) {
90 for(int j = 0; j < 3; j++) {
91 assertEquals(AFTER_EXPECTED_MESSAGES[i],
92 s_messages.get(lastBeforeIndex + (i * 3) + j));
93 }
94 }
95 }
96
97 private void checkStaticJoinPoints(Class clazz, List data) {
98 assertEquals("staticjoinpoints number does not match",
99 12,
100 data.size()
101 );
102
103 Class signatureClass = StaticInitializerSignatureImpl.class;
104
105 for(int i = 0; i < data.size(); i++) {
106 StaticJoinPoint sjp = (StaticJoinPoint) data.get(i);
107
108 assertEquals(clazz,
109 sjp.getCallerClass());
110
111 assertEquals(clazz,
112 sjp.getCalleeClass());
113
114 assertEquals(JoinPointType.STATIC_INITIALIZATION,
115 sjp.getType());
116
117 Signature signature = sjp.getSignature();
118 assertNotNull(signature);
119
120 assertEquals(signatureClass,
121 signature.getClass());
122
123 assertEquals(clazz,
124 signature.getDeclaringType());
125
126 EnclosingStaticJoinPoint esjp = sjp.getEnclosingStaticJoinPoint();
127
128 assertNotNull(esjp);
129
130 assertEquals(JoinPointType.STATIC_INITIALIZATION,
131 esjp.getType());
132
133 Signature enclSig = esjp.getSignature();
134
135 assertNotNull(enclSig);
136
137 assertEquals(signatureClass,
138 enclSig.getClass());
139
140 assertEquals(clazz,
141 enclSig.getDeclaringType());
142
143 }
144 }
145
146 private void checkJoinPoints(Class clazz) {
147 assertEquals("joinpoints number does not match",
148 12,
149 s_staticJoinPoints.size()
150 );
151
152 Class siRtti = StaticInitializationRttiImpl.class;
153
154 for(int i = 0; i < s_joinPoints.size(); i++) {
155 JoinPoint jp = (JoinPoint) s_joinPoints.get(i);
156
157 assertNull(jp.getCaller());
158
159 assertNull(jp.getThis());
160
161 assertNull(jp.getCallee());
162
163 assertNull(jp.getTarget());
164
165 Rtti rtti = jp.getRtti();
166
167 assertNotNull(rtti);
168
169 assertEquals(siRtti,
170 rtti.getClass());
171
172 assertEquals(clazz,
173 rtti.getDeclaringType()
174 );
175
176 assertNull(rtti.getThis());
177
178 assertNull(rtti.getTarget());
179 }
180 }
181
182 public static void main(String[] args) {
183 junit.textui.TestRunner.run(suite());
184 }
185
186 public static junit.framework.Test suite() {
187 return new junit.framework.TestSuite(StaticInitializationTest.class);
188 }
189 }