aboutsummaryrefslogtreecommitdiffstats
path: root/weaver5/java5-testsrc/org/aspectj/weaver/tools/Java15PointcutExpressionTest.java
blob: 4a0ecb28ab2f2896611260484cb68f395aed7436 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
/* *******************************************************************
 * Copyright (c) 2005 Contributors.
 * All rights reserved. 
 * This program and the accompanying materials are made available 
 * under the terms of the Eclipse Public License v1.0 
 * which accompanies this distribution and is available at 
 * http://eclipse.org/legal/epl-v10.html 
 *  
 * Contributors: 
 *   Adrian Colyer			Initial implementation
 * ******************************************************************/
package org.aspectj.weaver.tools;

import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.reflect.Method;
import java.util.Date;
import java.util.List;

import org.aspectj.lang.annotation.Pointcut;

import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;

/**
 * @author colyer
 *
 */
public class Java15PointcutExpressionTest extends TestCase {

	public static Test suite() {
		TestSuite suite = new TestSuite("Java15PointcutExpressionTest");
		suite.addTestSuite(Java15PointcutExpressionTest.class);
		return suite;
	}
	
	private PointcutParser parser;
	private Method a;
	private Method b;
	private Method c;
	private Method d;
	
	public void testAtThis() {
		PointcutExpression atThis = parser.parsePointcutExpression("@this(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
		ShadowMatch sMatch1 = atThis.matchesMethodExecution(a);
		ShadowMatch sMatch2 = atThis.matchesMethodExecution(b);
		assertTrue("maybe matches A",sMatch1.maybeMatches());
		assertTrue("maybe matches B",sMatch2.maybeMatches());
		JoinPointMatch jp1 = sMatch1.matchesJoinPoint(new A(), new A(), new Object[0]);
		assertFalse("does not match",jp1.matches());
		JoinPointMatch jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[0]);
		assertTrue("matches",jp2.matches());
	}
	
	public void testAtTarget() {
		PointcutExpression atTarget = parser.parsePointcutExpression("@target(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
		ShadowMatch sMatch1 = atTarget.matchesMethodExecution(a);
		ShadowMatch sMatch2 = atTarget.matchesMethodExecution(b);
		assertTrue("maybe matches A",sMatch1.maybeMatches());
		assertTrue("maybe matches B",sMatch2.maybeMatches());
		JoinPointMatch jp1 = sMatch1.matchesJoinPoint(new A(), new A(), new Object[0]);
		assertFalse("does not match",jp1.matches());
		JoinPointMatch jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[0]);
		assertTrue("matches",jp2.matches());		
	}
	
	public void testAtThisWithBinding() {
		PointcutParameter param = parser.createPointcutParameter("a",MyAnnotation.class);
		B myB = new B();
		MyAnnotation bAnnotation = B.class.getAnnotation(MyAnnotation.class);
		PointcutExpression atThis = parser.parsePointcutExpression("@this(a)",A.class,new PointcutParameter[] {param});
		ShadowMatch sMatch1 = atThis.matchesMethodExecution(a);
		ShadowMatch sMatch2 = atThis.matchesMethodExecution(b);
		assertTrue("maybe matches A",sMatch1.maybeMatches());
		assertTrue("maybe matches B",sMatch2.maybeMatches());
		JoinPointMatch jp1 = sMatch1.matchesJoinPoint(new A(), new A(), new Object[0]);
		assertFalse("does not match",jp1.matches());
		JoinPointMatch jp2 = sMatch2.matchesJoinPoint(myB, myB, new Object[0]);
		assertTrue("matches",jp2.matches());
		assertEquals(1,jp2.getParameterBindings().length);
		assertEquals("should be myB's annotation",bAnnotation,jp2.getParameterBindings()[0].getBinding());
	}
	
