aoqi@0: /* aoqi@0: * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: */ aoqi@0: aoqi@0: /* aoqi@0: * @test aoqi@0: * @bug 6430241 aoqi@0: * @summary Hard to disable symbol file feature through API aoqi@0: */ aoqi@0: aoqi@0: import java.io.*; aoqi@0: import java.util.*; aoqi@0: import javax.tools.*; aoqi@0: aoqi@0: import com.sun.source.util.JavacTask; aoqi@0: import com.sun.tools.javac.api.JavacTool; aoqi@0: aoqi@0: public class T6430241 { aoqi@0: public static void main(String... args) throws Exception { aoqi@0: new T6430241().run(); aoqi@0: } aoqi@0: aoqi@0: void run() throws Exception { aoqi@0: setup(); aoqi@0: testCommandLine(); aoqi@0: testSimpleAPI(); aoqi@0: testTaskAPI(); aoqi@0: aoqi@0: if (errors > 0) aoqi@0: throw new Exception(errors + " errors found"); aoqi@0: } aoqi@0: aoqi@0: void setup() throws Exception { aoqi@0: classesDir = new File("classes"); aoqi@0: classesDir.mkdirs(); aoqi@0: aoqi@0: emptyDir = new File("empty"); aoqi@0: emptyDir.mkdirs(); aoqi@0: aoqi@0: bootClassPath = System.getProperty("sun.boot.class.path"); aoqi@0: aoqi@0: File srcDir = new File("src"); aoqi@0: String test = "import sun.misc.Unsafe; class Test { }"; aoqi@0: testFile = writeFile(srcDir, "Test.java", test); aoqi@0: } aoqi@0: aoqi@0: //----- tests for command line invocation aoqi@0: aoqi@0: void testCommandLine() throws Exception { aoqi@0: testCommandLine(true); aoqi@0: testCommandLine(true, "-Xbootclasspath/p:" + emptyDir); aoqi@0: testCommandLine(false, "-Xbootclasspath:" + bootClassPath); aoqi@0: testCommandLine(true, "-Xbootclasspath/a:" + emptyDir); aoqi@0: testCommandLine(false, "-XDignore.symbol.file"); aoqi@0: System.err.println(); aoqi@0: } aoqi@0: aoqi@0: void testCommandLine(boolean expectWarnings, String... opts) throws Exception { aoqi@0: System.err.println("test command line: " + Arrays.asList(opts)); aoqi@0: aoqi@0: String[] args = initArgs(opts); aoqi@0: aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter pw = new PrintWriter(sw); aoqi@0: int rc = com.sun.tools.javac.Main.compile(args, pw); aoqi@0: String out = showOutput(sw.toString()); aoqi@0: aoqi@0: checkCompilationOK(rc); aoqi@0: checkOutput(out, expectWarnings); aoqi@0: } aoqi@0: aoqi@0: //----- tests for simple API invocation aoqi@0: aoqi@0: void testSimpleAPI() { aoqi@0: testSimpleAPI(true); aoqi@0: testSimpleAPI(true, "-Xbootclasspath/p:" + emptyDir); aoqi@0: testSimpleAPI(false, "-Xbootclasspath:" + bootClassPath); aoqi@0: testSimpleAPI(true, "-Xbootclasspath/a:" + emptyDir); aoqi@0: testSimpleAPI(false, "-XDignore.symbol.file"); aoqi@0: System.err.println(); aoqi@0: } aoqi@0: aoqi@0: void testSimpleAPI(boolean expectWarnings, String... opts) { aoqi@0: System.err.println("test simple API: " + Arrays.asList(opts)); aoqi@0: aoqi@0: String[] args = initArgs(opts); aoqi@0: aoqi@0: ByteArrayOutputStream baos = new ByteArrayOutputStream(); aoqi@0: PrintStream ps = new PrintStream(baos); aoqi@0: aoqi@0: JavacTool tool = JavacTool.create(); aoqi@0: int rc = tool.run(null, null, ps, args); aoqi@0: aoqi@0: String out = showOutput(baos.toString()); aoqi@0: aoqi@0: checkCompilationOK(rc); aoqi@0: checkOutput(out, expectWarnings); aoqi@0: } aoqi@0: aoqi@0: //----- tests for CompilationTask API invocation aoqi@0: aoqi@0: void testTaskAPI() throws Exception { aoqi@0: List bcp = new ArrayList(); aoqi@0: for (String f: bootClassPath.split(File.pathSeparator)) { aoqi@0: if (!f.isEmpty()) aoqi@0: bcp.add(new File(f)); aoqi@0: } aoqi@0: aoqi@0: testTaskAPI(true, null); aoqi@0: testTaskAPI(false, bcp); aoqi@0: System.err.println(); aoqi@0: } aoqi@0: aoqi@0: void testTaskAPI(boolean expectWarnings, Iterable pcp) throws Exception { aoqi@0: System.err.println("test task API: " + pcp); aoqi@0: aoqi@0: JavacTool tool = JavacTool.create(); aoqi@0: StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); aoqi@0: aoqi@0: if (pcp != null) aoqi@0: fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, pcp); aoqi@0: aoqi@0: Iterable files = fm.getJavaFileObjects(testFile); aoqi@0: aoqi@0: StringWriter sw = new StringWriter(); aoqi@0: PrintWriter pw = new PrintWriter(sw); aoqi@0: JavacTask task = tool.getTask(pw, fm, null, null, null, files); aoqi@0: boolean ok = task.call(); aoqi@0: String out = showOutput(sw.toString()); aoqi@0: aoqi@0: checkCompilationOK(ok); aoqi@0: checkOutput(out, expectWarnings); aoqi@0: } aoqi@0: aoqi@0: //----- utility methods aoqi@0: aoqi@0: /** aoqi@0: * Create a file with given content. aoqi@0: */ aoqi@0: File writeFile(File dir, String path, String content) throws IOException { aoqi@0: File f = new File(dir, path); aoqi@0: f.getParentFile().mkdirs(); aoqi@0: FileWriter out = new FileWriter(f); aoqi@0: try { aoqi@0: out.write(content); aoqi@0: } finally { aoqi@0: out.close(); aoqi@0: } aoqi@0: return f; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Initialize args for compilation with given opts. aoqi@0: * @return opts -d classesDir testFile aoqi@0: */ aoqi@0: String[] initArgs(String[] opts) { aoqi@0: List args = new ArrayList(); aoqi@0: args.addAll(Arrays.asList(opts)); aoqi@0: args.add("-d"); aoqi@0: args.add(classesDir.getPath()); aoqi@0: args.add(testFile.getPath()); aoqi@0: return args.toArray(new String[args.size()]); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Show output from compilation if non empty. aoqi@0: */ aoqi@0: String showOutput(String out) { aoqi@0: if (!out.isEmpty()) aoqi@0: System.err.println(out); aoqi@0: return out; aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Verify compilation succeeeded. aoqi@0: */ aoqi@0: void checkCompilationOK(boolean ok) { aoqi@0: if (!ok) aoqi@0: error("compilation failed"); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Verify compilation succeeeded. aoqi@0: */ aoqi@0: void checkCompilationOK(int rc) { aoqi@0: if (rc != 0) aoqi@0: error("compilation failed, rc: " + rc); aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Check whether output contains warnings if and only if warnings aoqi@0: * are expected. aoqi@0: */ aoqi@0: void checkOutput(String out, boolean expectWarnings) { aoqi@0: boolean foundWarnings = out.contains("warning"); aoqi@0: if (foundWarnings) { aoqi@0: if (!expectWarnings) aoqi@0: error("unexpected warnings found"); aoqi@0: } else { aoqi@0: if (expectWarnings) aoqi@0: error("expected warnings not found"); aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: /** aoqi@0: * Report an error. aoqi@0: */ aoqi@0: void error(String msg) { aoqi@0: System.err.println("error: " + msg); aoqi@0: errors++; aoqi@0: } aoqi@0: aoqi@0: String bootClassPath; aoqi@0: File classesDir; aoqi@0: File emptyDir; aoqi@0: File testFile; aoqi@0: int errors; aoqi@0: }