author | jjg |
Wed, 29 Oct 2014 17:25:23 -0700 | |
changeset 27319 | 030080f03e4f |
parent 8034 | d3b29ef9c062 |
child 27579 | d1a63c99cdd5 |
permissions | -rw-r--r-- |
8034 | 1 |
/* |
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
2 |
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved. |
8034 | 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(); |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
137 |
try (StandardJavaFileManager fm = tool.getStandardFileManager(null, null, null)) { |
8034 | 138 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
139 |
if (pcp != null) |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
140 |
fm.setLocation(StandardLocation.PLATFORM_CLASS_PATH, pcp); |
8034 | 141 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
142 |
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(testFile); |
8034 | 143 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
144 |
StringWriter sw = new StringWriter(); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
145 |
PrintWriter pw = new PrintWriter(sw); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
146 |
JavacTask task = tool.getTask(pw, fm, null, null, null, files); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
147 |
boolean ok = task.call(); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
148 |
String out = showOutput(sw.toString()); |
8034 | 149 |
|
27319
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
150 |
checkCompilationOK(ok); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
151 |
checkOutput(out, expectWarnings); |
030080f03e4f
8062348: langtools tests should close file manager (group 1)
jjg
parents:
8034
diff
changeset
|
152 |
} |
8034 | 153 |
} |
154 |
||
155 |
//----- utility methods |
|
156 |
||
157 |
/** |
|
158 |
* Create a file with given content. |
|
159 |
*/ |
|
160 |
File writeFile(File dir, String path, String content) throws IOException { |
|
161 |
File f = new File(dir, path); |
|
162 |
f.getParentFile().mkdirs(); |
|
163 |
FileWriter out = new FileWriter(f); |
|
164 |
try { |
|
165 |
out.write(content); |
|
166 |
} finally { |
|
167 |
out.close(); |
|
168 |
} |
|
169 |
return f; |
|
170 |
} |
|
171 |
||
172 |
/** |
|
173 |
* Initialize args for compilation with given opts. |
|
174 |
* @return opts -d classesDir testFile |
|
175 |
*/ |
|
176 |
String[] initArgs(String[] opts) { |
|
177 |
List<String> args = new ArrayList<String>(); |
|
178 |
args.addAll(Arrays.asList(opts)); |
|
179 |
args.add("-d"); |
|
180 |
args.add(classesDir.getPath()); |
|
181 |
args.add(testFile.getPath()); |
|
182 |
return args.toArray(new String[args.size()]); |
|
183 |
} |
|
184 |
||
185 |
/** |
|
186 |
* Show output from compilation if non empty. |
|
187 |
*/ |
|
188 |
String showOutput(String out) { |
|
189 |
if (!out.isEmpty()) |
|
190 |
System.err.println(out); |
|
191 |
return out; |
|
192 |
} |
|
193 |
||
194 |
/** |
|
195 |
* Verify compilation succeeeded. |
|
196 |
*/ |
|
197 |
void checkCompilationOK(boolean ok) { |
|
198 |
if (!ok) |
|
199 |
error("compilation failed"); |
|
200 |
} |
|
201 |
||
202 |
/** |
|
203 |
* Verify compilation succeeeded. |
|
204 |
*/ |
|
205 |
void checkCompilationOK(int rc) { |
|
206 |
if (rc != 0) |
|
207 |
error("compilation failed, rc: " + rc); |
|
208 |
} |
|
209 |
||
210 |
/** |
|
211 |
* Check whether output contains warnings if and only if warnings |
|
212 |
* are expected. |
|
213 |
*/ |
|
214 |
void checkOutput(String out, boolean expectWarnings) { |
|
215 |
boolean foundWarnings = out.contains("warning"); |
|
216 |
if (foundWarnings) { |
|
217 |
if (!expectWarnings) |
|
218 |
error("unexpected warnings found"); |
|
219 |
} else { |
|
220 |
if (expectWarnings) |
|
221 |
error("expected warnings not found"); |
|
222 |
} |
|
223 |
} |
|
224 |
||
225 |
/** |
|
226 |
* Report an error. |
|
227 |
*/ |
|
228 |
void error(String msg) { |
|
229 |
System.err.println("error: " + msg); |
|
230 |
errors++; |
|
231 |
} |
|
232 |
||
233 |
String bootClassPath; |
|
234 |
File classesDir; |
|
235 |
File emptyDir; |
|
236 |
File testFile; |
|
237 |
int errors; |
|
238 |
} |