View | Details | Raw Unified | Return to bug 92398 | Differences between
and this patch

Collapse All | Expand All

(-)src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java (+285 lines)
Lines 10-17 Link Here
10
 *******************************************************************************/
10
 *******************************************************************************/
11
package org.eclipse.jdt.core.tests.compiler.regression;
11
package org.eclipse.jdt.core.tests.compiler.regression;
12
12
13
import java.io.File;
14
import java.io.FileNotFoundException;
15
import java.io.FileOutputStream;
16
import java.io.PrintWriter;
13
import junit.framework.Test;
17
import junit.framework.Test;
14
18
19
import org.eclipse.jdt.core.tests.util.Util;
15
import org.eclipse.jdt.internal.compiler.batch.Main;
20
import org.eclipse.jdt.internal.compiler.batch.Main;
16
21
17
public class BatchCompilerTest extends AbstractRegressionTest {
22
public class BatchCompilerTest extends AbstractRegressionTest {
Lines 20-26 Link Here
20
}
25
}
21
public static Test suite() {
26
public static Test suite() {
22
	return setupSuite(testClass());
27
	return setupSuite(testClass());
28
	// TODO find a way to reduce the number of command line tests to one per 
29
	//      test run (aka do not add 1.3, 1.4, 1.5 supplementary level)
23
}
30
}
31
32
	/**
33
	 * Run a compilation test that is expected to complete successfully and
34
	 * compare the outputs to expected ones.
35
	 * 
36
	 * @param testFiles
37
	 *            the source files, given as a suite of file name, file content;
38
	 *            file names are relative to the output directory
39
	 * @param commandLine
40
	 *            the command line to pass to
41
	 *            {@link Main#compile(String) Main#compile}
42
	 * @param expectedSuccessOutOutputString
43
	 *            the expected contents of the standard output stream; pass null
44
	 *            to bypass the comparison
45
	 * @param expectedSuccessErrOutputString
46
	 *            the expected contents of the standard error output stream;
47
	 *            pass null to bypass the comparison
48
	 * @param shouldFlushOutputDirectory
49
	 *            pass true to get the output directory flushed before the test
50
	 *            runs
51
	 */
52
	protected void runConformTest(String[] testFiles, String commandLine,
53
			String expectedSuccessOutOutputString,
54
			String expectedSuccessErrOutputString,
55
			boolean shouldFlushOutputDirectory) {
56
		File outputDirectory = new File(OUTPUT_DIR);
57
		if (shouldFlushOutputDirectory)
58
			Util.flushDirectoryContent(outputDirectory);
59
		try {
60
			if (!outputDirectory.isDirectory()) {
61
				outputDirectory.mkdirs();
62
			}
63
			PrintWriter sourceFileWriter;
64
			for (int i = 0; i < testFiles.length; i += 2) {
65
				sourceFileWriter = new PrintWriter(new FileOutputStream(
66
						OUTPUT_DIR + File.separator + testFiles[i]));
67
				sourceFileWriter.write(testFiles[i + 1]);
68
				sourceFileWriter.close();
69
			}
70
		} catch (FileNotFoundException e) {
71
			e.printStackTrace();
72
			throw new RuntimeException(e);
73
		}
74
		String printerWritersNameRoot = OUTPUT_DIR + File.separator
75
				+ testName();
76
		String outFileName = printerWritersNameRoot + "out.txt", errFileName = printerWritersNameRoot + "err.txt"; //$NON-NLS-1$ //$NON-NLS-2$
77
		Main batchCompiler;
78
		try {
79
			batchCompiler = new Main(new PrintWriter(new FileOutputStream(
80
					outFileName)), new PrintWriter(new FileOutputStream(
81
					errFileName)), false);
82
		} catch (FileNotFoundException e) {
83
			System.out.println(getClass().getName() + '#' + getName());
84
			e.printStackTrace();
85
			throw new RuntimeException(e);
86
		}
87
		boolean compileOK;
88
		try {
89
			compileOK = batchCompiler.compile(Main.tokenize(commandLine));
90
		} catch (RuntimeException e) {
91
			compileOK = false;
92
			System.out.println(getClass().getName() + '#' + getName());
93
			e.printStackTrace();
94
			throw e;
95
		}
96
		String outOutputString = Util.fileContent(outFileName), errOutputString = Util
97
				.fileContent(errFileName);
98
		boolean compareOK = false;
99
		if (compileOK) {
100
			compareOK = (expectedSuccessOutOutputString == null || outOutputString
101
					.equals(expectedSuccessOutOutputString))
102
					&& (expectedSuccessErrOutputString == null || errOutputString
103
							.equals(expectedSuccessErrOutputString));
104
		}
105
		if (!compileOK || !compareOK) {
106
			System.out.println(getClass().getName() + '#' + getName());
107
			for (int i = 0; i < testFiles.length; i += 2) {
108
				System.out.print(testFiles[i]);
109
				System.out.println(" ["); //$NON-NLS-1$
110
				System.out.println(testFiles[i + 1]);
111
				System.out.println("]"); //$NON-NLS-1$
112
			}
113
		}
114
		if (!compileOK)
115
			System.out.println(errOutputString);
116
		if (compileOK && !compareOK) {
117
			System.out.println("--[START OUT]--\n"
118
					+ Util.displayString(outOutputString)
119
					+ "\n---[END OUT]---\n--[START ERR]--\n"
120
					+ Util.displayString(errOutputString)
121
					+ "\n---[END ERR]--\n");
122
		}
123
		assertTrue("Unexpected problems: " + errOutputString, compileOK);
124
		assertTrue("Unexpected output for invocation with arguments ["
125
				+ commandLine + "]:\n" + "--[START]--\n" + outOutputString
126
				+ errOutputString + "---[END]---\n", compareOK);
127
	}
