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.withincode;
008:
009: import java.util.ArrayList;
010: import java.util.List;
011:
012: import junit.framework.TestCase;
013:
014: import org.codehaus.aspectwerkz.joinpoint.CatchClauseRtti;
015: import org.codehaus.aspectwerkz.joinpoint.CatchClauseSignature;
016: import org.codehaus.aspectwerkz.joinpoint.ConstructorRtti;
017: import org.codehaus.aspectwerkz.joinpoint.ConstructorSignature;
018: import org.codehaus.aspectwerkz.joinpoint.EnclosingStaticJoinPoint;
019: import org.codehaus.aspectwerkz.joinpoint.FieldRtti;
020: import org.codehaus.aspectwerkz.joinpoint.FieldSignature;
021: import org.codehaus.aspectwerkz.joinpoint.JoinPoint;
022: import org.codehaus.aspectwerkz.joinpoint.MethodRtti;
023: import org.codehaus.aspectwerkz.joinpoint.MethodSignature;
024: import org.codehaus.aspectwerkz.joinpoint.Rtti;
025: import org.codehaus.aspectwerkz.joinpoint.StaticJoinPoint;
026: import org.codehaus.aspectwerkz.joinpoint.impl.StaticInitializerSignatureImpl;
027: import org.codehaus.aspectwerkz.joinpoint.management.JoinPointType;
028:
029: /**
030: * Test for withincode(clinit).
031: *
032: * @author <a href="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
033: */
034: public class WithincodeClinitTest extends TestCase {
035: private static List s_messages = new ArrayList();
036: private static List s_staticJoinPoints = new ArrayList();
037: private static List s_joinPoints = new ArrayList();
038:
039: private static final String[] EXPECTED_MSGS = { "beforeCtorCall",
040: "beforeWithincodeClinitCtorCall",
041: "beforeWithincodeClinitPatternCtorCall",
042: "afterReturningCtorCall", "afterCtorCall",
043: "afterWithincodeClinitCtorCall",
044: "afterWithincodeClinitPatternCtorCall", "beforeGetSet",
045: "afterReturningGetSet", "afterGetSet", "beforeGetSet",
046: "afterReturningGetSet", "afterGetSet", "beforeMethodCall",
047: "afterThrowingTypeMethodCall", "afterThrowingMethodCall",
048: "afterFinallyMethodCall", "beforeHandler" };
049:
050: private static final Class[] EXPECTED_SIGNATURES = new Class[] {
051: ConstructorSignature.class, ConstructorSignature.class,
052: ConstructorSignature.class, ConstructorSignature.class,
053: FieldSignature.class, FieldSignature.class,
054: FieldSignature.class, FieldSignature.class,
055: FieldSignature.class, FieldSignature.class,
056: FieldSignature.class, FieldSignature.class,
057: MethodSignature.class, MethodSignature.class,
058: MethodSignature.class, MethodSignature.class,
059: MethodSignature.class, CatchClauseSignature.class,
060: CatchClauseSignature.class };
061:
062: private static final JoinPointType[] EXPECTED_JP_TYPES = new JoinPointType[] {
063: JoinPointType.CONSTRUCTOR_CALL,
064: JoinPointType.CONSTRUCTOR_CALL,
065: JoinPointType.CONSTRUCTOR_CALL,
066: JoinPointType.CONSTRUCTOR_CALL, JoinPointType.FIELD_SET,
067: JoinPointType.FIELD_SET, JoinPointType.FIELD_SET,
068: JoinPointType.FIELD_SET, JoinPointType.FIELD_GET,
069: JoinPointType.FIELD_GET, JoinPointType.FIELD_GET,
070: JoinPointType.FIELD_GET, JoinPointType.METHOD_CALL,
071: JoinPointType.METHOD_CALL, JoinPointType.METHOD_CALL,
072: JoinPointType.METHOD_CALL, JoinPointType.METHOD_CALL,
073: JoinPointType.HANDLER, JoinPointType.HANDLER };
074:
075: private static final Class ENCLOSING_SJP_CLASS = StaticInitializerSignatureImpl.class;
076: private static final JoinPointType ENCLOSING_SJP_TYPE = JoinPointType.STATIC_INITIALIZATION;
077: private static final String CALLER_CLASS_NAME = "test.withincode.Target";
078: private static final String CALLER_INSTANCE = "null";
079:
080: private static final String[] CALLEE_CLASS_NAME = {
081: "test.withincode.Target$CtorCallTarget",
082: "test.withincode.Target$CtorCallTarget",
083: "test.withincode.Target$CtorCallTarget",
084: "test.withincode.Target$CtorCallTarget",
085: "test.withincode.Target", "test.withincode.Target",
086: "test.withincode.Target", "test.withincode.Target",
087: "test.withincode.Target", "test.withincode.Target",
088: "test.withincode.Target", "test.withincode.Target",
089: "test.withincode.Target", "test.withincode.Target",
090: "test.withincode.Target", "test.withincode.Target",
091: "test.withincode.Target",
092: "test.handler.HandlerTestBeforeException",
093: "test.handler.HandlerTestBeforeException" };
094:
095: private static final Class[] RTTI_CLASS = new Class[] {
096: ConstructorRtti.class, ConstructorRtti.class,
097: ConstructorRtti.class, ConstructorRtti.class,
098: FieldRtti.class, FieldRtti.class, FieldRtti.class,
099: FieldRtti.class, FieldRtti.class, FieldRtti.class,
100: FieldRtti.class, FieldRtti.class, MethodRtti.class,
101: MethodRtti.class, MethodRtti.class, MethodRtti.class,
102: MethodRtti.class, CatchClauseRtti.class,
103: CatchClauseRtti.class };
104:
105: public void testWithincode() {
106: Class clazz = Target.class;
107:
108: try {
109: // enfore clazz clinit triggering
110: Object fake = clazz.newInstance();
111: } catch (Exception e) {
112: fail(e.toString());
113: }
114:
115: checkMessages();
116:
117: checkStaticJoinPoints();
118:
119: checkJoinPoints();
120:
121: }
122:
123: private void checkMessages() {
124: assertEquals("no of occured messages", EXPECTED_MSGS.length,
125: s_messages.size());
126:
127: for (int i = 0; i < EXPECTED_MSGS.length; i++) {
128: assertEquals("expected message: " + EXPECTED_MSGS[i],
129: EXPECTED_MSGS[i], s_messages.get(i));
130: }
131: }
132:
133: private void checkStaticJoinPoints() {
134: assertEquals("captured SJP signature",
135: EXPECTED_SIGNATURES.length, s_staticJoinPoints.size());
136:
137: for (int i = 0; i < EXPECTED_SIGNATURES.length; i++) {
138: StaticJoinPoint sjp = (StaticJoinPoint) s_staticJoinPoints
139: .get(i);
140:
141: assertTrue(EXPECTED_SIGNATURES[i].isAssignableFrom(sjp
142: .getSignature().getClass()));
143:
144: assertEquals(EXPECTED_JP_TYPES[i], sjp.getType());
145:
146: EnclosingStaticJoinPoint esjp = sjp
147: .getEnclosingStaticJoinPoint();
148:
149: assertTrue(ENCLOSING_SJP_CLASS.isAssignableFrom(esjp
150: .getSignature().getClass()));
151:
152: assertEquals(ENCLOSING_SJP_TYPE, esjp.getType());
153: }
154: }
155:
156: private void checkJoinPoints() {
157: assertEquals("captured JP signature",
158: EXPECTED_SIGNATURES.length, s_joinPoints.size());
159:
160: for (int i = 0; i < s_joinPoints.size(); i++) {
161: JoinPoint jp = (JoinPoint) s_joinPoints.get(i);
162:
163: assertEquals(CALLER_CLASS_NAME, jp.getCallerClass()
164: .getName());
165: assertEquals(CALLEE_CLASS_NAME[i], jp.getCalleeClass()
166: .getName());
167: assertEquals(CALLER_INSTANCE, String
168: .valueOf(jp.getCaller()));
169: assertEquals(CALLER_INSTANCE, String.valueOf(jp.getThis()));
170:
171: if (i < 4 || i > s_joinPoints.size() - 3) { // CTORS and HANDLERS CALLEE
172: assertNotNull(jp.getCallee());
173: assertNotNull(jp.getTarget());
174: } else {
175: assertEquals(CALLER_INSTANCE, String.valueOf(jp
176: .getCallee()));
177: assertEquals(CALLER_INSTANCE, String.valueOf(jp
178: .getTarget()));
179: }
180:
181: Rtti rtti = jp.getRtti();
182:
183: assertTrue("expected " + RTTI_CLASS[i].getName()
184: + " found " + rtti.getClass().getName(),
185: RTTI_CLASS[i].isAssignableFrom(rtti.getClass()));
186: }
187: }
188:
189: public static void addMessage(final String msg) {
190: s_messages.add(msg);
191: }
192:
193: public static void addSJP(StaticJoinPoint sjp) {
194: s_staticJoinPoints.add(sjp);
195: }
196:
197: public static void addJP(JoinPoint jp) {
198: s_joinPoints.add(jp);
199: }
200:
201: public static void main(String[] args) {
202: junit.textui.TestRunner.run(WithincodeClinitTest.class);
203: }
204:
205: public static junit.framework.Test suite() {
206: return new junit.framework.TestSuite(WithincodeClinitTest.class);
207: }
208:
209: }
|