231 Files.write(file, List.of("package p;")); |
232 Files.write(file, List.of("package p;")); |
232 testError(file, "", "error: no class declared in source file"); |
233 testError(file, "", "error: no class declared in source file"); |
233 } |
234 } |
234 |
235 |
235 @Test |
236 @Test |
236 public void testWrongClass(Path base) throws IOException { |
237 public void testLoadClass(Path base) throws IOException { |
|
238 Path src1 = base.resolve("src1"); |
|
239 Path file1 = src1.resolve("LoadClass.java"); |
|
240 tb.writeJavaFiles(src1, |
|
241 "class LoadClass {\n" |
|
242 + " public static void main(String... args) {\n" |
|
243 + " System.out.println(\"on classpath\");\n" |
|
244 + " };\n" |
|
245 + "}\n"); |
|
246 Path classes1 = Files.createDirectories(base.resolve("classes")); |
|
247 new JavacTask(tb) |
|
248 .outdir(classes1) |
|
249 .files(file1) |
|
250 .run(); |
|
251 String log1 = new JavaTask(tb) |
|
252 .classpath(classes1.toString()) |
|
253 .className("LoadClass") |
|
254 .run(Task.Expect.SUCCESS) |
|
255 .getOutput(Task.OutputKind.STDOUT); |
|
256 checkEqual("stdout", log1.trim(), |
|
257 "on classpath"); |
|
258 |
|
259 Path src2 = base.resolve("src2"); |
|
260 Path file2 = src2.resolve("LoadClass.java"); |
|
261 tb.writeJavaFiles(src2, |
|
262 "class LoadClass {\n" |
|
263 + " public static void main(String... args) {\n" |
|
264 + " System.out.println(\"in source file\");\n" |
|
265 + " };\n" |
|
266 + "}\n"); |
|
267 String log2 = new JavaTask(tb) |
|
268 .classpath(classes1.toString()) |
|
269 .className(file2.toString()) |
|
270 .run(Task.Expect.SUCCESS) |
|
271 .getOutput(Task.OutputKind.STDOUT); |
|
272 checkEqual("stdout", log2.trim(), |
|
273 "in source file"); |
|
274 } |
|
275 |
|
276 @Test |
|
277 public void testGetResource(Path base) throws IOException { |
237 Path src = base.resolve("src"); |
278 Path src = base.resolve("src"); |
238 Path file = src.resolve("WrongClass.java"); |
279 Path file = src.resolve("GetResource.java"); |
239 tb.writeJavaFiles(src, "class WrongClass { }"); |
280 tb.writeJavaFiles(src, |
|
281 "class GetResource {\n" |
|
282 + " public static void main(String... args) {\n" |
|
283 + " System.out.println(GetResource.class.getClassLoader().getResource(\"GetResource.class\"));\n" |
|
284 + " };\n" |
|
285 + "}\n"); |
240 Path classes = Files.createDirectories(base.resolve("classes")); |
286 Path classes = Files.createDirectories(base.resolve("classes")); |
241 new JavacTask(tb) |
287 new JavacTask(tb) |
242 .outdir(classes) |
288 .outdir(classes) |
243 .files(file) |
289 .files(file) |
244 .run(); |
290 .run(); |
|
291 |
245 String log = new JavaTask(tb) |
292 String log = new JavaTask(tb) |
246 .classpath(classes.toString()) |
293 .classpath(classes.toString()) |
247 .className(file.toString()) |
294 .className(file.toString()) |
248 .run(Task.Expect.FAIL) |
295 .run(Task.Expect.SUCCESS) |
249 .getOutput(Task.OutputKind.STDERR); |
296 .getOutput(Task.OutputKind.STDOUT); |
250 checkEqual("stderr", log.trim(), |
297 checkMatch("stdout", log.trim(), |
251 "error: class found on application class path: WrongClass"); |
298 Pattern.compile("sourcelauncher-memoryclassloader[0-9]+:GetResource.class")); |
|
299 } |
|
300 |
|
301 @Test |
|
302 public void testGetResources(Path base) throws IOException { |
|
303 Path src = base.resolve("src"); |
|
304 Path file = src.resolve("GetResources.java"); |
|
305 tb.writeJavaFiles(src, |
|
306 "import java.io.*; import java.net.*; import java.util.*;\n" |
|
307 + "class GetResources {\n" |
|
308 + " public static void main(String... args) throws IOException {\n" |
|
309 + " Enumeration<URL> e =\n" |
|
310 + " GetResources.class.getClassLoader().getResources(\"GetResources.class\");\n" |
|
311 + " while (e.hasMoreElements()) System.out.println(e.nextElement());\n" |
|
312 + " };\n" |
|
313 + "}\n"); |
|
314 Path classes = Files.createDirectories(base.resolve("classes")); |
|
315 new JavacTask(tb) |
|
316 .outdir(classes) |
|
317 .files(file) |
|
318 .run(); |
|
319 |
|
320 List<String> log = new JavaTask(tb) |
|
321 .classpath(classes.toString()) |
|
322 .className(file.toString()) |
|
323 .run(Task.Expect.SUCCESS) |
|
324 .getOutputLines(Task.OutputKind.STDOUT); |
|
325 checkMatch("stdout:0", log.get(0).trim(), |
|
326 Pattern.compile("sourcelauncher-memoryclassloader[0-9]+:GetResources.class")); |
|
327 checkMatch("stdout:1", log.get(1).trim(), |
|
328 Pattern.compile("file:/.*/testGetResources/classes/GetResources.class")); |
252 } |
329 } |
253 |
330 |
254 @Test |
331 @Test |
255 public void testSyntaxErr(Path base) throws IOException { |
332 public void testSyntaxErr(Path base) throws IOException { |
256 tb.writeJavaFiles(base, "class SyntaxErr {"); |
333 tb.writeJavaFiles(base, "class SyntaxErr {"); |
292 "1 error\n"; |
369 "1 error\n"; |
293 Result r = run(mainSrc.resolve("HelloWorld.java"), javacArgs, classArgs); |
370 Result r = run(mainSrc.resolve("HelloWorld.java"), javacArgs, classArgs); |
294 checkEmpty("stdout", r.stdOut); |
371 checkEmpty("stdout", r.stdOut); |
295 checkEqual("stderr", r.stdErr, expectStdErr); |
372 checkEqual("stderr", r.stdErr, expectStdErr); |
296 checkFault("exception", r.exception, "error: compilation failed"); |
373 checkFault("exception", r.exception, "error: compilation failed"); |
297 |
374 } |
|
375 |
|
376 @Test |
|
377 public void testClassNotFound(Path base) throws IOException { |
|
378 Path src = base.resolve("src"); |
|
379 Path file = src.resolve("ClassNotFound.java"); |
|
380 tb.writeJavaFiles(src, |
|
381 "class ClassNotFound {\n" |
|
382 + " public static void main(String... args) {\n" |
|
383 + " try {\n" |
|
384 + " Class.forName(\"NoSuchClass\");\n" |
|
385 + " System.out.println(\"no exception\");\n" |
|
386 + " System.exit(1);\n" |
|
387 + " } catch (ClassNotFoundException e) {\n" |
|
388 + " System.out.println(\"Expected exception thrown: \" + e);\n" |
|
389 + " }\n" |
|
390 + " };\n" |
|
391 + "}\n"); |
|
392 Path classes = Files.createDirectories(base.resolve("classes")); |
|
393 new JavacTask(tb) |
|
394 .outdir(classes) |
|
395 .files(file) |
|
396 .run(); |
|
397 |
|
398 String log = new JavaTask(tb) |
|
399 .classpath(classes.toString()) |
|
400 .className(file.toString()) |
|
401 .run(Task.Expect.SUCCESS) |
|
402 .getOutput(Task.OutputKind.STDOUT); |
|
403 checkEqual("stdout", log.trim(), |
|
404 "Expected exception thrown: java.lang.ClassNotFoundException: NoSuchClass"); |
298 } |
405 } |
299 |
406 |
300 // For any source file that is invoked through the OS shebang mechanism, invalid shebang |
407 // For any source file that is invoked through the OS shebang mechanism, invalid shebang |
301 // lines will be caught and handled by the OS, before the launcher is even invoked. |
408 // lines will be caught and handled by the OS, before the launcher is even invoked. |
302 // However, if such a file is passed directly to the launcher, perhaps using the --source |
409 // However, if such a file is passed directly to the launcher, perhaps using the --source |
469 } |
576 } |
470 |
577 |
471 void checkEqual(String name, String found, String expect) { |
578 void checkEqual(String name, String found, String expect) { |
472 expect = expect.replace("\n", tb.lineSeparator); |
579 expect = expect.replace("\n", tb.lineSeparator); |
473 out.println(name + ": " + found); |
580 out.println(name + ": " + found); |
474 out.println(name + ": " + found); |
|
475 if (!expect.equals(found)) { |
581 if (!expect.equals(found)) { |
476 error("Unexpected output; expected: " + expect); |
582 error("Unexpected output; expected: " + expect); |
|
583 } |
|
584 } |
|
585 |
|
586 void checkMatch(String name, String found, Pattern expect) { |
|
587 out.println(name + ": " + found); |
|
588 if (!expect.matcher(found).matches()) { |
|
589 error("Unexpected output; expected match for: " + expect); |
477 } |
590 } |
478 } |
591 } |
479 |
592 |
480 void checkEmpty(String name, String found) { |
593 void checkEmpty(String name, String found) { |
481 out.println(name + ": " + found); |
594 out.println(name + ": " + found); |