Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: f4d308d47627a4e5cc57a19b525a8d135a119554 (plain) (blame)
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
/*******************************************************************************
 * Copyright (c) 2008 - 2014 Walter Harley and others
 * 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://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    eclipse@cafewalter.com - initial API and implementation
 *******************************************************************************/

package org.eclipse.jdt.apt.pluggable.tests;

import java.util.List;

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

import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.apt.core.util.AptConfig;
import org.eclipse.jdt.apt.pluggable.tests.processors.buildertester.BugsProc;
import org.eclipse.jdt.apt.pluggable.tests.processors.buildertester.InheritedAnnoProc;
import org.eclipse.jdt.apt.pluggable.tests.processors.buildertester.TestFinalRoundProc;
import org.eclipse.jdt.core.IJavaProject;

/**
 * Tests covering the IDE's ability to process the correct set of files.
 */
public class BuilderTests extends TestBase
{

	public BuilderTests(String name) {
		super(name);
	}

	public static Test suite() {
		return new TestSuite(BuilderTests.class);
	}
	
	/**
	 * Verify that a new type generated in the final round does not get
	 * annotations processed, but does get compiled. The JSR269 spec is somewhat
	 * vague about whether it should be possible to generate a new type during
	 * the final round (since the final round does not happen until after a
	 * round in which no new types are generated); but apparently javac behaves
	 * this way.
	 * <p>
	 * See <a href="http://bugs.eclipse.org/329156">Bug 329156</a> and <a
	 * href="http://bugs.sun.com/view_bug.do?bug_id=6634138">the corresponding
	 * bug in javac</a>, which Sun fixed.
	 */
	public void testFinalRound() throws Throwable {
		ProcessorTestStatus.reset();
		TestFinalRoundProc.resetNumRounds();
		IJavaProject jproj = createJavaProject(_projectName);
		disableJava5Factories(jproj);
		IProject proj = jproj.getProject();
		IPath projPath = proj.getFullPath();
		IPath root = projPath.append("src");
		
		// The @FinalRoundTestTrigger processor does not generate any files when it
		// first runs; but on its final round it then generates a new Java type
		// that is annotated with @FinalRoundTestTrigger.
		env.addClass(root, "t", "Foo",
				"package t;\n" +
				"import org.eclipse.jdt.apt.pluggable.tests.annotations.FinalRoundTestTrigger;\n" +
				"@FinalRoundTestTrigger\n" +
				"public class Foo {}"
		);
		AptConfig.setEnabled(jproj, true);
		
		fullBuild();
		expectingNoProblems();
		
		// Processor should have run total of two rounds; compiled classes
		// should include Foo and FinalRoundGen.
		assertEquals(2, TestFinalRoundProc.getNumRounds());
		expectingUniqueCompiledClasses(new String[] {"t.Foo", "g.FinalRoundGen"});
	}
	
