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