2121 * questions.
2222 */
2323
24- import java .nio . file . Path ;
24+ import java .io . IOException ;
2525import java .nio .file .Files ;
26- import jdk . jpackage . internal . ApplicationLayout ;
26+ import java . nio . file . Path ;
2727import jdk .jpackage .test .PackageTest ;
28- import jdk .jpackage .test .PackageType ;
2928import jdk .jpackage .test .TKit ;
3029import jdk .jpackage .test .Annotations .Test ;
31- import jdk .jpackage .test .Annotations .Parameter ;
3230import jdk .jpackage .test .Annotations .Parameters ;
3331import java .util .Arrays ;
3432import java .util .Collection ;
3533import java .util .List ;
34+ import static java .util .stream .Collectors .joining ;
35+ import java .util .stream .Stream ;
36+ import jdk .jpackage .internal .IOUtils ;
37+ import jdk .jpackage .test .Functional .ThrowingFunction ;
38+ import jdk .jpackage .test .JPackageCommand ;
3639
37- import jdk .internal .util .OSVersion ;
3840
3941/**
40- * Tests generation of packages with input folder containing empty folders .
42+ * Tests generation of packages with additional content in app image .
4143 */
4244
4345/*
5557 */
5658public class AppContentTest {
5759
58- private static final String TEST_JAVA = TKit .TEST_SRC_ROOT .resolve (
59- "apps/PrintEnv.java" ).toString ();
60- private static final String TEST_DUKE = TKit .TEST_SRC_ROOT .resolve (
61- "apps/dukeplug.png" ).toString ();
62- private static final String TEST_DIR = TKit .TEST_SRC_ROOT .resolve (
63- "apps" ).toString ();
64- private static final String TEST_BAD = TKit .TEST_SRC_ROOT .resolve (
65- "non-existant" ).toString ();
60+ private static final String TEST_JAVA = "apps/PrintEnv.java" ;
61+ private static final String TEST_DUKE = "apps/dukeplug.png" ;
62+ private static final String TEST_DIR = "apps" ;
63+ private static final String TEST_BAD = "non-existant" ;
64+
65+ // On OSX `--app-content` paths will be copied into the "Contents" folder
66+ // of the output app image.
67+ // "codesign" imposes restrictions on the directory structure of "Contents" folder.
68+ // In particular, random files should be placed in "Contents/Resources" folder
69+ // otherwise "codesign" will fail to sign.
70+ // Need to prepare arguments for `--app-content` accordingly.
71+ private final static boolean copyInResources = TKit .isOSX ();
6672
6773 private final List <String > testPathArgs ;
6874
@@ -82,37 +88,90 @@ public AppContentTest(String... testPathArgs) {
8288
8389 @ Test
8490 public void test () throws Exception {
85-
86- // On macOS signing may or may not work for modified app bundles.
87- // It works on macOS 15 and up, but fails on macOS below 15.
8891 final int expectedJPackageExitCode ;
89- final boolean isMacOS15 = (OSVersion .current ().compareTo (
90- new OSVersion (15 , 0 , 0 )) > 0 );
91- if (testPathArgs .contains (TEST_BAD ) || (TKit .isOSX () && !isMacOS15 )) {
92+ if (testPathArgs .contains (TEST_BAD )) {
9293 expectedJPackageExitCode = 1 ;
9394 } else {
9495 expectedJPackageExitCode = 0 ;
9596 }
9697
98+ var appContentInitializer = new AppContentInitializer (testPathArgs );
99+
97100 new PackageTest ().configureHelloApp ()
98- .addInitializer (cmd -> {
99- for (String arg : testPathArgs ) {
100- cmd .addArguments ("--app-content" , arg );
101- }
102- })
101+ .addRunOnceInitializer (appContentInitializer ::initAppContent )
102+ .addInitializer (appContentInitializer ::applyTo )
103103 .addInstallVerifier (cmd -> {
104- ApplicationLayout appLayout = cmd .appLayout ();
105- Path contentDir = appLayout .contentDirectory ();
104+ Path baseDir = getAppContentRoot (cmd );
106105 for (String arg : testPathArgs ) {
107106 List <String > paths = Arrays .asList (arg .split ("," ));
108107 for (String p : paths ) {
109108 Path name = Path .of (p ).getFileName ();
110- TKit .assertPathExists (contentDir .resolve (name ), true );
109+ TKit .assertPathExists (baseDir .resolve (name ), true );
111110 }
112111 }
113112
114113 })
115114 .setExpectedExitCode (expectedJPackageExitCode )
116115 .run ();
116+ }
117+
118+ private static Path getAppContentRoot (JPackageCommand cmd ) {
119+ Path contentDir = cmd .appLayout ().contentDirectory ();
120+ if (copyInResources ) {
121+ return contentDir .resolve ("Resources" );
122+ } else {
123+ return contentDir ;
117124 }
125+ }
126+
127+ private static final class AppContentInitializer {
128+ AppContentInitializer (List <String > appContentArgs ) {
129+ appContentPathGroups = appContentArgs .stream ().map (arg -> {
130+ return Stream .of (arg .split ("," )).map (Path ::of ).toList ();
131+ }).toList ();
132+ }
133+
134+ void initAppContent () {
135+ jpackageArgs = appContentPathGroups .stream ()
136+ .map (AppContentInitializer ::initAppContentPaths )
137+ .<String >mapMulti ((appContentPaths , consumer ) -> {
138+ consumer .accept ("--app-content" );
139+ consumer .accept (
140+ appContentPaths .stream ().map (Path ::toString ).collect (
141+ joining ("," )));
142+ }).toList ();
143+ }
144+
145+ void applyTo (JPackageCommand cmd ) {
146+ cmd .addArguments (jpackageArgs );
147+ }
148+
149+ private static Path copyAppContentPath (Path appContentPath ) throws IOException {
150+ var appContentArg = TKit .createTempDirectory ("app-content" ).resolve ("Resources" );
151+ var srcPath = TKit .TEST_SRC_ROOT .resolve (appContentPath );
152+ var dstPath = appContentArg .resolve (srcPath .getFileName ());
153+ Files .createDirectories (dstPath .getParent ());
154+ IOUtils .copyRecursive (srcPath , dstPath );
155+ return appContentArg ;
156+ }
157+
158+ private static List <Path > initAppContentPaths (List <Path > appContentPaths ) {
159+ if (copyInResources ) {
160+ return appContentPaths .stream ().map (appContentPath -> {
161+ if (appContentPath .endsWith (TEST_BAD )) {
162+ return appContentPath ;
163+ } else {
164+ return ThrowingFunction .toFunction (
165+ AppContentInitializer ::copyAppContentPath ).apply (
166+ appContentPath );
167+ }
168+ }).toList ();
169+ } else {
170+ return appContentPaths .stream ().map (TKit .TEST_SRC_ROOT ::resolve ).toList ();
171+ }
172+ }
173+
174+ private List <String > jpackageArgs ;
175+ private final List <List <Path >> appContentPathGroups ;
176+ }
118177}
0 commit comments