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.staticinitialization;
008:
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import junit.framework.TestCase;
013:
014: import org.codehaus.aspectwerkz.joinpoint.EnclosingStaticJoinPoint;
015: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
016: import org.codehaus.aspectwerkz.joinpoint.Rtti;
017: import org.codehaus.aspectwerkz.joinpoint.Signature;
018: import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
019: import org.codehaus.aspectwerkz.joinpoint.impl.StaticInitializationRttiImpl;
020: import org.codehaus.aspectwerkz.joinpoint.impl.StaticInitializerSignatureImpl;
021: import org.codehaus.aspectwerkz.joinpoint.management.JoinPointType;
022: import test.CallerSideAdviceTest;
023:
024: /**
025: * Test for staticinitialization pointcuts.
026: *
027: * @author <a href="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
028: */
029: public class StaticInitializationTest extends TestCase {
030: public static final String[] BEFORE_EXPECTED_MESSAGES = {
031: "beforeStaticinitialization",
032: "aroundStaticinitializationSJP",
033: "aroundStaticinitializationJP" };
034:
035: public static final String[] AFTER_EXPECTED_MESSAGES = {
036: "afterReturningStaticinitialization",
037: "afterStaticinititalization" };
038:
039: public static final String CLINIT_EXECUTION_MESSAGE = "<clinit>.execution";
040:
041: public static List s_messages = new ArrayList();
042: public static List s_staticJoinPoints = new ArrayList();
043: public static List s_joinPoints = new ArrayList();
044:
045: public void testStaticInitializer() throws ClassNotFoundException {
046: Class reflectClazz = Class
047: .forName("test.staticinitialization.ClinitTarget");
048: try {
049: // required to run the clinit on Java 1.5
050: reflectClazz.newInstance();
051: } catch (Exception e) {
052: fail(e.toString());
053: }
054:
055: checkMessages();
056:
057: checkStaticJoinPoints(reflectClazz, s_staticJoinPoints);
058: checkStaticJoinPoints(reflectClazz, s_joinPoints);
059:
060: checkJoinPoints(reflectClazz);
061: }
062:
063: private void checkMessages() {
064: int messages = 3 * (BEFORE_EXPECTED_MESSAGES.length + AFTER_EXPECTED_MESSAGES.length) + 1;
065:
066: assertEquals("logged messages should match", messages,
067: s_messages.size());
068:
069: for (int i = 0; i < BEFORE_EXPECTED_MESSAGES.length; i++) {
070: for (int j = 0; j < 3; j++) {
071: assertEquals(BEFORE_EXPECTED_MESSAGES[i], s_messages
072: .get(i * 3 + j));
073: }
074: }
075:
076: int lastBeforeIndex = 3 * BEFORE_EXPECTED_MESSAGES.length;
077:
078: assertEquals("clinit was expected to execute",
079: CLINIT_EXECUTION_MESSAGE, s_messages
080: .get(lastBeforeIndex));
081:
082: lastBeforeIndex++;
083:
084: for (int i = 0; i < AFTER_EXPECTED_MESSAGES.length; i++) {
085: for (int j = 0; j < 3; j++) {
086: assertEquals(AFTER_EXPECTED_MESSAGES[i], s_messages
087: .get(lastBeforeIndex + (i * 3) + j));
088: }
089: }
090: }
091:
092: private void checkStaticJoinPoints(Class clazz, List data) {
093: assertEquals("staticjoinpoints number does not match", 12, data
094: .size());
095:
096: Class signatureClass = StaticInitializerSignatureImpl.class;
097:
098: for (int i = 0; i < data.size(); i++) {
099: StaticJoinPoint sjp = (StaticJoinPoint) data.get(i);
100:
101: assertEquals(clazz, sjp.getCallerClass());
102:
103: assertEquals(clazz, sjp.getCalleeClass());
104:
105: assertEquals(JoinPointType.STATIC_INITIALIZATION, sjp
106: .getType());
107:
108: Signature signature = sjp.getSignature();
109: assertNotNull(signature);
110:
111: assertEquals(signatureClass, signature.getClass());
112:
113: assertEquals(clazz, signature.getDeclaringType());
114:
115: EnclosingStaticJoinPoint esjp = sjp
116: .getEnclosingStaticJoinPoint();
117:
118: assertNotNull(esjp);
119:
120: assertEquals(JoinPointType.STATIC_INITIALIZATION, esjp
121: .getType());
122:
123: Signature enclSig = esjp.getSignature();
124:
125: assertNotNull(enclSig);
126:
127: assertEquals(signatureClass, enclSig.getClass());
128:
129: assertEquals(clazz, enclSig.getDeclaringType());
130:
131: }
132: }
133:
134: private void checkJoinPoints(Class clazz) {
135: assertEquals("joinpoints number does not match", 12,
136: s_staticJoinPoints.size());
137:
138: Class siRtti = StaticInitializationRttiImpl.class;
139:
140: for (int i = 0; i < s_joinPoints.size(); i++) {
141: JoinPoint jp = (JoinPoint) s_joinPoints.get(i);
142:
143: assertNull(jp.getCaller());
144:
145: assertNull(jp.getThis());
146:
147: assertNull(jp.getCallee());
148:
149: assertNull(jp.getTarget());
150:
151: Rtti rtti = jp.getRtti();
152:
153: assertNotNull(rtti);
154:
155: assertEquals(siRtti, rtti.getClass());
156:
157: assertEquals(clazz, rtti.getDeclaringType());
158:
159: assertNull(rtti.getThis());
160:
161: assertNull(rtti.getTarget());
162: }
163: }
164:
165: public static void main(String[] args) {
166: junit.textui.TestRunner.run(suite());
167: }
168:
169: public static junit.framework.Test suite() {
170: return new junit.framework.TestSuite(
171: StaticInitializationTest.class);
172: }
173: }
|