8062314: Update tools/javac/plugin/showtype/Test.java to use ToolBox.java
authorsogoel
Tue, 04 Nov 2014 13:21:25 -0800
changeset 27385 fb4a5f118316
parent 27384 51aa4299ae7b
child 27386 784414cffd9a
8062314: Update tools/javac/plugin/showtype/Test.java to use ToolBox.java Reviewed-by: jjg
langtools/test/tools/javac/plugin/showtype/Test.java
langtools/test/tools/lib/ToolBox.java
--- a/langtools/test/tools/javac/plugin/showtype/Test.java	Tue Nov 04 09:04:13 2014 +0100
+++ b/langtools/test/tools/javac/plugin/showtype/Test.java	Tue Nov 04 13:21:25 2014 -0800
@@ -24,6 +24,9 @@
 /**
  *  @test
  *  @bug 8001098 8004961 8004082
+ *  @library /tools/lib
+ *  @build ToolBox
+ *  @run main Test
  *  @summary Provide a simple light-weight "plug-in" mechanism for javac
  */
 
@@ -58,14 +61,16 @@
     final List<String> ref2;
     final JavaCompiler compiler;
     final StandardJavaFileManager fm;
+    ToolBox tb = new ToolBox();
 
     Test() throws Exception {
-        testSrc = new File(System.getProperty("test.src"));
+        testSrc = new File(tb.testSrc);
         pluginSrc = new File(testSrc, "ShowTypePlugin.java");
         pluginClasses = new File("plugin");
+        tb.createDirectories(pluginClasses.toPath());
         pluginJar = new File("plugin.jar");
-        ref1 = readFile(testSrc, "Identifiers.out");
-        ref2 = readFile(testSrc, "Identifiers_PI.out");
+        ref1 = tb.readAllLines((new File(testSrc,"Identifiers.out")).toPath());
+        ref2 = tb.readAllLines((new File(testSrc,"Identifiers_PI.out")).toPath());
         compiler = ToolProvider.getSystemJavaCompiler();
         fm = compiler.getStandardFileManager(null, null, null);
     }
@@ -74,11 +79,15 @@
         try {
             // compile the plugin explicitly, to a non-standard directory
             // so that we don't find it on the wrong path by accident
-            pluginClasses.mkdirs();
-            compile("-d", pluginClasses.getPath(), pluginSrc.getPath());
-            writeFile(new File(pluginClasses, "META-INF/services/com.sun.source.util.Plugin"),
-                    "ShowTypePlugin\n");
-            jar("cf", pluginJar.getPath(), "-C", pluginClasses.getPath(), ".");
+            tb.new JavacTask()
+              .options("-d", pluginClasses.getPath())
+              .files(pluginSrc.getPath())
+              .run();
+
+            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 PI", ref2);
@@ -100,14 +109,13 @@
         Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(identifiers);
 
         System.err.println("test api: " + options + " " + files);
-
-        StringWriter sw = new StringWriter();
-        PrintWriter pw = new PrintWriter(sw);
-        boolean ok = compiler.getTask(pw, fm, null, options, null, files).call();
-        String out = sw.toString();
-        System.err.println(out);
-        if (!ok)
-            error("testCommandLine: compilation failed");
+        ToolBox.Result result = tb.new JavacTask(ToolBox.Mode.API)
+                                  .fileManager(fm)
+                                  .options(opt)
+                                  .files(identifiers.toPath())
+                                  .run(ToolBox.Expect.SUCCESS)
+                                  .writeAll();
+        String out = result.getOutput(ToolBox.OutputKind.DIRECT);
         checkOutput(out, ref);
     }
 
@@ -120,14 +128,11 @@
             identifiers.getPath() };
 
         System.err.println("test command line: " + Arrays.asList(args));
-
-        StringWriter sw = new StringWriter();
-        PrintWriter pw = new PrintWriter(sw);
-        int rc = com.sun.tools.javac.Main.compile(args, pw);
-        String out = sw.toString();
-        System.err.println(out);
-        if (rc != 0)
-            error("testCommandLine: compilation failed");
+        ToolBox.Result result = tb.new JavacTask(ToolBox.Mode.CMDLINE)
+                                  .options(args)
+                                  .run(ToolBox.Expect.SUCCESS)
+                                  .writeAll();
+        String out = result.getOutput(ToolBox.OutputKind.DIRECT);
         checkOutput(out, ref);
     }
 
@@ -140,31 +145,6 @@
         }
     }
 
-    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) {
         System.err.println(msg);
         errors++;
--- a/langtools/test/tools/lib/ToolBox.java	Tue Nov 04 09:04:13 2014 +0100
+++ b/langtools/test/tools/lib/ToolBox.java	Tue Nov 04 13:21:25 2014 -0800
@@ -1475,7 +1475,7 @@
                         @Override
                         public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                             try {
-                                JarEntry e = new JarEntry(base.relativize(file).toString());
+                                JarEntry e = new JarEntry(base.relativize(file).normalize().toString());
                                 jos.putNextEntry(e);
                                 jos.write(Files.readAllBytes(file));
                                 jos.closeEntry();