Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 6bff09ee1b7b0e18d27d7ba042d93a31dfca9383 (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
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
/*******************************************************************************
 * Copyright (c) 2005, 2018 BEA Systems, Inc. 
 * 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:
 *    sbandow@bea.com - initial API and implementation
 *    
 *******************************************************************************/

package org.eclipse.jdt.apt.tests;

import java.io.File;
import java.util.Map;

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

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.runtime.IPath;
import org.eclipse.jdt.apt.core.internal.AptProject;
import org.eclipse.jdt.apt.core.internal.generatedfile.GeneratedFileManager;
import org.eclipse.jdt.apt.core.util.AptConfig;
import org.eclipse.jdt.apt.tests.annotations.ProcessorTestStatus;
import org.eclipse.jdt.apt.tests.annotations.filegen.TextGenAnnotationProcessor;
import org.eclipse.jdt.core.IJavaProject;
import org.eclipse.jdt.core.JavaCore;

public class FileGenerationTests extends APTTestBase {

	public FileGenerationTests(final String name)
	{
		super(name);
	}

	public static Test suite()
	{
		return new TestSuite(FileGenerationTests.class);
	}
	
	public void testSourceGenPackages() throws Exception
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		
		String code = 
				"package test;" + "\n" +
				"import org.eclipse.jdt.apt.tests.annotations.filegen.FileGenLocationAnnotation;" + "\n" +
				"@FileGenLocationAnnotation" + "\n" +
				"public class Test" + "\n" +
				"{" + "\n" +
				"}";

		env.addClass(srcRoot, "test", "Test", code);

		fullBuild( project.getFullPath() );
		expectingNoProblems();

		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}

	public void testSourceGenOverwrite() throws Exception
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();

		String code = 
			"package test;" + "\n" +
			"import org.eclipse.jdt.apt.tests.annotations.filegen.FirstGenAnnotation;" + "\n" +
			"@FirstGenAnnotation" + "\n" +
			"public class Test" + "\n" +
			"{" + "\n" +
			"}";

		env.addClass(srcRoot, "test", "Test", code);
		
		fullBuild( project.getFullPath() );
		expectingNoProblems();

		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}

	public void testSourceGenAfterDirChange() throws Exception
	{
		IJavaProject jproj = env.getJavaProject( getProjectName() );
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();

		String code = 
			"package test;" + "\n" +
			"import org.eclipse.jdt.apt.tests.annotations.helloworld.HelloWorldAnnotation;" + "\n" +
			"@HelloWorldAnnotation" + "\n" +
			"public class Test" + "\n" +
			"{" + "\n" +
			"	generatedfilepackage.GeneratedFileTest gft;" + "\n" +
			"}";

		AptConfig.setGenSrcDir(jproj, "__foo_src");
		
		env.addClass(srcRoot, "test", "Test", code);

		fullBuild( project.getFullPath() );
		expectingNoProblems();

		Map<String,String> options = AptConfig.getProcessorOptions(jproj, false);
		String sourcepath = options.get("-sourcepath");
		
		assertTrue(sourcepath.contains("__foo_src"));
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}
	
	public void testSourceGenSubDir() throws Exception
	{
		IJavaProject jproj = env.getJavaProject( getProjectName() );
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();

		String code = 
			"package test;" + "\n" +
			"import org.eclipse.jdt.apt.tests.annotations.helloworld.HelloWorldAnnotation;" + "\n" +
			"@HelloWorldAnnotation" + "\n" +
			"public class Test" + "\n" +
			"{" + "\n" +
			"	generatedfilepackage.GeneratedFileTest gft;" + "\n" +
			"}";

		AptConfig.setGenSrcDir(jproj, "gen/foo");
		env.addClass(srcRoot, "test", "Test", code);

		fullBuild( project.getFullPath() );
		expectingNoProblems();
		
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
	}
	
	public void testTextFileGen() throws Exception {
		// enable the test only on windows for now
		if (System.getProperty("os.name").indexOf("Windows") == -1) return;
		final String TEXT_FILE_NAME = "TextFile.txt";

		clearProcessorResult(TextGenAnnotationProcessor.class);
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		
		String code = 
				"package test;" + "\n" +
				"import org.eclipse.jdt.apt.tests.annotations.filegen.TextGenAnnotation;" + "\n" +
				"@TextGenAnnotation(\"" + TEXT_FILE_NAME + "\")" + "\n" +
				"public class Test" + "\n" +
				"{" + "\n" +
				"}";

		env.addClass(srcRoot, "test", "Test", code);

		fullBuild( project.getFullPath() );
		expectingNoProblems();
		
		// Look for the file
		Map<String,String> options = AptConfig.getProcessorOptions(JavaCore.create(project), false);
		// We'll find it in the binary output directory
		String outputRootPath = options.get("-d");
		File theFile = new File(new File(outputRootPath), TEXT_FILE_NAME);
		
		assertTrue("File was not found: " + theFile.getAbsolutePath(), theFile.exists());
		
		incrementalBuild( project.getFullPath() );
		expectingNoProblems();
		checkProcessorResult(TextGenAnnotationProcessor.class);
		assertTrue("File was not found: " + theFile.getAbsolutePath(), theFile.exists());
		
		// Change the annotation to specify an illegal filename, and an exception should be thrown
		code = 
			"package test;" + "\n" +
			"import org.eclipse.jdt.apt.tests.annotations.filegen.TextGenAnnotation;" + "\n" +
			"@TextGenAnnotation(\">.txt\")" + "\n" +
			"public class Test" + "\n" +
			"{" + "\n" +
			"}";
		env.addClass(srcRoot, "test", "Test", code);
		
		incrementalBuild( project.getFullPath() );
		expectingNoProblems();
		assertEquals("Could not generate text file due to IOException", getProcessorResult(TextGenAnnotationProcessor.class));
		assertTrue("File was found, but should be deleted: " + theFile.getAbsolutePath(), !theFile.exists());
		
		// remove the annotation, and the file should be deleted and processor should not run
		code = 
			"package test;" + "\n" +
			"public class Test" + "\n" +
			"{" + "\n" +
			"}";
		env.addClass(srcRoot, "test", "Test", code);
		
		incrementalBuild( project.getFullPath() );
		expectingNoProblems();
		assertEquals(null, getProcessorResult(TextGenAnnotationProcessor.class));
		assertTrue("File was found, but should be deleted: " + theFile.getAbsolutePath(), !theFile.exists());
	}
	
	public void testIsGeneratedOrParentFile() throws Exception
	{
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();
		
		String code = 
				"package test;" + "\n" +
				"import org.eclipse.jdt.apt.tests.annotations.filegen.FileGenLocationAnnotation;" + "\n" +
				"@FileGenLocationAnnotation" + "\n" +
				"public class Test" + "\n" +
				"{" + "\n" +
				"}";

		env.addClass(srcRoot, "test", "Test", code);
		
		fullBuild( project.getFullPath() );
		expectingNoProblems();

		AptProject aptProj = new AptProject(env.getJavaProject(getProjectName()));
		GeneratedFileManager gfm = aptProj.getGeneratedFileManager(false);
		String genSrcDir = AptConfig.getGenSrcDir(env.getJavaProject(getProjectName()));
		String P = File.separator;

		IFile parentFile = project.getFile("src" + P + "test" + P + "Test.java");
		IFile generatedFile = project.getFile(genSrcDir + P + "test" + P + "A.java");

		assertTrue("expected src/test/Test.java to be designated as parent file", gfm.isParentFile(parentFile));
		assertTrue("expected .apt_generated/test/A.java to be designated as generated file", gfm.isGeneratedFile(generatedFile));
	}

	public void testTestSourceGen() throws Exception
	{
		IJavaProject jproj = env.getJavaProject( getProjectName() );
		IProject project = env.getProject( getProjectName() );
		IPath srcRoot = getSourcePath();

		String mainCode = 
				"package test;" + "\n" +
				"import org.eclipse.jdt.apt.tests.annotations.helloworld.HelloWorldAnnotation;" + "\n" +
				"@HelloWorldAnnotation(\"GeneratedFileMain\")" + "\n" +
				"public class Main" + "\n" +
				"{" + "\n" +
				"	generatedfilepackage.GeneratedFileMain gfm;" + "\n" +
				"}";
		String testCode = 
				"package test;" + "\n" +
				"import org.eclipse.jdt.apt.tests.annotations.helloworld.HelloWorldAnnotation;" + "\n" +
				"@HelloWorldAnnotation" + "\n" +
				"public class Test" + "\n" +
				"{" + "\n" +
				"	generatedfilepackage.GeneratedFileTest gft;" + "\n" +
				"	generatedfilepackage.GeneratedFileMain gfm;" + "\n" +
				"}";

		AptConfig.setGenSrcDir(jproj, "__main_foo_src");
		AptConfig.setGenTestSrcDir(jproj, "__test_foo_src");

		IPath testSrcRoot = addTestSourceFolder();

		env.addClass(srcRoot, "test", "Main", mainCode);
		env.addClass(testSrcRoot, "test", "Test", testCode);

		fullBuild( project.getFullPath() );
		expectingNoProblems();

		assertTrue(AptConfig.getProcessorOptions(jproj, false).get("-sourcepath").contains("__main_foo_src"));
		assertTrue(AptConfig.getProcessorOptions(jproj, true).get("-sourcepath").contains("__test_foo_src"));
		assertEquals(ProcessorTestStatus.NO_ERRORS, ProcessorTestStatus.getErrors());
		
		String P = File.separator;

		AptProject aptProj = new AptProject(env.getJavaProject(getProjectName()));
		GeneratedFileManager maingfm = aptProj.getGeneratedFileManager(false);
		GeneratedFileManager testgfm = aptProj.getGeneratedFileManager(true);

		IFile mainParentFile = project.getFile("src" + P + "test" + P + "Main.java");
		IFile mainGeneratedFile = project.getFile("__main_foo_src" + P + "generatedfilepackage" + P + "GeneratedFileMain.java");

		IFile testParentFile = project.getFile("src-tests" + P + "test" + P + "Test.java");
		IFile testGeneratedFile = project.getFile("__test_foo_src" + P + "generatedfilepackage" + P + "GeneratedFileTest.java");

		assertTrue("expected "
				+ mainParentFile.getProjectRelativePath()
				+ " to be designated as main parent file", maingfm.isParentFile(mainParentFile));
		assertTrue("expected " + mainGeneratedFile.getProjectRelativePath()
				+ " to be designated as main generated file", maingfm.isGeneratedFile(mainGeneratedFile));
		assertFalse("expected "
				+ testParentFile.getProjectRelativePath()
				+ " not to be designated as main parent file", maingfm.isParentFile(testParentFile));
		assertFalse("expected " + testGeneratedFile.getProjectRelativePath()
				+ " not to be designated as main generated file", maingfm.isGeneratedFile(testGeneratedFile));

		assertTrue("expected "
				+ testParentFile.getProjectRelativePath()
				+ " to be designated as test parent file", testgfm.isParentFile(testParentFile));
		assertTrue("expected " + testGeneratedFile.getProjectRelativePath()
				+ " to be designated as test generated file", testgfm.isGeneratedFile(testGeneratedFile));
		assertFalse("expected "
				+ mainParentFile.getProjectRelativePath()
				+ " not to be designated as test parent file", testgfm.isParentFile(mainParentFile));
		assertFalse("expected " + mainGeneratedFile.getProjectRelativePath()
				+ " not to be designated as test generated file", testgfm.isGeneratedFile(mainGeneratedFile));

	}

}

Back to the top