Index: src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java =================================================================== RCS file: /home/eclipse/org.eclipse.jdt.core.tests.compiler/src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java,v retrieving revision 1.8 diff -u -r1.8 BatchCompilerTest.java --- src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java 23 Feb 2005 02:52:37 -0000 1.8 +++ src/org/eclipse/jdt/core/tests/compiler/regression/BatchCompilerTest.java 6 May 2005 16:33:03 -0000 @@ -10,8 +10,13 @@ *******************************************************************************/ package org.eclipse.jdt.core.tests.compiler.regression; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.PrintWriter; import junit.framework.Test; +import org.eclipse.jdt.core.tests.util.Util; import org.eclipse.jdt.internal.compiler.batch.Main; public class BatchCompilerTest extends AbstractRegressionTest { @@ -20,7 +25,204 @@ } public static Test suite() { return setupSuite(testClass()); + // TODO find a way to reduce the number of command line tests to one per + // test run (aka do not add 1.3, 1.4, 1.5 supplementary level) } + + /** + * Run a compilation test that is expected to complete successfully and + * compare the outputs to expected ones. + * + * @param testFiles + * the source files, given as a suite of file name, file content; + * file names are relative to the output directory + * @param commandLine + * the command line to pass to + * {@link Main#compile(String) Main#compile} + * @param expectedSuccessOutOutputString + * the expected contents of the standard output stream; pass null + * to bypass the comparison + * @param expectedSuccessErrOutputString + * the expected contents of the standard error output stream; + * pass null to bypass the comparison + * @param shouldFlushOutputDirectory + * pass true to get the output directory flushed before the test + * runs + */ + protected void runConformTest(String[] testFiles, String commandLine, + String expectedSuccessOutOutputString, + String expectedSuccessErrOutputString, + boolean shouldFlushOutputDirectory) { + File outputDirectory = new File(OUTPUT_DIR); + if (shouldFlushOutputDirectory) + Util.flushDirectoryContent(outputDirectory); + try { + if (!outputDirectory.isDirectory()) { + outputDirectory.mkdirs(); + } + PrintWriter sourceFileWriter; + for (int i = 0; i < testFiles.length; i += 2) { + sourceFileWriter = new PrintWriter(new FileOutputStream( + OUTPUT_DIR + File.separator + testFiles[i])); + sourceFileWriter.write(testFiles[i + 1]); + sourceFileWriter.close(); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + String printerWritersNameRoot = OUTPUT_DIR + File.separator + + testName(); + String outFileName = printerWritersNameRoot + "out.txt", errFileName = printerWritersNameRoot + "err.txt"; //$NON-NLS-1$ //$NON-NLS-2$ + Main batchCompiler; + try { + batchCompiler = new Main(new PrintWriter(new FileOutputStream( + outFileName)), new PrintWriter(new FileOutputStream( + errFileName)), false); + } catch (FileNotFoundException e) { + System.out.println(getClass().getName() + '#' + getName()); + e.printStackTrace(); + throw new RuntimeException(e); + } + boolean compileOK; + try { + compileOK = batchCompiler.compile(Main.tokenize(commandLine)); + } catch (RuntimeException e) { + compileOK = false; + System.out.println(getClass().getName() + '#' + getName()); + e.printStackTrace(); + throw e; + } + String outOutputString = Util.fileContent(outFileName), errOutputString = Util + .fileContent(errFileName); + boolean compareOK = false; + if (compileOK) { + compareOK = (expectedSuccessOutOutputString == null || outOutputString + .equals(expectedSuccessOutOutputString)) + && (expectedSuccessErrOutputString == null || errOutputString + .equals(expectedSuccessErrOutputString)); + } + if (!compileOK || !compareOK) { + System.out.println(getClass().getName() + '#' + getName()); + for (int i = 0; i < testFiles.length; i += 2) { + System.out.print(testFiles[i]); + System.out.println(" ["); //$NON-NLS-1$ + System.out.println(testFiles[i + 1]); + System.out.println("]"); //$NON-NLS-1$ + } + } + if (!compileOK) + System.out.println(errOutputString); + if (compileOK && !compareOK) { + System.out.println("--[START OUT]--\n" + + Util.displayString(outOutputString) + + "\n---[END OUT]---\n--[START ERR]--\n" + + Util.displayString(errOutputString) + + "\n---[END ERR]--\n"); + } + assertTrue("Unexpected problems: " + errOutputString, compileOK); + assertTrue("Unexpected output for invocation with arguments [" + + commandLine + "]:\n" + "--[START]--\n" + outOutputString + + errOutputString + "---[END]---\n", compareOK); + } + + /** + * Run a compilation test that is expected to fail and compare the outputs + * to expected ones. + * + * @param testFiles + * the source files, given as a suite of file name, file content; + * file names are relative to the output directory + * @param commandLine + * the command line to pass to + * {@link Main#compile(String) Main#compile} + * @param expectedSuccessOutOutputString + * the expected contents of the standard output stream; pass null + * to bypass the comparison + * @param expectedSuccessErrOutputString + * the expected contents of the standard error output stream; + * pass null to bypass the comparison + * @param shouldFlushOutputDirectory + * pass true to get the output directory flushed before the test + * runs + */ + protected void runNegativeTest(String[] testFiles, String commandLine, + String expectedSuccessOutOutputString, + String expectedSuccessErrOutputString, + boolean shouldFlushOutputDirectory) { + File outputDirectory = new File(OUTPUT_DIR); + if (shouldFlushOutputDirectory) + Util.flushDirectoryContent(outputDirectory); + try { + if (!outputDirectory.isDirectory()) { + outputDirectory.mkdirs(); + } + PrintWriter sourceFileWriter; + for (int i = 0; i < testFiles.length; i += 2) { + sourceFileWriter = new PrintWriter(new FileOutputStream( + OUTPUT_DIR + File.separator + testFiles[i])); + sourceFileWriter.write(testFiles[i + 1]); + sourceFileWriter.close(); + } + } catch (FileNotFoundException e) { + e.printStackTrace(); + throw new RuntimeException(e); + } + String printerWritersNameRoot = OUTPUT_DIR + File.separator + + testName(); + String outFileName = printerWritersNameRoot + "out.txt", errFileName = printerWritersNameRoot + "err.txt"; //$NON-NLS-1$ //$NON-NLS-2$ + Main batchCompiler; + try { + batchCompiler = new Main(new PrintWriter(new FileOutputStream( + outFileName)), new PrintWriter(new FileOutputStream( + errFileName)), false); + } catch (FileNotFoundException e) { + System.out.println(getClass().getName() + '#' + getName()); + e.printStackTrace(); + throw new RuntimeException(e); + } + boolean compileKO; + try { + compileKO = !batchCompiler.compile(Main.tokenize(commandLine)); + } catch (RuntimeException e) { + compileKO = false; + System.out.println(getClass().getName() + '#' + getName()); + e.printStackTrace(); + throw e; + } + String outOutputString = Util.fileContent(outFileName), errOutputString = Util + .fileContent(errFileName); + boolean compareOK = false; + if (compileKO) { + compareOK = (expectedSuccessOutOutputString == null || outOutputString + .equals(expectedSuccessOutOutputString)) + && (expectedSuccessErrOutputString == null || errOutputString + .equals(expectedSuccessErrOutputString)); + } + if (!compileKO || !compareOK) { + System.out.println(getClass().getName() + '#' + getName()); + for (int i = 0; i < testFiles.length; i += 2) { + System.out.print(testFiles[i]); + System.out.println(" ["); //$NON-NLS-1$ + System.out.println(testFiles[i + 1]); + System.out.println("]"); //$NON-NLS-1$ + } + } + if (!compileKO) + System.out.println(errOutputString); + if (compileKO && !compareOK) { + System.out.println("--[START OUT]--\n" + + Util.displayString(outOutputString) + + "\n---[END OUT]---\n--[START ERR]--\n" + + Util.displayString(errOutputString) + + "\n---[END ERR]--\n"); + } + assertTrue("Unexpected success: " + errOutputString, compileKO); + assertTrue("Unexpected output for invocation with arguments [" + + commandLine + "]:\n" + "--[START]--\n" + outOutputString + + errOutputString + "---[END]---\n", compareOK); + } + public void test01() { 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"; @@ -117,6 +319,89 @@ expected, result); } +// test the tester - runConformTest +public void test007(){ + this.runConformTest( + new String[] { + "X.java", + "import java.util.List;\n" + + "\n" + + "@SuppressWarnings(\"all\"//$NON-NLS-1$\n" + + ")\n" + + "public class X {\n" + + " public static void main(String[] args) {\n" + + " if (false) {\n" + + " ;\n" + + " } else {\n" + + " }\n" + + " // Zork z;\n" + + " }\n" + + "}" + }, + "\"" + OUTPUT_DIR + File.separator + "X.java\"" //$NON-NLS-1$ //$NON-NLS-2$ + + " -1.5" //$NON-NLS-1$ + + " -g -preserveAllLocals" //$NON-NLS-1$ + + " -bootclasspath d:/jdk1.5.0/jre/lib/rt.jar" //$NON-NLS-1$ + + " -cp d:/jdk1.5.0/jre/lib/jce.jar" //$NON-NLS-1$ + + " -verbose -warn:+deprecation,syntheticAccess,uselessTypeCheck,unsafe,finalBound,unusedLocal" //$NON-NLS-1$ + + " -proceedOnError -showversion -referenceInfo" //$NON-NLS-1$ + + " -d \"" + OUTPUT_DIR + "\"", //$NON-NLS-1$ //$NON-NLS-2$ + "Eclipse Java Compiler 0.554, pre-3.1.0 milestone-7, Copyright IBM Corp 2000, 2005. All rights reserved.\r\n" + + "[1 .class file generated]\r\n", + "----------\r\n" + + "1. WARNING in C:\\Documents and Settings\\mdl\\comptest\\regression\\X.java\r\n" + + " (at line 1)\r\n" + + " import java.util.List;\r\n" + + " ^^^^^^^^^^^^^^\r\n" + + "[warning name] The import java.util.List is never used\r\n" + + "----------\r\n" + + "1 problem (1 warning)", true); +} +// test the tester - runNegativeTest +public void test008(){ + this.runNegativeTest( + new String[] { + "X.java", + "import java.util.List;\n" + + "\n" + + "@SuppressWarnings(\"all\"//$NON-NLS-1$\n" + + ")\n" + + "public class X {\n" + + " public static void main(String[] args) {\n" + + " if (false) {\n" + + " ;\n" + + " } else {\n" + + " }\n" + + " Zork z;\n" + + " }\n" + + "}" + }, + "\"" + OUTPUT_DIR + File.separator + "X.java\"" //$NON-NLS-1$ //$NON-NLS-2$ + + " -1.5" //$NON-NLS-1$ + + " -g -preserveAllLocals" //$NON-NLS-1$ + + " -bootclasspath d:/jdk1.5.0/jre/lib/rt.jar" //$NON-NLS-1$ + + " -cp d:/jdk1.5.0/jre/lib/jce.jar" //$NON-NLS-1$ + + " -verbose -warn:+deprecation,syntheticAccess,uselessTypeCheck,unsafe,finalBound,unusedLocal" //$NON-NLS-1$ + + " -proceedOnError -showversion -referenceInfo" //$NON-NLS-1$ + + " -d \"" + OUTPUT_DIR + "\"", //$NON-NLS-1$ //$NON-NLS-2$ + "Eclipse Java Compiler 0.554, pre-3.1.0 milestone-7, Copyright IBM Corp 2000, 2005. All rights reserved.\r\n" + + "[1 .class file generated]\r\n", + "----------\r\n" + + "1. WARNING in C:\\Documents and Settings\\mdl\\comptest\\regression\\X.java\r\n" + + " (at line 1)\r\n" + + " import java.util.List;\r\n" + + " ^^^^^^^^^^^^^^\r\n" + + "[warning name] The import java.util.List is never used\r\n" + + "----------\r\n" + + "----------\r\n" + + "2. ERROR in C:\\Documents and Settings\\mdl\\comptest\\regression\\X.java\r\n" + + " (at line 11)\r\n" + + " Zork z;\r\n" + + " ^^^^\r\n" + + "[warning name] Zork cannot be resolved to a type\r\n" + + "----------\r\n" + + "2 problems (1 error, 1 warning)", true); +} public static Class testClass() { return BatchCompilerTest.class; }