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.expression;
008:
009: import java.lang.reflect.Constructor;
010: import java.lang.reflect.Field;
011: import java.lang.reflect.Method;
012:
013: import junit.extensions.TestSetup;
014: import junit.framework.Test;
015: import junit.framework.TestCase;
016: import junit.framework.TestSuite;
017:
018: import org.codehaus.aspectwerkz.expression.ExpressionContext;
019: import org.codehaus.aspectwerkz.expression.ExpressionInfo;
020: import org.codehaus.aspectwerkz.expression.PointcutType;
021: import org.codehaus.aspectwerkz.reflect.ClassInfo;
022: import org.codehaus.aspectwerkz.reflect.ConstructorInfo;
023: import org.codehaus.aspectwerkz.reflect.FieldInfo;
024: import org.codehaus.aspectwerkz.reflect.MethodInfo;
025: import org.codehaus.aspectwerkz.reflect.ReflectHelper;
026: import org.codehaus.aspectwerkz.reflect.impl.java.JavaClassInfo;
027:
028: /**
029: * Unit test for annotation matching.
030: *
031: * @author <a href="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
032: * @version $Revision: 1.3 $
033: */
034: public class AnnotationExpressionTest extends TestCase {
035: protected static final String NAMESPACE = "TESTING";
036:
037: protected static ClassInfo s_declaringType = JavaClassInfo
038: .getClassInfo(AnnotationTarget.class);
039: protected static ClassInfo s_innerType = JavaClassInfo
040: .getClassInfo(AnnotationTarget.ClassLevelAnnotation.class);
041:
042: protected static ConstructorInfo s_constructor;
043: protected static ConstructorInfo s_innerConstructor;
044:
045: protected static MethodInfo s_method;
046: protected static MethodInfo s_innerMethod;
047:
048: protected static FieldInfo s_field;
049: protected static FieldInfo s_innerField;
050:
051: static {
052: try {
053: setMeUp();
054: } catch (Throwable t) {
055: throw new Error(t.toString());
056: }
057: }
058:
059: public void testConstructor() {
060: assertTrue(new ExpressionInfo("call(@DefaultConstructor)",
061: NAMESPACE).getExpression().match(
062: new ExpressionContext(PointcutType.CALL, s_constructor,
063: null)));
064:
065: assertTrue(new ExpressionInfo(
066: "call(@DefaultConstructor new())", NAMESPACE)
067: .getExpression().match(
068: new ExpressionContext(PointcutType.CALL,
069: s_constructor, null)));
070:
071: assertTrue(new ExpressionInfo(
072: "call(@DefaultConstructor new(..))", NAMESPACE)
073: .getExpression().match(
074: new ExpressionContext(PointcutType.CALL,
075: s_constructor, null)));
076:
077: assertTrue(new ExpressionInfo(
078: "call(@DefaultConstructor *.new())", NAMESPACE)
079: .getExpression().match(
080: new ExpressionContext(PointcutType.CALL,
081: s_constructor, null)));
082:
083: assertTrue(new ExpressionInfo(
084: "call(@DefaultConstructor test.*.AnnotationTarget.new())",
085: NAMESPACE).getExpression().match(
086: new ExpressionContext(PointcutType.CALL, s_constructor,
087: null)));
088:
089: assertTrue(new ExpressionInfo(
090: "call(@DefaultConstructor test.expression.AnnotationTarget.new())",
091: NAMESPACE).getExpression().match(
092: new ExpressionContext(PointcutType.CALL, s_constructor,
093: null)));
094:
095: assertFalse(new ExpressionInfo("call(@OtherConstructor)",
096: NAMESPACE).getExpression().match(
097: new ExpressionContext(PointcutType.CALL, s_constructor,
098: null)));
099:
100: assertFalse(new ExpressionInfo(
101: "call(@DefaultConstructor new(*))", NAMESPACE)
102: .getExpression().match(
103: new ExpressionContext(PointcutType.CALL,
104: s_constructor, null)));
105:
106: assertFalse(new ExpressionInfo(
107: "call(@DefaultConstructor test.expression.AnnotationTargetWRONG.new())",
108: NAMESPACE).getExpression().match(
109: new ExpressionContext(PointcutType.CALL, s_constructor,
110: null)));
111:
112: assertTrue(new ExpressionInfo(
113: "within(test.expression.AnnotationTarget) && execution(@DefaultConstructor)",
114: NAMESPACE).getExpression().match(
115: new ExpressionContext(PointcutType.EXECUTION,
116: s_constructor, s_declaringType)));
117:
118: assertTrue(new ExpressionInfo(
119: "within(test.expression.*) && execution(@DefaultConstructor)",
120: NAMESPACE).getExpression().match(
121: new ExpressionContext(PointcutType.EXECUTION,
122: s_constructor, s_declaringType)));
123:
124: assertTrue(new ExpressionInfo(
125: "within(@Service) && execution(@DefaultConstructor)",
126: NAMESPACE).getExpression().match(
127: new ExpressionContext(PointcutType.EXECUTION,
128: s_innerConstructor, s_innerType)));
129:
130: // HINT: constructor on AnnotationTarget
131: assertTrue(new ExpressionInfo(
132: "!within(@Service) && execution(@DefaultConstructor)",
133: NAMESPACE).getExpression().match(
134: new ExpressionContext(PointcutType.EXECUTION,
135: s_constructor, s_declaringType)));
136:
137: // HINT: constructor of inner (@Service)
138: assertFalse(new ExpressionInfo(
139: "!within(@Service) && execution(@DefaultConstructor)",
140: NAMESPACE).getExpression().match(
141: new ExpressionContext(PointcutType.EXECUTION,
142: s_innerConstructor, s_innerType)));
143:
144: assertTrue(new ExpressionInfo(
145: "withincode(@DefaultConstructor)", NAMESPACE)
146: .getExpression().match(
147: new ExpressionContext(PointcutType.WITHIN,
148: s_constructor, s_constructor)));
149:
150: assertTrue(new ExpressionInfo(
151: "withincode(@DefaultConstructor test.expression.AnnotationTarget.new())",
152: NAMESPACE).getExpression().match(
153: new ExpressionContext(PointcutType.WITHIN,
154: s_constructor, s_constructor)));
155:
156: assertTrue(new ExpressionInfo(
157: "call(@Asynch) && withincode(@DefaultConstructor)",
158: NAMESPACE).getExpression().match(
159: new ExpressionContext(PointcutType.CALL, s_innerMethod,
160: s_innerConstructor)));
161:
162: }
163:
164: public void testHasMethod() {
165: assertTrue(new ExpressionInfo("hasmethod(@Asynch)", NAMESPACE)
166: .getExpression().match(
167: new ExpressionContext(PointcutType.EXECUTION,
168: s_method, null)));
169:
170: assertTrue(new ExpressionInfo(
171: "hasmethod(@Asynch void methodOneAsynch())", NAMESPACE)
172: .getExpression().match(
173: new ExpressionContext(PointcutType.CALL,
174: s_method, s_method)));
175:
176: // HINT hasmethod on constructor
177: assertTrue(new ExpressionInfo("hasmethod(@DefaultConstructor)",
178: NAMESPACE).getExpression().match(
179: new ExpressionContext(PointcutType.EXECUTION,
180: s_constructor, null)));
181:
182: assertTrue(new ExpressionInfo(
183: "hasmethod(@DefaultConstructor new())", NAMESPACE)
184: .getExpression().match(
185: new ExpressionContext(PointcutType.CALL,
186: s_constructor, s_constructor)));
187: }
188:
189: public void testHasField() throws Exception {
190: assertTrue(new ExpressionInfo("hasfield(@Persistable)",
191: NAMESPACE).getExpression().match(
192: new ExpressionContext(PointcutType.EXECUTION, s_field,
193: null)));
194:
195: assertTrue(new ExpressionInfo("hasfield(@Persistable)",
196: NAMESPACE).getExpression().match(
197: new ExpressionContext(PointcutType.CALL, s_field,
198: s_field)));
199:
200: assertTrue(new ExpressionInfo(
201: "hasfield(@Persistable) && !within(@Service)",
202: NAMESPACE).getExpression().match(
203: new ExpressionContext(PointcutType.CALL, s_field,
204: s_declaringType)));
205:
206: assertFalse(new ExpressionInfo(
207: "hasfield(@Persistable) && !within(@Service)",
208: NAMESPACE).getExpression().match(
209: new ExpressionContext(PointcutType.CALL, s_field,
210: s_innerType)));
211:
212: assertTrue(new ExpressionInfo(
213: "hasfield(@Persistable) && within(@Service)", NAMESPACE)
214: .getExpression().match(
215: new ExpressionContext(PointcutType.CALL,
216: s_innerField, s_innerType)));
217: }
218:
219: public void testFieldAttributes() {
220: assertTrue(new ExpressionInfo("set(@Persistable)", NAMESPACE)
221: .getExpression().match(
222: new ExpressionContext(PointcutType.SET,
223: s_field, null)));
224:
225: assertTrue(new ExpressionInfo(
226: "get(@Persistable) && !within(@Service)", NAMESPACE)
227: .getExpression().match(
228: new ExpressionContext(PointcutType.GET,
229: s_field, s_declaringType)));
230:
231: assertTrue(new ExpressionInfo(
232: "set(@Persistable) && within(@Service)", NAMESPACE)
233: .getExpression().match(
234: new ExpressionContext(PointcutType.SET,
235: s_innerField, s_innerType)));
236:
237: assertFalse(new ExpressionInfo(
238: "get(@Persistable) && !within(@Service)", NAMESPACE)
239: .getExpression().match(
240: new ExpressionContext(PointcutType.GET,
241: s_field, s_innerType)));
242:
243: assertFalse(new ExpressionInfo(
244: "get(@Persistable) && !within(@Service)", NAMESPACE)
245: .getExpression().match(
246: new ExpressionContext(PointcutType.GET,
247: s_innerField, s_innerType)));
248: }
249:
250: public static void main(String[] args) {
251: junit.textui.TestRunner.run(suite());
252: }
253:
254: public static junit.framework.Test suite() {
255: return new junit.framework.TestSuite(
256: AnnotationExpressionTest.class);
257: }
258:
259: protected static void setMeUp() throws Exception {
260: Class clazz = AnnotationTarget.class;
261: Class[] noParams = new Class[0];
262:
263: Method method = clazz.getMethod("methodOneAsynch", noParams);
264: s_method = s_declaringType.getMethod(ReflectHelper
265: .calculateHash(method));
266:
267: Constructor constructor = clazz.getConstructor(noParams);
268: s_constructor = s_declaringType.getConstructor(ReflectHelper
269: .calculateHash(constructor));
270:
271: Field field = clazz.getDeclaredField("m_annotatedField");
272: s_field = s_declaringType.getField(ReflectHelper
273: .calculateHash(field));
274:
275: clazz = AnnotationTarget.ClassLevelAnnotation.class;
276: method = clazz.getMethod("innerMethodAsynch", noParams);
277: s_innerMethod = s_innerType.getMethod(ReflectHelper
278: .calculateHash(method));
279:
280: constructor = clazz.getConstructor(noParams);
281: s_innerConstructor = s_innerType.getConstructor(ReflectHelper
282: .calculateHash(constructor));
283:
284: field = clazz.getDeclaredField("m_innerField");
285: s_innerField = s_innerType.getField(ReflectHelper
286: .calculateHash(field));
287: }
288: }
|