128
129
	/**
130
	 * Run a compilation test that is expected to fail and compare the outputs
131
	 * to expected ones.
132
	 * 
133
	 * @param testFiles
134
	 *            the source files, given as a suite of file name, file content;
135
	 *            file names are relative to the output directory
136
	 * @param commandLine
137
	 *            the command line to pass to
138
	 *            {@link Main#compile(String) Main#compile}
139
	 * @param expectedSuccessOutOutputString
140
	 *            the expected contents of the standard output stream; pass null
141
	 *            to bypass the comparison
142
	 * @param expectedSuccessErrOutputString
143
	 *            the expected contents of the standard error output stream;
144
	 *            pass null to bypass the comparison
145
	 * @param shouldFlushOutputDirectory
146
	 *            pass true to get the output directory flushed before the test
147
	 *            runs
148
	 */
149
	protected void runNegativeTest(String[] testFiles, String commandLine,
150
			String expectedSuccessOutOutputString,
151
			String expectedSuccessErrOutputString,
152
			boolean shouldFlushOutputDirectory) {
153
		File outputDirectory = new File(OUTPUT_DIR);
154
		if (shouldFlushOutputDirectory)
155
			Util.flushDirectoryContent(outputDirectory);
156
		try {
157
			if (!outputDirectory.isDirectory()) {
158
				outputDirectory.mkdirs();
159
			}
160
			PrintWriter sourceFileWriter;
161
			for (int i = 0; i < testFiles.length; i += 2) {
162
				sourceFileWriter = new PrintWriter(new FileOutputStream(
163
						OUTPUT_DIR + File.separator + testFiles[i]));
164
				sourceFileWriter.write(testFiles[i + 1]);
165
				sourceFileWriter.close();
166
			}
167
		} catch (FileNotFoundException e) {
168
			e.printStackTrace();
169
			throw new RuntimeException(e);
170
		}
171
		String printerWritersNameRoot = OUTPUT_DIR + File.separator
172
				+ testName();
173
		String outFileName = printerWritersNameRoot + "out.txt", errFileName = printerWritersNameRoot + "err.txt"; //$NON-NLS-1$ //$NON-NLS-2$
174
		Main batchCompiler;
175
		try {
176
			batchCompiler = new Main(new PrintWriter(new FileOutputStream(
177
					outFileName)), new PrintWriter(new FileOutputStream(
178
					errFileName)), false);
179
		} catch (FileNotFoundException e) {
180
			System.out.println(getClass().getName() + '#' + getName());
181
			e.printStackTrace();
182
			throw new RuntimeException(e);
183
		}
184
		boolean compileKO;
185
		try {
186
			compileKO = !batchCompiler.compile(Main.tokenize(commandLine));
187
		} catch (RuntimeException e) {
188
			compileKO = false;
189
			System.out.println(getClass().getName() + '#' + getName());
190
			e.printStackTrace();
191
			throw e;
192
		}
193
		String outOutputString = Util.fileContent(outFileName), errOutputString = Util
194
				.fileContent(errFileName);
195
		boolean compareOK = false;
196
		if (compileKO) {
197
			compareOK = (expectedSuccessOutOutputString == null || outOutputString
198
					.equals(expectedSuccessOutOutputString))
199
					&& (expectedSuccessErrOutputString == null || errOutputString
200
							.equals(expectedSuccessErrOutputString));
201
		}
202
		if (!compileKO || !compareOK) {
203
			System.out.println(getClass().getName() + '#' + getName());
204
			for (int i = 0; i < testFiles.length; i += 2) {
205
				System.out.print(testFiles[i]);
206
				System.out.println(" ["); //$NON-NLS-1$
207
				System.out.println(testFiles[i + 1]);
208
				System.out.println("]"); //$NON-NLS-1$
209
			}
210
		}
211
		if (!compileKO)
212
			System.out.println(errOutputString);
213
		if (compileKO && !compareOK) {
214
			System.out.println("--[START OUT]--\n"
215
					+ Util.displayString(outOutputString)
216
					+ "\n---[END OUT]---\n--[START ERR]--\n"
217
					+ Util.displayString(errOutputString)
218
					+ "\n---[END ERR]--\n");
219
		}
220
		assertTrue("Unexpected success: " + errOutputString, compileKO);
221
		assertTrue("Unexpected output for invocation with arguments ["
222
				+ commandLine + "]:\n" + "--[START]--\n" + outOutputString
223
				+ errOutputString + "---[END]---\n", compareOK);
224
	}
