--- a/test/langtools/tools/javac/launcher/SourceLauncherTest.java Tue Sep 25 09:34:51 2018 -0700
+++ b/test/langtools/tools/javac/launcher/SourceLauncherTest.java Tue Sep 25 10:30:32 2018 -0700
@@ -47,6 +47,7 @@
import java.util.Collections;
import java.util.List;
import java.util.Properties;
+import java.util.regex.Pattern;
import com.sun.tools.javac.launcher.Main;
@@ -233,22 +234,98 @@
}
@Test
- public void testWrongClass(Path base) throws IOException {
+ public void testLoadClass(Path base) throws IOException {
+ Path src1 = base.resolve("src1");
+ Path file1 = src1.resolve("LoadClass.java");
+ tb.writeJavaFiles(src1,
+ "class LoadClass {\n"
+ + " public static void main(String... args) {\n"
+ + " System.out.println(\"on classpath\");\n"
+ + " };\n"
+ + "}\n");
+ Path classes1 = Files.createDirectories(base.resolve("classes"));
+ new JavacTask(tb)
+ .outdir(classes1)
+ .files(file1)
+ .run();
+ String log1 = new JavaTask(tb)
+ .classpath(classes1.toString())
+ .className("LoadClass")
+ .run(Task.Expect.SUCCESS)
+ .getOutput(Task.OutputKind.STDOUT);
+ checkEqual("stdout", log1.trim(),
+ "on classpath");
+
+ Path src2 = base.resolve("src2");
+ Path file2 = src2.resolve("LoadClass.java");
+ tb.writeJavaFiles(src2,
+ "class LoadClass {\n"
+ + " public static void main(String... args) {\n"
+ + " System.out.println(\"in source file\");\n"
+ + " };\n"
+ + "}\n");
+ String log2 = new JavaTask(tb)
+ .classpath(classes1.toString())
+ .className(file2.toString())
+ .run(Task.Expect.SUCCESS)
+ .getOutput(Task.OutputKind.STDOUT);
+ checkEqual("stdout", log2.trim(),
+ "in source file");
+ }
+
+ @Test
+ public void testGetResource(Path base) throws IOException {
Path src = base.resolve("src");
- Path file = src.resolve("WrongClass.java");
- tb.writeJavaFiles(src, "class WrongClass { }");
+ Path file = src.resolve("GetResource.java");
+ tb.writeJavaFiles(src,
+ "class GetResource {\n"
+ + " public static void main(String... args) {\n"
+ + " System.out.println(GetResource.class.getClassLoader().getResource(\"GetResource.class\"));\n"
+ + " };\n"
+ + "}\n");
Path classes = Files.createDirectories(base.resolve("classes"));
new JavacTask(tb)
.outdir(classes)
.files(file)
.run();
+
String log = new JavaTask(tb)
.classpath(classes.toString())
.className(file.toString())
- .run(Task.Expect.FAIL)
- .getOutput(Task.OutputKind.STDERR);
- checkEqual("stderr", log.trim(),
- "error: class found on application class path: WrongClass");
+ .run(Task.Expect.SUCCESS)
+ .getOutput(Task.OutputKind.STDOUT);
+ checkMatch("stdout", log.trim(),
+ Pattern.compile("sourcelauncher-memoryclassloader[0-9]+:GetResource.class"));
+ }
+
+ @Test
+ public void testGetResources(Path base) throws IOException {
+ Path src = base.resolve("src");
+ Path file = src.resolve("GetResources.java");
+ tb.writeJavaFiles(src,
+ "import java.io.*; import java.net.*; import java.util.*;\n"
+ + "class GetResources {\n"
+ + " public static void main(String... args) throws IOException {\n"
+ + " Enumeration<URL> e =\n"
+ + " GetResources.class.getClassLoader().getResources(\"GetResources.class\");\n"
+ + " while (e.hasMoreElements()) System.out.println(e.nextElement());\n"
+ + " };\n"
+ + "}\n");
+ Path classes = Files.createDirectories(base.resolve("classes"));
+ new JavacTask(tb)
+ .outdir(classes)
+ .files(file)
+ .run();
+
+ List<String> log = new JavaTask(tb)
+ .classpath(classes.toString())
+ .className(file.toString())
+ .run(Task.Expect.SUCCESS)
+ .getOutputLines(Task.OutputKind.STDOUT);
+ checkMatch("stdout:0", log.get(0).trim(),
+ Pattern.compile("sourcelauncher-memoryclassloader[0-9]+:GetResources.class"));
+ checkMatch("stdout:1", log.get(1).trim(),
+ Pattern.compile("file:/.*/testGetResources/classes/GetResources.class"));
}
@Test
@@ -294,7 +371,37 @@
checkEmpty("stdout", r.stdOut);
checkEqual("stderr", r.stdErr, expectStdErr);
checkFault("exception", r.exception, "error: compilation failed");
+ }
+ @Test
+ public void testClassNotFound(Path base) throws IOException {
+ Path src = base.resolve("src");
+ Path file = src.resolve("ClassNotFound.java");
+ tb.writeJavaFiles(src,
+ "class ClassNotFound {\n"
+ + " public static void main(String... args) {\n"
+ + " try {\n"
+ + " Class.forName(\"NoSuchClass\");\n"
+ + " System.out.println(\"no exception\");\n"
+ + " System.exit(1);\n"
+ + " } catch (ClassNotFoundException e) {\n"
+ + " System.out.println(\"Expected exception thrown: \" + e);\n"
+ + " }\n"
+ + " };\n"
+ + "}\n");
+ Path classes = Files.createDirectories(base.resolve("classes"));
+ new JavacTask(tb)
+ .outdir(classes)
+ .files(file)
+ .run();
+
+ String log = new JavaTask(tb)
+ .classpath(classes.toString())
+ .className(file.toString())
+ .run(Task.Expect.SUCCESS)
+ .getOutput(Task.OutputKind.STDOUT);
+ checkEqual("stdout", log.trim(),
+ "Expected exception thrown: java.lang.ClassNotFoundException: NoSuchClass");
}
// For any source file that is invoked through the OS shebang mechanism, invalid shebang
@@ -471,12 +578,18 @@
void checkEqual(String name, String found, String expect) {
expect = expect.replace("\n", tb.lineSeparator);
out.println(name + ": " + found);
- out.println(name + ": " + found);
if (!expect.equals(found)) {
error("Unexpected output; expected: " + expect);
}
}
+ void checkMatch(String name, String found, Pattern expect) {
+ out.println(name + ": " + found);
+ if (!expect.matcher(found).matches()) {
+ error("Unexpected output; expected match for: " + expect);
+ }
+ }
+
void checkEmpty(String name, String found) {
out.println(name + ": " + found);
if (!found.isEmpty()) {