4141import java .util .regex .Pattern ;
4242import java .util .stream .Stream ;
4343import jdk .jpackage .internal .util .TokenReplace ;
44- import jdk .jpackage .test .Annotations .Parameter ;
4544import jdk .jpackage .test .Annotations .ParameterSupplier ;
4645import jdk .jpackage .test .Annotations .Test ;
4746import jdk .jpackage .test .CannedFormattedString ;
@@ -119,13 +118,71 @@ private static String makeToken(String v) {
119118 private final TokenReplace tokenReplace = new TokenReplace (token ());
120119 }
121120
122- public record TestSpec (Optional <PackageType > type , Optional <String > appDesc , List <String > addArgs ,
121+ record PackageTypeSpec (Optional <PackageType > type , boolean anyNativeType ) implements CannedFormattedString .CannedArgument {
122+ PackageTypeSpec {
123+ Objects .requireNonNull (type );
124+ if (type .isPresent () && anyNativeType ) {
125+ throw new IllegalArgumentException ();
126+ }
127+ }
128+
129+ PackageTypeSpec (PackageType type ) {
130+ this (Optional .of (type ), false );
131+ }
132+
133+ boolean isSupported () {
134+ if (anyNativeType ) {
135+ return NATIVE_TYPE .isPresent ();
136+ } else {
137+ return type .orElseThrow ().isSupported ();
138+ }
139+ }
140+
141+ PackageType resolvedType () {
142+ return type .or (() -> NATIVE_TYPE ).orElseThrow (PackageType ::throwSkippedExceptionIfNativePackagingUnavailable );
143+ }
144+
145+ @ Override
146+ public String value () {
147+ return resolvedType ().getType ();
148+ }
149+
150+ @ Override
151+ public final String toString () {
152+ if (anyNativeType ) {
153+ return "NATIVE" ;
154+ } else {
155+ return type .orElseThrow ().toString ();
156+ }
157+ }
158+
159+ private static Optional <PackageType > defaultNativeType () {
160+ final Collection <PackageType > nativeTypes ;
161+ if (TKit .isLinux ()) {
162+ nativeTypes = PackageType .LINUX ;
163+ } else if (TKit .isOSX ()) {
164+ nativeTypes = PackageType .MAC ;
165+ } else if (TKit .isWindows ()) {
166+ nativeTypes = List .of (PackageType .WIN_MSI );
167+ } else {
168+ throw TKit .throwUnknownPlatformError ();
169+ }
170+
171+ return nativeTypes .stream ().filter (PackageType ::isSupported ).findFirst ();
172+ }
173+
174+ static final PackageTypeSpec NATIVE = new PackageTypeSpec (Optional .empty (), true );
175+
176+ private static final Optional <PackageType > NATIVE_TYPE = defaultNativeType ();
177+ }
178+
179+ public record TestSpec (Optional <PackageTypeSpec > type , Optional <String > appDesc , List <String > addArgs ,
123180 List <String > removeArgs , List <CannedFormattedString > expectedErrors ) {
124181
125182 static final class Builder {
126183
127184 Builder type (PackageType v ) {
128- type = v ;
185+ type = Optional . ofNullable ( v ). map ( PackageTypeSpec :: new ). orElse ( null ) ;
129186 return this ;
130187 }
131188
@@ -134,7 +191,8 @@ Builder notype() {
134191 }
135192
136193 Builder nativeType () {
137- return type (NATIVE_TYPE );
194+ type = PackageTypeSpec .NATIVE ;
195+ return this ;
138196 }
139197
140198 Builder appDesc (String v ) {
@@ -207,7 +265,8 @@ Builder error(String key, Object ... args) {
207265 }
208266
209267 Builder invalidTypeArg (String arg , String ... otherArgs ) {
210- return addArgs (arg ).addArgs (otherArgs ).error ("ERR_InvalidTypeOption" , arg , type .getType ());
268+ Objects .requireNonNull (type );
269+ return addArgs (arg ).addArgs (otherArgs ).error ("ERR_InvalidTypeOption" , arg , type );
211270 }
212271
213272 Builder unsupportedPlatformOption (String arg , String ... otherArgs ) {
@@ -219,7 +278,7 @@ TestSpec create() {
219278 List .copyOf (addArgs ), List .copyOf (removeArgs ), List .copyOf (expectedErrors ));
220279 }
221280
222- private PackageType type = PackageType .IMAGE ;
281+ private PackageTypeSpec type = new PackageTypeSpec ( PackageType .IMAGE ) ;
223282 private String appDesc = DEFAULT_APP_DESC ;
224283 private List <String > addArgs = new ArrayList <>();
225284 private List <String > removeArgs = new ArrayList <>();
@@ -242,9 +301,13 @@ void test() {
242301 test (Map .of ());
243302 }
244303
304+ boolean isSupported () {
305+ return type .map (PackageTypeSpec ::isSupported ).orElse (true );
306+ }
307+
245308 void test (Map <Token , Function <JPackageCommand , Object >> tokenValueSuppliers ) {
246309 final var cmd = appDesc .map (JPackageCommand ::helloAppImage ).orElseGet (JPackageCommand ::new );
247- type .ifPresent (cmd ::setPackageType );
310+ type .map ( PackageTypeSpec :: resolvedType ). ifPresent (cmd ::setPackageType );
248311
249312 removeArgs .forEach (cmd ::removeArgumentWithValue );
250313 cmd .addArguments (addArgs );
@@ -407,6 +470,7 @@ public static Collection<Object[]> invalidAppVersion() {
407470
408471 @ Test
409472 @ ParameterSupplier ("basic" )
473+ @ ParameterSupplier ("testRuntimeInstallerInvalidOptions" )
410474 @ ParameterSupplier (value ="testWindows" , ifOS = WINDOWS )
411475 @ ParameterSupplier (value ="testMac" , ifOS = MACOS )
412476 @ ParameterSupplier (value ="testLinux" , ifOS = LINUX )
@@ -418,19 +482,27 @@ public static void test(TestSpec spec) {
418482 spec .test ();
419483 }
420484
421- @ Test
422- @ Parameter ({"--input" , "foo" })
423- @ Parameter ({"--module-path" , "dir" })
424- @ Parameter ({"--add-modules" , "java.base" })
425- @ Parameter ({"--main-class" , "Hello" })
426- @ Parameter ({"--arguments" , "foo" })
427- @ Parameter ({"--java-options" , "-Dfoo.bar=10" })
428- @ Parameter ({"--add-launcher" , "foo=foo.properties" })
429- @ Parameter ({"--app-content" , "dir" })
430- @ Parameter (value ="--win-console" , ifOS = WINDOWS )
431- public static void testRuntimeInstallerInvalidOptions (String ... args ) {
432- testSpec ().noAppDesc ().nativeType ().addArgs ("--runtime-image" , Token .JAVA_HOME .token ()).addArgs (args )
433- .error ("ERR_NoInstallerEntryPoint" , args [0 ]).create ().test ();
485+ public static Collection <Object []> testRuntimeInstallerInvalidOptions () {
486+ Stream <List <String >> argsStream = Stream .of (
487+ List .of ("--input" , "foo" ),
488+ List .of ("--module-path" , "dir" ),
489+ List .of ("--add-modules" , "java.base" ),
490+ List .of ("--main-class" , "Hello" ),
491+ List .of ("--arguments" , "foo" ),
492+ List .of ("--java-options" , "-Dfoo.bar=10" ),
493+ List .of ("--add-launcher" , "foo=foo.properties" ),
494+ List .of ("--app-content" , "dir" ));
495+
496+ if (TKit .isWindows ()) {
497+ argsStream = Stream .concat (argsStream , Stream .of (List .of ("--win-console" )));
498+ }
499+
500+ return fromTestSpecBuilders (argsStream .map (args -> {
501+ return testSpec ().noAppDesc ().nativeType ()
502+ .addArgs ("--runtime-image" , Token .JAVA_HOME .token ())
503+ .addArgs (args )
504+ .error ("ERR_NoInstallerEntryPoint" , args .getFirst ());
505+ }));
434506 }
435507
436508 @ Test
@@ -567,9 +639,7 @@ public static Collection<Object[]> testLinux() {
567639 testSpec ().type (PackageType .LINUX_RPM ).addArgs ("--linux-package-name" , "#" )
568640 .error ("error.rpm-invalid-value-for-package-name" , "#" )
569641 .error ("error.rpm-invalid-value-for-package-name.advice" )
570- ).map (TestSpec .Builder ::create ).filter (spec -> {
571- return spec .type ().orElseThrow ().isSupported ();
572- }).toList ());
642+ ).map (TestSpec .Builder ::create ).toList ());
573643
574644 return toTestArgs (testCases .stream ());
575645 }
@@ -669,20 +739,14 @@ private static void defaultInit(JPackageCommand cmd, List<CannedFormattedString>
669739 cmd .validateOutput (expectedErrors .toArray (CannedFormattedString []::new ));
670740 }
671741
672- private static PackageType defaultNativeType () {
673- if (TKit .isLinux ()) {
674- return PackageType .LINUX .stream ().filter (PackageType ::isSupported ).findFirst ().orElseThrow ();
675- } else if (TKit .isOSX ()) {
676- return PackageType .MAC_DMG ;
677- } else if (TKit .isWindows ()) {
678- return PackageType .WIN_MSI ;
679- } else {
680- throw new UnsupportedOperationException ();
681- }
682- }
683-
684742 private static <T > Collection <Object []> toTestArgs (Stream <T > stream ) {
685- return stream .map (v -> {
743+ return stream .filter (v -> {
744+ if (v instanceof TestSpec ts ) {
745+ return ts .isSupported ();
746+ } else {
747+ return true ;
748+ }
749+ }).map (v -> {
686750 return new Object [] {v };
687751 }).toList ();
688752 }
@@ -696,6 +760,4 @@ private static String adjustTextStreamVerifierArg(String str) {
696760 }
697761
698762 private static final Pattern LINE_SEP_REGEXP = Pattern .compile ("\\ R" );
699-
700- private static final PackageType NATIVE_TYPE = defaultNativeType ();
701763}
0 commit comments