1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184
|
/*
* Copyright (c) 2022, 2024, 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.
*/
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.Optional;
import jdk.jpackage.test.AdditionalLauncher;
import jdk.jpackage.test.PackageTest;
import jdk.jpackage.test.Annotations.Test;
import jdk.jpackage.test.Functional.ThrowingConsumer;
import jdk.jpackage.test.HelloApp;
import jdk.jpackage.test.JPackageCommand;
import jdk.jpackage.test.LinuxHelper;
import jdk.jpackage.test.PackageType;
import jdk.jpackage.test.TKit;
/**
* Test per-user configuration of app launchers created by jpackage.
*/
/*
* @test
* @summary pre-user configuration of app launchers
* @library /test/jdk/tools/jpackage/helpers
* @key jpackagePlatformPackage
* @requires jpackage.test.SQETest == null
* @build jdk.jpackage.test.*
* @compile PerUserCfgTest.java
* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
* --jpt-run=PerUserCfgTest
*/
public class PerUserCfgTest {
@Test
public static void test() throws IOException {
// Create a number of .cfg files with different startup args
JPackageCommand cfgCmd = JPackageCommand.helloAppImage().setFakeRuntime()
.setArgumentValue("--dest", TKit.createTempDirectory("cfg-files").toString());
addLauncher(cfgCmd, "a");
addLauncher(cfgCmd, "b");
cfgCmd.execute();
new PackageTest().configureHelloApp().addInstallVerifier(cmd -> {
if (cmd.isPackageUnpacked("Not running per-user configuration tests")) {
return;
}
Path launcherPath = cmd.appLauncherPath();
if (!cmd.canRunLauncher(String.format(
"Not running %s launcher and per-user configuration tests",
launcherPath))) {
return;
}
final PackageType type = cmd.packageType();
if (PackageType.MAC.contains(type)) {
withConfigFile(cmd, cfgCmd.appLauncherCfgPath("a"),
getUserHomeDir().resolve("Library/Application Support").resolve(
cmd.name()), theCmd -> {
runMainLauncher(cmd, "a");
});
} else if (PackageType.LINUX.contains(type)) {
final String pkgName = LinuxHelper.getPackageName(cmd);
final Path homeDir = getUserHomeDir();
withConfigFile(cmd, cfgCmd.appLauncherCfgPath("a"),
homeDir.resolve(".local").resolve(pkgName), theCmd -> {
runMainLauncher(cmd, "a");
});
withConfigFile(cmd, cfgCmd.appLauncherCfgPath("b"),
homeDir.resolve("." + pkgName), theCmd -> {
runMainLauncher(cmd, "b");
});
withConfigFile(cmd, cfgCmd.appLauncherCfgPath("b"),
homeDir.resolve("." + pkgName), theCmd -> {
runMainLauncher(cmd, "b");
withConfigFile(cmd, cfgCmd.appLauncherCfgPath("a"),
homeDir.resolve(".local").resolve(pkgName),
theCmd2 -> {
runMainLauncher(cmd, "a");
});
});
} else if (PackageType.WINDOWS.contains(type)) {
final Path appData = getDirFromEnvVariable("APPDATA");
final Path localAppData = getDirFromEnvVariable("LOCALAPPDATA");
if (appData == null || localAppData == null) {
TKit.trace(String.format(
"Not running per-user configuration tests because some of the environment varibles are not set. "
+ "Run jtreg with -e:APPDATA,LOCALAPPDATA option to fix the problem"));
} else {
withConfigFile(cmd, cfgCmd.appLauncherCfgPath("a"),
appData.resolve(cmd.name()), theCmd -> {
runMainLauncher(cmd, "a");
});
withConfigFile(cmd, cfgCmd.appLauncherCfgPath("b"),
localAppData.resolve(cmd.name()), theCmd -> {
runMainLauncher(cmd, "b");
});
withConfigFile(cmd, cfgCmd.appLauncherCfgPath("b"),
appData.resolve(cmd.name()), theCmd -> {
runMainLauncher(cmd, "b");
withConfigFile(cmd, cfgCmd.appLauncherCfgPath("a"),
localAppData.resolve(cmd.name()),
theCmd2 -> {
runMainLauncher(cmd, "a");
});
});
}
}
runMainLauncher(cmd);
}).run();
}
private static void addLauncher(JPackageCommand cmd, String name) {
new AdditionalLauncher(name) {
@Override
protected void verify(JPackageCommand cmd) {}
}.setDefaultArguments(name).applyTo(cmd);
}
private static Path getUserHomeDir() {
return getDirFromEnvVariable("HOME");
}
private static Path getDirFromEnvVariable(String envVariableName) {
return Optional.ofNullable(System.getenv(envVariableName)).map(Path::of).orElse(
null);
}
private static void withConfigFile(JPackageCommand cmd, Path srcCfgFile,
Path outputCfgFileDir, ThrowingConsumer<JPackageCommand> action) throws
Throwable {
Path targetCfgFile = outputCfgFileDir.resolve(cmd.appLauncherCfgPath(
null).getFileName());
TKit.assertPathExists(targetCfgFile, false);
try (var dirCleaner = TKit.createDirectories(targetCfgFile.getParent())) {
Files.copy(srcCfgFile, targetCfgFile);
try {
TKit.traceFileContents(targetCfgFile, "cfg file");
action.accept(cmd);
} finally {
Files.deleteIfExists(targetCfgFile);
}
}
}
private static void runMainLauncher(JPackageCommand cmd,
String... expectedArgs) {
HelloApp.assertApp(cmd.appLauncherPath()).addDefaultArguments(List.of(
expectedArgs)).executeAndVerifyOutput();
}
}
|