diff --git a/jdk/test/jdk/internal/jimage/JImageTest.java b/jdk/test/jdk/internal/jimage/JImageTest.java index 9d4817e4b85bf3dfdf2b8bb1b0531bf334225873..51c0bfdbf57215ed4196f6d6e2853697273c563d 100644 --- a/jdk/test/jdk/internal/jimage/JImageTest.java +++ b/jdk/test/jdk/internal/jimage/JImageTest.java @@ -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; - - static class Tool { - private int exit = 0; - private String out = ""; - private String err = ""; + private static void jimage(String... jimageArgs) throws Exception { + ArrayList args = new ArrayList<>(); + args.add("-ms8m"); + args.add("jdk.tools.jimage.Main"); + args.addAll(Arrays.asList(jimageArgs)); - 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()); - } - }); - } + ProcessBuilder builder = ProcessTools.createJavaProcessBuilder(args.toArray(new String[args.size()])); + int res = builder.inheritIO().start().waitFor(); - 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 @@ public class JImageTest { 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"); } + } }