001: /*
002: * $Id: AdvisedClassFilterExpressionTest.java,v 1.2 2004/12/20 13:14:21 avasseur Exp $
003: * $Date: 2004/12/20 13:14:21 $
004: */
005: package test.expression;
006:
007: import org.codehaus.aspectwerkz.exception.DefinitionException;
008: import org.codehaus.aspectwerkz.expression.ExpressionContext;
009: import org.codehaus.aspectwerkz.expression.ExpressionInfo;
010: import org.codehaus.aspectwerkz.expression.ExpressionNamespace;
011: import org.codehaus.aspectwerkz.expression.PointcutType;
012: import org.codehaus.aspectwerkz.reflect.ClassInfo;
013: import org.codehaus.aspectwerkz.reflect.ConstructorInfo;
014: import org.codehaus.aspectwerkz.reflect.FieldInfo;
015: import org.codehaus.aspectwerkz.reflect.MethodInfo;
016: import org.codehaus.aspectwerkz.reflect.impl.java.JavaClassInfo;
017: import org.codehaus.aspectwerkz.reflect.impl.java.JavaMethodInfo;
018:
019: import junit.framework.TestSuite;
020:
021: /**
022: * Test unit for AdvisedClassFilterExpressionVisitor
023: *
024: * @author <a href="mailto:the_mindstorm@evolva.ro">Alex Popescu</a>
025: * @version $Revision: 1.2 $
026: */
027: public class AdvisedClassFilterExpressionTest extends
028: AnnotationExpressionTest {
029:
030: static {
031: try {
032: setMeUp();
033: } catch (Throwable t) {
034: throw new Error(t.toString());
035: }
036: }
037:
038: public void testAdvisedConstructor() {
039: assertTrue(new ExpressionInfo(
040: "call(@DefaultConstructor test.*.AnnotationTarget.new())",
041: NAMESPACE).getExpression().match(
042: new ExpressionContext(PointcutType.CALL, s_constructor,
043: null)));
044:
045: assertFalse(new ExpressionInfo(
046: "call(@OtherConstructor test.*.AnnotationTarget.new())",
047: NAMESPACE).getExpression().match(
048: new ExpressionContext(PointcutType.CALL, s_constructor,
049: null)));
050:
051: assertTrue(new ExpressionInfo(
052: "call(@OtherConstructor test.*.AnnotationTarget.new())",
053: NAMESPACE).getAdvisedClassFilterExpression().match(
054: new ExpressionContext(PointcutType.CALL, s_constructor,
055: null)));
056:
057: assertTrue(new ExpressionInfo(
058: "call(@DefaultConstructor test.expression.AnnotationTarget.new())",
059: NAMESPACE).getExpression().match(
060: new ExpressionContext(PointcutType.CALL, s_constructor,
061: null)));
062:
063: assertFalse(new ExpressionInfo(
064: "call(@OtherConstructor test.expression.AnnotationTarget.new())",
065: NAMESPACE).getExpression().match(
066: new ExpressionContext(PointcutType.CALL, s_constructor,
067: null)));
068:
069: assertTrue(new ExpressionInfo(
070: "call(@OtherConstructor test.expression.AnnotationTarget.new())",
071: NAMESPACE).getAdvisedClassFilterExpression().match(
072: new ExpressionContext(PointcutType.CALL, s_constructor,
073: null)));
074:
075: assertFalse(new ExpressionInfo("call(@OtherConstructor)",
076: NAMESPACE).getExpression().match(
077: new ExpressionContext(PointcutType.CALL, s_constructor,
078: null)));
079:
080: // no type info
081: assertTrue(new ExpressionInfo("call(@OtherConstructor)",
082: NAMESPACE).getAdvisedClassFilterExpression().match(
083: new ExpressionContext(PointcutType.CALL, s_constructor,
084: null)));
085:
086: assertFalse(new ExpressionInfo(
087: "call(@DefaultConstructor test.expression.AnnotationTargetWRONG.new())",
088: NAMESPACE).getAdvisedClassFilterExpression().match(
089: new ExpressionContext(PointcutType.CALL, s_constructor,
090: null)));
091:
092: assertTrue(new ExpressionInfo(
093: "within(test.expression.AnnotationTarget) && execution(@DefaultConstructor)",
094: NAMESPACE).getAdvisedClassFilterExpression().match(
095: new ExpressionContext(PointcutType.EXECUTION,
096: s_constructor, s_declaringType)));
097:
098: assertFalse(new ExpressionInfo(
099: "within(test.expression.AnnotationTarget) && execution(@OtherConstructor)",
100: NAMESPACE).getExpression().match(
101: new ExpressionContext(PointcutType.EXECUTION,
102: s_constructor, s_declaringType)));
103:
104: assertTrue(new ExpressionInfo(
105: "within(test.expression.AnnotationTarget) && execution(@OtherConstructor)",
106: NAMESPACE).getAdvisedClassFilterExpression().match(
107: new ExpressionContext(PointcutType.EXECUTION,
108: s_constructor, s_declaringType)));
109:
110: assertFalse(new ExpressionInfo(
111: "within(test.expressionFAKE.*) && execution(@DefaultConstructor)",
112: NAMESPACE).getExpression().match(
113: new ExpressionContext(PointcutType.EXECUTION,
114: s_constructor, s_declaringType)));
115:
116: assertFalse(new ExpressionInfo(
117: "within(test.expressionFAKE.*) && execution(@DefaultConstructor)",
118: NAMESPACE).getAdvisedClassFilterExpression().match(
119: new ExpressionContext(PointcutType.EXECUTION,
120: s_constructor, s_declaringType)));
121:
122: assertTrue(new ExpressionInfo(
123: "within(@Service test.expression.*) && execution(@DefaultConstructor test.expression.*.new())",
124: NAMESPACE).getExpression().match(
125: new ExpressionContext(PointcutType.EXECUTION,
126: s_innerConstructor, s_innerType)));
127:
128: assertFalse(new ExpressionInfo(
129: "within(!@Service test.expression.*) && execution(@DefaultConstructor test.expression.*.new())",
130: NAMESPACE).getAdvisedClassFilterExpression().match(
131: new ExpressionContext(PointcutType.EXECUTION,
132: s_innerConstructor, s_innerType)));
133:
134: assertTrue(new ExpressionInfo(
135: "withincode(@DefaultConstructor test.expression.AnnotationTarget$ClassLevelAnnotation.new())",
136: NAMESPACE).getExpression().match(
137: new ExpressionContext(PointcutType.WITHIN,
138: s_innerConstructor, s_innerConstructor)));
139: }
140:
141: public void testAdvisedMethod() {
142: assertTrue(new ExpressionInfo(
143: "call(@Asynch void test.*.AnnotationTarget.methodOneAsynch())",
144: NAMESPACE).getExpression()
145: .match(
146: new ExpressionContext(PointcutType.CALL,
147: s_method, null)));
148:
149: assertFalse(new ExpressionInfo(
150: "call(@Synch void test.*.AnnotationTarget.methodOneAsynch())",
151: NAMESPACE).getExpression()
152: .match(
153: new ExpressionContext(PointcutType.CALL,
154: s_method, null)));
155:
156: assertTrue(new ExpressionInfo(
157: "call(@Synch int test.*.AnnotationTarget.methodOneAsynch())",
158: NAMESPACE).getAdvisedClassFilterExpression()
159: .match(
160: new ExpressionContext(PointcutType.CALL,
161: s_method, null)));
162:
163: assertFalse(new ExpressionInfo("call(@Synch)", NAMESPACE)
164: .getExpression().match(
165: new ExpressionContext(PointcutType.CALL,
166: s_method, null)));
167:
168: // no type info
169: assertTrue(new ExpressionInfo("call(@Synch)", NAMESPACE)
170: .getAdvisedClassFilterExpression().match(
171: new ExpressionContext(PointcutType.CALL,
172: s_method, null)));
173:
174: assertFalse(new ExpressionInfo(
175: "call(@Asynch * test.expression.AnnotationTargetWRONG.methodOneAsynch())",
176: NAMESPACE).getAdvisedClassFilterExpression()
177: .match(
178: new ExpressionContext(PointcutType.CALL,
179: s_method, null)));
180:
181: assertFalse(new ExpressionInfo(
182: "within(test.expression.AnnotationTarget) && execution(@Synch)",
183: NAMESPACE).getExpression().match(
184: new ExpressionContext(PointcutType.EXECUTION, s_method,
185: s_declaringType)));
186:
187: // HINT no type in execution
188: assertTrue(new ExpressionInfo(
189: "within(test.expression.AnnotationTarget) && execution(@Synch)",
190: NAMESPACE).getAdvisedClassFilterExpression().match(
191: new ExpressionContext(PointcutType.EXECUTION, s_method,
192: s_declaringType)));
193:
194: assertTrue(new ExpressionInfo(
195: "within(@Service test.expression.AnnotationTarget$ClassLevelAnnotation) && execution(@Asynch void innerMethodAsynch())",
196: NAMESPACE).getExpression().match(
197: new ExpressionContext(PointcutType.EXECUTION,
198: s_innerMethod, s_innerType)));
199:
200: assertFalse(new ExpressionInfo(
201: "within(!@Service test.expression.AnnotationTarget$ClassLevelAnnotation) && execution(@Asynch void innerMethodAsynch())",
202: NAMESPACE).getAdvisedClassFilterExpression().match(
203: new ExpressionContext(PointcutType.EXECUTION,
204: s_innerMethod, s_innerType)));
205:
206: assertTrue(new ExpressionInfo(
207: "withincode(@Asynch void test.expression.AnnotationTarget$ClassLevelAnnotation.innerMethodAsynch())",
208: NAMESPACE).getExpression().match(
209: new ExpressionContext(PointcutType.WITHIN,
210: s_innerMethod, s_innerMethod)));
211:
212: // HINT annotation and return not considered
213: assertTrue(new ExpressionInfo(
214: "withincode(!@Asynch int test.expression.AnnotationTarget$ClassLevelAnnotation.innerMethodAsynch())",
215: NAMESPACE).getAdvisedClassFilterExpression().match(
216: new ExpressionContext(PointcutType.WITHIN,
217: s_innerMethod, s_innerMethod)));
218: }
219:
220: public void testAdvisedHasMethod() {
221: assertTrue(new ExpressionInfo(
222: "hasmethod(@Asynch void test.expression.AnnotationTarget.methodOneAsynch())",
223: NAMESPACE).getExpression().match(
224: new ExpressionContext(PointcutType.EXECUTION, s_method,
225: null)));
226:
227: assertFalse(new ExpressionInfo(
228: "hasmethod(!@Asynch int test.expression.AnnotationTarget.methodOneAsynch())",
229: NAMESPACE).getAdvisedClassFilterExpression().match(
230: new ExpressionContext(PointcutType.EXECUTION, s_method,
231: null)));
232:
233: assertTrue(new ExpressionInfo(
234: "hasmethod(@DefaultConstructor test.expression.*.new())",
235: NAMESPACE).getExpression().match(
236: new ExpressionContext(PointcutType.EXECUTION,
237: s_innerConstructor, null)));
238:
239: assertFalse(new ExpressionInfo(
240: "hasmethod(!@DefaultConstructor test.expression.*.new())",
241: NAMESPACE).getAdvisedClassFilterExpression().match(
242: new ExpressionContext(PointcutType.EXECUTION,
243: s_innerConstructor, null)));
244: }
245:
246: public void testAdvisedHasField() throws Exception {
247: assertTrue(new ExpressionInfo(
248: "hasfield(@Persistable java.lang.Object+ test.expression.AnnotationTarget.m_annotatedField)",
249: NAMESPACE).getExpression().match(
250: new ExpressionContext(PointcutType.GET, s_field,
251: s_declaringType)));
252:
253: assertFalse(new ExpressionInfo(
254: "hasfield(!@Persistable int test.expression.AnnotationTarget.m_annotatedField)",
255: NAMESPACE).getAdvisedClassFilterExpression().match(
256: new ExpressionContext(PointcutType.GET, s_field,
257: s_declaringType)));
258: }
259:
260: public void testAdvisedFieldAttributes() {
261: assertTrue(new ExpressionInfo(
262: "set(@Persistable java.lang.Object+ test.expression.AnnotationTarget.m_annotatedField)",
263: NAMESPACE).getExpression().match(
264: new ExpressionContext(PointcutType.SET, s_field, null)));
265:
266: assertTrue(new ExpressionInfo(
267: "set(!@Persistable int test.expression.AnnotationTarget.m_annotatedField)",
268: NAMESPACE).getAdvisedClassFilterExpression().match(
269: new ExpressionContext(PointcutType.SET, s_field, null)));
270:
271: // HINT wrong field type
272: assertFalse(new ExpressionInfo(
273: "get(@Persistable int test.expression.AnnotationTarget.m_annotatedField) && !within(@Service)",
274: NAMESPACE).getExpression().match(
275: new ExpressionContext(PointcutType.GET, s_field,
276: s_declaringType)));
277:
278: // HINT field type ignored
279: assertTrue(new ExpressionInfo(
280: "get(@Persistable int test.expression.AnnotationTarget.m_annotatedField) && !within(@Service)",
281: NAMESPACE).getAdvisedClassFilterExpression().match(
282: new ExpressionContext(PointcutType.GET, s_field,
283: s_declaringType)));
284:
285: assertFalse(new ExpressionInfo(
286: "get(@Persistable java.lang.Object m_innerField) && within(!@Service test.expression.*)",
287: NAMESPACE).getExpression().match(
288: new ExpressionContext(PointcutType.GET, s_innerField,
289: s_innerType)));
290:
291: // HINT annotations and types ignored
292: assertTrue(new ExpressionInfo(
293: "get(!@Persistable String m_innerField) && within(@Service test.expression.*)",
294: NAMESPACE).getAdvisedClassFilterExpression().match(
295: new ExpressionContext(PointcutType.GET, s_innerField,
296: s_innerType)));
297: }
298:
299: public static junit.framework.Test suite() {
300: return new junit.framework.TestSuite(
301: AdvisedClassFilterExpressionTest.class);
302: }
303:
304: public static void main(String[] args) {
305: junit.textui.TestRunner.run(suite());
306: }
307:
308: }
|