Skip to content

Commit 3487f8c

Browse files
author
Alexey Semenyuk
committed
8350102: Decouple jpackage test-lib Executor.Result and Executor classes
Reviewed-by: almatvee
1 parent 70a6c0b commit 3487f8c

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;
@@ -76,6 +77,10 @@ public Executor setToolProvider(JavaTool v) {
7677
return setToolProvider(v.asToolProvider());
7778
}
7879

80+
public Optional<Path> getExecutable() {
81+
return Optional.ofNullable(executable);
82+
}
83+
7984
public Executor setDirectory(Path v) {
8085
directory = v;
8186
return this;
@@ -173,10 +178,10 @@ public Executor dumpOutput(boolean v) {
173178
return this;
174179
}
175180

176-
public class Result {
181+
public record Result(int exitCode, List<String> output, Supplier<String> cmdline) {
177182

178-
Result(int exitCode) {
179-
this.exitCode = exitCode;
183+
public Result {
184+
Objects.requireNonNull(cmdline);
180185
}
181186

182187
public String getFirstLineOfOutput() {
@@ -187,14 +192,10 @@ public List<String> getOutput() {
187192
return output;
188193
}
189194

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

@@ -205,9 +206,6 @@ public Result assertExitCodeIsZero() {
205206
public int getExitCode() {
206207
return exitCode;
207208
}
208-
209-
final int exitCode;
210-
private List<String> output;
211209
}
212210

213211
public Result executeWithoutExitCodeCheck() {
@@ -408,28 +406,34 @@ private Result runExecutable() throws IOException, InterruptedException {
408406
}
409407
}
410408

411-
Result reply = new Result(process.waitFor());
412-
trace("Done. Exit code: " + reply.exitCode);
409+
final int exitCode = process.waitFor();
410+
trace("Done. Exit code: " + exitCode);
413411

412+
final List<String> output;
414413
if (outputLines != null) {
415-
reply.output = Collections.unmodifiableList(outputLines);
414+
output = Collections.unmodifiableList(outputLines);
415+
} else {
416+
output = null;
416417
}
417-
return reply;
418+
return createResult(exitCode, output);
418419
}
419420

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

429+
private Result createResult(int exitCode, List<String> output) {
430+
return new Result(exitCode, output, this::getPrintableCommandLine);
431+
}
428432

429433
private Result runToolProvider() throws IOException {
430434
if (!withSavedOutput()) {
431435
if (saveOutputType.contains(SaveOutputType.DUMP)) {
432-
return runToolProvider(System.out, System.err);
436+
return createResult(runToolProvider(System.out, System.err), null);
433437
}
434438

435439
PrintStream nullPrintStream = new PrintStream(new OutputStream() {
@@ -438,36 +442,40 @@ public void write(int b) {
438442
// Nop
439443
}
440444
});
441-
return runToolProvider(nullPrintStream, nullPrintStream);
445+
return createResult(runToolProvider(nullPrintStream, nullPrintStream), null);
442446
}
443447

444448
try (ByteArrayOutputStream buf = new ByteArrayOutputStream();
445449
PrintStream ps = new PrintStream(buf)) {
446-
Result reply = runToolProvider(ps, ps);
450+
final var exitCode = runToolProvider(ps, ps);
447451
ps.flush();
452+
final List<String> output;
448453
try (BufferedReader bufReader = new BufferedReader(new StringReader(
449454
buf.toString()))) {
450455
if (saveOutputType.contains(SaveOutputType.FIRST_LINE)) {
451456
String firstLine = bufReader.lines().findFirst().orElse(null);
452457
if (firstLine != null) {
453-
reply.output = List.of(firstLine);
458+
output = List.of(firstLine);
459+
} else {
460+
output = null;
454461
}
455462
} else if (saveOutputType.contains(SaveOutputType.FULL)) {
456-
reply.output = bufReader.lines().collect(
457-
Collectors.toUnmodifiableList());
463+
output = bufReader.lines().collect(Collectors.toUnmodifiableList());
464+
} else {
465+
output = null;
458466
}
459467

460468
if (saveOutputType.contains(SaveOutputType.DUMP)) {
461469
Stream<String> lines;
462470
if (saveOutputType.contains(SaveOutputType.FULL)) {
463-
lines = reply.output.stream();
471+
lines = output.stream();
464472
} else {
465473
lines = bufReader.lines();
466474
}
467475
lines.forEach(System.out::println);
468476
}
469477
}
470-
return reply;
478+
return createResult(exitCode, output);
471479
}
472480
}
473481

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

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

6161
public JPackageCommand() {
6262
prerequisiteActions = new Actions();
@@ -799,7 +799,7 @@ public Executor.Result execute(int expectedExitCode) {
799799
outputValidator.accept(result.getOutput().stream());
800800
}
801801

802-
if (result.exitCode == 0) {
802+
if (result.exitCode() == 0) {
803803
executeVerifyActions();
804804
}
805805

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

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

75-
if (result.exitCode == 1605) {
75+
if (result.exitCode() == 1605) {
7676
// ERROR_UNKNOWN_PRODUCT, attempt to uninstall not installed
7777
// package
7878
return;
@@ -81,7 +81,7 @@ private static void runMsiexecWithRetries(Executor misexec) {
8181
// The given Executor may either be of an msiexec command or an
8282
// unpack.bat script containing the msiexec command. In the later
8383
// case, when misexec returns 1618, the unpack.bat may return 1603
84-
if ((result.exitCode == 1618) || (result.exitCode == 1603)) {
84+
if ((result.exitCode() == 1618) || (result.exitCode() == 1603)) {
8585
// Another installation is already in progress.
8686
// Wait a little and try again.
8787
Long timeout = 1000L * (attempt + 3); // from 3 to 10 seconds
@@ -523,7 +523,7 @@ static String queryRegistryValue(String keyPath, String valueName) {
523523
var status = Executor.of("reg", "query", keyPath, "/v", valueName)
524524
.saveOutput()
525525
.executeWithoutExitCodeCheck();
526-
if (status.exitCode == 1) {
526+
if (status.exitCode() == 1) {
527527
// Should be the case of no such registry value or key
528528
String lookupString = "ERROR: The system was unable to find the specified registry key or value.";
529529
TKit.assertTextStream(lookupString)

0 commit comments

Comments
 (0)