1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/test/tools/javac/api/T6430241.java Thu Jan 13 11:48:10 2011 -0800 1.3 @@ -0,0 +1,237 @@ 1.4 + /* 1.5 + * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. 1.11 + * 1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.14 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.15 + * version 2 for more details (a copy is included in the LICENSE file that 1.16 + * accompanied this code). 1.17 + * 1.18 + * You should have received a copy of the GNU General Public License version 1.19 + * 2 along with this work; if not, write to the Free Software Foundation, 1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.21 + * 1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.23 + * or visit www.oracle.com if you need additional information or have any 1.24 + * questions. 1.25 + */ 1.26 + 1.27 + /* 1.28 + * @test 1.29 + * @bug 6430241 1.30 + * @summary Hard to disable symbol file feature through API 1.31 + */ 1.32 + 1.33 + import java.io.*; 1.34 + import java.util.*; 1.35 + import javax.tools.*; 1.36 + 1.37 + import com.sun.source.util.JavacTask; 1.38 + import com.sun.tools.javac.api.JavacTool; 1.39 + 1.40 + public class T6430241 { 1.41 + public static void main(String... args) throws Exception { 1.42 + new T6430241().run(); 1.43 + } 1.44 + 1.45 + void run() throws Exception { 1.46 + setup(); 1.47 + testCommandLine(); 1.48 + testSimpleAPI(); 1.49 + testTaskAPI(); 1.50 + 1.51 + if (errors > 0) 1.52 + throw new Exception(errors + " errors found"); 1.53 + } 1.54 + 1.55 + void setup() throws Exception { 1.56 + classesDir = new File("classes"); 1.57 + classesDir.mkdirs(); 1.58 + 1.59 + emptyDir = new File("empty"); 1.60 + emptyDir.mkdirs(); 1.61 + 1.62 + bootClassPath = System.getProperty("sun.boot.class.path"); 1.63 + 1.64 + File srcDir = new File("src"); 1.65 + String test = "import sun.misc.Unsafe; class Test { }"; 1.66 + testFile = writeFile(srcDir, "Test.java", test); 1.67 + } 1.68 + 1.69 + //----- tests for command line invocation 1.70 + 1.71 + void testCommandLine() throws Exception { 1.72 + testCommandLine(true); 1.73 + testCommandLine(true, "-Xbootclasspath/p:" + emptyDir); 1.74 + testCommandLine(false, "-Xbootclasspath:" + bootClassPath); 1.75 + testCommandLine(true, "-Xbootclasspath/a:" + emptyDir); 1.76 + testCommandLine(false, "-XDignore.symbol.file"); 1.77 + System.err.println(); 1.78 + } 1.79 + 1.80 + void testCommandLine(boolean expectWarnings, String... opts) throws Exception { 1.81 + System.err.println("test command line: " + Arrays.asList(opts)); 1.82 + 1.83 + String[] args = initArgs(opts); 1.84 + 1.85 + StringWriter sw = new StringWriter(); 1.86 + PrintWriter pw = new PrintWriter(sw); 1.87 + int rc = com.sun.tools.javac.Main.compile(args, pw); 1.88 + String out = showOutput(sw.toString()); 1.89 + 1.90 + checkCompilationOK(rc); 1.91 + checkOutput(out, expectWarnings); 1.92 + } 1.93 + 1.94 + //----- tests for simple API invocation 1.95 + 1.96 + void testSimpleAPI() { 1.97 + testSimpleAPI(true); 1.98 + testSimpleAPI(true, "-Xbootclasspath/p:" + emptyDir); 1.99 + testSimpleAPI(false, "-Xbootclasspath:" + bootClassPath); 1.100 + testSimpleAPI(true, "-Xbootclasspath/a:" + emptyDir); 1.101 + testSimpleAPI(false, "-XDignore.symbol.file"); 1.102 + System.err.println(); 1.103 + } 1.104 + 1.105 + void testSimpleAPI(boolean expectWarnings, String... opts) { 1.106 + System.err.println("test simple API: " + Arrays.asList(opts)); 1.107 + 1.108 + String[] args = initArgs(opts); 1.109 + 1.110 + ByteArrayOutputStream baos = new ByteArrayOutputStream(); 1.111 + PrintStream ps = new PrintStream(baos); 1.112 + 1.113 + JavacTool tool = JavacTool.create(); 1.114 + int rc = tool.run(null, null, ps, args); 1.115 + 1.116 + String out = showOutput(baos.toString()); 1.117 + 1.118 + checkCompilationOK(rc); 1.119 + checkOutput(out, expectWarnings); 1.120 + } 1.121 + 1.122 + //----- tests for CompilationTask API invocation 1.123 + 1.124 + void testTaskAPI() throws Exception { 1.125 + List<File> bcp = new ArrayList<File>(); 1.126 + for (String f: bootClassPath.split(File.pathSeparator)) { 1.127 + if (!f.isEmpty()) 1.128 + bcp.add(new File(f)); 1.129 + } 1.130 + 1.131 + testTaskAPI(true, null); 1.132 + testTaskAPI(false, bcp); 1.133 + System.err.println(); 1.134 + } 1.135 + 1.136 + void testTaskAPI(boolean expectWarnings, Iterable<? extends File> pcp) throws Exception { 1.137 + System.err.println("test task API: " + pcp); 1.138 + 1.139 + JavacTool tool = JavacTool.create(); 1.140 + StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null); 1.141 + 1.142 + if (pcp != null) 1.143 + fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, pcp); 1.144 + 1.145 + Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(testFile); 1.146 + 1.147 + StringWriter sw = new StringWriter(); 1.148 + PrintWriter pw = new PrintWriter(sw); 1.149 + JavacTask task = tool.getTask(pw, fm, null, null, null, files); 1.150 + boolean ok = task.call(); 1.151 + String out = showOutput(sw.toString()); 1.152 + 1.153 + checkCompilationOK(ok); 1.154 + checkOutput(out, expectWarnings); 1.155 + } 1.156 + 1.157 + //----- utility methods 1.158 + 1.159 + /** 1.160 + * Create a file with given content. 1.161 + */ 1.162 + File writeFile(File dir, String path, String content) throws IOException { 1.163 + File f = new File(dir, path); 1.164 + f.getParentFile().mkdirs(); 1.165 + FileWriter out = new FileWriter(f); 1.166 + try { 1.167 + out.write(content); 1.168 + } finally { 1.169 + out.close(); 1.170 + } 1.171 + return f; 1.172 + } 1.173 + 1.174 + /** 1.175 + * Initialize args for compilation with given opts. 1.176 + * @return opts -d classesDir testFile 1.177 + */ 1.178 + String[] initArgs(String[] opts) { 1.179 + List<String> args = new ArrayList<String>(); 1.180 + args.addAll(Arrays.asList(opts)); 1.181 + args.add("-d"); 1.182 + args.add(classesDir.getPath()); 1.183 + args.add(testFile.getPath()); 1.184 + return args.toArray(new String[args.size()]); 1.185 + } 1.186 + 1.187 + /** 1.188 + * Show output from compilation if non empty. 1.189 + */ 1.190 + String showOutput(String out) { 1.191 + if (!out.isEmpty()) 1.192 + System.err.println(out); 1.193 + return out; 1.194 + } 1.195 + 1.196 + /** 1.197 + * Verify compilation succeeeded. 1.198 + */ 1.199 + void checkCompilationOK(boolean ok) { 1.200 + if (!ok) 1.201 + error("compilation failed"); 1.202 + } 1.203 + 1.204 + /** 1.205 + * Verify compilation succeeeded. 1.206 + */ 1.207 + void checkCompilationOK(int rc) { 1.208 + if (rc != 0) 1.209 + error("compilation failed, rc: " + rc); 1.210 + } 1.211 + 1.212 + /** 1.213 + * Check whether output contains warnings if and only if warnings 1.214 + * are expected. 1.215 + */ 1.216 + void checkOutput(String out, boolean expectWarnings) { 1.217 + boolean foundWarnings = out.contains("warning"); 1.218 + if (foundWarnings) { 1.219 + if (!expectWarnings) 1.220 + error("unexpected warnings found"); 1.221 + } else { 1.222 + if (expectWarnings) 1.223 + error("expected warnings not found"); 1.224 + } 1.225 + } 1.226 + 1.227 + /** 1.228 + * Report an error. 1.229 + */ 1.230 + void error(String msg) { 1.231 + System.err.println("error: " + msg); 1.232 + errors++; 1.233 + } 1.234 + 1.235 + String bootClassPath; 1.236 + File classesDir; 1.237 + File emptyDir; 1.238 + File testFile; 1.239 + int errors; 1.240 + }