changeset 11731:84c5527f742b jdk9-b60

8077506: Simplify test JImageTest Reviewed-by: alanb Contributed-by: james.laskey@oracle.com
author jlaskey
date Fri, 10 Apr 2015 14:21:24 -0300
parents e571b36aa08c
children c5b5d9045728 24d02d2cc675
files test/jdk/internal/jimage/JImageTest.java
diffstat 1 files changed, 21 insertions(+), 100 deletions(-) [+]
line wrap: on
line diff
--- a/test/jdk/internal/jimage/JImageTest.java	Fri Apr 10 09:15:34 2015 +0800
+++ b/test/jdk/internal/jimage/JImageTest.java	Fri Apr 10 14:21:24 2015 -0300
@@ -22,121 +22,41 @@
  */
 /*
  * @test
+ * @library /lib/testlibrary
+ * @build jdk.testlibrary.*
  * @summary Test to see if jimage tool extracts and recreates correctly.
  * @run main/timeout=360 JImageTest
  */
 
-import java.io.IOException;
-import java.io.InputStream;
-import java.io.InputStreamReader;
-import java.io.OutputStream;
-import java.io.OutputStreamWriter;
 import java.nio.file.Files;
 import java.nio.file.Path;
+import java.nio.file.Path;
 import java.nio.file.Paths;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+import jdk.testlibrary.ProcessTools;
+
 
 /**
  * Basic test for jimage tool.
  */
 public class JImageTest {
-    private static Runtime runtime = Runtime.getRuntime();
-    private static int errors = 0;
+    private static void jimage(String... jimageArgs) throws Exception {
+        ArrayList<String> args = new ArrayList<>();
+        args.add("-ms8m");
+        args.add("jdk.tools.jimage.Main");
+        args.addAll(Arrays.asList(jimageArgs));
 
-    static class Tool {
-        private int exit = 0;
-        private String out = "";
-        private String err = "";
+        ProcessBuilder builder = ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()]));
+        int res = builder.inheritIO().start().waitFor();
 
-        private void feedInputStream(String in, OutputStream out) {
-            try (OutputStreamWriter outputStream = new OutputStreamWriter(out)) {
-                if (in != null) {
-                    outputStream.write(in, 0, in.length());
-                }
-            } catch (final IOException ex) {
-                // Process was not expecting input.  May be normal state of affairs.
-            }
-        }
-
-        private Thread getOutputStreamThread(InputStream in, StringBuilder out) {
-            return new Thread(() -> {
-                final char buffer[] = new char[1024];
-
-                try (final InputStreamReader inputStream = new InputStreamReader(in)) {
-                    for (int length; (length = inputStream.read(buffer, 0, buffer.length)) != -1; ) {
-                        out.append(buffer, 0, length);
-                    }
-                } catch (final IOException ex) {
-                    out.append(ex.toString());
-                }
-            });
-        }
-
-        Tool(String[] args) {
-            this(null, args);
-        }
-
-        Tool(String in, String[] args) {
-            ProcessBuilder processBuilder = new ProcessBuilder(args);
-            exec(processBuilder, in);
-        }
-
-        private void exec(ProcessBuilder processBuilder, String in) {
-            StringBuilder outBuilder = new StringBuilder();
-            StringBuilder errBuilder = new StringBuilder();
-
-            try {
-                Process process = processBuilder.start();
-
-                Thread outThread = getOutputStreamThread(process.getInputStream(), outBuilder);
-                Thread errThread = getOutputStreamThread(process.getErrorStream(), errBuilder);
-
-                outThread.start();
-                errThread.start();
-
-                feedInputStream(in, process.getOutputStream());
-
-                exit = process.waitFor();
-                outThread.join();
-                errThread.join();
-            } catch (IOException | InterruptedException ex) {
-                ex.printStackTrace();
-                exit = -1;
-            }
-
-            out = outBuilder.toString();
-            err = errBuilder.toString();
-        }
-
-        int getExit() {
-            return exit;
-        }
-
-        String getOut() {
-            return out;
-        }
-
-        String getErr() {
-            return err;
+        if (res != 0) {
+            throw new RuntimeException("JImageTest tool FAILED");
         }
     }
 
-    private static void exec(String... args) {
-        Tool tool = new Tool(args);
-        int exit = tool.getExit();
-
-        if (exit != 0) {
-            errors++;
-            System.err.println("----------Tool.out----------");
-            System.err.append(tool.getOut());
-            System.err.println("----------Tool.err----------");
-            System.err.append(tool.getErr());
-            System.err.println("----------Tool.exit----------");
-            System.err.println("Error code = " + exit);
-            throw new RuntimeException("JImageTest FAIL");
-        }
-    }
-
-    public static void main(String[] args) {
+    public static void main(String[] args) throws Exception {
         final String JAVA_HOME = System.getProperty("java.home");
         Path jimagePath = Paths.get(JAVA_HOME, "bin", "jimage");
         Path bootimagePath = Paths.get(JAVA_HOME, "lib", "modules", "bootmodules.jimage");
@@ -147,12 +67,13 @@
             String extractDir = Paths.get(".", "extract").toAbsolutePath().toString();
             String recreateImage = Paths.get(".", "recreate.jimage").toAbsolutePath().toString();
 
-            exec(new String[] {jimage, "extract", "--dir", extractDir, bootimage});
-            exec(new String[] {jimage, "recreate", "--dir", extractDir, recreateImage});
+            jimage("extract", "--dir", extractDir, bootimage);
+            jimage("recreate", "--dir", extractDir, recreateImage);
 
             System.out.println("Test successful");
          } else {
             System.out.println("Test skipped, no module image");
          }
+
     }
 }