aboutsummaryrefslogtreecommitdiffstats
path: root/tests/src/test/java/org/aspectj/systemtest/incremental/tools/AnnotationProcessingTests.java
blob: a220384b74b5f2041901210ec1e32035b0746759 (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
/*******************************************************************************
 * 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.incremental.tools;

import java.io.File;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

import org.aspectj.util.FileUtil;

public class AnnotationProcessingTests extends AbstractMultiProjectIncrementalAjdeInteractionTestbed {


	@Override
	protected void tearDown() throws Exception {
		super.tearDown();
		new File("Advise_aaa.java").delete();
		new File("Advise_ccc.java").delete();
		new File("Advise_boo.java").delete();
		new File("Advise_too.java").delete();
		new File("AroundAdvise_aaa.java").delete();
		new File("AroundAdvise_ccc.java").delete();
		if (new File("../run-all-junit-tests/generated/test/SomeCallbacks.java").exists()) {
			FileUtil.deleteContents(new File("../run-all-junit-tests/generated"));
			new File("../run-all-junit-tests/generated").delete();
		}
	}

	// Basic test: turns on annotation processing and tries to run the DemoProcessor
	public void testAnnotationProcessing1() throws Exception {
		createAndBuildAnnotationProcessorProject("ProcessorProject");
		initialiseProject("ProcessorConsumer1");
		configureProcessorOptions("ProcessorConsumer1","DemoProcessor");
		configureNonStandardCompileOptions("ProcessorConsumer1", "-showWeaveInfo");

		Map<String, String> javaOptions = new Hashtable<>();
		javaOptions.put("org.eclipse.jdt.core.compiler.compliance", "1.8");
		javaOptions.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "1.8");
		javaOptions.put("org.eclipse.jdt.core.compiler.source", "1.8");
		javaOptions.put("org.eclipse.jdt.core.compiler.processAnnotations","enabled");
		configureJavaOptionsMap("ProcessorConsumer1", javaOptions);

		configureNewProjectDependency("ProcessorConsumer1", "ProcessorProject");
		configureNonStandardCompileOptions("ProcessorConsumer1", "-showWeaveInfo");
		build("ProcessorConsumer1");
		checkWasFullBuild();
		checkCompiledFiles("ProcessorConsumer1","Advise_ccc.java","Advise_aaa.java","Code.java");
		assertEquals(2,getWeavingMessages("ProcessorConsumer1").size());
		String out = runMethod("ProcessorConsumer1", "Code", "runner");
		assertEquals("aaa running\nccc running\n",out.replace("\r",""));
	}

	// services file in processor project
	public void testAnnotationProcessing2() throws Exception {
		createAndBuildAnnotationProcessorProject("ProcessorProject2"); // This has a META-INF services entry for DemoProcessor

		initialiseProject("ProcessorConsumer2");
		// Paths here are the path to DemoProcessor (compiled into the output folder of the ProcessorProject2) and the path to
		// the META-INF file declaring DemoProcessor (since it is not copied to that same output folder) - this exists in the test src
		// folder for ProcessorProject2
		configureProcessorPath("ProcessorConsumer2", getCompilerForProjectWithName("ProcessorProject2").getCompilerConfiguration().getOutputLocationManager().getDefaultOutputLocation().toString()+File.pathSeparator+
				new File(testdataSrcDir + File.separatorChar + "ProcessorProject2" + File.separatorChar + "base"+File.separatorChar+"src").toString());

		Map<String, String> javaOptions = new Hashtable<>();
		javaOptions.put("org.eclipse.jdt.core.compiler.compliance", "1.8");
		javaOptions.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "1.8");
		javaOptions.put("org.eclipse.jdt.core.compiler.source", "1.8");
		javaOptions.put("org.eclipse.jdt.core.compiler.processAnnotations","enabled");
		configureJavaOptionsMap("ProcessorConsumer2", javaOptions);
		initialiseProject("ProcessorConsumer2");
		configureNewProjectDependency("ProcessorConsumer2", "ProcessorProject");
		configureNonStandardCompileOptions("ProcessorConsumer2", "-showWeaveInfo");
		build("ProcessorConsumer2");
		checkWasFullBuild();
		checkCompiledFiles("ProcessorConsumer2","Advise_ccc.java","Advise_aaa.java","Code.java");
		assertEquals(2,getWeavingMessages("ProcessorConsumer2").size());
		String out = runMethod("ProcessorConsumer2", "Code", "runner");
		assertEquals("aaa running\nccc running\n",out.replace("\r",""));
	}

	// Two processors
	public void testAnnotationProcessing3() throws Exception {
		createAndBuildAnnotationProcessorProject("ProcessorProject2");
		createAndBuildAnnotationProcessorProject("ProcessorProject3");
		initialiseProject("ProcessorConsumer1");
		// Paths here are the path to DemoProcessor/DemoProcessor2 compiled code and the path to
		// the META-INF file declaring DemoProcessor/DemoProcessor2 (since they are not copied to that same output folder) -
		// these exists in the test src folders for ProcessorProject2/ProcessorProject3
		configureProcessorPath("ProcessorConsumer1",
				getCompilerForProjectWithName("ProcessorProject3").getCompilerConfiguration().getOutputLocationManager().getDefaultOutputLocation().toString()+File.pathSeparator+
				new File(testdataSrcDir + File.separatorChar + "ProcessorProject3" + File.separatorChar + "base"+File.separatorChar+"src").toString()
				+File.pathSeparator+
				getCompilerForProjectWithName("ProcessorProject2").getCompilerConfiguration().getOutputLocationManager().getDefaultOutputLocation().toString()+File.pathSeparator+
				new File(testdataSrcDir + File.separatorChar + "ProcessorProject2" + File.separatorChar + "base"+File.separatorChar+"src").toString()
				);

		// The order here is DemoProcessor2 then DemoProcessor - to get the second one to run I changed DemoProcessor2 to operate on a
		// specific annotation (java.lang.SuppressWarnings) and return false at the end

		configureNonStandardCompileOptions("ProcessorConsumer1", "-showWeaveInfo");

		Map<String, String> javaOptions = new Hashtable<>();
		javaOptions.put("org.eclipse.jdt.core.compiler.compliance", "1.8");
		javaOptions.put("org.eclipse.jdt.core.compiler.codegen.targetPlatform", "1.8");
		javaOptions.put("org.eclipse.jdt.core.compiler.source", "1.8");
		javaOptions.put("org.eclipse.jdt.core.compiler.processAnnotations","enabled");
		configureJavaOptionsMap("ProcessorConsumer1", javaOptions);

		configureNewProjectDependency("ProcessorConsumer1", "ProcessorProject");
		configureNonStandardCompileOptions("ProcessorConsumer1", "-showWeaveInfo");
		build("ProcessorConsumer1");
		checkWasFullBuild();
		checkCompiledFiles("ProcessorConsumer1","Advise_ccc.java","Advise_aaa.java","Code.java","AroundAdvise_ccc.java","AroundAdvise_aaa.java");
		assertEquals(4,getWeavingMessages("ProcessorConsumer1").size());
		String out = runMethod("ProcessorConsumer1", "Code", "runner");
		assertEquals("aaa running\nAround advice on aaa running\nccc running\nAround advice on ccc running\n",out.replace("\r",""));
	}

	// Tests:
	// TODO Incremental compilation - what does that mean with annotation processors?

	// ---

	private void createAndBuildAnnotationProcessorProject(String processorProjectName) {
		initialiseProject(processorProjectName);
		build(processorProjectName);
		checkWasFullBuild();
		assertNoErrors(processorProjectName);
	}

	private void configureProcessorOptions(String projectName, String processor) {
		configureProcessor(projectName, "DemoProcessor");
		// Assume all processors from processor project
		configureProcessorPath(projectName, getCompilerForProjectWithName("ProcessorProject").getCompilerConfiguration().getOutputLocationManager().getDefaultOutputLocation().toString());
	}

	private void checkCompiledFiles(String projectName, String... expectedCompiledFiles) {
		List<String> compiledFiles = new ArrayList<>(getCompiledFiles(projectName));
		if (compiledFiles.size()!=expectedCompiledFiles.length) {
			fail("Expected #"+expectedCompiledFiles.length+" files to be compiled but found that #"+compiledFiles.size()+" files were compiled.\nCompiled="+compiledFiles);
		}
		for (String expectedCompiledFile: expectedCompiledFiles) {
			String toRemove = null;
			for (String compiledFile: compiledFiles) {
				String cfile = compiledFile.substring(compiledFile.lastIndexOf(File.separator)+1);
				if (cfile.equals(expectedCompiledFile)) {
					toRemove = compiledFile;
					break;
				}
			}
			if (toRemove!=null) compiledFiles.remove(toRemove);
		}
		// Anything left in compiledFiles wasn't expected to be built
		if (compiledFiles.size()!=0) {
			fail("These were not expected to be compiled: "+compiledFiles);
		}
	}


}