50453
|
1 |
/*
|
|
2 |
* Copyright (c) 2018, 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 8192920
|
|
27 |
* @summary Test source launcher
|
|
28 |
* @library /tools/lib
|
|
29 |
* @modules jdk.compiler/com.sun.tools.javac.api
|
|
30 |
* jdk.compiler/com.sun.tools.javac.launcher
|
|
31 |
* jdk.compiler/com.sun.tools.javac.main
|
|
32 |
* @build toolbox.JavaTask toolbox.JavacTask toolbox.TestRunner toolbox.ToolBox
|
|
33 |
* @run main SourceLauncherTest
|
|
34 |
*/
|
|
35 |
|
|
36 |
import java.io.ByteArrayOutputStream;
|
|
37 |
import java.io.IOException;
|
|
38 |
import java.io.PrintStream;
|
|
39 |
import java.io.PrintWriter;
|
|
40 |
import java.io.StringWriter;
|
|
41 |
import java.lang.reflect.InvocationTargetException;
|
|
42 |
import java.nio.file.Files;
|
|
43 |
import java.nio.file.Path;
|
|
44 |
import java.nio.file.Paths;
|
|
45 |
import java.util.ArrayList;
|
|
46 |
import java.util.Collections;
|
|
47 |
import java.util.List;
|
|
48 |
import java.util.Properties;
|
|
49 |
|
|
50 |
import com.sun.tools.javac.launcher.Main;
|
|
51 |
|
|
52 |
import toolbox.JavaTask;
|
|
53 |
import toolbox.JavacTask;
|
|
54 |
import toolbox.Task;
|
|
55 |
import toolbox.TestRunner;
|
|
56 |
import toolbox.TestRunner;
|
|
57 |
import toolbox.ToolBox;
|
|
58 |
|
|
59 |
public class SourceLauncherTest extends TestRunner {
|
|
60 |
public static void main(String... args) throws Exception {
|
|
61 |
SourceLauncherTest t = new SourceLauncherTest();
|
|
62 |
t.runTests(m -> new Object[] { Paths.get(m.getName()) });
|
|
63 |
}
|
|
64 |
|
|
65 |
SourceLauncherTest() {
|
|
66 |
super(System.err);
|
|
67 |
tb = new ToolBox();
|
|
68 |
System.err.println("version: " + thisVersion);
|
|
69 |
}
|
|
70 |
|
|
71 |
private final ToolBox tb;
|
|
72 |
private static final String thisVersion = System.getProperty("java.specification.version");
|
|
73 |
|
|
74 |
/*
|
|
75 |
* Positive tests.
|
|
76 |
*/
|
|
77 |
|
|
78 |
@Test
|
|
79 |
public void testHelloWorld(Path base) throws IOException {
|
|
80 |
tb.writeJavaFiles(base,
|
|
81 |
"import java.util.Arrays;\n" +
|
|
82 |
"class HelloWorld {\n" +
|
|
83 |
" public static void main(String... args) {\n" +
|
|
84 |
" System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
|
|
85 |
" }\n" +
|
|
86 |
"}");
|
|
87 |
testSuccess(base.resolve("HelloWorld.java"), "Hello World! [1, 2, 3]\n");
|
|
88 |
}
|
|
89 |
|
|
90 |
@Test
|
|
91 |
public void testHelloWorldInPackage(Path base) throws IOException {
|
|
92 |
tb.writeJavaFiles(base,
|
|
93 |
"package hello;\n" +
|
|
94 |
"import java.util.Arrays;\n" +
|
|
95 |
"class World {\n" +
|
|
96 |
" public static void main(String... args) {\n" +
|
|
97 |
" System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
|
|
98 |
" }\n" +
|
|
99 |
"}");
|
|
100 |
testSuccess(base.resolve("hello").resolve("World.java"), "Hello World! [1, 2, 3]\n");
|
|
101 |
}
|
|
102 |
|
|
103 |
@Test
|
|
104 |
public void testHelloWorldWithAux(Path base) throws IOException {
|
|
105 |
tb.writeJavaFiles(base,
|
|
106 |
"import java.util.Arrays;\n" +
|
|
107 |
"class HelloWorld {\n" +
|
|
108 |
" public static void main(String... args) {\n" +
|
|
109 |
" Aux.write(args);\n" +
|
|
110 |
" }\n" +
|
|
111 |
"}\n" +
|
|
112 |
"class Aux {\n" +
|
|
113 |
" static void write(String... args) {\n" +
|
|
114 |
" System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
|
|
115 |
" }\n" +
|
|
116 |
"}");
|
|
117 |
testSuccess(base.resolve("HelloWorld.java"), "Hello World! [1, 2, 3]\n");
|
|
118 |
}
|
|
119 |
|
|
120 |
@Test
|
|
121 |
public void testHelloWorldWithShebang(Path base) throws IOException {
|
|
122 |
tb.writeJavaFiles(base,
|
|
123 |
"#!/usr/bin/java --source 11\n" +
|
|
124 |
"import java.util.Arrays;\n" +
|
|
125 |
"class HelloWorld {\n" +
|
|
126 |
" public static void main(String... args) {\n" +
|
|
127 |
" System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
|
|
128 |
" }\n" +
|
|
129 |
"}");
|
|
130 |
Files.copy(base.resolve("HelloWorld.java"), base.resolve("HelloWorld"));
|
|
131 |
testSuccess(base.resolve("HelloWorld"), "Hello World! [1, 2, 3]\n");
|
|
132 |
}
|
|
133 |
|
|
134 |
@Test
|
|
135 |
public void testNoAnnoProcessing(Path base) throws IOException {
|
|
136 |
Path annoSrc = base.resolve("annoSrc");
|
|
137 |
tb.writeJavaFiles(annoSrc,
|
|
138 |
"import java.util.*;\n" +
|
|
139 |
"import javax.annotation.processing.*;\n" +
|
|
140 |
"import javax.lang.model.element.*;\n" +
|
|
141 |
"@SupportedAnnotationTypes(\"*\")\n" +
|
|
142 |
"public class AnnoProc extends AbstractProcessor {\n" +
|
|
143 |
" public boolean process(Set<? extends TypeElement> annos, RoundEnvironment rEnv) {\n" +
|
|
144 |
" throw new Error(\"Annotation processor should not be invoked\");\n" +
|
|
145 |
" }\n" +
|
|
146 |
"}\n");
|
|
147 |
Path annoClasses = Files.createDirectories(base.resolve("classes"));
|
|
148 |
new JavacTask(tb)
|
|
149 |
.outdir(annoClasses)
|
|
150 |
.files(annoSrc.resolve("AnnoProc.java").toString())
|
|
151 |
.run();
|
|
152 |
Path serviceFile = annoClasses.resolve("META-INF").resolve("services")
|
|
153 |
.resolve("javax.annotation.processing.Processor");
|
|
154 |
tb.writeFile(serviceFile, "AnnoProc");
|
|
155 |
|
|
156 |
Path mainSrc = base.resolve("mainSrc");
|
|
157 |
tb.writeJavaFiles(mainSrc,
|
|
158 |
"import java.util.Arrays;\n" +
|
|
159 |
"class HelloWorld {\n" +
|
|
160 |
" public static void main(String... args) {\n" +
|
|
161 |
" System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
|
|
162 |
" }\n" +
|
|
163 |
"}");
|
|
164 |
|
|
165 |
List<String> javacArgs = List.of("-classpath", annoClasses.toString());
|
|
166 |
List<String> classArgs = List.of("1", "2", "3");
|
|
167 |
String expect = "Hello World! [1, 2, 3]\n";
|
|
168 |
Result r = run(mainSrc.resolve("HelloWorld.java"), javacArgs, classArgs);
|
|
169 |
checkEqual("stdout", r.stdOut, expect);
|
|
170 |
checkEmpty("stderr", r.stdErr);
|
|
171 |
checkNull("exception", r.exception);
|
|
172 |
}
|
|
173 |
|
|
174 |
@Test
|
|
175 |
public void testEnablePreview(Path base) throws IOException {
|
|
176 |
tb.writeJavaFiles(base,
|
|
177 |
"import java.util.Arrays;\n" +
|
|
178 |
"class HelloWorld {\n" +
|
|
179 |
" public static void main(String... args) {\n" +
|
|
180 |
" System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
|
|
181 |
" }\n" +
|
|
182 |
"}");
|
|
183 |
|
|
184 |
String log = new JavaTask(tb)
|
|
185 |
.vmOptions("--enable-preview", "--source", thisVersion)
|
|
186 |
.className(base.resolve("HelloWorld.java").toString())
|
|
187 |
.classArgs("1", "2", "3")
|
|
188 |
.run(Task.Expect.SUCCESS)
|
|
189 |
.getOutput(Task.OutputKind.STDOUT);
|
|
190 |
checkEqual("stdout", log.trim(), "Hello World! [1, 2, 3]");
|
|
191 |
}
|
|
192 |
|
|
193 |
void testSuccess(Path file, String expect) throws IOException {
|
|
194 |
Result r = run(file, Collections.emptyList(), List.of("1", "2", "3"));
|
|
195 |
checkEqual("stdout", r.stdOut, expect);
|
|
196 |
checkEmpty("stderr", r.stdErr);
|
|
197 |
checkNull("exception", r.exception);
|
|
198 |
}
|
|
199 |
|
|
200 |
/*
|
|
201 |
* Negative tests: such as cannot find or execute main method.
|
|
202 |
*/
|
|
203 |
|
|
204 |
@Test
|
|
205 |
public void testHelloWorldWithShebangJava(Path base) throws IOException {
|
|
206 |
tb.writeJavaFiles(base,
|
|
207 |
"#!/usr/bin/java --source 11\n" +
|
|
208 |
"import java.util.Arrays;\n" +
|
|
209 |
"class HelloWorld {\n" +
|
|
210 |
" public static void main(String... args) {\n" +
|
|
211 |
" System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
|
|
212 |
" }\n" +
|
|
213 |
"}");
|
|
214 |
Path file = base.resolve("HelloWorld.java");
|
|
215 |
testError(file,
|
|
216 |
file + ":1: error: illegal character: '#'\n" +
|
|
217 |
"#!/usr/bin/java --source 11\n" +
|
|
218 |
"^\n" +
|
|
219 |
file + ":1: error: class, interface, or enum expected\n" +
|
|
220 |
"#!/usr/bin/java --source 11\n" +
|
|
221 |
" ^\n" +
|
|
222 |
"2 errors\n",
|
|
223 |
"error: compilation failed");
|
|
224 |
}
|
|
225 |
|
|
226 |
@Test
|
|
227 |
public void testNoClass(Path base) throws IOException {
|
|
228 |
Files.createDirectories(base);
|
|
229 |
Path file = base.resolve("NoClass.java");
|
|
230 |
Files.write(file, List.of("package p;"));
|
|
231 |
testError(file, "", "error: no class declared in file");
|
|
232 |
}
|
|
233 |
|
|
234 |
@Test
|
|
235 |
public void testWrongClass(Path base) throws IOException {
|
|
236 |
Path src = base.resolve("src");
|
|
237 |
Path file = src.resolve("WrongClass.java");
|
|
238 |
tb.writeJavaFiles(src, "class WrongClass { }");
|
|
239 |
Path classes = Files.createDirectories(base.resolve("classes"));
|
|
240 |
new JavacTask(tb)
|
|
241 |
.outdir(classes)
|
|
242 |
.files(file)
|
|
243 |
.run();
|
|
244 |
String log = new JavaTask(tb)
|
|
245 |
.classpath(classes.toString())
|
|
246 |
.className(file.toString())
|
|
247 |
.run(Task.Expect.FAIL)
|
|
248 |
.getOutput(Task.OutputKind.STDERR);
|
|
249 |
checkEqual("stderr", log.trim(),
|
|
250 |
"error: class found on application class path: WrongClass");
|
|
251 |
}
|
|
252 |
|
|
253 |
@Test
|
|
254 |
public void testSyntaxErr(Path base) throws IOException {
|
|
255 |
tb.writeJavaFiles(base, "class SyntaxErr {");
|
|
256 |
Path file = base.resolve("SyntaxErr.java");
|
|
257 |
testError(file,
|
|
258 |
file + ":1: error: reached end of file while parsing\n" +
|
|
259 |
"class SyntaxErr {\n" +
|
|
260 |
" ^\n" +
|
|
261 |
"1 error\n",
|
|
262 |
"error: compilation failed");
|
|
263 |
}
|
|
264 |
|
|
265 |
@Test
|
|
266 |
public void testNoSourceOnClassPath(Path base) throws IOException {
|
|
267 |
Path auxSrc = base.resolve("auxSrc");
|
|
268 |
tb.writeJavaFiles(auxSrc,
|
|
269 |
"public class Aux {\n" +
|
|
270 |
" static final String MESSAGE = \"Hello World\";\n" +
|
|
271 |
"}\n");
|
|
272 |
|
|
273 |
Path mainSrc = base.resolve("mainSrc");
|
|
274 |
tb.writeJavaFiles(mainSrc,
|
|
275 |
"import java.util.Arrays;\n" +
|
|
276 |
"class HelloWorld {\n" +
|
|
277 |
" public static void main(String... args) {\n" +
|
|
278 |
" System.out.println(Aux.MESSAGE + Arrays.toString(args));\n" +
|
|
279 |
" }\n" +
|
|
280 |
"}");
|
|
281 |
|
|
282 |
List<String> javacArgs = List.of("-classpath", auxSrc.toString());
|
|
283 |
List<String> classArgs = List.of("1", "2", "3");
|
|
284 |
String expectStdErr =
|
|
285 |
"testNoSourceOnClassPath/mainSrc/HelloWorld.java:4: error: cannot find symbol\n" +
|
|
286 |
" System.out.println(Aux.MESSAGE + Arrays.toString(args));\n" +
|
|
287 |
" ^\n" +
|
|
288 |
" symbol: variable Aux\n" +
|
|
289 |
" location: class HelloWorld\n" +
|
|
290 |
"1 error\n";
|
|
291 |
Result r = run(mainSrc.resolve("HelloWorld.java"), javacArgs, classArgs);
|
|
292 |
checkEmpty("stdout", r.stdOut);
|
|
293 |
checkEqual("stderr", r.stdErr, expectStdErr);
|
|
294 |
checkFault("exception", r.exception, "error: compilation failed");
|
|
295 |
|
|
296 |
}
|
|
297 |
|
|
298 |
// For any source file that is invoked through the OS shebang mechanism, invalid shebang
|
|
299 |
// lines will be caught and handled by the OS, before the launcher is even invoked.
|
|
300 |
// However, if such a file is passed directly to the launcher, perhaps using the --source
|
|
301 |
// option, a well-formed shebang line will be removed but a badly-formed one will be not be
|
|
302 |
// removed and will cause compilation errors.
|
|
303 |
@Test
|
|
304 |
public void testBadShebang(Path base) throws IOException {
|
|
305 |
tb.writeJavaFiles(base,
|
|
306 |
"#/usr/bin/java --source 11\n" +
|
|
307 |
"import java.util.Arrays;\n" +
|
|
308 |
"class HelloWorld {\n" +
|
|
309 |
" public static void main(String... args) {\n" +
|
|
310 |
" System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
|
|
311 |
" }\n" +
|
|
312 |
"}");
|
|
313 |
Path file = base.resolve("HelloWorld.java");
|
|
314 |
testError(file,
|
|
315 |
file + ":1: error: illegal character: '#'\n" +
|
|
316 |
"#/usr/bin/java --source 11\n" +
|
|
317 |
"^\n" +
|
|
318 |
file + ":1: error: class, interface, or enum expected\n" +
|
|
319 |
"#/usr/bin/java --source 11\n" +
|
|
320 |
" ^\n" +
|
|
321 |
"2 errors\n",
|
|
322 |
"error: compilation failed");
|
|
323 |
}
|
|
324 |
|
|
325 |
@Test
|
|
326 |
public void testBadSourceOpt(Path base) throws IOException {
|
|
327 |
Files.createDirectories(base);
|
|
328 |
Path file = base.resolve("DummyClass.java");
|
|
329 |
Files.write(file, List.of("class DummyClass { }"));
|
|
330 |
Properties sysProps = System.getProperties();
|
|
331 |
Properties p = new Properties(sysProps);
|
|
332 |
p.setProperty("jdk.internal.javac.source", "<BAD>");
|
|
333 |
System.setProperties(p);
|
|
334 |
try {
|
|
335 |
testError(file, "", "error: invalid value for --source option: <BAD>");
|
|
336 |
} finally {
|
|
337 |
System.setProperties(sysProps);
|
|
338 |
}
|
|
339 |
}
|
|
340 |
|
|
341 |
@Test
|
|
342 |
public void testEnablePreviewNoSource(Path base) throws IOException {
|
|
343 |
tb.writeJavaFiles(base,
|
|
344 |
"import java.util.Arrays;\n" +
|
|
345 |
"class HelloWorld {\n" +
|
|
346 |
" public static void main(String... args) {\n" +
|
|
347 |
" System.out.println(\"Hello World! \" + Arrays.toString(args));\n" +
|
|
348 |
" }\n" +
|
|
349 |
"}");
|
|
350 |
|
|
351 |
String log = new JavaTask(tb)
|
|
352 |
.vmOptions("--enable-preview")
|
|
353 |
.className(base.resolve("HelloWorld.java").toString())
|
|
354 |
.run(Task.Expect.FAIL)
|
|
355 |
.getOutput(Task.OutputKind.STDERR);
|
|
356 |
checkEqual("stderr", log.trim(),
|
|
357 |
"error: --enable-preview must be used with --source");
|
|
358 |
}
|
|
359 |
|
|
360 |
@Test
|
|
361 |
public void testNoMain(Path base) throws IOException {
|
|
362 |
tb.writeJavaFiles(base, "class NoMain { }");
|
|
363 |
testError(base.resolve("NoMain.java"), "",
|
|
364 |
"error: can't find main(String[]) method in class: NoMain");
|
|
365 |
}
|
|
366 |
|
|
367 |
@Test
|
|
368 |
public void testMainBadParams(Path base) throws IOException {
|
|
369 |
tb.writeJavaFiles(base,
|
|
370 |
"class BadParams { public static void main() { } }");
|
|
371 |
testError(base.resolve("BadParams.java"), "",
|
|
372 |
"error: can't find main(String[]) method in class: BadParams");
|
|
373 |
}
|
|
374 |
|
|
375 |
@Test
|
|
376 |
public void testMainNotPublic(Path base) throws IOException {
|
|
377 |
tb.writeJavaFiles(base,
|
|
378 |
"class NotPublic { static void main(String... args) { } }");
|
|
379 |
testError(base.resolve("NotPublic.java"), "",
|
|
380 |
"error: 'main' method is not declared 'public static'");
|
|
381 |
}
|
|
382 |
|
|
383 |
@Test
|
|
384 |
public void testMainNotStatic(Path base) throws IOException {
|
|
385 |
tb.writeJavaFiles(base,
|
|
386 |
"class NotStatic { public void main(String... args) { } }");
|
|
387 |
testError(base.resolve("NotStatic.java"), "",
|
|
388 |
"error: 'main' method is not declared 'public static'");
|
|
389 |
}
|
|
390 |
|
|
391 |
@Test
|
|
392 |
public void testMainNotVoid(Path base) throws IOException {
|
|
393 |
tb.writeJavaFiles(base,
|
|
394 |
"class NotVoid { public static int main(String... args) { return 0; } }");
|
|
395 |
testError(base.resolve("NotVoid.java"), "",
|
|
396 |
"error: 'main' method is not declared with a return type of 'void'");
|
|
397 |
}
|
|
398 |
|
|
399 |
@Test
|
|
400 |
public void testClassInModule(Path base) throws IOException {
|
|
401 |
tb.writeJavaFiles(base, "package java.net; class InModule { }");
|
|
402 |
Path file = base.resolve("java").resolve("net").resolve("InModule.java");
|
|
403 |
testError(file,
|
|
404 |
file + ":1: error: package exists in another module: java.base\n" +
|
|
405 |
"package java.net; class InModule { }\n" +
|
|
406 |
"^\n" +
|
|
407 |
"1 error\n",
|
|
408 |
"error: compilation failed");
|
|
409 |
}
|
|
410 |
|
|
411 |
void testError(Path file, String expectStdErr, String expectFault) throws IOException {
|
|
412 |
Result r = run(file, Collections.emptyList(), List.of("1", "2", "3"));
|
|
413 |
checkEmpty("stdout", r.stdOut);
|
|
414 |
checkEqual("stderr", r.stdErr, expectStdErr);
|
|
415 |
checkFault("exception", r.exception, expectFault);
|
|
416 |
}
|
|
417 |
|
|
418 |
/*
|
|
419 |
* Tests in which main throws an exception.
|
|
420 |
*/
|
|
421 |
@Test
|
|
422 |
public void testTargetException1(Path base) throws IOException {
|
|
423 |
tb.writeJavaFiles(base,
|
|
424 |
"import java.util.Arrays;\n" +
|
|
425 |
"class Thrower {\n" +
|
|
426 |
" public static void main(String... args) {\n" +
|
|
427 |
" throwWhenZero(Integer.parseInt(args[0]));\n" +
|
|
428 |
" }\n" +
|
|
429 |
" static void throwWhenZero(int arg) {\n" +
|
|
430 |
" if (arg == 0) throw new Error(\"zero!\");\n" +
|
|
431 |
" throwWhenZero(arg - 1);\n" +
|
|
432 |
" }\n" +
|
|
433 |
"}");
|
|
434 |
Path file = base.resolve("Thrower.java");
|
|
435 |
Result r = run(file, Collections.emptyList(), List.of("3"));
|
|
436 |
checkEmpty("stdout", r.stdOut);
|
|
437 |
checkEmpty("stderr", r.stdErr);
|
|
438 |
checkTrace("exception", r.exception,
|
|
439 |
"java.lang.Error: zero!",
|
|
440 |
"at Thrower.throwWhenZero(Thrower.java:7)",
|
|
441 |
"at Thrower.throwWhenZero(Thrower.java:8)",
|
|
442 |
"at Thrower.throwWhenZero(Thrower.java:8)",
|
|
443 |
"at Thrower.throwWhenZero(Thrower.java:8)",
|
|
444 |
"at Thrower.main(Thrower.java:4)");
|
|
445 |
}
|
|
446 |
|
|
447 |
Result run(Path file, List<String> runtimeArgs, List<String> appArgs) {
|
|
448 |
List<String> args = new ArrayList<>();
|
|
449 |
args.add(file.toString());
|
|
450 |
args.addAll(appArgs);
|
|
451 |
|
|
452 |
PrintStream prev = System.out;
|
|
453 |
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
|
454 |
try (PrintStream out = new PrintStream(baos, true)) {
|
|
455 |
System.setOut(out);
|
|
456 |
StringWriter sw = new StringWriter();
|
|
457 |
try (PrintWriter err = new PrintWriter(sw, true)) {
|
|
458 |
Main m = new Main(err);
|
|
459 |
m.run(toArray(runtimeArgs), toArray(args));
|
|
460 |
return new Result(baos.toString(), sw.toString(), null);
|
|
461 |
} catch (Throwable t) {
|
|
462 |
return new Result(baos.toString(), sw.toString(), t);
|
|
463 |
}
|
|
464 |
} finally {
|
|
465 |
System.setOut(prev);
|
|
466 |
}
|
|
467 |
}
|
|
468 |
|
|
469 |
void checkEqual(String name, String found, String expect) {
|
|
470 |
expect = expect.replace("\n", tb.lineSeparator);
|
|
471 |
out.println(name + ": " + found);
|
|
472 |
out.println(name + ": " + found);
|
|
473 |
if (!expect.equals(found)) {
|
|
474 |
error("Unexpected output; expected: " + expect);
|
|
475 |
}
|
|
476 |
}
|
|
477 |
|
|
478 |
void checkEmpty(String name, String found) {
|
|
479 |
out.println(name + ": " + found);
|
|
480 |
if (!found.isEmpty()) {
|
|
481 |
error("Unexpected output; expected empty string");
|
|
482 |
}
|
|
483 |
}
|
|
484 |
|
|
485 |
void checkNull(String name, Throwable found) {
|
|
486 |
out.println(name + ": " + found);
|
|
487 |
if (found != null) {
|
|
488 |
error("Unexpected exception; expected null");
|
|
489 |
}
|
|
490 |
}
|
|
491 |
|
|
492 |
void checkFault(String name, Throwable found, String expect) {
|
|
493 |
expect = expect.replace("\n", tb.lineSeparator);
|
|
494 |
out.println(name + ": " + found);
|
|
495 |
if (found == null) {
|
|
496 |
error("No exception thrown; expected Main.Fault");
|
|
497 |
} else {
|
|
498 |
if (!(found instanceof Main.Fault)) {
|
|
499 |
error("Unexpected exception; expected Main.Fault");
|
|
500 |
}
|
|
501 |
if (!(found.getMessage().equals(expect))) {
|
|
502 |
error("Unexpected detail message; expected: " + expect);
|
|
503 |
}
|
|
504 |
}
|
|
505 |
}
|
|
506 |
|
|
507 |
void checkTrace(String name, Throwable found, String... expect) {
|
|
508 |
if (!(found instanceof InvocationTargetException)) {
|
|
509 |
error("Unexpected exception; expected InvocationTargetException");
|
|
510 |
out.println("Found:");
|
|
511 |
found.printStackTrace(out);
|
|
512 |
}
|
|
513 |
StringWriter sw = new StringWriter();
|
|
514 |
try (PrintWriter pw = new PrintWriter(sw)) {
|
|
515 |
((InvocationTargetException) found).getTargetException().printStackTrace(pw);
|
|
516 |
}
|
|
517 |
String trace = sw.toString();
|
|
518 |
out.println(name + ":\n" + trace);
|
|
519 |
String[] traceLines = trace.trim().split("[\r\n]+\\s+");
|
|
520 |
try {
|
|
521 |
tb.checkEqual(List.of(traceLines), List.of(expect));
|
|
522 |
} catch (Error e) {
|
|
523 |
error(e.getMessage());
|
|
524 |
}
|
|
525 |
}
|
|
526 |
|
|
527 |
String[] toArray(List<String> list) {
|
|
528 |
return list.toArray(new String[list.size()]);
|
|
529 |
}
|
|
530 |
|
|
531 |
class Result {
|
|
532 |
private final String stdOut;
|
|
533 |
private final String stdErr;
|
|
534 |
private final Throwable exception;
|
|
535 |
|
|
536 |
Result(String stdOut, String stdErr, Throwable exception) {
|
|
537 |
this.stdOut = stdOut;
|
|
538 |
this.stdErr = stdErr;
|
|
539 |
this.exception = exception;
|
|
540 |
}
|
|
541 |
}
|
|
542 |
}
|