Skip to content

Commit 4bd783f

Browse files
committed
backport 3487f8cbd55b06d332d897a010ae8eb371dd4956
1 parent 049f629 commit 4bd783f

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.regex.Pattern;
4142
import java.util.spi.ToolProvider;
@@ -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() {
@@ -367,28 +365,34 @@ private Result runExecutable() throws IOException, InterruptedException {
367365
}
368366
}
369367

370-
Result reply = new Result(process.waitFor());
371-
trace("Done. Exit code: " + reply.exitCode);
368+
final int exitCode = process.waitFor();
369+
trace("Done. Exit code: " + exitCode);
372370

371+
final List<String> output;
373372
if (outputLines != null) {
374-
reply.output = Collections.unmodifiableList(outputLines);
373+
output = Collections.unmodifiableList(outputLines);
374+
} else {
375+
output = null;
375376
}
376-
return reply;
377+
return createResult(exitCode, output);
377378
}
378379

379-
private Result runToolProvider(PrintStream out, PrintStream err) {
380+
private int runToolProvider(PrintStream out, PrintStream err) {
380381
trace("Execute " + getPrintableCommandLine() + "...");
381-
Result reply = new Result(toolProvider.run(out, err, args.toArray(
382-
String[]::new)));
383-
trace("Done. Exit code: " + reply.exitCode);
384-
return reply;
382+
final int exitCode = toolProvider.run(out, err, args.toArray(
383+
String[]::new));
384+
trace("Done. Exit code: " + exitCode);
385+
return exitCode;
385386
}
386387

388+
private Result createResult(int exitCode, List<String> output) {
389+
return new Result(exitCode, output, this::getPrintableCommandLine);
390+
}
387391

388392
private Result runToolProvider() throws IOException {
389393
if (!withSavedOutput()) {
390394
if (saveOutputType.contains(SaveOutputType.DUMP)) {
391-
return runToolProvider(System.out, System.err);
395+
return createResult(runToolProvider(System.out, System.err), null);
392396
}
393397

394398
PrintStream nullPrintStream = new PrintStream(new OutputStream() {
@@ -397,36 +401,40 @@ public void write(int b) {
397401
// Nop
398402
}
399403
});
400-
return runToolProvider(nullPrintStream, nullPrintStream);
404+
return createResult(runToolProvider(nullPrintStream, nullPrintStream), null);
401405
}
402406

403407
try (ByteArrayOutputStream buf = new ByteArrayOutputStream();
404408
PrintStream ps = new PrintStream(buf)) {
405-
Result reply = runToolProvider(ps, ps);
409+
final var exitCode = runToolProvider(ps, ps);
406410
ps.flush();
411+
final List<String> output;
407412
try (BufferedReader bufReader = new BufferedReader(new StringReader(
408413
buf.toString()))) {
409414
if (saveOutputType.contains(SaveOutputType.FIRST_LINE)) {
410415
String firstLine = bufReader.lines().findFirst().orElse(null);
411416
if (firstLine != null) {
412-
reply.output = List.of(firstLine);
417+
output = List.of(firstLine);
418+
} else {
419+
output = null;
413420
}
414421
} else if (saveOutputType.contains(SaveOutputType.FULL)) {
415-
reply.output = bufReader.lines().collect(
416-
Collectors.toUnmodifiableList());
422+
output = bufReader.lines().collect(Collectors.toUnmodifiableList());
423+
} else {
424+
output = null;
417425
}
418426

419427
if (saveOutputType.contains(SaveOutputType.DUMP)) {
420428
Stream<String> lines;
421429
if (saveOutputType.contains(SaveOutputType.FULL)) {
422-
lines = reply.output.stream();
430+
lines = output.stream();
423431
} else {
424432
lines = bufReader.lines();
425433
}
426434
lines.forEach(System.out::println);
427435
}
428436
}
429-
return reply;
437+
return createResult(exitCode, output);
430438
}
431439
}
432440

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
@@ -82,7 +82,7 @@ private static void runMsiexecWithRetries(Executor misexec) {
8282
for (int attempt = 0; attempt < 8; ++attempt) {
8383
result = misexec.executeWithoutExitCodeCheck();
8484

85-
if (result.exitCode == 1605) {
85+
if (result.exitCode() == 1605) {
8686
// ERROR_UNKNOWN_PRODUCT, attempt to uninstall not installed
8787
// package
8888
return;
@@ -91,7 +91,7 @@ private static void runMsiexecWithRetries(Executor misexec) {
9191
// The given Executor may either be of an msiexec command or an
9292
// unpack.bat script containing the msiexec command. In the later
9393
// case, when misexec returns 1618, the unpack.bat may return 1603
94-
if ((result.exitCode == 1618) || (result.exitCode == 1603)) {
94+
if ((result.exitCode() == 1618) || (result.exitCode() == 1603)) {
9595
// Another installation is already in progress.
9696
// Wait a little and try again.
9797
Long timeout = 1000L * (attempt + 3); // from 3 to 10 seconds
@@ -353,7 +353,7 @@ private static String queryRegistryValue(String keyPath, String valueName) {
353353
var status = Executor.of("reg", "query", keyPath, "/v", valueName)
354354
.saveOutput()
355355
.executeWithoutExitCodeCheck();
356-
if (status.exitCode == 1) {
356+
if (status.exitCode() == 1) {
357357
// Should be the case of no such registry value or key
358358
String lookupString = "ERROR: The system was unable to find the specified registry key or value.";
359359
TKit.assertTextStream(lookupString)

0 commit comments

Comments
 (0)