]> source.dussan.org Git - aspectj.git/blob
5325883638618bb7b8f04af6a36109e92296301a
[aspectj.git] /
1 /*******************************************************************************
2  * Copyright (c) 2008 Contributors
3  * All rights reserved. This program and the accompanying materials 
4  * are made available under the terms of the Eclipse Public License v1.0
5  * which accompanies this distribution, and is available at
6  * http://www.eclipse.org/legal/epl-v10.html
7  * 
8  * Contributors:
9  *     Andy Clement
10  *******************************************************************************/
11 package org.aspectj.matcher.tools;
12
13 import junit.framework.TestCase;
14
15 import org.aspectj.weaver.ResolvedMember;
16 import org.aspectj.weaver.ResolvedType;
17 import org.aspectj.weaver.World;
18 import org.aspectj.weaver.tools.StandardPointcutExpression;
19 import org.aspectj.weaver.tools.StandardPointcutParser;
20
21 /**
22  * Test the use of the pointcut parser and matching infrastructure. The org.aspectj.matcher.tools infrastructure used should not be
23  * aware of what kind of World it is working with and only operate in terms of the type abstraction expressed in the
24  * org.aspectj.matcher project (so Members, etc). These tests require some testdata types.
25  * 
26  * This is based on the Reflection oriented PointcutExpressionTest in the weaver project.
27  * 
28  * @author Andy Clement
29  */
30 public abstract class CommonAdvancedPointcutExpressionTests extends TestCase {
31
32         private World world;
33         private StandardPointcutParser pointcutParser;
34
35         protected abstract World getWorld();
36
37         protected void setUp() throws Exception {
38                 super.setUp();
39                 world = getWorld();
40                 pointcutParser = StandardPointcutParser.getPointcutParserSupportingAllPrimitives(world);
41         }
42
43         public void testResolvingOneType() {
44                 assertFalse(world.resolve("testdata.SomeAnnotation").isMissing());
45                 assertFalse(world.resolve("testdata.MethodLevelAnnotation").isMissing());
46                 assertFalse(world.resolve("testdata.AnnotatedClass").isMissing());
47         }
48
49         public void testTypeLevelAnnotationMatchingWithStaticInitialization01() {
50                 StandardPointcutExpression ex = pointcutParser.parsePointcutExpression("staticinitialization(@testdata.SomeAnnotation *)");
51                 ResolvedType jlString = world.resolve("java.lang.String");
52                 ResolvedType tAnnotatedClass = world.resolve("testdata.AnnotatedClass");
53
54                 assertTrue(ex.matchesStaticInitialization(tAnnotatedClass).alwaysMatches());
55                 assertTrue(ex.matchesStaticInitialization(jlString).neverMatches());
56         }
57
58         public void testTypeLevelAnnotationMatchingWithExecution01() {
59                 StandardPointcutExpression ex = pointcutParser.parsePointcutExpression("execution(* (@testdata.SomeAnnotation *).*(..))");
60                 ResolvedType jlString = world.resolve("java.lang.String");
61                 ResolvedType tAnnotatedClass = world.resolve("testdata.AnnotatedClass");
62                 assertTrue(ex.matchesMethodExecution(getMethod(tAnnotatedClass, "annotatedMethod", "()V")).alwaysMatches());
63                 assertTrue(ex.matchesMethodExecution(getMethod(jlString, "valueOf", "(Z)Ljava/lang/String;")).neverMatches());
64         }
65
66         public void testMethodLevelAnnotationMatchingWithExecution01() {
67                 StandardPointcutExpression ex = pointcutParser
68                                 .parsePointcutExpression("execution(@testdata.MethodLevelAnnotation * *(..))");
69                 ResolvedType jlString = world.resolve("java.lang.String");
70                 ResolvedType tAnnotatedClass = world.resolve("testdata.AnnotatedClass");
71                 assertTrue(ex.matchesMethodExecution(getMethod(tAnnotatedClass, "annotatedMethod", "()V")).alwaysMatches());
72                 assertTrue(ex.matchesMethodExecution(getMethod(tAnnotatedClass, "nonAnnotatedMethod", "()V")).neverMatches());
73                 assertTrue(ex.matchesMethodExecution(getMethod(jlString, "valueOf", "(Z)Ljava/lang/String;")).neverMatches());
74         }
75
76         //
77         // ResolvedMember stringSplitMethod = getMethod(jlString, "split", "(Ljava/lang/String;I)[Ljava/lang/String;");
78         // ResolvedMember stringValueOfIntMethod = getMethod(jlString, "valueOf", "(I)Ljava/lang/String;");
79         // ResolvedMember listAddMethod = getMethod(juList, "add", "(Ljava/lang/Object;)Z");
80
81         // public void testResolveTypeAndRetrieveMethod() {
82         // ResolvedType type = world.resolve("java.lang.String");
83         // assertNotNull(type);
84         // ResolvedMember method = getMethod(type, "valueOf", "(Z)Ljava/lang/String;"); // grab the method 'String valueOf()'
85         // assertNotNull(method);
86         // }
87         //
88         // public void testMethodExecutionMatching01() {
89         // checkAlwaysMatches("execution(String valueOf(boolean))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
90         // }
91         //
92         // public void testMethodExecutionMatching02() {
93         // checkAlwaysMatches("execution(* *val*(..))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
94         // checkAlwaysMatches("execution(String *(..))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
95         // checkAlwaysMatches("execution(* *(boolean))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
96         // checkAlwaysMatches("execution(* j*..*.valueOf(..))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
97         // checkAlwaysMatches("execution(* *(*))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
98         //
99         // checkNeverMatches("execution(* vulueOf(..))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
100         // checkNeverMatches("execution(int *(..))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
101         // checkNeverMatches("execution(* valueOf(String))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
102         // checkNeverMatches("execution(private * valueOf(..))", "java.lang.String", "valueOf", "(Z)Ljava/lang/String;");
103         // }
104         //
105         // public void testMethodExecutionMatching03() {
106         // checkAlwaysMatches("execution(* *())", "java.util.List", "toArray", "()[Ljava/lang/Object;");
107         // checkAlwaysMatches("execution(*[] *())", "java.util.List", "toArray", "()[Ljava/lang/Object;");
108         // checkAlwaysMatches("execution(*b*[] *())", "java.util.List", "toArray", "()[Ljava/lang/Object;");
109         // }
110         //
111         // public void testMethodMatchesStaticInitialization() {
112         // StandardPointcutExpression ex = pointcutParser.parsePointcutExpression("staticinitialization(java.lang.String)");
113         // assertNotNull(ex);
114         //
115         // ResolvedType jlString = world.resolve("java.lang.String");
116         //
117         // boolean b = ex.matchesStaticInitialization(jlString).alwaysMatches();
118         // assertTrue(b);
119         // }
120
121         // public void testMethodExecutionMatching04() {
122         // was execution((* *..A.aa(..))
123         // assertTrue("Should match execution of A.aa", ex.matchesMethodExecution(aa).alwaysMatches());
124         // assertTrue("Should match execution of B.aa", ex.matchesMethodExecution(bsaa).alwaysMatches());
125         // assertTrue("Should not match execution of A.a", ex.matchesMethodExecution(a).neverMatches());
126         // ex = p.parsePointcutExpression("call(* *..A.a*(int))");
127         // assertTrue("Should not match execution of A.a", ex.matchesMethodExecution(a).neverMatches());
128         //
129         // // test this
130         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
131         // assertTrue("Should match A", ex.matchesMethodExecution(a).alwaysMatches());
132         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
133         // assertTrue("Maybe matches B", ex.matchesMethodExecution(a).maybeMatches());
134         // assertFalse("Maybe matches B", ex.matchesMethodExecution(a).alwaysMatches());
135         //
136         // // test target
137         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
138         // assertTrue("Should match A", ex.matchesMethodExecution(a).alwaysMatches());
139         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
140         // assertTrue("Maybe matches B", ex.matchesMethodExecution(a).maybeMatches());
141         // assertFalse("Maybe matches B", ex.matchesMethodExecution(a).alwaysMatches());
142         //
143         // // test args
144         // ex = p.parsePointcutExpression("args(..,int)");
145         // assertTrue("Should match A.aa", ex.matchesMethodExecution(aa).alwaysMatches());
146         // assertTrue("Should match A.aaa", ex.matchesMethodExecution(aaa).alwaysMatches());
147         // assertTrue("Should not match A.a", ex.matchesMethodExecution(a).neverMatches());
148         //
149         // // within
150         // ex = p.parsePointcutExpression("within(*..A)");
151         // assertTrue("Matches in class A", ex.matchesMethodExecution(a).alwaysMatches());
152         // assertTrue("Does not match in class B", ex.matchesMethodExecution(bsaa).neverMatches());
153         //
154         // // withincode
155         // ex = p.parsePointcutExpression("withincode(* a*(..))");
156         // assertTrue("Should not match", ex.matchesMethodExecution(a).neverMatches());
157
158         // public void testMatchesMethodCall() {
159         // PointcutExpression ex = p.parsePointcutExpression("call(* *..A.a*(..))");
160         // assertTrue("Should match call to A.a()", ex.matchesMethodCall(a, a).alwaysMatches());
161         // assertTrue("Should match call to A.aaa()", ex.matchesMethodCall(aaa, a).alwaysMatches());
162         // assertTrue("Should match call to B.aa()", ex.matchesMethodCall(bsaa, a).alwaysMatches());
163         // assertTrue("Should not match call to B.b()", ex.matchesMethodCall(b, a).neverMatches());
164         // ex = p.parsePointcutExpression("call(* *..A.a*(int))");
165         // assertTrue("Should match call to A.aa()", ex.matchesMethodCall(aa, a).alwaysMatches());
166         // assertTrue("Should not match call to A.a()", ex.matchesMethodCall(a, a).neverMatches());
167         // ex = p.parsePointcutExpression("call(void aaa(..)) && this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
168         // assertTrue("Should match call to A.aaa() from Client", ex.matchesMethodCall(aaa, foo).alwaysMatches());
169         // ex = p.parsePointcutExpression("call(void aaa(..)) && this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
170         // assertTrue("Should match call to A.aaa() from B", ex.matchesMethodCall(aaa, b).alwaysMatches());
171         // assertTrue("May match call to A.aaa() from A", ex.matchesMethodCall(aaa, a).maybeMatches());
172         // assertFalse("May match call to A.aaa() from A", ex.matchesMethodCall(aaa, a).alwaysMatches());
173         // ex = p.parsePointcutExpression("execution(* *.*(..))");
174         // assertTrue("Should not match call to A.aa", ex.matchesMethodCall(aa, a).neverMatches());
175         // // this
176         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
177         // assertTrue("Should match Client", ex.matchesMethodCall(a, foo).alwaysMatches());
178         // assertTrue("Should not match A", ex.matchesMethodCall(a, a).neverMatches());
179         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
180         // assertTrue("Should maybe match B", ex.matchesMethodCall(bsaa, a).maybeMatches());
181         // assertFalse("Should maybe match B", ex.matchesMethodCall(bsaa, a).alwaysMatches());
182         // // target
183         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
184         // assertTrue("Should not match Client", ex.matchesMethodCall(a, a).neverMatches());
185         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
186         // assertTrue("Should match A", ex.matchesMethodCall(a, a).alwaysMatches());
187         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
188         // assertTrue("Should maybe match A", ex.matchesMethodCall(aa, a).maybeMatches());
189         // assertFalse("Should maybe match A", ex.matchesMethodCall(aa, a).alwaysMatches());
190         // // test args
191         // ex = p.parsePointcutExpression("args(..,int)");
192         // assertTrue("Should match A.aa", ex.matchesMethodCall(aa, a).alwaysMatches());
193         // assertTrue("Should match A.aaa", ex.matchesMethodCall(aaa, a).alwaysMatches());
194         // assertTrue("Should not match A.a", ex.matchesMethodCall(a, a).neverMatches());
195         // // within
196         // ex = p.parsePointcutExpression("within(*..A)");
197         // assertTrue("Matches in class A", ex.matchesMethodCall(a, a).alwaysMatches());
198         // assertTrue("Does not match in class B", ex.matchesMethodCall(a, b).neverMatches());
199         // assertTrue("Matches in class A", ex.matchesMethodCall(a, A.class).alwaysMatches());
200         // assertTrue("Does not match in class B", ex.matchesMethodCall(a, B.class).neverMatches());
201         // // withincode
202         // ex = p.parsePointcutExpression("withincode(* a*(..))");
203         // assertTrue("Should match", ex.matchesMethodCall(b, bsaa).alwaysMatches());
204         // assertTrue("Should not match", ex.matchesMethodCall(b, b).neverMatches());
205         // }
206         // public void testMatchesConstructorCall() {
207         // PointcutExpression ex = p.parsePointcutExpression("call(new(String))");
208         // assertTrue("Should match A(String)", ex.matchesConstructorCall(asCons, b).alwaysMatches());
209         // assertTrue("Should match B(String)", ex.matchesConstructorCall(bsStringCons, b).alwaysMatches());
210         // assertTrue("Should not match B()", ex.matchesConstructorCall(bsCons, foo).neverMatches());
211         // ex = p.parsePointcutExpression("call(*..A.new(String))");
212         // assertTrue("Should match A(String)", ex.matchesConstructorCall(asCons, b).alwaysMatches());
213         // assertTrue("Should not match B(String)", ex.matchesConstructorCall(bsStringCons, foo).neverMatches());
214         // // this
215         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
216         // assertTrue("Should match Client", ex.matchesConstructorCall(asCons, foo).alwaysMatches());
217         // assertTrue("Should not match A", ex.matchesConstructorCall(asCons, a).neverMatches());
218         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
219         // assertTrue("Should maybe match B", ex.matchesConstructorCall(asCons, a).maybeMatches());
220         // assertFalse("Should maybe match B", ex.matchesConstructorCall(asCons, a).alwaysMatches());
221         // // target
222         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
223         // assertTrue("Should not match Client", ex.matchesConstructorCall(asCons, foo).neverMatches());
224         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
225         // assertTrue("Should not match A (no target)", ex.matchesConstructorCall(asCons, a).neverMatches());
226         // // args
227         // ex = p.parsePointcutExpression("args(String)");
228         // assertTrue("Should match A(String)", ex.matchesConstructorCall(asCons, b).alwaysMatches());
229         // assertTrue("Should match B(String)", ex.matchesConstructorCall(bsStringCons, foo).alwaysMatches());
230         // assertTrue("Should not match B()", ex.matchesConstructorCall(bsCons, foo).neverMatches());
231         // // within
232         // ex = p.parsePointcutExpression("within(*..A)");
233         // assertTrue("Matches in class A", ex.matchesConstructorCall(asCons, a).alwaysMatches());
234         // assertTrue("Does not match in class B", ex.matchesConstructorCall(asCons, b).neverMatches());
235         // // withincode
236         // ex = p.parsePointcutExpression("withincode(* a*(..))");
237         // assertTrue("Should match", ex.matchesConstructorCall(bsCons, aa).alwaysMatches());
238         // assertTrue("Should not match", ex.matchesConstructorCall(bsCons, b).neverMatches());
239         // }
240         //
241         // public void testMatchesConstructorExecution() {
242         // PointcutExpression ex = p.parsePointcutExpression("execution(new(String))");
243         // assertTrue("Should match A(String)", ex.matchesConstructorExecution(asCons).alwaysMatches());
244         // assertTrue("Should match B(String)", ex.matchesConstructorExecution(bsStringCons).alwaysMatches());
245         // assertTrue("Should not match B()", ex.matchesConstructorExecution(bsCons).neverMatches());
246         // ex = p.parsePointcutExpression("execution(*..A.new(String))");
247         // assertTrue("Should match A(String)", ex.matchesConstructorExecution(asCons).alwaysMatches());
248         // assertTrue("Should not match B(String)", ex.matchesConstructorExecution(bsStringCons).neverMatches());
249         //
250         // // test this
251         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
252         // assertTrue("Should match A", ex.matchesConstructorExecution(asCons).alwaysMatches());
253         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
254         // assertTrue("Maybe matches B", ex.matchesConstructorExecution(asCons).maybeMatches());
255         // assertFalse("Maybe matches B", ex.matchesConstructorExecution(asCons).alwaysMatches());
256         // assertTrue("Should match B", ex.matchesConstructorExecution(bsCons).alwaysMatches());
257         // assertTrue("Does not match client", ex.matchesConstructorExecution(clientCons).neverMatches());
258         //
259         // // test target
260         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
261         // assertTrue("Should match A", ex.matchesConstructorExecution(asCons).alwaysMatches());
262         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
263         // assertTrue("Maybe matches B", ex.matchesConstructorExecution(asCons).maybeMatches());
264         // assertFalse("Maybe matches B", ex.matchesConstructorExecution(asCons).alwaysMatches());
265         // assertTrue("Should match B", ex.matchesConstructorExecution(bsCons).alwaysMatches());
266         // assertTrue("Does not match client", ex.matchesConstructorExecution(clientCons).neverMatches());
267         //
268         // // within
269         // ex = p.parsePointcutExpression("within(*..A)");
270         // assertTrue("Matches in class A", ex.matchesConstructorExecution(asCons).alwaysMatches());
271         // assertTrue("Does not match in class B", ex.matchesConstructorExecution(bsCons).neverMatches());
272         //
273         // // withincode
274         // ex = p.parsePointcutExpression("withincode(* a*(..))");
275         // assertTrue("Does not match", ex.matchesConstructorExecution(bsCons).neverMatches());
276         //
277         // // args
278         // ex = p.parsePointcutExpression("args(String)");
279         // assertTrue("Should match A(String)", ex.matchesConstructorExecution(asCons).alwaysMatches());
280         // assertTrue("Should match B(String)", ex.matchesConstructorExecution(bsStringCons).alwaysMatches());
281         // assertTrue("Should not match B()", ex.matchesConstructorExecution(bsCons).neverMatches());
282         // }
283         //
284         // public void testMatchesAdviceExecution() {
285         // PointcutExpression ex = p.parsePointcutExpression("adviceexecution()");
286         // assertTrue("Should match (advice) A.a", ex.matchesAdviceExecution(a).alwaysMatches());
287         // // test this
288         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
289         // assertTrue("Should match Client", ex.matchesAdviceExecution(foo).alwaysMatches());
290         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
291         // assertTrue("Maybe matches B", ex.matchesAdviceExecution(a).maybeMatches());
292         // assertFalse("Maybe matches B", ex.matchesAdviceExecution(a).alwaysMatches());
293         // assertTrue("Does not match client", ex.matchesAdviceExecution(foo).neverMatches());
294         //
295         // // test target
296         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
297         // assertTrue("Should match Client", ex.matchesAdviceExecution(foo).alwaysMatches());
298         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
299         // assertTrue("Maybe matches B", ex.matchesAdviceExecution(a).maybeMatches());
300         // assertFalse("Maybe matches B", ex.matchesAdviceExecution(a).alwaysMatches());
301         // assertTrue("Does not match client", ex.matchesAdviceExecution(foo).neverMatches());
302         //
303         // // test within
304         // ex = p.parsePointcutExpression("within(*..A)");
305         // assertTrue("Matches in class A", ex.matchesAdviceExecution(a).alwaysMatches());
306         // assertTrue("Does not match in class B", ex.matchesAdviceExecution(b).neverMatches());
307         //
308         // // withincode
309         // ex = p.parsePointcutExpression("withincode(* a*(..))");
310         // assertTrue("Does not match", ex.matchesAdviceExecution(a).neverMatches());
311         //
312         // // test args
313         // ex = p.parsePointcutExpression("args(..,int)");
314         // assertTrue("Should match A.aa", ex.matchesAdviceExecution(aa).alwaysMatches());
315         // assertTrue("Should match A.aaa", ex.matchesAdviceExecution(aaa).alwaysMatches());
316         // assertTrue("Should not match A.a", ex.matchesAdviceExecution(a).neverMatches());
317         // }
318         //
319         // public void testMatchesHandler() {
320         // PointcutExpression ex = p.parsePointcutExpression("handler(Exception)");
321         // assertTrue("Should match catch(Exception)", ex.matchesHandler(Exception.class, Client.class).alwaysMatches());
322         // assertTrue("Should not match catch(Throwable)", ex.matchesHandler(Throwable.class, Client.class).neverMatches());
323         // // test this
324         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
325         // assertTrue("Should match Client", ex.matchesHandler(Exception.class, foo).alwaysMatches());
326         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
327         // assertTrue("Maybe matches B", ex.matchesHandler(Exception.class, a).maybeMatches());
328         // assertFalse("Maybe matches B", ex.matchesHandler(Exception.class, a).alwaysMatches());
329         // assertTrue("Does not match client", ex.matchesHandler(Exception.class, foo).neverMatches());
330         // // target - no target for exception handlers
331         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
332         // assertTrue("Should match Client", ex.matchesHandler(Exception.class, foo).neverMatches());
333         // // args
334         // ex = p.parsePointcutExpression("args(Exception)");
335         // assertTrue("Should match Exception", ex.matchesHandler(Exception.class, foo).alwaysMatches());
336         // assertTrue("Should match RuntimeException", ex.matchesHandler(RuntimeException.class, foo).alwaysMatches());
337         // assertTrue("Should not match String", ex.matchesHandler(String.class, foo).neverMatches());
338         // assertTrue("Maybe matches Throwable", ex.matchesHandler(Throwable.class, foo).maybeMatches());
339         // assertFalse("Maybe matches Throwable", ex.matchesHandler(Throwable.class, foo).alwaysMatches());
340         // // within
341         // ex = p.parsePointcutExpression("within(*..Client)");
342         // assertTrue("Matches in class Client", ex.matchesHandler(Exception.class, foo).alwaysMatches());
343         // assertTrue("Does not match in class B", ex.matchesHandler(Exception.class, b).neverMatches());
344         // // withincode
345         // ex = p.parsePointcutExpression("withincode(* a*(..))");
346         // assertTrue("Matches within aa", ex.matchesHandler(Exception.class, aa).alwaysMatches());
347         // assertTrue("Does not match within b", ex.matchesHandler(Exception.class, b).neverMatches());
348         // }
349         //
350         // public void testMatchesInitialization() {
351         // PointcutExpression ex = p.parsePointcutExpression("initialization(new(String))");
352         // assertTrue("Should match A(String)", ex.matchesInitialization(asCons).alwaysMatches());
353         // assertTrue("Should match B(String)", ex.matchesInitialization(bsStringCons).alwaysMatches());
354         // assertTrue("Should not match B()", ex.matchesInitialization(bsCons).neverMatches());
355         // ex = p.parsePointcutExpression("initialization(*..A.new(String))");
356         // assertTrue("Should match A(String)", ex.matchesInitialization(asCons).alwaysMatches());
357         // assertTrue("Should not match B(String)", ex.matchesInitialization(bsStringCons).neverMatches());
358         // // test this
359         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
360         // assertTrue("Should match A", ex.matchesInitialization(asCons).alwaysMatches());
361         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
362         // assertTrue("Maybe matches B", ex.matchesInitialization(asCons).maybeMatches());
363         // assertFalse("Maybe matches B", ex.matchesInitialization(asCons).alwaysMatches());
364         //
365         // // test target
366         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
367         // assertTrue("Should match A", ex.matchesInitialization(asCons).alwaysMatches());
368         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
369         // assertTrue("Maybe matches B", ex.matchesInitialization(asCons).maybeMatches());
370         // assertFalse("Maybe matches B", ex.matchesInitialization(asCons).alwaysMatches());
371         // // within
372         // ex = p.parsePointcutExpression("within(*..A)");
373         // assertTrue("Matches in class A", ex.matchesInitialization(asCons).alwaysMatches());
374         // assertTrue("Does not match in class B", ex.matchesInitialization(bsCons).neverMatches());
375         // // withincode
376         // ex = p.parsePointcutExpression("withincode(* a*(..))");
377         // assertTrue("Does not match", ex.matchesInitialization(bsCons).neverMatches());
378         // // args
379         // ex = p.parsePointcutExpression("args(String)");
380         // assertTrue("Should match A(String)", ex.matchesInitialization(asCons).alwaysMatches());
381         // assertTrue("Should match B(String)", ex.matchesInitialization(bsStringCons).alwaysMatches());
382         // assertTrue("Should not match B()", ex.matchesInitialization(bsCons).neverMatches());
383         // }
384         //
385         // public void testMatchesPreInitialization() {
386         // PointcutExpression ex = p.parsePointcutExpression("preinitialization(new(String))");
387         // assertTrue("Should match A(String)", ex.matchesPreInitialization(asCons).alwaysMatches());
388         // assertTrue("Should match B(String)", ex.matchesPreInitialization(bsStringCons).alwaysMatches());
389         // assertTrue("Should not match B()", ex.matchesPreInitialization(bsCons).neverMatches());
390         // ex = p.parsePointcutExpression("preinitialization(*..A.new(String))");
391         // assertTrue("Should match A(String)", ex.matchesPreInitialization(asCons).alwaysMatches());
392         // assertTrue("Should not match B(String)", ex.matchesPreInitialization(bsStringCons).neverMatches());
393         // // test this
394         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
395         // assertTrue("No match, no this at preinit", ex.matchesPreInitialization(asCons).neverMatches());
396         //
397         // // test target
398         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
399         // assertTrue("No match, no target at preinit", ex.matchesPreInitialization(asCons).neverMatches());
400         //
401         // // within
402         // ex = p.parsePointcutExpression("within(*..A)");
403         // assertTrue("Matches in class A", ex.matchesPreInitialization(asCons).alwaysMatches());
404         // assertTrue("Does not match in class B", ex.matchesPreInitialization(bsCons).neverMatches());
405         // // withincode
406         // ex = p.parsePointcutExpression("withincode(* a*(..))");
407         // assertTrue("Does not match", ex.matchesPreInitialization(bsCons).neverMatches());
408         // // args
409         // ex = p.parsePointcutExpression("args(String)");
410         // assertTrue("Should match A(String)", ex.matchesPreInitialization(asCons).alwaysMatches());
411         // assertTrue("Should match B(String)", ex.matchesPreInitialization(bsStringCons).alwaysMatches());
412         // assertTrue("Should not match B()", ex.matchesPreInitialization(bsCons).neverMatches());
413         // }
414         //
415         // public void testMatchesStaticInitialization() {
416         // // staticinit
417         // PointcutExpression ex = p.parsePointcutExpression("staticinitialization(*..A+)");
418         // assertTrue("Matches A", ex.matchesStaticInitialization(A.class).alwaysMatches());
419         // assertTrue("Matches B", ex.matchesStaticInitialization(B.class).alwaysMatches());
420         // assertTrue("Doesn't match Client", ex.matchesStaticInitialization(Client.class).neverMatches());
421         // // this
422         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
423         // assertTrue("No this", ex.matchesStaticInitialization(A.class).neverMatches());
424         // // target
425         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
426         // assertTrue("No target", ex.matchesStaticInitialization(A.class).neverMatches());
427         //
428         // // args
429         // ex = p.parsePointcutExpression("args()");
430         // assertTrue("No args", ex.matchesStaticInitialization(A.class).alwaysMatches());
431         // ex = p.parsePointcutExpression("args(String)");
432         // assertTrue("No args", ex.matchesStaticInitialization(A.class).neverMatches());
433         //
434         // // within
435         // ex = p.parsePointcutExpression("within(*..A)");
436         // assertTrue("Matches in class A", ex.matchesStaticInitialization(A.class).alwaysMatches());
437         // assertTrue("Does not match in class B", ex.matchesStaticInitialization(B.class).neverMatches());
438         //
439         // // withincode
440         // ex = p.parsePointcutExpression("withincode(* a*(..))");
441         // assertTrue("Does not match", ex.matchesStaticInitialization(A.class).neverMatches());
442         // }
443         //
444         // public void testMatchesFieldSet() {
445         // PointcutExpression ex = p.parsePointcutExpression("set(* *..A+.*)");
446         // assertTrue("matches x", ex.matchesFieldSet(x, a).alwaysMatches());
447         // assertTrue("matches y", ex.matchesFieldSet(y, foo).alwaysMatches());
448         // assertTrue("does not match n", ex.matchesFieldSet(n, foo).neverMatches());
449         // // this
450         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
451         // assertTrue("matches Client", ex.matchesFieldSet(x, foo).alwaysMatches());
452         // assertTrue("does not match A", ex.matchesFieldSet(n, a).neverMatches());
453         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
454         // assertTrue("maybe matches A", ex.matchesFieldSet(x, a).maybeMatches());
455         // assertFalse("maybe matches A", ex.matchesFieldSet(x, a).alwaysMatches());
456         // // target
457         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
458         // assertTrue("matches B", ex.matchesFieldSet(y, foo).alwaysMatches());
459         // assertTrue("maybe matches A", ex.matchesFieldSet(x, foo).maybeMatches());
460         // assertFalse("maybe matches A", ex.matchesFieldSet(x, foo).alwaysMatches());
461         // // args
462         // ex = p.parsePointcutExpression("args(int)");
463         // assertTrue("matches x", ex.matchesFieldSet(x, a).alwaysMatches());
464         // assertTrue("matches y", ex.matchesFieldSet(y, a).alwaysMatches());
465         // assertTrue("does not match n", ex.matchesFieldSet(n, a).neverMatches());
466         // // within
467         // ex = p.parsePointcutExpression("within(*..A)");
468         // assertTrue("Matches in class A", ex.matchesFieldSet(x, a).alwaysMatches());
469         // assertTrue("Does not match in class B", ex.matchesFieldSet(x, b).neverMatches());
470         // // withincode
471         // ex = p.parsePointcutExpression("withincode(* a*(..))");
472         // assertTrue("Should match", ex.matchesFieldSet(x, aa).alwaysMatches());
473         // assertTrue("Should not match", ex.matchesFieldSet(x, b).neverMatches());
474         // }
475         //
476         // public void testMatchesFieldGet() {
477         // PointcutExpression ex = p.parsePointcutExpression("get(* *..A+.*)");
478         // assertTrue("matches x", ex.matchesFieldGet(x, a).alwaysMatches());
479         // assertTrue("matches y", ex.matchesFieldGet(y, foo).alwaysMatches());
480         // assertTrue("does not match n", ex.matchesFieldGet(n, foo).neverMatches());
481         // // this
482         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.Client)");
483         // assertTrue("matches Client", ex.matchesFieldGet(x, foo).alwaysMatches());
484         // assertTrue("does not match A", ex.matchesFieldGet(n, a).neverMatches());
485         // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
486         // assertTrue("maybe matches A", ex.matchesFieldGet(x, a).maybeMatches());
487         // assertFalse("maybe matches A", ex.matchesFieldGet(x, a).alwaysMatches());
488         // // target
489         // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
490         // assertTrue("matches B", ex.matchesFieldGet(y, foo).alwaysMatches());
491         // assertTrue("maybe matches A", ex.matchesFieldGet(x, foo).maybeMatches());
492         // assertFalse("maybe matches A", ex.matchesFieldGet(x, foo).alwaysMatches());
493         // // args - no args at get join point
494         // ex = p.parsePointcutExpression("args(int)");
495         // assertTrue("matches x", ex.matchesFieldGet(x, a).neverMatches());
496         // // within
497         // ex = p.parsePointcutExpression("within(*..A)");
498         // assertTrue("Matches in class A", ex.matchesFieldGet(x, a).alwaysMatches());
499         // assertTrue("Does not match in class B", ex.matchesFieldGet(x, b).neverMatches());
500         // // withincode
501         // ex = p.parsePointcutExpression("withincode(* a*(..))");
502         // assertTrue("Should match", ex.matchesFieldGet(x, aa).alwaysMatches());
503         // assertTrue("Should not match", ex.matchesFieldGet(x, b).neverMatches());
504         // }
505         //
506         // public void testArgsMatching() {
507         // // too few args
508         // PointcutExpression ex = p.parsePointcutExpression("args(*,*,*,*)");
509         // assertTrue("Too few args", ex.matchesMethodExecution(foo).neverMatches());
510         // assertTrue("Matching #args", ex.matchesMethodExecution(bar).alwaysMatches());
511         // // one too few + ellipsis
512         // ex = p.parsePointcutExpression("args(*,*,*,..)");
513         // assertTrue("Matches with ellipsis", ex.matchesMethodExecution(foo).alwaysMatches());
514         // // exact number + ellipsis
515         // assertTrue("Matches with ellipsis", ex.matchesMethodExecution(bar).alwaysMatches());
516         // assertTrue("Does not match with ellipsis", ex.matchesMethodExecution(a).neverMatches());
517         // // too many + ellipsis
518         // ex = p.parsePointcutExpression("args(*,..,*)");
519         // assertTrue("Matches with ellipsis", ex.matchesMethodExecution(bar).alwaysMatches());
520         // assertTrue("Does not match with ellipsis", ex.matchesMethodExecution(a).neverMatches());
521         // assertTrue("Matches with ellipsis", ex.matchesMethodExecution(aaa).alwaysMatches());
522         // // exact match
523         // ex = p.parsePointcutExpression("args(String,int,Number)");
524         // assertTrue("Matches exactly", ex.matchesMethodExecution(foo).alwaysMatches());
525         // // maybe match
526         // ex = p.parsePointcutExpression("args(String,int,Double)");
527         // assertTrue("Matches maybe", ex.matchesMethodExecution(foo).maybeMatches());
528         // assertFalse("Matches maybe", ex.matchesMethodExecution(foo).alwaysMatches());
529         // // never match
530         // ex = p.parsePointcutExpression("args(String,Integer,Number)");
531         // if (LangUtil.is15VMOrGreater()) {
532         // assertTrue("matches", ex.matchesMethodExecution(foo).alwaysMatches());
533         // } else {
534         // assertTrue("Does not match", ex.matchesMethodExecution(foo).neverMatches());
535         // }
536         // }
537         //
538         // // public void testMatchesDynamically() {
539         // // // everything other than this,target,args should just return true
540         // // PointcutExpression ex = p.parsePointcutExpression("call(* *.*(..)) && execution(* *.*(..)) &&" +
541         // // "get(* *) && set(* *) && initialization(new(..)) && preinitialization(new(..)) &&" +
542         // // "staticinitialization(X) && adviceexecution() && within(Y) && withincode(* *.*(..)))");
543         // // assertTrue("Matches dynamically",ex.matchesDynamically(a,b,new Object[0]));
544         // // // this
545         // // ex = p.parsePointcutExpression("this(String)");
546         // // assertTrue("String matches",ex.matchesDynamically("",this,new Object[0]));
547         // // assertFalse("Object doesn't match",ex.matchesDynamically(new Object(),this,new Object[0]));
548         // // ex = p.parsePointcutExpression("this(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
549         // // assertTrue("A matches",ex.matchesDynamically(new A(""),this,new Object[0]));
550         // // assertTrue("B matches",ex.matchesDynamically(new B(""),this,new Object[0]));
551         // // // target
552         // // ex = p.parsePointcutExpression("target(String)");
553         // // assertTrue("String matches",ex.matchesDynamically(this,"",new Object[0]));
554         // // assertFalse("Object doesn't match",ex.matchesDynamically(this,new Object(),new Object[0]));
555         // // ex = p.parsePointcutExpression("target(org.aspectj.weaver.tools.PointcutExpressionTest.A)");
556         // // assertTrue("A matches",ex.matchesDynamically(this,new A(""),new Object[0]));
557         // // assertTrue("B matches",ex.matchesDynamically(this,new B(""),new Object[0]));
558         // // // args
559         // // ex = p.parsePointcutExpression("args(*,*,*,*)");
560         // // assertFalse("Too few args",ex.matchesDynamically(null,null,new Object[]{a,b}));
561         // // assertTrue("Matching #args",ex.matchesDynamically(null,null,new Object[]{a,b,aa,aaa}));
562         // // // one too few + ellipsis
563         // // ex = p.parsePointcutExpression("args(*,*,*,..)");
564         // // assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b,aa,aaa}));
565         // // // exact number + ellipsis
566         // // assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b,aa}));
567         // // assertFalse("Does not match with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b}));
568         // // // too many + ellipsis
569         // // ex = p.parsePointcutExpression("args(*,..,*)");
570         // // assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b,aa,aaa}));
571         // // assertFalse("Does not match with ellipsis",ex.matchesDynamically(null,null,new Object[]{a}));
572         // // assertTrue("Matches with ellipsis",ex.matchesDynamically(null,null,new Object[]{a,b}));
573         // // // exact match
574         // // ex = p.parsePointcutExpression("args(String,int,Number)");
575         // // assertTrue("Matches exactly",ex.matchesDynamically(null,null,new Object[]{"",new Integer(5),new Double(5.0)}));
576         // // ex = p.parsePointcutExpression("args(String,Integer,Number)");
577         // // assertTrue("Matches exactly",ex.matchesDynamically(null,null,new Object[]{"",new Integer(5),new Double(5.0)}));
578         // // // never match
579         // // ex = p.parsePointcutExpression("args(String,Integer,Number)");
580         // // assertFalse("Does not match",ex.matchesDynamically(null,null,new Object[]{a,b,aa}));
581         // // }
582         //
583         // public void testGetPointcutExpression() {
584         // PointcutExpression ex = p.parsePointcutExpression("staticinitialization(*..A+)");
585         // assertEquals("staticinitialization(*..A+)", ex.getPointcutExpression());
586         // }
587         //
588         // public void testCouldMatchJoinPointsInType() {
589         // PointcutExpression ex = p.parsePointcutExpression("execution(* org.aspectj.weaver.tools.PointcutExpressionTest.B.*(..))");
590         // assertTrue("Could maybe match String (as best we know at this point)", ex.couldMatchJoinPointsInType(String.class));
591         // assertTrue("Will always match B", ex.couldMatchJoinPointsInType(B.class));
592         // ex = p.parsePointcutExpression("within(org.aspectj.weaver.tools.PointcutExpressionTest.B)");
593         // assertFalse("Will never match String", ex.couldMatchJoinPointsInType(String.class));
594         // assertTrue("Will always match B", ex.couldMatchJoinPointsInType(B.class));
595         // }
596         //
597         // public void testMayNeedDynamicTest() {
598         // PointcutExpression ex = p.parsePointcutExpression("execution(* org.aspectj.weaver.tools.PointcutExpressionTest.B.*(..))");
599         // assertFalse("No dynamic test needed", ex.mayNeedDynamicTest());
600         // ex = p
601         // .parsePointcutExpression("execution(* org.aspectj.weaver.tools.PointcutExpressionTest.B.*(..)) && args(org.aspectj.weaver.tools.PointcutExpressionTest.X)");
602         // assertTrue("Dynamic test needed", ex.mayNeedDynamicTest());
603         // }
604
605         // static class A {
606         // public A(String s) {
607         // }
608         //
609         // public void a() {
610         // }
611         //
612         // public void aa(int i) {
613         // }
614         //
615         // public void aaa(String s, int i) {
616         // }
617         //
618         // int x;
619         // }
620         //
621         // static class B extends A {
622         // public B() {
623         // super("");
624         // }
625         //
626         // public B(String s) {
627         // super(s);
628         // }
629         //
630         // public String b() {
631         // return null;
632         // }
633         //
634         // public void aa(int i) {
635         // }
636         //
637         // int y;
638         // }
639         //
640         // static class Client {
641         // public Client() {
642         // }
643         //
644         // Number n;
645         //
646         // public void foo(String s, int i, Number n) {
647         // }
648         //
649         // public void bar(String s, int i, Integer i2, Number n) {
650         // }
651         // }
652         //
653         // static class X {
654         // }
655
656         private ResolvedMember getMethod(ResolvedType type, String methodName, String methodSignature) {
657                 ResolvedMember[] methods = type.getDeclaredMethods();
658                 for (int i = 0; i < methods.length; i++) {
659                         if (methods[i].getName().equals(methodName)
660                                         && (methodSignature == null || methodSignature.equals(methods[i].getSignature()))) {
661                                 return methods[i];
662                         }
663                 }
664                 return null;
665         }
666
667         @SuppressWarnings("unused")
668         private void checkAlwaysMatches(String pointcutExpression, String type, String methodName, String methodSignature) {
669                 StandardPointcutExpression ex = pointcutParser.parsePointcutExpression(pointcutExpression);
670                 assertNotNull(ex);
671                 ResolvedType resolvedType = world.resolve(type);
672                 ResolvedMember method = getMethod(resolvedType, methodName, methodSignature);
673                 assertNotNull(method);
674                 boolean b = ex.matchesMethodExecution(method).alwaysMatches();
675                 assertTrue(b);
676         }
677
678         @SuppressWarnings("unused")
679         private void checkNeverMatches(String pointcutExpression, String type, String methodName, String methodSignature) {
680                 StandardPointcutExpression ex = pointcutParser.parsePointcutExpression(pointcutExpression);
681                 assertNotNull(ex);
682                 ResolvedType resolvedType = world.resolve(type);
683                 ResolvedMember method = getMethod(resolvedType, methodName, methodSignature);
684                 assertNotNull(method);
685                 boolean b = ex.matchesMethodExecution(method).neverMatches();
686                 assertTrue(b);
687         }
688
689 }