	public void testAtTargetWithBinding() {
		PointcutParameter param = parser.createPointcutParameter("a",MyAnnotation.class);
		B myB = new B();
		MyAnnotation bAnnotation = B.class.getAnnotation(MyAnnotation.class);
		PointcutExpression atThis = parser.parsePointcutExpression("@target(a)",A.class,new PointcutParameter[] {param});
		ShadowMatch sMatch1 = atThis.matchesMethodExecution(a);
		ShadowMatch sMatch2 = atThis.matchesMethodExecution(b);
		assertTrue("maybe matches A",sMatch1.maybeMatches());
		assertTrue("maybe matches B",sMatch2.maybeMatches());
		JoinPointMatch jp1 = sMatch1.matchesJoinPoint(new A(), new A(), new Object[0]);
		assertFalse("does not match",jp1.matches());
		JoinPointMatch jp2 = sMatch2.matchesJoinPoint(myB, myB, new Object[0]);
		assertTrue("matches",jp2.matches());
		assertEquals(1,jp2.getParameterBindings().length);
		assertEquals("should be myB's annotation",bAnnotation,jp2.getParameterBindings()[0].getBinding());
	}
	
	public void testAtArgs() {
		PointcutExpression atArgs = parser.parsePointcutExpression("@args(..,org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
		ShadowMatch sMatch1 = atArgs.matchesMethodExecution(a);
		ShadowMatch sMatch2 = atArgs.matchesMethodExecution(c);
		assertTrue("never matches A",sMatch1.neverMatches());
		assertTrue("maybe matches C",sMatch2.maybeMatches());
		JoinPointMatch jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[]{new A(),new B()});
		assertTrue("matches",jp2.matches());	
		
		atArgs = parser.parsePointcutExpression("@args(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation,org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
		sMatch1 = atArgs.matchesMethodExecution(a);
		sMatch2 = atArgs.matchesMethodExecution(c);
		assertTrue("never matches A",sMatch1.neverMatches());
		assertTrue("maybe matches C",sMatch2.maybeMatches());
		JoinPointMatch jp1 = sMatch2.matchesJoinPoint(new A(), new A(), new Object[] {new A(), new B()});
		assertFalse("does not match",jp1.matches());
		jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[] {new B(),new B()});
		assertTrue("matches",jp2.matches());					
	}
	
	public void testAtArgs2() {
		PointcutExpression atArgs = parser.parsePointcutExpression("@args(*, org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
		ShadowMatch sMatch1 = atArgs.matchesMethodExecution(c);
		ShadowMatch sMatch2 = atArgs.matchesMethodExecution(d);
		assertTrue("maybe matches c",sMatch1.maybeMatches());
		assertTrue("maybe matches d",sMatch2.maybeMatches());
		JoinPointMatch jp1 = sMatch1.matchesJoinPoint(new B(), new B(), new Object[] {new A(), new B()});
		assertTrue("matches",jp1.matches());
		JoinPointMatch jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[] {new A(),new A()});
		assertFalse("does not match",jp2.matches());									
	}
	
	public void testAtArgsWithBinding() {
		PointcutParameter p1 = parser.createPointcutParameter("a",MyAnnotation.class);
		PointcutParameter p2 = parser.createPointcutParameter("b", MyAnnotation.class);
		PointcutExpression atArgs = parser.parsePointcutExpression("@args(..,a)",A.class,new PointcutParameter[] {p1});
		ShadowMatch sMatch2 = atArgs.matchesMethodExecution(c);
		assertTrue("maybe matches C",sMatch2.maybeMatches());
		JoinPointMatch jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[]{new A(),new B()});
		assertTrue("matches",jp2.matches());
		assertEquals(1,jp2.getParameterBindings().length);
		MyAnnotation bAnnotation = B.class.getAnnotation(MyAnnotation.class);
		assertEquals("annotation on B",bAnnotation,jp2.getParameterBindings()[0].getBinding());
		
