Skip to content

Commit 571408e

Browse files
committed
8350102: Decouple jpackage test-lib Executor.Result and Executor classes
Backport-of: 3487f8cbd55b06d332d897a010ae8eb371dd4956
1 parent d167b4d commit 571408e

File tree

3 files changed

+41
-33
lines changed

3 files changed

+41
-33
lines changed

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/Executor.java

Lines changed: 36 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
import java.util.HashSet;
3737
import java.util.List;
3838
import java.util.Objects;
39+
import java.util.Optional;
3940
import java.util.Set;
4041
import java.util.function.Supplier;
4142
import java.util.regex.Pattern;
@@ -77,6 +78,10 @@ public Executor setToolProvider(JavaTool v) {
7778
return setToolProvider(v.asToolProvider());
7879
}
7980

81+
public Optional<Path> getExecutable() {
82+
return Optional.ofNullable(executable);
83+
}
84+
8085
public Executor setDirectory(Path v) {
8186
directory = v;
8287
return this;
@@ -174,10 +179,10 @@ public Executor dumpOutput(boolean v) {
174179
return this;
175180
}
176181

177-
public class Result {
182+
public record Result(int exitCode, List<String> output, Supplier<String> cmdline) {
178183

179-
Result(int exitCode) {
180-
this.exitCode = exitCode;
184+
public Result {
185+
Objects.requireNonNull(cmdline);
181186
}
182187

183188
public String getFirstLineOfOutput() {
@@ -188,14 +193,10 @@ public List<String> getOutput() {
188193
return output;
189194
}
190195

191-
public String getPrintableCommandLine() {
192-
return Executor.this.getPrintableCommandLine();
193-
}
194-
195196
public Result assertExitCodeIs(int expectedExitCode) {
196197
TKit.assertEquals(expectedExitCode, exitCode, String.format(
197198
"Check command %s exited with %d code",
198-
getPrintableCommandLine(), expectedExitCode));
199+
cmdline.get(), expectedExitCode));
199200
return this;
200201
}
201202

@@ -206,9 +207,6 @@ public Result assertExitCodeIsZero() {
206207
public int getExitCode() {
207208
return exitCode;
208209
}
209-
210-
final int exitCode;
211-
private List<String> output;
212210
}
213211

214212
public Result executeWithoutExitCodeCheck() {
@@ -406,28 +404,34 @@ private Result runExecutable() throws IOException, InterruptedException {
406404
}
407405
}
408406

409-
Result reply = new Result(process.waitFor());
410-
trace("Done. Exit code: " + reply.exitCode);
407+
final int exitCode = process.waitFor();
408+
trace("Done. Exit code: " + exitCode);
411409

410+
final List<String> output;
412411
if (outputLines != null) {
413-
reply.output = Collections.unmodifiableList(outputLines);
412+
output = Collections.unmodifiableList(outputLines);
413+
} else {
414+
output = null;
414415
}
415-
return reply;
416+
return createResult(exitCode, output);
416417
}
417418

418-
private Result runToolProvider(PrintStream out, PrintStream err) {
419+
private int runToolProvider(PrintStream out, PrintStream err) {
419420
trace("Execute " + getPrintableCommandLine() + "...");
420-
Result reply = new Result(toolProvider.run(out, err, args.toArray(
421-
String[]::new)));
422-
trace("Done. Exit code: " + reply.exitCode);
423-
return reply;
421+
final int exitCode = toolProvider.run(out, err, args.toArray(
422+
String[]::new));
423+
trace("Done. Exit code: " + exitCode);
424+
return exitCode;
424425
}
425426

427+
private Result createResult(int exitCode, List<String> output) {
428+
return new Result(exitCode, output, this::getPrintableCommandLine);
429+
}
426430

427431
private Result runToolProvider() throws IOException {
428432
if (!withSavedOutput()) {
429433
if (saveOutputType.contains(SaveOutputType.DUMP)) {
430-
return runToolProvider(System.out, System.err);
434+
return createResult(runToolProvider(System.out, System.err), null);
431435
}
432436

433437
PrintStream nullPrintStream = new PrintStream(new OutputStream() {
@@ -436,36 +440,40 @@ public void write(int b) {
436440
// Nop
437441
}
438442
});
439-
return runToolProvider(nullPrintStream, nullPrintStream);
443+
return createResult(runToolProvider(nullPrintStream, nullPrintStream), null);
440444
}
441445

442446
try (ByteArrayOutputStream buf = new ByteArrayOutputStream();
443447
PrintStream ps = new PrintStream(buf)) {
444-
Result reply = runToolProvider(ps, ps);
448+
final var exitCode = runToolProvider(ps, ps);
445449
ps.flush();
450+
final List<String> output;
446451
try (BufferedReader bufReader = new BufferedReader(new StringReader(
447452
buf.toString()))) {
448453
if (saveOutputType.contains(SaveOutputType.FIRST_LINE)) {
449454
String firstLine = bufReader.lines().findFirst().orElse(null);
450455
if (firstLine != null) {
451-
reply.output = List.of(firstLine);
456+
output = List.of(firstLine);
457+
} else {
458+
output = null;
452459
}
453460
} else if (saveOutputType.contains(SaveOutputType.FULL)) {
454-
reply.output = bufReader.lines().collect(
455-
Collectors.toUnmodifiableList());
461+
output = bufReader.lines().collect(Collectors.toUnmodifiableList());
462+
} else {
463+
output = null;
456464
}
457465

458466
if (saveOutputType.contains(SaveOutputType.DUMP)) {
459467
Stream<String> lines;
460468
if (saveOutputType.contains(SaveOutputType.FULL)) {
461-
lines = reply.output.stream();
469+
lines = output.stream();
462470
} else {
463471
lines = bufReader.lines();
464472
}
465473
lines.forEach(System.out::println);
466474
}
467475
}
468-
return reply;
476+
return createResult(exitCode, output);
469477
}
470478
}
471479

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/JPackageCommand.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@
5757
* anything. The simplest is to compile test application and pack in a jar for
5858
* use on jpackage command line.
5959
*/
60-
public final class JPackageCommand extends CommandArguments<JPackageCommand> {
60+
public class JPackageCommand extends CommandArguments<JPackageCommand> {
6161

6262
public JPackageCommand() {
6363
prerequisiteActions = new Actions();
@@ -733,7 +733,7 @@ public Executor.Result execute(int expectedExitCode) {
733733
.createExecutor()
734734
.execute(expectedExitCode);
735735

736-
if (result.exitCode == 0) {
736+
if (result.exitCode() == 0) {
737737
executeVerifyActions();
738738
}
739739

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/WindowsHelper.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ private static void runMsiexecWithRetries(Executor misexec) {
6666
for (int attempt = 0; attempt < 8; ++attempt) {
6767
result = misexec.executeWithoutExitCodeCheck();
6868

69-
if (result.exitCode == 1605) {
69+
if (result.exitCode() == 1605) {
7070
// ERROR_UNKNOWN_PRODUCT, attempt to uninstall not installed
7171
// package
7272
return;
@@ -75,7 +75,7 @@ private static void runMsiexecWithRetries(Executor misexec) {
7575
// The given Executor may either be of an msiexec command or an
7676
// unpack.bat script containing the msiexec command. In the later
7777
// case, when misexec returns 1618, the unpack.bat may return 1603
78-
if ((result.exitCode == 1618) || (result.exitCode == 1603)) {
78+
if ((result.exitCode() == 1618) || (result.exitCode() == 1603)) {
7979
// Another installation is already in progress.
8080
// Wait a little and try again.
8181
Long timeout = 1000L * (attempt + 3); // from 3 to 10 seconds
@@ -337,7 +337,7 @@ private static String queryRegistryValue(String keyPath, String valueName) {
337337
var status = Executor.of("reg", "query", keyPath, "/v", valueName)
338338
.saveOutput()
339339
.executeWithoutExitCodeCheck();
340-
if (status.exitCode == 1) {
340+
if (status.exitCode() == 1) {
341341
// Should be the case of no such registry value or key
342342
String lookupString = "ERROR: The system was unable to find the specified registry key or value.";
343343
TKit.assertTextStream(lookupString)

0 commit comments

Comments
 (0)