	/**
	 * Verify that a class whose superclass is annotated with an inherited annotation
	 * gets treated the same as if the annotation were present on the class itself.
	 * See <a href="http://bugs.eclipse.org/270754">Bug 270754</a>.
	 */
	public void disabled_testInheritedAnnotation() throws Throwable {
		ProcessorTestStatus.reset();
		IJavaProject jproj = createJavaProject(_projectName);
		disableJava5Factories(jproj);
		IProject proj = jproj.getProject();
		IPath projPath = proj.getFullPath();
		IPath root = projPath.append("src");
		
		env.addClass(root, "", "Base",
				"import org.eclipse.jdt.apt.pluggable.tests.annotations.InheritedTrigger;\n" +
				"@InheritedTrigger(0)\n" +
				"public class Base {}"
		);

		// Because Sub extends Base, it should be treated as if it were annotated with InheritedTrigger
		env.addClass(root, "", "Sub",
				"public class Sub extends Base {\n" +
				"}"
		);
			
		AptConfig.setEnabled(jproj, true);
		
		fullBuild();
		expectingNoProblems();
		List<String> elements = InheritedAnnoProc.getProcessedElements();
		assertTrue(elements.contains("Base"));
		assertTrue(elements.contains("Sub"));
		assertTrue("Processor did not run", ProcessorTestStatus.processorRan());
		assertEquals("Processor reported errors", ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());

		// Modify base class and verify that both base and subclass get processed
		InheritedAnnoProc.clearProcessedElements();
		env.addClass(root, "", "Base",
				"import org.eclipse.jdt.apt.pluggable.tests.annotations.InheritedTrigger;\n" +
				"@InheritedTrigger(1)\n" +
				"public class Base {}"
		);
		incrementalBuild();
		expectingNoProblems();
		elements = InheritedAnnoProc.getProcessedElements();
		assertTrue(elements.contains("Base"));
		assertTrue(elements.contains("Sub"));
		assertTrue("Processor did not run", ProcessorTestStatus.processorRan());
		assertEquals("Processor reported errors", ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
		
		// Modify subclass and verify that it gets processed
		InheritedAnnoProc.clearProcessedElements();
		env.addClass(root, "", "Sub",
				"public class Sub extends Base {\n" +
				" // this line is new\n" +
				"}"
		);
		incrementalBuild();
		expectingNoProblems();
		elements = InheritedAnnoProc.getProcessedElements();
		assertTrue(elements.contains("Sub"));
		assertTrue("Processor did not run", ProcessorTestStatus.processorRan());
		assertEquals("Processor reported errors", ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}
	
	/**
	 * Deleting FooEvent caused the reference to FooEvent in 
	 * FooImpl to appear as a package in one case and as a type in another,
	 * in turn causing a null binding to be passed to APT.
	 *
	 * See <a href="http://bugs.eclipse.org/295948">Bug 295948</a>.
	 */
	public void testBug295948() throws Throwable {
		ProcessorTestStatus.reset();
		IJavaProject jproj = createJavaProject(_projectName);
		disableJava5Factories(jproj);
		IProject proj = jproj.getProject();
		IPath projPath = proj.getFullPath();
		IPath root = projPath.append("src");
		
		env.addClass(root, "test295948", "FooEvent",
				"package test295948;\n" +
				"public class FooEvent {\n" + 
				"    public interface Handler {\n" + 
				"        void handle(FooEvent event);\n" + 
				"    }\n" + 
				"}\n" + 
				"\n"
		);
		
		IPath fooImplClass = env.addClass(root, "test295948", "FooImpl",
				"package test295948;\n" +
				"public class FooImpl implements FooEvent.Handler {\n" + 
				"    @Override\n" + 
				"    public void handle(FooEvent event) {\n" + 
				"    }\n" + 
				"}\n"
		);
		
		AptConfig.setEnabled(jproj, true);
		
		fullBuild();
		expectingNoProblems();
		
		// Delete FooEvent and recompile
		proj.findMember("src/test295948/FooEvent.java").delete(false, null);
		incrementalBuild();
		expectingProblemsFor(fooImplClass,
				"Problem : FooEvent cannot be resolved to a type [ resource : </" + _projectName + "/src/test295948/FooImpl.java> range : <108,116> category : <40> severity : <2>]\n" + 
				"Problem : FooEvent cannot be resolved to a type [ resource : </" + _projectName + "/src/test295948/FooImpl.java> range : <52,60> category : <40> severity : <2>]");
	}	
	// https://bugs.eclipse.org/bugs/show_bug.cgi?id=407841
	public void testBbug407841() throws Throwable {
		int old = org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.MAX_AT_ONCE;
		try {
			org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.MAX_AT_ONCE =1;
			ProcessorTestStatus.reset();
			IJavaProject jproj = createJavaProject(_projectName);
			disableJava5Factories(jproj);
			IProject proj = jproj.getProject();
			IdeTestUtils.copyResources(proj, "targets/bug407841", "src/targets/bug407841");
			
			AptConfig.setEnabled(jproj, true);
			fullBuild();
			expectingNoProblems();
			assertEquals("Elements should have been processed", 0, BugsProc.getUnprocessedElements());
			assertEquals("Elements should have been processed", 3, BugsProc.getNumRounds());
		} finally {
			org.eclipse.jdt.internal.core.builder.AbstractImageBuilder.MAX_AT_ONCE = old;
		}
	}
	
}

Back to the top