From 1a5ca0699506747bb4bb15d107029e42fb01e5c7 Mon Sep 17 00:00:00 2001 From: Alexey Semenyuk Date: Mon, 10 Feb 2025 20:32:27 -0500 Subject: [PATCH 1/2] Decouple Executor.Result and Executor classes --- .../helpers/jdk/jpackage/test/Executor.java | 59 ++++++++++--------- .../jdk/jpackage/test/JPackageCommand.java | 4 +- .../jdk/jpackage/test/WindowsHelper.java | 6 +- 3 files changed, 36 insertions(+), 33 deletions(-) diff --git a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java index b00f72a328edd..ae5be16c087fb 100644 --- a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java +++ b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java @@ -173,10 +173,10 @@ public Executor dumpOutput(boolean v) { return this; } - public class Result { + public record Result(int exitCode, List output, Supplier cmdline) { - Result(int exitCode) { - this.exitCode = exitCode; + public Result { + Objects.requireNonNull(cmdline); } public String getFirstLineOfOutput() { @@ -187,14 +187,10 @@ public List getOutput() { return output; } - public String getPrintableCommandLine() { - return Executor.this.getPrintableCommandLine(); - } - public Result assertExitCodeIs(int expectedExitCode) { TKit.assertEquals(expectedExitCode, exitCode, String.format( "Check command %s exited with %d code", - getPrintableCommandLine(), expectedExitCode)); + cmdline.get(), expectedExitCode)); return this; } @@ -205,9 +201,6 @@ public Result assertExitCodeIsZero() { public int getExitCode() { return exitCode; } - - final int exitCode; - private List output; } public Result executeWithoutExitCodeCheck() { @@ -408,28 +401,34 @@ private Result runExecutable() throws IOException, InterruptedException { } } - Result reply = new Result(process.waitFor()); - trace("Done. Exit code: " + reply.exitCode); + final int exitCode = process.waitFor(); + trace("Done. Exit code: " + exitCode); + final List output; if (outputLines != null) { - reply.output = Collections.unmodifiableList(outputLines); + output = Collections.unmodifiableList(outputLines); + } else { + output = null; } - return reply; + return createResult(exitCode, output); } - private Result runToolProvider(PrintStream out, PrintStream err) { + private int runToolProvider(PrintStream out, PrintStream err) { trace("Execute " + getPrintableCommandLine() + "..."); - Result reply = new Result(toolProvider.run(out, err, args.toArray( - String[]::new))); - trace("Done. Exit code: " + reply.exitCode); - return reply; + final int exitCode = toolProvider.run(out, err, args.toArray( + String[]::new)); + trace("Done. Exit code: " + exitCode); + return exitCode; } + private Result createResult(int exitCode, List output) { + return new Result(exitCode, null, this::getPrintableCommandLine); + } private Result runToolProvider() throws IOException { if (!withSavedOutput()) { if (saveOutputType.contains(SaveOutputType.DUMP)) { - return runToolProvider(System.out, System.err); + return createResult(runToolProvider(System.out, System.err), null); } PrintStream nullPrintStream = new PrintStream(new OutputStream() { @@ -438,36 +437,40 @@ public void write(int b) { // Nop } }); - return runToolProvider(nullPrintStream, nullPrintStream); + return createResult(runToolProvider(nullPrintStream, nullPrintStream), null); } try (ByteArrayOutputStream buf = new ByteArrayOutputStream(); PrintStream ps = new PrintStream(buf)) { - Result reply = runToolProvider(ps, ps); + final var exitCode = runToolProvider(ps, ps); ps.flush(); + final List output; try (BufferedReader bufReader = new BufferedReader(new StringReader( buf.toString()))) { if (saveOutputType.contains(SaveOutputType.FIRST_LINE)) { String firstLine = bufReader.lines().findFirst().orElse(null); if (firstLine != null) { - reply.output = List.of(firstLine); + output = List.of(firstLine); + } else { + output = null; } } else if (saveOutputType.contains(SaveOutputType.FULL)) { - reply.output = bufReader.lines().collect( - Collectors.toUnmodifiableList()); + output = bufReader.lines().collect(Collectors.toUnmodifiableList()); + } else { + output = null; } if (saveOutputType.contains(SaveOutputType.DUMP)) { Stream lines; if (saveOutputType.contains(SaveOutputType.FULL)) { - lines = reply.output.stream(); + lines = output.stream(); } else { lines = bufReader.lines(); } lines.forEach(System.out::println); } } - return reply; + return createResult(exitCode, output); } } diff --git a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/JPackageCommand.java b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/JPackageCommand.java index bbf7d429f08cf..418d7377ec637 100644 --- a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/JPackageCommand.java +++ b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/JPackageCommand.java @@ -56,7 +56,7 @@ * anything. The simplest is to compile test application and pack in a jar for * use on jpackage command line. */ -public final class JPackageCommand extends CommandArguments { +public class JPackageCommand extends CommandArguments { public JPackageCommand() { prerequisiteActions = new Actions(); @@ -799,7 +799,7 @@ public Executor.Result execute(int expectedExitCode) { outputValidator.accept(result.getOutput().stream()); } - if (result.exitCode == 0) { + if (result.exitCode() == 0) { executeVerifyActions(); } diff --git a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/WindowsHelper.java b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/WindowsHelper.java index 9d289c565404c..48643463e0b56 100644 --- a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/WindowsHelper.java +++ b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/WindowsHelper.java @@ -72,7 +72,7 @@ private static void runMsiexecWithRetries(Executor misexec) { for (int attempt = 0; attempt < 8; ++attempt) { result = misexec.executeWithoutExitCodeCheck(); - if (result.exitCode == 1605) { + if (result.exitCode() == 1605) { // ERROR_UNKNOWN_PRODUCT, attempt to uninstall not installed // package return; @@ -81,7 +81,7 @@ private static void runMsiexecWithRetries(Executor misexec) { // The given Executor may either be of an msiexec command or an // unpack.bat script containing the msiexec command. In the later // case, when misexec returns 1618, the unpack.bat may return 1603 - if ((result.exitCode == 1618) || (result.exitCode == 1603)) { + if ((result.exitCode() == 1618) || (result.exitCode() == 1603)) { // Another installation is already in progress. // Wait a little and try again. Long timeout = 1000L * (attempt + 3); // from 3 to 10 seconds @@ -523,7 +523,7 @@ static String queryRegistryValue(String keyPath, String valueName) { var status = Executor.of("reg", "query", keyPath, "/v", valueName) .saveOutput() .executeWithoutExitCodeCheck(); - if (status.exitCode == 1) { + if (status.exitCode() == 1) { // Should be the case of no such registry value or key String lookupString = "ERROR: The system was unable to find the specified registry key or value."; TKit.assertTextStream(lookupString) From 4037ef280fb941e9963cb219f33365f80127d323 Mon Sep 17 00:00:00 2001 From: Alexey Semenyuk Date: Tue, 11 Feb 2025 20:22:47 -0500 Subject: [PATCH 2/2] Bugfix Executor.createResult() and add Executor.getExecutable() --- .../tools/jpackage/helpers/jdk/jpackage/test/Executor.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java index ae5be16c087fb..a8a849cb9e786 100644 --- a/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java +++ b/test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java @@ -36,6 +36,7 @@ import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.Optional; import java.util.Set; import java.util.function.Supplier; import java.util.regex.Pattern; @@ -76,6 +77,10 @@ public Executor setToolProvider(JavaTool v) { return setToolProvider(v.asToolProvider()); } + public Optional getExecutable() { + return Optional.ofNullable(executable); + } + public Executor setDirectory(Path v) { directory = v; return this; @@ -422,7 +427,7 @@ private int runToolProvider(PrintStream out, PrintStream err) { } private Result createResult(int exitCode, List output) { - return new Result(exitCode, null, this::getPrintableCommandLine); + return new Result(exitCode, output, this::getPrintableCommandLine); } private Result runToolProvider() throws IOException {