aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/test/java/org/aspectj/systemtest/ajc186/Ajc186Tests.java
blob: ba80be718e56b10ce84e5456456868f5a95012d3 (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
/*******************************************************************************
 * Copyright (c) 2014 Contributors
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v 2.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/org/documents/epl-2.0/EPL-2.0.txt
 *
 * Contributors:
 *    Andy Clement - initial API and implementation
 *******************************************************************************/
package org.aspectj.systemtest.ajc186;

import java.lang.reflect.Method;
import java.net.URL;
import java.net.URLClassLoader;

import org.aspectj.testing.XMLBasedAjcTestCase;
import org.aspectj.weaver.tools.ContextBasedMatcher;
import org.aspectj.weaver.tools.DefaultMatchingContext;
import org.aspectj.weaver.tools.FuzzyBoolean;
import org.aspectj.weaver.tools.MatchingContext;
import org.aspectj.weaver.tools.PointcutDesignatorHandler;
import org.aspectj.weaver.tools.PointcutExpression;
import org.aspectj.weaver.tools.PointcutParser;

import junit.framework.Test;

/**
 * @author Andy Clement
 */
public class Ajc186Tests extends org.aspectj.testing.XMLBasedAjcTestCase {

	private class FooDesignatorHandler implements PointcutDesignatorHandler {

		private String askedToParse;
		public boolean simulateDynamicTest = false;

		public String getDesignatorName() {
			return "foo";
		}

		public ContextBasedMatcher parse(String expression) {
			this.askedToParse = expression;
			return new FooPointcutExpression(expression, this.simulateDynamicTest);
		}

		public String getExpressionLastAskedToParse() {
			return this.askedToParse;
		}
	}

	private class FooPointcutExpression implements ContextBasedMatcher {

		private final String beanNamePattern;
		private final boolean simulateDynamicTest;

		public FooPointcutExpression(String beanNamePattern,
				boolean simulateDynamicTest) {
			this.beanNamePattern = beanNamePattern;
			this.simulateDynamicTest = simulateDynamicTest;
		}

		public boolean couldMatchJoinPointsInType(Class aClass) {
			System.out.println("wubble?");
			return true;
		}

		public boolean couldMatchJoinPointsInType(Class aClass,
				MatchingContext context) {
			System.out.println("wibble?");
			if (this.beanNamePattern.equals(context.getBinding("beanName"))) {
				return true;
			} else {
				return false;
			}
		}

		public boolean mayNeedDynamicTest() {
			return this.simulateDynamicTest;
		}

		public FuzzyBoolean matchesStatically(MatchingContext matchContext) {
			System.out.println("wobble?");
			if (this.simulateDynamicTest)
				return FuzzyBoolean.MAYBE;
			if (this.beanNamePattern
					.equals(matchContext.getBinding("beanName"))) {
				return FuzzyBoolean.YES;
			} else {
				return FuzzyBoolean.NO;
			}
		}

		public boolean matchesDynamically(MatchingContext matchContext) {
			System.out.println("wabble?");
			return this.beanNamePattern.equals(matchContext
					.getBinding("beanName"));
		}
	}

	public void testLambdaBeans() throws Exception {
		runTest("lambda beans");

		// Load the 1.8 compiled code
		URLClassLoader ucl = new URLClassLoader(new URL[] {ajc.getSandboxDirectory().toURI().toURL()},this.getClass().getClassLoader());
		Class<?> applicationClass = Class.forName("Application",false,ucl);
		assertNotNull(applicationClass);
		Object instance = applicationClass.getDeclaredConstructor().newInstance();
		Method works = applicationClass.getDeclaredMethod("fromInnerClass");
		works.setAccessible(true);
		Runnable r = (Runnable) works.invoke(instance);
		// r.getClass().getName() == Application$1

		Method fails = applicationClass.getDeclaredMethod("fromLambdaExpression");
		fails.setAccessible(true);
		Runnable r2 = (Runnable) fails.invoke(instance);
		// r2.getClass().getName() == Application$$Lambda$1/1652149987

//		JavaClass jc = getClassFrom(ajc.getSandboxDirectory(), "Application");
		PointcutParser parser = PointcutParser
				.getPointcutParserSupportingAllPrimitivesAndUsingSpecifiedClassloaderForResolution(ucl);
		FooDesignatorHandler beanHandler = new FooDesignatorHandler();
		parser.registerPointcutDesignatorHandler(beanHandler);
		PointcutExpression pc = parser.parsePointcutExpression("foo(myBean)");
		DefaultMatchingContext context = new DefaultMatchingContext();
		pc.setMatchingContext(context);

		context.addContextBinding("beanName", "myBean");
		assertTrue(pc.couldMatchJoinPointsInType(r.getClass()));

		context.addContextBinding("beanName", "yourBean");
		assertFalse(pc.couldMatchJoinPointsInType(r.getClass()));

		context.addContextBinding("beanName", "myBean");
		assertTrue(pc.couldMatchJoinPointsInType(r2.getClass()));

		context.addContextBinding("beanName", "yourBean");
		assertFalse(pc.couldMatchJoinPointsInType(r2.getClass()));
	}


	public void testMissingExtends() throws Exception {
		runTest("missing extends on generic target");
	}

	public void testMissingMethod_462821() throws Exception {
		runTest("missing method");
	}

	// ---

	public static Test suite() {
		return XMLBasedAjcTestCase.loadSuite(Ajc186Tests.class);
	}

	@Override
	protected java.net.URL getSpecFile() {
		return getClassResource("ajc186.xml");
	}

}