		atArgs = parser.parsePointcutExpression("@args(a,b)",A.class,new PointcutParameter[] {p1,p2});
		sMatch2 = atArgs.matchesMethodExecution(c);
		assertTrue("maybe matches C",sMatch2.maybeMatches());
		jp2 = sMatch2.matchesJoinPoint(new B(), new B(), new Object[] {new B(),new B()});
		assertTrue("matches",jp2.matches());							
		assertEquals(2,jp2.getParameterBindings().length);
		assertEquals("annotation on B",bAnnotation,jp2.getParameterBindings()[0].getBinding());
		assertEquals("annotation on B",bAnnotation,jp2.getParameterBindings()[1].getBinding());		
	}
	
	public void testAtWithin() {
		PointcutExpression atWithin = parser.parsePointcutExpression("@within(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
		ShadowMatch sMatch1 = atWithin.matchesMethodExecution(a);
		ShadowMatch sMatch2 = atWithin.matchesMethodExecution(b);
		assertTrue("does not match a",sMatch1.neverMatches());
		assertTrue("matches b",sMatch2.alwaysMatches());
	}
	
	public void testAtWithinWithBinding() {
		PointcutParameter p1 = parser.createPointcutParameter("x",MyAnnotation.class);
		PointcutExpression atWithin = parser.parsePointcutExpression("@within(x)",B.class,new PointcutParameter[] {p1});
		ShadowMatch sMatch1 = atWithin.matchesMethodExecution(a);
		ShadowMatch sMatch2 = atWithin.matchesMethodExecution(b);
		assertTrue("does not match a",sMatch1.neverMatches());
		assertTrue("matches b",sMatch2.alwaysMatches());
		JoinPointMatch jpm = sMatch2.matchesJoinPoint(new B(), new B(), new Object[0]);
		assertTrue(jpm.matches());
		assertEquals(1,jpm.getParameterBindings().length);
		MyAnnotation bAnnotation = B.class.getAnnotation(MyAnnotation.class);
		assertEquals("annotation on B",bAnnotation,jpm.getParameterBindings()[0].getBinding());		
	}
	
	public void testAtWithinCode() {
		PointcutExpression atWithinCode = parser.parsePointcutExpression("@withincode(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
		ShadowMatch sMatch1 = atWithinCode.matchesMethodCall(a,b);
		ShadowMatch sMatch2 = atWithinCode.matchesMethodCall(a,a);
		assertTrue("does not match from b",sMatch1.neverMatches());
		assertTrue("matches from a",sMatch2.alwaysMatches());		
	}
	
	public void testAtWithinCodeWithBinding() {
		PointcutParameter p1 = parser.createPointcutParameter("x",MyAnnotation.class);
		PointcutExpression atWithinCode = parser.parsePointcutExpression("@withincode(x)",A.class,new PointcutParameter[] {p1});
		ShadowMatch sMatch2 = atWithinCode.matchesMethodCall(a,a);
		assertTrue("matches from a",sMatch2.alwaysMatches());
		JoinPointMatch jpm = sMatch2.matchesJoinPoint(new A(), new A(), new Object[0]);
		assertEquals(1,jpm.getParameterBindings().length);
		MyAnnotation annOna = a.getAnnotation(MyAnnotation.class);
		assertEquals("MyAnnotation on a",annOna,jpm.getParameterBindings()[0].getBinding());
	}
	
	public void testAtAnnotation() {
		PointcutExpression atAnnotation = parser.parsePointcutExpression("@annotation(org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation)");
		ShadowMatch sMatch1 = atAnnotation.matchesMethodCall(b,a);
		ShadowMatch sMatch2 = atAnnotation.matchesMethodCall(a,a);
		assertTrue("does not match call to b",sMatch1.neverMatches());
		assertTrue("matches call to a",sMatch2.alwaysMatches());				
	}
	
	public void testAtAnnotationWithBinding() {
		PointcutParameter p1 = parser.createPointcutParameter("x",MyAnnotation.class);
		PointcutExpression atAnnotation = parser.parsePointcutExpression("@annotation(x)",A.class,new PointcutParameter[] {p1});
		ShadowMatch sMatch2 = atAnnotation.matchesMethodCall(a,a);
		assertTrue("matches call to a",sMatch2.alwaysMatches());				
		JoinPointMatch jpm = sMatch2.matchesJoinPoint(new A(), new A(), new Object[0]);
		assertTrue(jpm.matches());
		assertEquals(1,jpm.getParameterBindings().length);
		MyAnnotation annOna = a.getAnnotation(MyAnnotation.class);
		assertEquals("MyAnnotation on a",annOna,jpm.getParameterBindings()[0].getBinding());		
	}
	
	public void testReferencePointcutNoParams() {
		PointcutExpression pc = parser.parsePointcutExpression("foo()",C.class,new PointcutParameter[0]);
		ShadowMatch sMatch1 = pc.matchesMethodCall(a,b);
		ShadowMatch sMatch2 = pc.matchesMethodExecution(a);
		assertTrue("no match on call",sMatch1.neverMatches());
		assertTrue("match on execution",sMatch2.alwaysMatches());
		
		pc = parser.parsePointcutExpression("org.aspectj.weaver.tools.Java15PointcutExpressionTest.C.foo()");
		sMatch1 = pc.matchesMethodCall(a,b);
		sMatch2 = pc.matchesMethodExecution(a);
		assertTrue("no match on call",sMatch1.neverMatches());
		assertTrue("match on execution",sMatch2.alwaysMatches());
	}
	
	public void testReferencePointcutParams() {
		PointcutParameter p1 = parser.createPointcutParameter("x",A.class);
		PointcutExpression pc = parser.parsePointcutExpression("goo(x)",C.class,new PointcutParameter[] {p1});

		ShadowMatch sMatch1 = pc.matchesMethodCall(a,b);
		ShadowMatch sMatch2 = pc.matchesMethodExecution(a);
		assertTrue("no match on call",sMatch1.neverMatches());
		assertTrue("match on execution",sMatch2.maybeMatches());
		A anA = new A();
		JoinPointMatch jpm = sMatch2.matchesJoinPoint(anA, new A(), new Object[0]);
		assertTrue(jpm.matches());
		assertEquals("should be bound to anA",anA,jpm.getParameterBindings()[0].getBinding());

	}
	
	public void testExecutionWithClassFileRetentionAnnotation() {
		PointcutExpression pc1 = parser.parsePointcutExpression("execution(@org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyAnnotation * *(..))");
		PointcutExpression pc2 = parser.parsePointcutExpression("execution(@org.aspectj.weaver.tools.Java15PointcutExpressionTest.MyClassFileRetentionAnnotation * *(..))");
		ShadowMatch sMatch = pc1.matchesMethodExecution(a);
		assertTrue("matches",sMatch.alwaysMatches());
		sMatch = pc2.matchesMethodExecution(a);
		assertTrue("no match",sMatch.neverMatches());
		sMatch = pc1.matchesMethodExecution(b);
		assertTrue("no match",sMatch.neverMatches());
		sMatch = pc2.matchesMethodExecution(b);
		assertTrue("matches",sMatch.alwaysMatches());
	}
	
	public void testGenericMethodSignatures() throws Exception{
		PointcutExpression ex = parser.parsePointcutExpression("execution(* set*(java.util.List<org.aspectj.weaver.tools.Java15PointcutExpressionTest.C>))");
		Method m = TestBean.class.getMethod("setFriends",List.class);
		ShadowMatch sm = ex.matchesMethodExecution(m);
		assertTrue("should match",sm.alwaysMatches());		
	}
	
	public void testAnnotationInExecution() throws Exception {
		PointcutExpression ex = parser.parsePointcutExpression("execution(@(org.springframework..*) * *(..))");		
	}
	
	public void testVarArgsMatching() throws Exception {
		PointcutExpression ex = parser.parsePointcutExpression("execution(* *(String...))");
		Method usesVarArgs = D.class.getMethod("varArgs",String[].class);
		Method noVarArgs = D.class.getMethod("nonVarArgs", String[].class);
		ShadowMatch sm1 = ex.matchesMethodExecution(usesVarArgs);
		assertTrue("should match",sm1.alwaysMatches());				
		ShadowMatch sm2 = ex.matchesMethodExecution(noVarArgs);
		assertFalse("should not match",sm2.alwaysMatches());				
	}
	
	public void testJavaLangMatching() throws Exception {
		PointcutExpression ex = parser.parsePointcutExpression("@within(java.lang.Deprecated)");
		Method foo = GoldenOldie.class.getMethod("foo");
		ShadowMatch sm1 = ex.matchesMethodExecution(foo);
		assertTrue("should match",sm1.alwaysMatches());
	}
	
	public void testReferencePCsInSameType() throws Exception {
		PointcutExpression ex = parser.parsePointcutExpression("org.aspectj.weaver.tools.Java15PointcutExpressionTest.NamedPointcutResolution.c()",NamedPointcutResolution.class,new PointcutParameter[0]);
		ShadowMatch sm = ex.matchesMethodExecution(a);
		assertTrue("should match",sm.alwaysMatches());
		sm = ex.matchesMethodExecution(b);
		assertTrue("does not match",sm.neverMatches());
	}
	
	public void testReferencePCsInOtherType() throws Exception {
		PointcutExpression ex = parser.parsePointcutExpression("org.aspectj.weaver.tools.Java15PointcutExpressionTest.ExternalReferrer.d()",ExternalReferrer.class,new PointcutParameter[0]);
		ShadowMatch sm = ex.matchesMethodExecution(a);
		assertTrue("should match",sm.alwaysMatches());
		sm = ex.matchesMethodExecution(b);
		assertTrue("does not match",sm.neverMatches());		
	}
	
	public void testArrayTypeInArgs() throws Exception {
		PointcutParameter[] params = new PointcutParameter[3];
		params[0] = parser.createPointcutParameter("d", Date.class);
		params[1] = parser.createPointcutParameter("s", String.class);
		params[2] = parser.createPointcutParameter("ss", String[].class);
		PointcutExpression ex = parser.parsePointcutExpression("org.aspectj.weaver.tools.Java15PointcutExpressionTest.UsesArrays.pc(d,s,ss)",UsesArrays.class,params);
	}
	
	protected void setUp() throws Exception {
		super.setUp();
		parser = PointcutParser.getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(this.getClass().getClassLoader());
		a = A.class.getMethod("a");
		b = B.class.getMethod("b");
		c = B.class.getMethod("c",new Class[] {A.class,B.class});
		d = B.class.getMethod("d",new Class[] {A.class,A.class});
	}

	@Retention(RetentionPolicy.RUNTIME)
	private @interface MyAnnotation {}
	
	private @interface MyClassFileRetentionAnnotation {}
	
	private static class A {
		@MyAnnotation public void a() {}
	}
	
	@MyAnnotation
	private static class B {
		@MyClassFileRetentionAnnotation public void b() {}
		public void c(A anA, B aB) {}
		
		public void d(A anA, A anotherA) {}
	}
	
	private static class C {
		
		@Pointcut("execution(* *(..))")
		public void foo() {}
		
		@Pointcut(value="execution(* *(..)) && this(x)", argNames="x")
		public void goo(A x) {}
	}
	
	private static class D {
		
		public void nonVarArgs(String[] strings) {};
		
		public void varArgs(String... strings) {};
		
	}
	
	static class TestBean {
		public void setFriends(List<C> friends) {}
	}

	@Deprecated
	static class GoldenOldie {
		public void foo() {}
	}
	
	private static class NamedPointcutResolution {
		
		@Pointcut("execution(* *(..))")
		public void a() {}
		
		@Pointcut("this(org.aspectj.weaver.tools.Java15PointcutExpressionTest.A)")
		public void b() {}
		
		@Pointcut("a() && b()")
		public void c() {}
	}
	
	private static class ExternalReferrer {
		
	  @Pointcut("org.aspectj.weaver.tools.Java15PointcutExpressionTest.NamedPointcutResolution.a() && " + 
			    "org.aspectj.weaver.tools.Java15PointcutExpressionTest.NamedPointcutResolution.b())")
	  public void d() {}
		
	}
	
	private static class UsesArrays {
		
		@Pointcut("execution(* *(..)) && args(d,s,ss)")
		public void pc(Date d, String s, String[] ss) {}
		
	}
}