diff --git a/test/jdk/tools/jpackage/junit/share/jdk.jpackage/jdk/jpackage/internal/cli/MainTest.java b/test/jdk/tools/jpackage/junit/share/jdk.jpackage/jdk/jpackage/internal/cli/MainTest.java index 41142382c39..246b3722cbf 100644 --- a/test/jdk/tools/jpackage/junit/share/jdk.jpackage/jdk/jpackage/internal/cli/MainTest.java +++ b/test/jdk/tools/jpackage/junit/share/jdk.jpackage/jdk/jpackage/internal/cli/MainTest.java @@ -36,6 +36,8 @@ import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Objects; +import java.util.Optional; +import java.util.function.BiFunction; import java.util.stream.Stream; import jdk.internal.util.OperatingSystem; import jdk.jpackage.test.JUnitAdapter; @@ -58,7 +60,9 @@ public class MainTest extends JUnitAdapter.TestSrcInitializer { // Print the tool version build().args("--version").expectVersion(), // Print the tool version - build().args("foo", "bar").expectErrors(I18N.format("error.non-option-arguments", 2)), + // Additional error messages may be printed if the default bundling operation + // can not be identified; don't verify these errors in the output. + build().args("foo", "bar").stderrMatchType(OutputMatchType.STARTS_WITH).expectErrors(I18N.format("error.non-option-arguments", 2)), // Valid command line requesting to print the full help. build().args("-h").expectFullHelp(), // Valid command line requesting to build a package and print the full help. @@ -68,12 +72,14 @@ public class MainTest extends JUnitAdapter.TestSrcInitializer { // Valid command line requesting to print the full help and the version of the tool. build().args("--help", "--version").expectVersionWithHelp(), // Invalid command line requesting to print the version of the tool. - build().args("foo", "--version").expectErrors(I18N.format("error.non-option-arguments", 1)) + // Additional error messages may be printed if the default bundling operation + // can not be identified; don't verify these errors in the output. + build().args("foo", "--version").stderrMatchType(OutputMatchType.STARTS_WITH).expectErrors(I18N.format("error.non-option-arguments", 1)) ).map(TestSpec.Builder::create).toList(); } - record TestSpec(List args, int expectedExitCode, List expectedStdout, List expectedStderr) { + record TestSpec(List args, int expectedExitCode, ExpectedOutput expectedStdout, ExpectedOutput expectedStderr) { TestSpec { Objects.requireNonNull(args); @@ -84,15 +90,19 @@ public class MainTest extends JUnitAdapter.TestSrcInitializer { void run() { var result = ExecutionResult.create(args.toArray(String[]::new)); assertEquals(expectedExitCode, result.exitCode()); - assertEquals(expectedStdout, result.stdout()); - assertEquals(expectedStderr, result.stderr()); + expectedStdout.test(result.stdout()); + expectedStderr.test(result.stderr()); } static final class Builder { TestSpec create() { - return new TestSpec(args, expectedExitCode, expectedStdout, expectedStderr); + return new TestSpec( + args, + expectedExitCode, + new ExpectedOutput(expectedStdout, Optional.ofNullable(stdoutMatchType).orElse(OutputMatchType.EQUALS)), + new ExpectedOutput(expectedStderr, Optional.ofNullable(stderrMatchType).orElse(OutputMatchType.EQUALS))); } Builder args(String... v) { @@ -104,6 +114,16 @@ public class MainTest extends JUnitAdapter.TestSrcInitializer { return this; } + Builder stdoutMatchType(OutputMatchType v) { + stdoutMatchType = v; + return this; + } + + Builder stderrMatchType(OutputMatchType v) { + stderrMatchType = v; + return this; + } + Builder expectStdout(String... lines) { return expectStdout(List.of(lines)); } @@ -164,6 +184,8 @@ public class MainTest extends JUnitAdapter.TestSrcInitializer { private List args = new ArrayList<>(); private int expectedExitCode; + private OutputMatchType stdoutMatchType; + private OutputMatchType stderrMatchType; private List expectedStdout = new ArrayList<>(); private List expectedStderr = new ArrayList<>(); } @@ -188,6 +210,37 @@ public class MainTest extends JUnitAdapter.TestSrcInitializer { } + private enum OutputMatchType { + EQUALS((_, actual) -> actual), + STARTS_WITH((expected, actual) -> { + if (expected.size() < actual.size()) { + return actual.subList(0, expected.size()); + } + return actual; + }), + ; + + OutputMatchType(BiFunction, List, List> mapper) { + this.mapper = Objects.requireNonNull(mapper); + } + + private final BiFunction, List, List> mapper; + } + + + private record ExpectedOutput(List content, OutputMatchType type) { + ExpectedOutput { + Objects.requireNonNull(content); + Objects.requireNonNull(type); + } + + void test(List lines) { + var filteredLines = type.mapper.apply(content, lines); + assertEquals(content, filteredLines); + } + } + + private static TestSpec.Builder build() { return new TestSpec.Builder(); }