Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 37 additions & 28 deletions test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,9 @@
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;
import java.util.spi.ToolProvider;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -76,6 +78,10 @@ public Executor setToolProvider(JavaTool v) {
return setToolProvider(v.asToolProvider());
}

public Optional<Path> getExecutable() {
return Optional.ofNullable(executable);
}

public Executor setDirectory(Path v) {
directory = v;
return this;
Expand Down Expand Up @@ -173,10 +179,10 @@ public Executor dumpOutput(boolean v) {
return this;
}

public class Result {
public record Result(int exitCode, List<String> output, Supplier<String> cmdline) {

Result(int exitCode) {
this.exitCode = exitCode;
public Result {
Objects.requireNonNull(cmdline);
}

public String getFirstLineOfOutput() {
Expand All @@ -187,14 +193,10 @@ public List<String> 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;
}

Expand All @@ -205,9 +207,6 @@ public Result assertExitCodeIsZero() {
public int getExitCode() {
return exitCode;
}

final int exitCode;
private List<String> output;
}

public Result executeWithoutExitCodeCheck() {
Expand Down Expand Up @@ -367,28 +366,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<String> 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<String> output) {
return new Result(exitCode, output, 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() {
Expand All @@ -397,36 +402,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<String> 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<String> 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);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,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<JPackageCommand> {
public class JPackageCommand extends CommandArguments<JPackageCommand> {

public JPackageCommand() {
prerequisiteActions = new Actions();
Expand Down Expand Up @@ -733,7 +733,7 @@ public Executor.Result execute(int expectedExitCode) {
.createExecutor()
.execute(expectedExitCode);

if (result.exitCode == 0) {
if (result.exitCode() == 0) {
executeVerifyActions();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,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;
Expand All @@ -91,7 +91,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
Expand Down Expand Up @@ -353,7 +353,7 @@ private 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)
Expand Down