41
41
import java .util .regex .Pattern ;
42
42
import java .util .stream .Stream ;
43
43
import jdk .jpackage .internal .util .TokenReplace ;
44
- import jdk .jpackage .test .Annotations .Parameter ;
45
44
import jdk .jpackage .test .Annotations .ParameterSupplier ;
46
45
import jdk .jpackage .test .Annotations .Test ;
47
46
import jdk .jpackage .test .CannedFormattedString ;
@@ -119,13 +118,71 @@ private static String makeToken(String v) {
119
118
private final TokenReplace tokenReplace = new TokenReplace (token ());
120
119
}
121
120
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 ,
123
180
List <String > removeArgs , List <CannedFormattedString > expectedErrors ) {
124
181
125
182
static final class Builder {
126
183
127
184
Builder type (PackageType v ) {
128
- type = v ;
185
+ type = Optional . ofNullable ( v ). map ( PackageTypeSpec :: new ). orElse ( null ) ;
129
186
return this ;
130
187
}
131
188
@@ -134,7 +191,8 @@ Builder notype() {
134
191
}
135
192
136
193
Builder nativeType () {
137
- return type (NATIVE_TYPE );
194
+ type = PackageTypeSpec .NATIVE ;
195
+ return this ;
138
196
}
139
197
140
198
Builder appDesc (String v ) {
@@ -207,7 +265,8 @@ Builder error(String key, Object ... args) {
207
265
}
208
266
209
267
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 );
211
270
}
212
271
213
272
Builder unsupportedPlatformOption (String arg , String ... otherArgs ) {
@@ -219,7 +278,7 @@ TestSpec create() {
219
278
List .copyOf (addArgs ), List .copyOf (removeArgs ), List .copyOf (expectedErrors ));
220
279
}
221
280
222
- private PackageType type = PackageType .IMAGE ;
281
+ private PackageTypeSpec type = new PackageTypeSpec ( PackageType .IMAGE ) ;
223
282
private String appDesc = DEFAULT_APP_DESC ;
224
283
private List <String > addArgs = new ArrayList <>();
225
284
private List <String > removeArgs = new ArrayList <>();
@@ -242,9 +301,13 @@ void test() {
242
301
test (Map .of ());
243
302
}
244
303
304
+ boolean isSupported () {
305
+ return type .map (PackageTypeSpec ::isSupported ).orElse (true );
306
+ }
307
+
245
308
void test (Map <Token , Function <JPackageCommand , Object >> tokenValueSuppliers ) {
246
309
final var cmd = appDesc .map (JPackageCommand ::helloAppImage ).orElseGet (JPackageCommand ::new );
247
- type .ifPresent (cmd ::setPackageType );
310
+ type .map ( PackageTypeSpec :: resolvedType ). ifPresent (cmd ::setPackageType );
248
311
249
312
removeArgs .forEach (cmd ::removeArgumentWithValue );
250
313
cmd .addArguments (addArgs );
@@ -407,6 +470,7 @@ public static Collection<Object[]> invalidAppVersion() {
407
470
408
471
@ Test
409
472
@ ParameterSupplier ("basic" )
473
+ @ ParameterSupplier ("testRuntimeInstallerInvalidOptions" )
410
474
@ ParameterSupplier (value ="testWindows" , ifOS = WINDOWS )
411
475
@ ParameterSupplier (value ="testMac" , ifOS = MACOS )
412
476
@ ParameterSupplier (value ="testLinux" , ifOS = LINUX )
@@ -418,19 +482,27 @@ public static void test(TestSpec spec) {
418
482
spec .test ();
419
483
}
420
484
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
+ }));
434
506
}
435
507
436
508
@ Test
@@ -567,9 +639,7 @@ public static Collection<Object[]> testLinux() {
567
639
testSpec ().type (PackageType .LINUX_RPM ).addArgs ("--linux-package-name" , "#" )
568
640
.error ("error.rpm-invalid-value-for-package-name" , "#" )
569
641
.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 ());
573
643
574
644
return toTestArgs (testCases .stream ());
575
645
}
@@ -669,20 +739,14 @@ private static void defaultInit(JPackageCommand cmd, List<CannedFormattedString>
669
739
cmd .validateOutput (expectedErrors .toArray (CannedFormattedString []::new ));
670
740
}
671
741
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
-
684
742
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 -> {
686
750
return new Object [] {v };
687
751
}).toList ();
688
752
}
@@ -696,6 +760,4 @@ private static String adjustTextStreamVerifierArg(String str) {
696
760
}
697
761
698
762
private static final Pattern LINE_SEP_REGEXP = Pattern .compile ("\\ R" );
699
-
700
- private static final PackageType NATIVE_TYPE = defaultNativeType ();
701
763
}
0 commit comments