Path: blob/master/test/jdk/tools/jpackage/share/AppLauncherEnvTest.java
66644 views
/*1* Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.nio.file.Path;25import java.util.List;26import java.util.Optional;27import java.util.function.BiFunction;28import jdk.jpackage.test.JPackageCommand;29import jdk.jpackage.test.Annotations.Test;30import jdk.jpackage.test.Executor;31import jdk.jpackage.test.TKit;3233/**34* Tests values of environment variables altered by jpackage launcher.35*/3637/*38* @test39* @summary Tests values of environment variables altered by jpackage launcher40* @library ../helpers41* @library /test/lib42* @build AppLauncherEnvTest43* @build jdk.jpackage.test.*44* @build AppLauncherEnvTest45* @modules jdk.jpackage/jdk.jpackage.internal46* @run main/othervm -Xmx512m jdk.jpackage.test.Main47* --jpt-run=AppLauncherEnvTest48*/49public class AppLauncherEnvTest {5051@Test52public static void test() throws Exception {53final String testAddDirProp = "jpackage.test.AppDir";5455JPackageCommand cmd = JPackageCommand56.helloAppImage(TEST_APP_JAVA + "*Hello")57.addArguments("--java-options", "-D" + testAddDirProp58+ "=$APPDIR");5960cmd.executeAndAssertImageCreated();6162final String envVarName = envVarName();6364final int attempts = 3;65final int waitBetweenAttemptsSeconds = 5;66List<String> output = new Executor()67.saveOutput()68.setExecutable(cmd.appLauncherPath().toAbsolutePath())69.addArguments("--print-env-var=" + envVarName)70.addArguments("--print-sys-prop=" + testAddDirProp)71.addArguments("--print-sys-prop=" + "java.library.path")72.executeAndRepeatUntilExitCode(0, attempts,73waitBetweenAttemptsSeconds).getOutput();7475BiFunction<Integer, String, String> getValue = (idx, name) -> {76return output.get(idx).substring((name + "=").length());77};7879final String actualEnvVarValue = getValue.apply(0, envVarName);80final String appDir = getValue.apply(1, testAddDirProp);8182final String expectedEnvVarValue = Optional.ofNullable(System.getenv(83envVarName)).orElse("") + File.pathSeparator + appDir;8485TKit.assertEquals(expectedEnvVarValue, actualEnvVarValue, String.format(86"Check value of %s env variable", envVarName));8788final String javaLibraryPath = getValue.apply(2, "java.library.path");89TKit.assertTrue(90List.of(javaLibraryPath.split(File.pathSeparator)).contains(91appDir), String.format(92"Check java.library.path system property [%s] contains app dir [%s]",93javaLibraryPath, appDir));94}9596private static String envVarName() {97if (TKit.isLinux()) {98return "LD_LIBRARY_PATH";99} else if (TKit.isWindows()) {100return "PATH";101} else if (TKit.isOSX()) {102return "DYLD_LIBRARY_PATH";103} else {104throw new IllegalStateException();105}106}107108private static final Path TEST_APP_JAVA = TKit.TEST_SRC_ROOT.resolve(109"apps/PrintEnv.java");110}111112113