225
24
public void test01() {
226
public void test01() {
25
	
227
	
26
		String commandLine = "-classpath \"D:/a folder\";d:/jdk1.4/jre/lib/rt.jar -1.4 -preserveAllLocals -g -verbose d:/eclipse/workspaces/development2.0/plugins/Bar/src2/ -d d:/test";
228
		String commandLine = "-classpath \"D:/a folder\";d:/jdk1.4/jre/lib/rt.jar -1.4 -preserveAllLocals -g -verbose d:/eclipse/workspaces/development2.0/plugins/Bar/src2/ -d d:/test";
Lines 117-122 Link Here
117
			expected,
319
			expected,
118
			result);
320
			result);
119
}
321
}
322
// test the tester - runConformTest
323
public void test007(){
324
	this.runConformTest(
325
		new String[] {
326
			"X.java",
327
			"import java.util.List;\n" + 
328
			"\n" + 
329
			"@SuppressWarnings(\"all\"//$NON-NLS-1$\n" + 
330
			")\n" + 
331
			"public class X {\n" + 
332
			"	public static void main(String[] args) {\n" + 
333
			"		if (false) {\n" + 
334
			"			;\n" + 
335
			"		} else {\n" + 
336
			"		}\n" + 
337
			"		// Zork z;\n" + 
338
			"	}\n" + 
339
			"}"
340
        },
341
        "\"" + OUTPUT_DIR +  File.separator + "X.java\"" //$NON-NLS-1$ //$NON-NLS-2$
342
        + " -1.5" //$NON-NLS-1$
343
        + " -g -preserveAllLocals" //$NON-NLS-1$
344
        + " -bootclasspath d:/jdk1.5.0/jre/lib/rt.jar" //$NON-NLS-1$
345
        + " -cp d:/jdk1.5.0/jre/lib/jce.jar" //$NON-NLS-1$
346
        + " -verbose -warn:+deprecation,syntheticAccess,uselessTypeCheck,unsafe,finalBound,unusedLocal" //$NON-NLS-1$
347
        + " -proceedOnError -showversion -referenceInfo" //$NON-NLS-1$
348
        + " -d \"" + OUTPUT_DIR + "\"", //$NON-NLS-1$ //$NON-NLS-2$
349
        "Eclipse Java Compiler 0.554, pre-3.1.0 milestone-7, Copyright IBM Corp 2000, 2005. All rights reserved.\r\n" + 
350
        "[1 .class file generated]\r\n", 
351
        "----------\r\n" + 
352
        "1. WARNING in C:\\Documents and Settings\\mdl\\comptest\\regression\\X.java\r\n" + 
353
        " (at line 1)\r\n" + 
354
        "	import java.util.List;\r\n" + 
355
        "	       ^^^^^^^^^^^^^^\r\n" + 
356
        "[warning name] The import java.util.List is never used\r\n" + 
357
        "----------\r\n" + 
358
        "1 problem (1 warning)", true);
359
}
360
// test the tester - runNegativeTest
361
public void test008(){
362
	this.runNegativeTest(
363
		new String[] {
364
			"X.java",
365
			"import java.util.List;\n" + 
366
			"\n" + 
367
			"@SuppressWarnings(\"all\"//$NON-NLS-1$\n" + 
368
			")\n" + 
369
			"public class X {\n" + 
370
			"	public static void main(String[] args) {\n" + 
371
			"		if (false) {\n" + 
372
			"			;\n" + 
373
			"		} else {\n" + 
374
			"		}\n" + 
375
			"		Zork z;\n" + 
376
			"	}\n" + 
377
			"}"
378
        },
379
        "\"" + OUTPUT_DIR +  File.separator + "X.java\"" //$NON-NLS-1$ //$NON-NLS-2$
380
        + " -1.5" //$NON-NLS-1$
381
        + " -g -preserveAllLocals" //$NON-NLS-1$
382
        + " -bootclasspath d:/jdk1.5.0/jre/lib/rt.jar" //$NON-NLS-1$
383
        + " -cp d:/jdk1.5.0/jre/lib/jce.jar" //$NON-NLS-1$
384
        + " -verbose -warn:+deprecation,syntheticAccess,uselessTypeCheck,unsafe,finalBound,unusedLocal" //$NON-NLS-1$
385
        + " -proceedOnError -showversion -referenceInfo" //$NON-NLS-1$
386
        + " -d \"" + OUTPUT_DIR + "\"", //$NON-NLS-1$ //$NON-NLS-2$
387
        "Eclipse Java Compiler 0.554, pre-3.1.0 milestone-7, Copyright IBM Corp 2000, 2005. All rights reserved.\r\n" + 
388
        "[1 .class file generated]\r\n", 
389
        "----------\r\n" + 
390
        "1. WARNING in C:\\Documents and Settings\\mdl\\comptest\\regression\\X.java\r\n" + 
391
        " (at line 1)\r\n" + 
392
        "	import java.util.List;\r\n" + 
393
        "	       ^^^^^^^^^^^^^^\r\n" + 
394
        "[warning name] The import java.util.List is never used\r\n" + 
395
        "----------\r\n" + 
396
        "----------\r\n" + 
397
        "2. ERROR in C:\\Documents and Settings\\mdl\\comptest\\regression\\X.java\r\n" + 
398
        " (at line 11)\r\n" + 
399
        "	Zork z;\r\n" + 
400
        "	^^^^\r\n" + 
401
        "[warning name] Zork cannot be resolved to a type\r\n" + 
402
        "----------\r\n" + 
403
        "2 problems (1 error, 1 warning)", true);
404
}
120
public static Class testClass() {
405
public static Class testClass() {
121
	return BatchCompilerTest.class;
406
	return BatchCompilerTest.class;
122
}
407
}

Return to bug 92398