test/tools/javac/api/T6430241.java

Thu, 13 Jan 2011 11:48:10 -0800

author
jjg
date
Thu, 13 Jan 2011 11:48:10 -0800
changeset 818
d33d8c381aa1
parent 0
959103a6100f
permissions
-rw-r--r--

6430241: Hard to disable symbol file feature through API
Reviewed-by: mcimadamore

aoqi@0 1 /*
aoqi@0 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
aoqi@0 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
aoqi@0 4 *
aoqi@0 5 * This code is free software; you can redistribute it and/or modify it
aoqi@0 6 * under the terms of the GNU General Public License version 2 only, as
aoqi@0 7 * published by the Free Software Foundation.
aoqi@0 8 *
aoqi@0 9 * This code is distributed in the hope that it will be useful, but WITHOUT
aoqi@0 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
aoqi@0 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
aoqi@0 12 * version 2 for more details (a copy is included in the LICENSE file that
aoqi@0 13 * accompanied this code).
aoqi@0 14 *
aoqi@0 15 * You should have received a copy of the GNU General Public License version
aoqi@0 16 * 2 along with this work; if not, write to the Free Software Foundation,
aoqi@0 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
aoqi@0 18 *
aoqi@0 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
aoqi@0 20 * or visit www.oracle.com if you need additional information or have any
aoqi@0 21 * questions.
aoqi@0 22 */
aoqi@0 23
aoqi@0 24 /*
aoqi@0 25 * @test
aoqi@0 26 * @bug 6430241
aoqi@0 27 * @summary Hard to disable symbol file feature through API
aoqi@0 28 */
aoqi@0 29
aoqi@0 30 import java.io.*;
aoqi@0 31 import java.util.*;
aoqi@0 32 import javax.tools.*;
aoqi@0 33
aoqi@0 34 import com.sun.source.util.JavacTask;
aoqi@0 35 import com.sun.tools.javac.api.JavacTool;
aoqi@0 36
aoqi@0 37 public class T6430241 {
aoqi@0 38 public static void main(String... args) throws Exception {
aoqi@0 39 new T6430241().run();
aoqi@0 40 }
aoqi@0 41
aoqi@0 42 void run() throws Exception {
aoqi@0 43 setup();
aoqi@0 44 testCommandLine();
aoqi@0 45 testSimpleAPI();
aoqi@0 46 testTaskAPI();
aoqi@0 47
aoqi@0 48 if (errors > 0)
aoqi@0 49 throw new Exception(errors + " errors found");
aoqi@0 50 }
aoqi@0 51
aoqi@0 52 void setup() throws Exception {
aoqi@0 53 classesDir = new File("classes");
aoqi@0 54 classesDir.mkdirs();
aoqi@0 55
aoqi@0 56 emptyDir = new File("empty");
aoqi@0 57 emptyDir.mkdirs();
aoqi@0 58
aoqi@0 59 bootClassPath = System.getProperty("sun.boot.class.path");
aoqi@0 60
aoqi@0 61 File srcDir = new File("src");
aoqi@0 62 String test = "import sun.misc.Unsafe; class Test { }";
aoqi@0 63 testFile = writeFile(srcDir, "Test.java", test);
aoqi@0 64 }
aoqi@0 65
aoqi@0 66 //----- tests for command line invocation
aoqi@0 67
aoqi@0 68 void testCommandLine() throws Exception {
aoqi@0 69 testCommandLine(true);
aoqi@0 70 testCommandLine(true, "-Xbootclasspath/p:" + emptyDir);
aoqi@0 71 testCommandLine(false, "-Xbootclasspath:" + bootClassPath);
aoqi@0 72 testCommandLine(true, "-Xbootclasspath/a:" + emptyDir);
aoqi@0 73 testCommandLine(false, "-XDignore.symbol.file");
aoqi@0 74 System.err.println();
aoqi@0 75 }
aoqi@0 76
aoqi@0 77 void testCommandLine(boolean expectWarnings, String... opts) throws Exception {
aoqi@0 78 System.err.println("test command line: " + Arrays.asList(opts));
aoqi@0 79
aoqi@0 80 String[] args = initArgs(opts);
aoqi@0 81
aoqi@0 82 StringWriter sw = new StringWriter();
aoqi@0 83 PrintWriter pw = new PrintWriter(sw);
aoqi@0 84 int rc = com.sun.tools.javac.Main.compile(args, pw);
aoqi@0 85 String out = showOutput(sw.toString());
aoqi@0 86
aoqi@0 87 checkCompilationOK(rc);
aoqi@0 88 checkOutput(out, expectWarnings);
aoqi@0 89 }
aoqi@0 90
aoqi@0 91 //----- tests for simple API invocation
aoqi@0 92
aoqi@0 93 void testSimpleAPI() {
aoqi@0 94 testSimpleAPI(true);
aoqi@0 95 testSimpleAPI(true, "-Xbootclasspath/p:" + emptyDir);
aoqi@0 96 testSimpleAPI(false, "-Xbootclasspath:" + bootClassPath);
aoqi@0 97 testSimpleAPI(true, "-Xbootclasspath/a:" + emptyDir);
aoqi@0 98 testSimpleAPI(false, "-XDignore.symbol.file");
aoqi@0 99 System.err.println();
aoqi@0 100 }
aoqi@0 101
aoqi@0 102 void testSimpleAPI(boolean expectWarnings, String... opts) {
aoqi@0 103 System.err.println("test simple API: " + Arrays.asList(opts));
aoqi@0 104
aoqi@0 105 String[] args = initArgs(opts);
aoqi@0 106
aoqi@0 107 ByteArrayOutputStream baos = new ByteArrayOutputStream();
aoqi@0 108 PrintStream ps = new PrintStream(baos);
aoqi@0 109
aoqi@0 110 JavacTool tool = JavacTool.create();
aoqi@0 111 int rc = tool.run(null, null, ps, args);
aoqi@0 112
aoqi@0 113 String out = showOutput(baos.toString());
aoqi@0 114
aoqi@0 115 checkCompilationOK(rc);
aoqi@0 116 checkOutput(out, expectWarnings);
aoqi@0 117 }
aoqi@0 118
aoqi@0 119 //----- tests for CompilationTask API invocation
aoqi@0 120
aoqi@0 121 void testTaskAPI() throws Exception {
aoqi@0 122 List<File> bcp = new ArrayList<File>();
aoqi@0 123 for (String f: bootClassPath.split(File.pathSeparator)) {
aoqi@0 124 if (!f.isEmpty())
aoqi@0 125 bcp.add(new File(f));
aoqi@0 126 }
aoqi@0 127
aoqi@0 128 testTaskAPI(true, null);
aoqi@0 129 testTaskAPI(false, bcp);
aoqi@0 130 System.err.println();
aoqi@0 131 }
aoqi@0 132
aoqi@0 133 void testTaskAPI(boolean expectWarnings, Iterable<? extends File> pcp) throws Exception {
aoqi@0 134 System.err.println("test task API: " + pcp);
aoqi@0 135
aoqi@0 136 JavacTool tool = JavacTool.create();
aoqi@0 137 StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null);
aoqi@0 138
aoqi@0 139 if (pcp != null)
aoqi@0 140 fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, pcp);
aoqi@0 141
aoqi@0 142 Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(testFile);
aoqi@0 143
aoqi@0 144 StringWriter sw = new StringWriter();
aoqi@0 145 PrintWriter pw = new PrintWriter(sw);
aoqi@0 146 JavacTask task = tool.getTask(pw, fm, null, null, null, files);
aoqi@0 147 boolean ok = task.call();
aoqi@0 148 String out = showOutput(sw.toString());
aoqi@0 149
aoqi@0 150 checkCompilationOK(ok);
aoqi@0 151 checkOutput(out, expectWarnings);
aoqi@0 152 }
aoqi@0 153
aoqi@0 154 //----- utility methods
aoqi@0 155
aoqi@0 156 /**
aoqi@0 157 * Create a file with given content.
aoqi@0 158 */
aoqi@0 159 File writeFile(File dir, String path, String content) throws IOException {
aoqi@0 160 File f = new File(dir, path);
aoqi@0 161 f.getParentFile().mkdirs();
aoqi@0 162 FileWriter out = new FileWriter(f);
aoqi@0 163 try {
aoqi@0 164 out.write(content);
aoqi@0 165 } finally {
aoqi@0 166 out.close();
aoqi@0 167 }
aoqi@0 168 return f;
aoqi@0 169 }
aoqi@0 170
aoqi@0 171 /**
aoqi@0 172 * Initialize args for compilation with given opts.
aoqi@0 173 * @return opts -d classesDir testFile
aoqi@0 174 */
aoqi@0 175 String[] initArgs(String[] opts) {
aoqi@0 176 List<String> args = new ArrayList<String>();
aoqi@0 177 args.addAll(Arrays.asList(opts));
aoqi@0 178 args.add("-d");
aoqi@0 179 args.add(classesDir.getPath());
aoqi@0 180 args.add(testFile.getPath());
aoqi@0 181 return args.toArray(new String[args.size()]);
aoqi@0 182 }
aoqi@0 183
aoqi@0 184 /**
aoqi@0 185 * Show output from compilation if non empty.
aoqi@0 186 */
aoqi@0 187 String showOutput(String out) {
aoqi@0 188 if (!out.isEmpty())
aoqi@0 189 System.err.println(out);
aoqi@0 190 return out;
aoqi@0 191 }
aoqi@0 192
aoqi@0 193 /**
aoqi@0 194 * Verify compilation succeeeded.
aoqi@0 195 */
aoqi@0 196 void checkCompilationOK(boolean ok) {
aoqi@0 197 if (!ok)
aoqi@0 198 error("compilation failed");
aoqi@0 199 }
aoqi@0 200
aoqi@0 201 /**
aoqi@0 202 * Verify compilation succeeeded.
aoqi@0 203 */
aoqi@0 204 void checkCompilationOK(int rc) {
aoqi@0 205 if (rc != 0)
aoqi@0 206 error("compilation failed, rc: " + rc);
aoqi@0 207 }
aoqi@0 208
aoqi@0 209 /**
aoqi@0 210 * Check whether output contains warnings if and only if warnings
aoqi@0 211 * are expected.
aoqi@0 212 */
aoqi@0 213 void checkOutput(String out, boolean expectWarnings) {
aoqi@0 214 boolean foundWarnings = out.contains("warning");
aoqi@0 215 if (foundWarnings) {
aoqi@0 216 if (!expectWarnings)
aoqi@0 217 error("unexpected warnings found");
aoqi@0 218 } else {
aoqi@0 219 if (expectWarnings)
aoqi@0 220 error("expected warnings not found");
aoqi@0 221 }
aoqi@0 222 }
aoqi@0 223
aoqi@0 224 /**
aoqi@0 225 * Report an error.
aoqi@0 226 */
aoqi@0 227 void error(String msg) {
aoqi@0 228 System.err.println("error: " + msg);
aoqi@0 229 errors++;
aoqi@0 230 }
aoqi@0 231
aoqi@0 232 String bootClassPath;
aoqi@0 233 File classesDir;
aoqi@0 234 File emptyDir;
aoqi@0 235 File testFile;
aoqi@0 236 int errors;
aoqi@0 237 }

mercurial