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.enclosingsjp;
008:
009: import java.lang.reflect.Constructor;
010: import java.lang.reflect.Method;
011: import java.util.ArrayList;
012: import java.util.List;
013:
014: import org.codehaus.aspectwerkz.joinpoint.ConstructorSignature;
015: import org.codehaus.aspectwerkz.joinpoint.EnclosingStaticJoinPoint;
016: import org.codehaus.aspectwerkz.joinpoint.MethodSignature;
017: import org.codehaus.aspectwerkz.joinpoint.Signature;
018: import org.codehaus.aspectwerkz.joinpoint.management.JoinPointType;
019:
020: import junit.framework.TestCase;
021:
022: public class EnclosingSJPTest extends TestCase {
023: private static List s_enclosingStaticJPList = new ArrayList();
024:
025: /**
026: * @see junit.framework.TestCase#setUp()
027: */
028: protected void setUp() throws Exception {
029: s_enclosingStaticJPList.clear();
030: }
031:
032: public static void registerEnclosingSJP(
033: EnclosingStaticJoinPoint esjp) {
034: s_enclosingStaticJPList.add(esjp);
035: }
036:
037: public void testConstructorEnclosing() throws NoSuchMethodException {
038: EnclosingTarget et = new EnclosingTarget();
039:
040: Class[] expectedSignaturesTypes = new Class[] {
041: ConstructorSignature.class, ConstructorSignature.class,
042: ConstructorSignature.class, ConstructorSignature.class,
043: ConstructorSignature.class, ConstructorSignature.class,
044: ConstructorSignature.class, ConstructorSignature.class };
045:
046: JoinPointType[] expectedJPTypes = new JoinPointType[] {
047: JoinPointType.CONSTRUCTOR_EXECUTION,
048: JoinPointType.CONSTRUCTOR_EXECUTION,
049: JoinPointType.CONSTRUCTOR_EXECUTION,
050: JoinPointType.CONSTRUCTOR_EXECUTION,
051: JoinPointType.CONSTRUCTOR_EXECUTION,
052: JoinPointType.CONSTRUCTOR_EXECUTION,
053: JoinPointType.CONSTRUCTOR_EXECUTION,
054: JoinPointType.CONSTRUCTOR_EXECUTION };
055:
056: check(expectedSignaturesTypes, expectedJPTypes,
057: s_enclosingStaticJPList);
058:
059: Constructor ctor = et.getClass().getConstructor(new Class[0]);
060:
061: for (int i = 0; i < s_enclosingStaticJPList.size(); i++) {
062: ConstructorSignature ctorSig = (ConstructorSignature) ((EnclosingStaticJoinPoint) s_enclosingStaticJPList
063: .get(i)).getSignature();
064:
065: assertTrue("" + ctor.toString(), ctor.equals(ctorSig
066: .getConstructor()));
067: }
068: }
069:
070: public void testHandlerEnclosing() throws NoSuchMethodException {
071: try {
072: throw new IllegalAccessException("msg1");
073: } catch (IllegalAccessException iae) {
074: ;
075: }
076:
077: EnclosingTarget et = new EnclosingTarget(1);
078:
079: Class[] expectedSignaturesTypes = new Class[] {
080: MethodSignature.class, MethodSignature.class,
081: ConstructorSignature.class, ConstructorSignature.class };
082:
083: JoinPointType[] expectedJPTypes = new JoinPointType[] {
084: JoinPointType.METHOD_EXECUTION,
085: JoinPointType.METHOD_EXECUTION,
086: JoinPointType.CONSTRUCTOR_EXECUTION,
087: JoinPointType.CONSTRUCTOR_EXECUTION };
088:
089: check(expectedSignaturesTypes, expectedJPTypes,
090: s_enclosingStaticJPList);
091:
092: Constructor ctor = et.getClass().getConstructor(
093: new Class[] { int.class });
094: Method meth = getClass()
095: .getMethod("testHandlerEnclosing", null);
096:
097: assertTrue(
098: meth.toString(),
099: meth
100: .equals(((MethodSignature) ((EnclosingStaticJoinPoint) s_enclosingStaticJPList
101: .get(0)).getSignature()).getMethod()));
102: assertTrue(
103: meth.toString(),
104: meth
105: .equals(((MethodSignature) ((EnclosingStaticJoinPoint) s_enclosingStaticJPList
106: .get(1)).getSignature()).getMethod()));
107: assertTrue(
108: ctor.toString(),
109: ctor
110: .equals(((ConstructorSignature) ((EnclosingStaticJoinPoint) s_enclosingStaticJPList
111: .get(2)).getSignature())
112: .getConstructor()));
113: assertTrue(
114: ctor.toString(),
115: ctor
116: .equals(((ConstructorSignature) ((EnclosingStaticJoinPoint) s_enclosingStaticJPList
117: .get(3)).getSignature())
118: .getConstructor()));
119: }
120:
121: public void testGetSet() throws NoSuchMethodException {
122: EnclosingTarget et = new EnclosingTarget(new Object());
123:
124: Class[] expectedSignatureTypes = new Class[] {
125: ConstructorSignature.class, ConstructorSignature.class,
126: ConstructorSignature.class, MethodSignature.class,
127: MethodSignature.class, MethodSignature.class,
128: MethodSignature.class, MethodSignature.class,
129: MethodSignature.class };
130:
131: JoinPointType[] expectedJPTypes = new JoinPointType[] {
132: JoinPointType.CONSTRUCTOR_EXECUTION,
133: JoinPointType.CONSTRUCTOR_EXECUTION,
134: JoinPointType.CONSTRUCTOR_EXECUTION,
135: JoinPointType.METHOD_EXECUTION,
136: JoinPointType.METHOD_EXECUTION,
137: JoinPointType.METHOD_EXECUTION,
138: JoinPointType.METHOD_EXECUTION,
139: JoinPointType.METHOD_EXECUTION,
140: JoinPointType.METHOD_EXECUTION };
141:
142: check(expectedSignatureTypes, expectedJPTypes,
143: s_enclosingStaticJPList);
144:
145: Constructor ctor = et.getClass().getConstructor(
146: new Class[] { Object.class });
147: Method setMethod = PointcutTarget.class.getMethod(
148: "setFieldValue", new Class[] { Object.class });
149: Method getMethod = PointcutTarget.class.getMethod(
150: "getFieldValue", null);
151:
152: for (int i = 0; i < 3; i++) {
153: EnclosingStaticJoinPoint esjp = (EnclosingStaticJoinPoint) s_enclosingStaticJPList
154: .get(i);
155: Constructor enclosingCtor = ((ConstructorSignature) esjp
156: .getSignature()).getConstructor();
157:
158: assertTrue(ctor.toString(), ctor.equals(enclosingCtor));
159: }
160:
161: for (int i = 3; i < 6; i++) {
162: EnclosingStaticJoinPoint esjp = (EnclosingStaticJoinPoint) s_enclosingStaticJPList
163: .get(i);
164: Method method = ((MethodSignature) esjp.getSignature())
165: .getMethod();
166:
167: assertTrue(setMethod.toString(), setMethod.equals(method));
168: }
169:
170: for (int i = 6; i < s_enclosingStaticJPList.size(); i++) {
171: EnclosingStaticJoinPoint esjp = (EnclosingStaticJoinPoint) s_enclosingStaticJPList
172: .get(i);
173: Method method = ((MethodSignature) esjp.getSignature())
174: .getMethod();
175:
176: assertTrue(getMethod.toString(), getMethod.equals(method));
177: }
178:
179: }
180:
181: public static void main(String[] args) {
182: junit.textui.TestRunner.run(EnclosingSJPTest.class);
183: }
184:
185: public static junit.framework.Test suite() {
186: return new junit.framework.TestSuite(EnclosingSJPTest.class);
187: }
188:
189: private void check(Class[] signatureClass,
190: JoinPointType[] enclosingTypes, List esjpList) {
191:
192: assertEquals(enclosingTypes.length, esjpList.size());
193:
194: for (int i = 0; i < enclosingTypes.length; i++) {
195: EnclosingStaticJoinPoint esjp = (EnclosingStaticJoinPoint) esjpList
196: .get(i);
197:
198: assertNotNull(
199: "EnclosingStaticJoinPoint should never be null",
200: esjp);
201:
202: assertNotNull("Signature should not be null", esjp
203: .getSignature());
204:
205: Signature sig = esjp.getSignature();
206:
207: if (sig instanceof ConstructorSignature) {
208: assertNotNull(((ConstructorSignature) sig)
209: .getConstructor());
210: } else if (sig instanceof MethodSignature) {
211: assertNotNull(((MethodSignature) sig).getMethod());
212: } else {
213: fail("unexpected signature type: "
214: + sig.getClass().getName());
215: }
216:
217: assertEquals("expectation on enclosing JP type failed",
218: enclosingTypes[i], esjp.getType());
219:
220: assertTrue(
221: "expectation on enclosing Signature class failed",
222: (signatureClass[i].isAssignableFrom(esjp
223: .getSignature().getClass())));
224:
225: }
226: }
227: }
|