Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -90,11 +90,13 @@ public static void writeArray(XMLStreamWriter xml, XmlConsumer content)

public static void writePList(XMLStreamWriter xml, XmlConsumer content)
throws XMLStreamException, IOException {
xml.writeDTD("plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"https://www.apple.com/DTDs/PropertyList-1.0.dtd\"");
xml.writeStartDocument();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do we need writeStartDocument()/writeEndDocument() calls?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To write XML declaration to Info.plist file. Without it reading PList file fails.

xml.writeDTD("<!DOCTYPE plist PUBLIC \"-//Apple//DTD PLIST 1.0//EN\" \"https://www.apple.com/DTDs/PropertyList-1.0.dtd\">");
xml.writeStartElement("plist");
xml.writeAttribute("version", "1.0");
content.accept(xml);
xml.writeEndElement();
xml.writeEndDocument();
}

public static void writeKey(XMLStreamWriter xml, String key)
Expand Down
27 changes: 27 additions & 0 deletions test/jdk/tools/jpackage/helpers/jdk/jpackage/test/MacHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,10 @@ public static PListReader readPListFromAppImage(Path appImage) {
return readPList(appImage.resolve("Contents/Info.plist"));
}

public static PListReader readPListFromEmbeddedRuntime(Path appImage) {
return readPList(appImage.resolve("Contents/runtime/Contents/Info.plist"));
}

public static PListReader readPList(Path path) {
TKit.assertReadableFileExists(path);
return ThrowingSupplier.toSupplier(() -> readPList(Files.readAllLines(
Expand Down Expand Up @@ -171,6 +175,29 @@ public static boolean appImageSigned(JPackageCommand cmd) {
return (cmd.hasArgument("--mac-signing-key-user-name") || cmd.hasArgument("--mac-app-image-sign-identity"));
}

public static Path createInputRuntimeImage() throws IOException {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not mac-specific helper function. You'd rather move it to JPackageCommand class.

BTW, there is a bunch of createInputRuntimeImage() functions already:

Instead of adding another one, let's consolidate them in one.


final Path runtimeImageDir;

if (JPackageCommand.DEFAULT_RUNTIME_IMAGE != null) {
runtimeImageDir = JPackageCommand.DEFAULT_RUNTIME_IMAGE;
} else {
runtimeImageDir = TKit.createTempDirectory("runtime-image").resolve("data");

new Executor().setToolProvider(JavaTool.JLINK)
.dumpOutput()
.addArguments(
"--output", runtimeImageDir.toString(),
"--add-modules", "java.desktop",
"--strip-debug",
"--no-header-files",
"--no-man-pages")
.execute();
}

return runtimeImageDir;
}

static PackageHandlers createDmgPackageHandlers() {
return new PackageHandlers(MacHelper::installDmg, MacHelper::uninstallDmg, MacHelper::unpackDmg);
}
Expand Down
150 changes: 150 additions & 0 deletions test/jdk/tools/jpackage/macosx/CustomInfoPListTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
/*
* Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/

/**
* Test --resource-dir with custom "Info.plist" for the top-level bundle
* and "Runtime-Info.plist" for the embedded runtime bundle
*/

/*
* @test
* @summary jpackage with --type image --resource-dir "Info.plist" and "Runtime-Info.plist"
* @library /test/jdk/tools/jpackage/helpers
* @key jpackagePlatformPackage
* @build jdk.jpackage.test.*
* @build CustomInfoPListTest
* @requires (os.family == "mac")
* @run main/othervm/timeout=1440 -Xmx512m jdk.jpackage.test.Main
* --jpt-run=CustomInfoPListTest
*/

import jdk.jpackage.test.TKit;
import jdk.jpackage.test.MacHelper;
import jdk.jpackage.test.JPackageCommand;
import jdk.jpackage.test.PackageType;

import java.io.IOException;
import java.io.StringWriter;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.concurrent.Executor;

import javax.xml.stream.XMLOutputFactory;

import jdk.jpackage.test.Annotations.Test;

import static jdk.jpackage.internal.util.PListWriter.writePList;
import static jdk.jpackage.internal.util.PListWriter.writeDict;
import static jdk.jpackage.internal.util.PListWriter.writeString;
import static jdk.jpackage.internal.util.XmlUtils.toXmlConsumer;
import static jdk.jpackage.internal.util.function.ThrowingSupplier.toSupplier;

public class CustomInfoPListTest {

private static final String BUNDLE_NAME_APP = "CustomAppName";
private static final String BUNDLE_NAME_EMBEDDED_RUNTIME = "CustomEmbeddedRuntimeName";
private static final String BUNDLE_NAME_RUNTIME = "CustomRuntimeName";

// We do not need full Info.plist for testing
private static String getInfoPListXML(String bundleName) {
Copy link
Member

@alexeysemenyukoracle alexeysemenyukoracle Sep 26, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use jdk.jpackage.internal.util.XmlUtils.createXml() helper to create xml file. It will format the output xml and will also eliminate the need for the patch in PListWriter class.

Copy link
Member Author

@sashamatveev sashamatveev Sep 29, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think using PListWriter is more convenient then jdk.jpackage.internal.util.XmlUtils.createXml(). I will prefer to use PListWriter.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not suggesting replacing PListWriter with XmlUtils.createXml(). I'm suggesting replacing XMLOutputFactory.newInstance().createXMLStreamWriter(buf) with XmlUtils.createXml():

private static void savePList(String bundleName, Path plistFile) throws XMLStreamException, IOException {
    XmlUtils.createXml(plistFile, xml -> { 
        writePList(xml, toXmlConsumer(() -> {
            writeDict(xml, toXmlConsumer(() -> {
                writeString(xml, "CFBundleName", bundleName);
                writeString(xml, "CFBundleIdentifier", "CustomInfoPListTest");
                writeString(xml, "CFBundleVersion", "1.0");
            }));
        }));
    });
}

return toSupplier(() -> {
var buf = new StringWriter();
var xml = XMLOutputFactory.newInstance().createXMLStreamWriter(buf);
writePList(xml, toXmlConsumer(() -> {
writeDict(xml, toXmlConsumer(() -> {
writeString(xml, "CFBundleName", bundleName);
writeString(xml, "CFBundleIdentifier", "CustomInfoPListTest");
writeString(xml, "CFBundleVersion", "1.0");
}));
}));
xml.flush();
xml.close();
return buf.toString();
}).get();
}

private static String getResourceDirWithCustomInfoPList(
String bundleName, boolean includeRuntimePList) {
final Path resources = TKit.createTempDirectory("resources");
try {
Files.writeString(resources.resolve("Info.plist"),
getInfoPListXML(bundleName));
if (includeRuntimePList) {
Files.writeString(resources.resolve("Runtime-Info.plist"),
getInfoPListXML(BUNDLE_NAME_EMBEDDED_RUNTIME));
}
} catch (IOException ex) {
throw new UncheckedIOException(ex);
}

return resources.toString();
}

@Test
public void testApp() {
JPackageCommand cmd = JPackageCommand.helloAppImage()
.addArguments("--resource-dir",
getResourceDirWithCustomInfoPList(BUNDLE_NAME_APP, true));

cmd.executeAndAssertHelloAppImageCreated();

var appPList = MacHelper.readPListFromAppImage(cmd.outputBundle());
TKit.assertEquals(BUNDLE_NAME_APP, appPList.queryValue("CFBundleName"), String.format(
"Check value of %s plist key", "CFBundleName"));

var runtimePList = MacHelper.readPListFromEmbeddedRuntime(cmd.outputBundle());
TKit.assertEquals(BUNDLE_NAME_EMBEDDED_RUNTIME, runtimePList.queryValue("CFBundleName"), String.format(
"Check value of %s plist key", "CFBundleName"));
}

@Test
public void testRuntime() throws IOException {
final var runtimeImage = MacHelper.createInputRuntimeImage();

final var runtimeBundleWorkDir = TKit.createTempDirectory("runtime-bundle");

final var unpackadeRuntimeBundleDir = runtimeBundleWorkDir.resolve("unpacked");

var cmd = new JPackageCommand()
.useToolProvider(true)
.ignoreDefaultRuntime(true)
.dumpOutput(true)
.setPackageType(PackageType.MAC_DMG)
.setArgumentValue("--name", "foo")
.addArguments("--runtime-image", runtimeImage)
.addArguments("--resource-dir",
getResourceDirWithCustomInfoPList(BUNDLE_NAME_RUNTIME, false))
.addArguments("--dest", runtimeBundleWorkDir);

cmd.execute();

MacHelper.withExplodedDmg(cmd, dmgImage -> {
if (dmgImage.endsWith(cmd.name() + ".jdk")) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't this test redundant?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. This function is called for all files in DMG such as /Volumes/foo/.background.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, ok. I see this sort of check in 5 locations.

I guess if you replace dmgImage.endsWith(cmd.name() + ".jdk") with dmgImage.endsWith(cmd.appInstallationDirectory().getFileName()) it will become self-explanatory.

var runtimePList = MacHelper.readPListFromAppImage(dmgImage);
TKit.assertEquals(BUNDLE_NAME_RUNTIME, runtimePList.queryValue("CFBundleName"),
String.format("Check value of %s plist key", "CFBundleName"));
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -94,32 +94,9 @@ private static JPackageCommand addSignOptions(JPackageCommand cmd, int certIndex
return cmd;
}

private static Path createInputRuntimeImage() throws IOException {

final Path runtimeImageDir;

if (JPackageCommand.DEFAULT_RUNTIME_IMAGE != null) {
runtimeImageDir = JPackageCommand.DEFAULT_RUNTIME_IMAGE;
} else {
runtimeImageDir = TKit.createTempDirectory("runtime-image").resolve("data");

new Executor().setToolProvider(JavaTool.JLINK)
.dumpOutput()
.addArguments(
"--output", runtimeImageDir.toString(),
"--add-modules", "java.desktop",
"--strip-debug",
"--no-header-files",
"--no-man-pages")
.execute();
}

return runtimeImageDir;
}

private static Path createInputRuntimeBundle(int certIndex) throws IOException {

final var runtimeImage = createInputRuntimeImage();
final var runtimeImage = MacHelper.createInputRuntimeImage();

final var runtimeBundleWorkDir = TKit.createTempDirectory("runtime-bundle");

Expand Down