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/*
5254 */
5355public class AppContentTest {
5456
55- private static final String TEST_JAVA = TKit .TEST_SRC_ROOT .resolve (
56- "apps/PrintEnv.java" ).toString ();
57- private static final String TEST_DUKE = TKit .TEST_SRC_ROOT .resolve (
58- "apps/dukeplug.png" ).toString ();
59- private static final String TEST_DIR = TKit .TEST_SRC_ROOT .resolve (
60- "apps" ).toString ();
61- private static final String TEST_BAD = TKit .TEST_SRC_ROOT .resolve (
62- "non-existant" ).toString ();
57+ private static final String TEST_JAVA = "apps/PrintEnv.java" ;
58+ private static final String TEST_DUKE = "apps/dukeplug.png" ;
59+ private static final String TEST_DIR = "apps" ;
60+ private static final String TEST_BAD = "non-existant" ;
61+
62+ // On OSX `--app-content` paths will be copied into the "Contents" folder
63+ // of the output app image.
64+ // "codesign" imposes restrictions on the directory structure of "Contents" folder.
65+ // In particular, random files should be placed in "Contents/Resources" folder
66+ // otherwise "codesign" will fail to sign.
67+ // Need to prepare arguments for `--app-content` accordingly.
68+ private final static boolean copyInResources = TKit .isOSX ();
6369
6470 private final List <String > testPathArgs ;
6571
@@ -79,37 +85,90 @@ public AppContentTest(String... testPathArgs) {
7985
8086 @ Test
8187 public void test () throws Exception {
82-
83- // On macOS signing may or may not work for modified app bundles.
84- // It works on macOS 15 and up, but fails on macOS below 15.
8588 final int expectedJPackageExitCode ;
86- final boolean isMacOS15 = (OSVersion .current ().compareTo (
87- new OSVersion (15 , 0 , 0 )) > 0 );
88- if (testPathArgs .contains (TEST_BAD ) || (TKit .isOSX () && !isMacOS15 )) {
89+ if (testPathArgs .contains (TEST_BAD )) {
8990 expectedJPackageExitCode = 1 ;
9091 } else {
9192 expectedJPackageExitCode = 0 ;
9293 }
9394
95+ var appContentInitializer = new AppContentInitializer (testPathArgs );
96+
9497 new PackageTest ().configureHelloApp ()
95- .addInitializer (cmd -> {
96- for (String arg : testPathArgs ) {
97- cmd .addArguments ("--app-content" , arg );
98- }
99- })
98+ .addRunOnceInitializer (appContentInitializer ::initAppContent )
99+ .addInitializer (appContentInitializer ::applyTo )
100100 .addInstallVerifier (cmd -> {
101- ApplicationLayout appLayout = cmd .appLayout ();
102- Path contentDir = appLayout .contentDirectory ();
101+ Path baseDir = getAppContentRoot (cmd );
103102 for (String arg : testPathArgs ) {
104103 List <String > paths = Arrays .asList (arg .split ("," ));
105104 for (String p : paths ) {
106105 Path name = Path .of (p ).getFileName ();
107- TKit .assertPathExists (contentDir .resolve (name ), true );
106+ TKit .assertPathExists (baseDir .resolve (name ), true );
108107 }
109108 }
110109
111110 })
112111 .setExpectedExitCode (expectedJPackageExitCode )
113112 .run ();
113+ }
114+
115+ private static Path getAppContentRoot (JPackageCommand cmd ) {
116+ Path contentDir = cmd .appLayout ().contentDirectory ();
117+ if (copyInResources ) {
118+ return contentDir .resolve ("Resources" );
119+ } else {
120+ return contentDir ;
114121 }
122+ }
123+
124+ private static final class AppContentInitializer {
125+ AppContentInitializer (List <String > appContentArgs ) {
126+ appContentPathGroups = appContentArgs .stream ().map (arg -> {
127+ return Stream .of (arg .split ("," )).map (Path ::of ).toList ();
128+ }).toList ();
129+ }
130+
131+ void initAppContent () {
132+ jpackageArgs = appContentPathGroups .stream ()
133+ .map (AppContentInitializer ::initAppContentPaths )
134+ .<String >mapMulti ((appContentPaths , consumer ) -> {
135+ consumer .accept ("--app-content" );
136+ consumer .accept (
137+ appContentPaths .stream ().map (Path ::toString ).collect (
138+ joining ("," )));
139+ }).toList ();
140+ }
141+
142+ void applyTo (JPackageCommand cmd ) {
143+ cmd .addArguments (jpackageArgs );
144+ }
145+
146+ private static Path copyAppContentPath (Path appContentPath ) throws IOException {
147+ var appContentArg = TKit .createTempDirectory ("app-content" ).resolve ("Resources" );
148+ var srcPath = TKit .TEST_SRC_ROOT .resolve (appContentPath );
149+ var dstPath = appContentArg .resolve (srcPath .getFileName ());
150+ Files .createDirectories (dstPath .getParent ());
151+ IOUtils .copyRecursive (srcPath , dstPath );
152+ return appContentArg ;
153+ }
154+
155+ private static List <Path > initAppContentPaths (List <Path > appContentPaths ) {
156+ if (copyInResources ) {
157+ return appContentPaths .stream ().map (appContentPath -> {
158+ if (appContentPath .endsWith (TEST_BAD )) {
159+ return appContentPath ;
160+ } else {
161+ return ThrowingFunction .toFunction (
162+ AppContentInitializer ::copyAppContentPath ).apply (
163+ appContentPath );
164+ }
165+ }).toList ();
166+ } else {
167+ return appContentPaths .stream ().map (TKit .TEST_SRC_ROOT ::resolve ).toList ();
168+ }
169+ }
170+
171+ private List <String > jpackageArgs ;
172+ private final List <List <Path >> appContentPathGroups ;
173+ }
115174}
0 commit comments