Skip to content

Commit 48ba237

Browse files
Added tests for DMG install dir. Fixed DMG bundler to report warning only when custom install dir differs from the default install dir.
1 parent dfcd0df commit 48ba237

File tree

5 files changed

+169
-33
lines changed

5 files changed

+169
-33
lines changed

src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/MacBaseInstallerBundler.java

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,19 +25,20 @@
2525

2626
package jdk.jpackage.internal;
2727

28+
import static jdk.jpackage.internal.StandardBundlerParam.APP_NAME;
29+
import static jdk.jpackage.internal.StandardBundlerParam.INSTALLER_NAME;
30+
import static jdk.jpackage.internal.StandardBundlerParam.INSTALL_DIR;
31+
import static jdk.jpackage.internal.StandardBundlerParam.PREDEFINED_APP_IMAGE;
32+
import static jdk.jpackage.internal.StandardBundlerParam.SIGN_BUNDLE;
33+
import static jdk.jpackage.internal.StandardBundlerParam.VERSION;
34+
2835
import java.io.IOException;
2936
import java.nio.file.Files;
3037
import java.nio.file.LinkOption;
3138
import java.nio.file.Path;
3239
import java.text.MessageFormat;
3340
import java.util.Map;
3441
import java.util.Optional;
35-
import static jdk.jpackage.internal.StandardBundlerParam.APP_NAME;
36-
import static jdk.jpackage.internal.StandardBundlerParam.INSTALLER_NAME;
37-
import static jdk.jpackage.internal.StandardBundlerParam.INSTALL_DIR;
38-
import static jdk.jpackage.internal.StandardBundlerParam.PREDEFINED_APP_IMAGE;
39-
import static jdk.jpackage.internal.StandardBundlerParam.VERSION;
40-
import static jdk.jpackage.internal.StandardBundlerParam.SIGN_BUNDLE;
4142
import jdk.jpackage.internal.util.FileUtils;
4243

