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