8062314: Update tools/javac/plugin/showtype/Test.java to use ToolBox.java

Reviewed-by: jjg
This commit is contained in:
Sonali Goel 2014-11-04 13:21:25 -08:00
parent 4a3d559ffb
commit 4ca3a232d1
2 changed files with 30 additions and 50 deletions

View file

@ -24,6 +24,9 @@
/** /**
* @test * @test
* @bug 8001098 8004961 8004082 * @bug 8001098 8004961 8004082
* @library /tools/lib
* @build ToolBox
* @run main Test
* @summary Provide a simple light-weight "plug-in" mechanism for javac * @summary Provide a simple light-weight "plug-in" mechanism for javac
*/ */
@ -58,14 +61,16 @@ public class Test {
final List<String> ref2; final List<String> ref2;
final JavaCompiler compiler; final JavaCompiler compiler;
final StandardJavaFileManager fm; final StandardJavaFileManager fm;
ToolBox tb = new ToolBox();
Test() throws Exception { Test() throws Exception {
testSrc = new File(System.getProperty("test.src")); testSrc = new File(tb.testSrc);
pluginSrc = new File(testSrc, "ShowTypePlugin.java"); pluginSrc = new File(testSrc, "ShowTypePlugin.java");
pluginClasses = new File("plugin"); pluginClasses = new File("plugin");
tb.createDirectories(pluginClasses.toPath());
pluginJar = new File("plugin.jar"); pluginJar = new File("plugin.jar");
ref1 = readFile(testSrc, "Identifiers.out"); ref1 = tb.readAllLines((new File(testSrc,"Identifiers.out")).toPath());
ref2 = readFile(testSrc, "Identifiers_PI.out"); ref2 = tb.readAllLines((new File(testSrc,"Identifiers_PI.out")).toPath());
compiler = ToolProvider.getSystemJavaCompiler(); compiler = ToolProvider.getSystemJavaCompiler();
fm = compiler.getStandardFileManager(null, null, null); fm = compiler.getStandardFileManager(null, null, null);
} }
@ -74,11 +79,15 @@ public class Test {
try { try {
// compile the plugin explicitly, to a non-standard directory // compile the plugin explicitly, to a non-standard directory
// so that we don't find it on the wrong path by accident // so that we don't find it on the wrong path by accident
pluginClasses.mkdirs(); tb.new JavacTask()
compile("-d", pluginClasses.getPath(), pluginSrc.getPath()); .options("-d", pluginClasses.getPath())
writeFile(new File(pluginClasses, "META-INF/services/com.sun.source.util.Plugin"), .files(pluginSrc.getPath())
"ShowTypePlugin\n"); .run();
jar("cf", pluginJar.getPath(), "-C", pluginClasses.getPath(), ".");
File plugin = new File(pluginClasses.getPath(), "META-INF/services/com.sun.source.util.Plugin");
tb.writeFile(plugin.getPath(),"ShowTypePlugin");
tb.new JarTask()
.run("cf", pluginJar.getPath(),"-C", pluginClasses.getPath(), ".");
testCommandLine("-Xplugin:showtype", ref1); testCommandLine("-Xplugin:showtype", ref1);
testCommandLine("-Xplugin:showtype PI", ref2); testCommandLine("-Xplugin:showtype PI", ref2);
@ -100,14 +109,13 @@ public class Test {
Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(identifiers); Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(identifiers);
System.err.println("test api: " + options + " " + files); System.err.println("test api: " + options + " " + files);
ToolBox.Result result = tb.new JavacTask(ToolBox.Mode.API)
StringWriter sw = new StringWriter(); .fileManager(fm)
PrintWriter pw = new PrintWriter(sw); .options(opt)
boolean ok = compiler.getTask(pw, fm, null, options, null, files).call(); .files(identifiers.toPath())
String out = sw.toString(); .run(ToolBox.Expect.SUCCESS)
System.err.println(out); .writeAll();
if (!ok) String out = result.getOutput(ToolBox.OutputKind.DIRECT);
error("testCommandLine: compilation failed");
checkOutput(out, ref); checkOutput(out, ref);
} }
@ -120,14 +128,11 @@ public class Test {
identifiers.getPath() }; identifiers.getPath() };
System.err.println("test command line: " + Arrays.asList(args)); System.err.println("test command line: " + Arrays.asList(args));
ToolBox.Result result = tb.new JavacTask(ToolBox.Mode.CMDLINE)
StringWriter sw = new StringWriter(); .options(args)
PrintWriter pw = new PrintWriter(sw); .run(ToolBox.Expect.SUCCESS)
int rc = com.sun.tools.javac.Main.compile(args, pw); .writeAll();
String out = sw.toString(); String out = result.getOutput(ToolBox.OutputKind.DIRECT);
System.err.println(out);
if (rc != 0)
error("testCommandLine: compilation failed");
checkOutput(out, ref); checkOutput(out, ref);
} }
@ -140,31 +145,6 @@ public class Test {
} }
} }
private void compile(String... args) throws Exception {
System.err.println("compile: " + Arrays.asList(args));
int rc = com.sun.tools.javac.Main.compile(args);
if (rc != 0)
throw new Exception("compiled failed, rc=" + rc);
}
private void jar(String... args) throws Exception {
System.err.println("jar: " + Arrays.asList(args));
boolean ok = new sun.tools.jar.Main(System.out, System.err, "jar").run(args);
if (!ok)
throw new Exception("jar failed");
}
private List<String> readFile(File dir, String name) throws IOException {
return Files.readAllLines(new File(dir, name).toPath(), Charset.defaultCharset());
}
private void writeFile(File f, String body) throws IOException {
f.getParentFile().mkdirs();
try (FileWriter out = new FileWriter(f)) {
out.write(body);
}
}
private void error(String msg) { private void error(String msg) {
System.err.println(msg); System.err.println(msg);
errors++; errors++;

View file

@ -1475,7 +1475,7 @@ public class ToolBox {
@Override @Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) { public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
try { try {
JarEntry e = new JarEntry(base.relativize(file).toString()); JarEntry e = new JarEntry(base.relativize(file).normalize().toString());
jos.putNextEntry(e); jos.putNextEntry(e);
jos.write(Files.readAllBytes(file)); jos.write(Files.readAllBytes(file));
jos.closeEntry(); jos.closeEntry();