4344
public abstract class MacBaseInstallerBundler extends AbstractBundler {
@@ -102,16 +103,20 @@ public abstract class MacBaseInstallerBundler extends AbstractBundler {
102103
static String getInstallDir(
103104
Map<String, ? super Object> params, boolean defaultOnly) {
104105
String returnValue = INSTALL_DIR.fetchFrom(params);
105-
if (defaultOnly && returnValue != null) {
106-
Log.info(I18N.getString("message.install-dir-ignored"));
106+
107+
final String defaultInstallDir;
108+
if (StandardBundlerParam.isRuntimeInstaller(params)) {
109+
defaultInstallDir = "/Library/Java/JavaVirtualMachines";
110+
} else {
111+
defaultInstallDir = "/Applications";
112+
}
113+
114+
if (defaultOnly && returnValue != null && !Path.of(returnValue).equals(Path.of(defaultInstallDir))) {
115+
Log.info(MessageFormat.format(I18N.getString("message.install-dir-ignored"), defaultInstallDir));
107116
returnValue = null;
108117
}
109118
if (returnValue == null) {
110-
if (StandardBundlerParam.isRuntimeInstaller(params)) {
111-
returnValue = "/Library/Java/JavaVirtualMachines";
112-
} else {
113-
returnValue = "/Applications";
114-
}
119+
returnValue = defaultInstallDir;
115120
}
116121
return returnValue;
117122
}

src/jdk.jpackage/macosx/classes/jdk/jpackage/internal/resources/MacResources.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ message.preparing-scripts=Preparing package scripts.
9191
message.preparing-distribution-dist=Preparing distribution.dist: {0}.
9292
message.signing.pkg=Warning: For signing PKG, you might need to set "Always Trust" for your certificate using "Keychain Access" tool.
9393
message.setfile.dmg=Setting custom icon on DMG file skipped because 'SetFile' utility was not found. Installing Xcode with Command Line Tools should resolve this issue.
94-
message.install-dir-ignored=Warning: "--install-dir" is not supported by DMG and will be default to /Applications.
94+
message.install-dir-ignored=Warning: "--install-dir" is not supported by DMG and will be default to {0}.
9595
message.codesign.failed.reason.app.content="codesign" failed and additional application content was supplied via the "--app-content" parameter. Probably the additional content broke the integrity of the application bundle and caused the failure. Ensure content supplied via the "--app-content" parameter does not break the integrity of the application bundle, or add it in the post-processing step.
9696
message.codesign.failed.reason.xcode.tools=Possible reason for "codesign" failure is missing Xcode with command line developer tools. Install Xcode with command line developer tools to see if it resolves the problem.
9797
warning.unsigned.app.image=Warning: Using unsigned app-image to build signed {0}.

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/JPackageCommand.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ public JPackageCommand(JPackageCommand cmd) {
7171
ignoreDefaultRuntime = cmd.ignoreDefaultRuntime;
7272
ignoreDefaultVerbose = cmd.ignoreDefaultVerbose;
7373
immutable = cmd.immutable;
74+
dmgInstallDir = cmd.dmgInstallDir;
7475
prerequisiteActions = new Actions(cmd.prerequisiteActions);
7576
verifyActions = new Actions(cmd.verifyActions);
7677
appLayoutAsserts = cmd.appLayoutAsserts;
@@ -501,7 +502,11 @@ public Path appInstallationDirectory() {
501502
}
502503

503504
if (TKit.isOSX()) {
504-
return MacHelper.getInstallationDirectory(this);
505+
if (packageType() == PackageType.MAC_DMG && dmgInstallDir != null) {
506+
return dmgInstallDir;
507+
} else {
508+
return MacHelper.getInstallationDirectory(this);
509+
}
505510
}
506511

507512
throw TKit.throwUnknownPlatformError();
@@ -867,6 +872,7 @@ private static JPackageCommand convertFromRuntime(JPackageCommand cmd) {
867872
var copy = new JPackageCommand(cmd);
868873
copy.immutable = false;
869874
copy.removeArgumentWithValue("--runtime-image");
875+
copy.dmgInstallDir = cmd.appInstallationDirectory();
870876
return copy;
871877
}
872878

@@ -1164,6 +1170,7 @@ public void run() {
11641170
private boolean ignoreDefaultRuntime;
11651171
private boolean ignoreDefaultVerbose;
11661172
private boolean immutable;
1173+
private Path dmgInstallDir;
11671174
private final Actions prerequisiteActions;
11681175
private final Actions verifyActions;
11691176
private Path executeInDirectory;

test/jdk/tools/jpackage/helpers/jdk/jpackage/test/MacHelper.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,34 +22,35 @@
2222
*/
2323
package jdk.jpackage.test;
2424

25+
import static java.util.stream.Collectors.toSet;
26+
2527
import java.io.ByteArrayInputStream;
2628
import java.io.IOException;
2729
import java.lang.reflect.InvocationTargetException;
2830
import java.lang.reflect.Method;
2931
import java.nio.charset.StandardCharsets;
3032
import java.nio.file.Files;
3133
import java.nio.file.Path;
32-
import java.util.List;
3334
import java.util.ArrayList;
35+
import java.util.List;
3436
import java.util.Optional;
3537
import java.util.Set;
3638
import java.util.regex.Pattern;
3739
import java.util.stream.Collectors;
38-
import static java.util.stream.Collectors.toSet;
3940
import java.util.stream.Stream;
4041
import javax.xml.parsers.DocumentBuilder;
4142
import javax.xml.parsers.DocumentBuilderFactory;
4243
import javax.xml.parsers.ParserConfigurationException;
4344
import javax.xml.xpath.XPath;
4445
import javax.xml.xpath.XPathConstants;
4546
import javax.xml.xpath.XPathFactory;
47+
import jdk.jpackage.internal.RetryExecutor;
48+
import jdk.jpackage.internal.util.PathUtils;
4649
import jdk.jpackage.internal.util.function.ThrowingConsumer;
4750
import jdk.jpackage.internal.util.function.ThrowingSupplier;
4851
import jdk.jpackage.test.PackageTest.PackageHandlers;
49-
import jdk.jpackage.internal.RetryExecutor;
50-
import jdk.jpackage.internal.util.PathUtils;
51-
import org.xml.sax.SAXException;
5252
import org.w3c.dom.NodeList;
53+
import org.xml.sax.SAXException;
5354

5455
public final class MacHelper {
5556

@@ -298,9 +299,18 @@ static String getBundleName(JPackageCommand cmd) {
298299

299300
static Path getInstallationDirectory(JPackageCommand cmd) {
300301
cmd.verifyIsOfType(PackageType.MAC);
301-
return Path.of(cmd.getArgumentValue("--install-dir",
302-
() -> cmd.isRuntime() ? "/Library/Java/JavaVirtualMachines" : "/Applications")).resolve(
303-
cmd.name() + (cmd.isRuntime() ? "" : ".app"));
302+
303+
final var defaultInstallLocation = Path.of(
304+
cmd.isRuntime() ? "/Library/Java/JavaVirtualMachines" : "/Applications");
305+
306+
final Path installLocation;
307+
if (cmd.packageType() == PackageType.MAC_DMG) {
308+
installLocation = defaultInstallLocation;
309+
} else {
310+
installLocation = cmd.getArgumentValue("--install-dir", () -> defaultInstallLocation, Path::of);
311+
}
312+
313+
return installLocation.resolve(cmd.name() + (cmd.isRuntime() ? "" : ".app"));
304314
}
305315

306316
static Path getUninstallCommand(JPackageCommand cmd) {

test/jdk/tools/jpackage/share/InstallDirTest.java

Lines changed: 124 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,20 @@
2222
*/
2323

2424
import java.nio.file.Path;
25+
import java.util.List;
26+
import java.util.Objects;
27+
import java.util.stream.Stream;
2528
import jdk.internal.util.OperatingSystem;
26-
import jdk.jpackage.test.TKit;
27-
import jdk.jpackage.test.PackageTest;
28-
import jdk.jpackage.test.PackageType;
2929
import jdk.jpackage.test.Annotations.Parameter;
30+
import jdk.jpackage.test.Annotations.ParameterSupplier;
3031
import jdk.jpackage.test.Annotations.Test;
32+
import jdk.jpackage.test.JPackageCommand;
33+
import jdk.jpackage.test.JPackageStringBundle;
34+
import jdk.jpackage.test.PackageTest;
35+
import jdk.jpackage.test.PackageType;
36+
import jdk.jpackage.test.RunnablePackageTest.Action;
37+
import jdk.jpackage.test.TKit;
38+
import jdk.jpackage.test.TKit.TextStreamVerifier;
3139

3240
/**
3341
* Test --install-dir parameter. Output of the test should be
@@ -92,11 +100,6 @@ public static void testCommon(Path installDir) {
92100
@Parameter("foo")
93101
@Parameter("/opt/foo/.././.")
94102
public static void testLinuxInvalid(String installDir) {
95-
testLinuxBad(installDir, "Invalid installation directory");
96-
}
97-
98-
private static void testLinuxBad(String installDir,
99-
String errorMessageSubstring) {
100103
new PackageTest().configureHelloApp()
101104
.setExpectedExitCode(1)
102105
.forTypes(PackageType.LINUX)
@@ -105,9 +108,120 @@ private static void testLinuxBad(String installDir,
105108
cmd.saveConsoleOutput(true);
106109
})
107110
.addBundleVerifier((cmd, result) -> {
108-
TKit.assertTextStream(errorMessageSubstring).apply(
109-
result.getOutput().stream());
111+
cmd.validateOutput(JPackageStringBundle.MAIN.cannedFormattedString("error.invalid-install-dir"));
110112
})
111113
.run();
112114
}
115+
116+
record DmgTestSpec(Path installDir, boolean installDirIgnored, boolean runtimeInstaller) {
117+
118+
DmgTestSpec {
119+
Objects.requireNonNull(installDir);
120+
}
121+
122+
static Builder build() {
123+
return new Builder();
124+
}
125+
126+
static final class Builder {
127+
128+
Builder ignoredInstallDir(String v) {
129+
installDir = Path.of(v);
130+
installDirIgnored = true;
131+
return this;
132+
}
133+
134+
Builder acceptedInstallDir(String v) {
135+
installDir = Path.of(v);
136+
installDirIgnored = false;
137+
return this;
138+
}
139+
140+
Builder runtimeInstaller() {
141+
runtimeInstaller = true;
142+
return this;
143+
}
144+
145+
DmgTestSpec create() {
146+
return new DmgTestSpec(installDir, installDirIgnored, runtimeInstaller);
147+
}
148+
149+
private Path installDir;
150+
private boolean installDirIgnored;
151+
private boolean runtimeInstaller;
152+
}
153+
154+
@Override
155+
public String toString() {
156+
final var sb = new StringBuilder();
157+
sb.append(installDir);
158+
if (installDirIgnored) {
159+
sb.append(", ignore");
160+
}
161+
if (runtimeInstaller) {
162+
sb.append(", runtime");
163+
}
164+
return sb.toString();
165+
}
166+
167+
void run() {
168+
final var test = new PackageTest().forTypes(PackageType.MAC_DMG).ignoreBundleOutputDir();
169+
if (runtimeInstaller) {
170+
test.addInitializer(cmd -> {
171+
cmd.removeArgumentWithValue("--input");
172+
});
173+
} else {
174+
test.configureHelloApp();
175+
}
176+
177+
test.addInitializer(JPackageCommand::setFakeRuntime).addInitializer(cmd -> {
178+
cmd.addArguments("--install-dir", installDir);
179+
cmd.validateOutput(createInstallDirWarningVerifier());
180+
}).run(Action.CREATE_AND_UNPACK);
181+
}
182+
183+
private TextStreamVerifier createInstallDirWarningVerifier() {
184+
final var verifier = TKit.assertTextStream(
185+
JPackageStringBundle.MAIN.cannedFormattedString("message.install-dir-ignored", defaultDmgInstallDir()).getValue());
186+
if (installDirIgnored) {
187+
return verifier;
188+
} else {
189+
return verifier.negate();
190+
}
191+
}
192+
193+
private String defaultDmgInstallDir() {
194+
if (runtimeInstaller) {
195+
return "/Library/Java/JavaVirtualMachines";
196+
} else {
197+
return "/Applications";
198+
}
199+
}
200+
}
201+
202+
@Test(ifOS = OperatingSystem.MACOS)
203+
@ParameterSupplier
204+
public static void testDmg(DmgTestSpec testSpec) {
205+
testSpec.run();
206+
}
207+
208+
public static List<Object[]> testDmg() {
209+
return Stream.of(
210+
DmgTestSpec.build().ignoredInstallDir("/foo"),
211+
DmgTestSpec.build().ignoredInstallDir("/foo/bar"),
212+
DmgTestSpec.build().ignoredInstallDir("/foo").runtimeInstaller(),
213+
DmgTestSpec.build().ignoredInstallDir("/foo/bar").runtimeInstaller(),
214+
215+
DmgTestSpec.build().ignoredInstallDir("/Library/Java/JavaVirtualMachines"),
216+
DmgTestSpec.build().ignoredInstallDir("/Applications").runtimeInstaller(),
217+
218+
DmgTestSpec.build().acceptedInstallDir("/Applications"),
219+
DmgTestSpec.build().ignoredInstallDir("/Applications/foo/bar/buz"),
220+
221+
DmgTestSpec.build().runtimeInstaller().acceptedInstallDir("/Library/Java/JavaVirtualMachines"),
222+
DmgTestSpec.build().runtimeInstaller().ignoredInstallDir("/Library/Java/JavaVirtualMachines/foo/bar/buz")
223+
).map(DmgTestSpec.Builder::create).map(testSpec -> {
224+
return new Object[] { testSpec };
225+
}).toList();
226+
}
113227
}

0 commit comments

Comments
 (0)