1 /*
2  * Copyright (c) 2021, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.
8  *
9  * This code is distributed in the hope that it will be useful, but WITHOUT
10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
12  * version 2 for more details (a copy is included in the LICENSE file that
13  * accompanied this code).
14  *
15  * You should have received a copy of the GNU General Public License version
16  * 2 along with this work; if not, write to the Free Software Foundation,
17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18  *
19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20  * or visit www.oracle.com if you need additional information or have any
21  * questions.
22  */
23 
24 import java.io.File;
25 import java.nio.file.Path;
26 import java.util.List;
27 import java.util.Optional;
28 import java.util.function.BiFunction;
29 import jdk.jpackage.test.JPackageCommand;
30 import jdk.jpackage.test.Annotations.Test;
31 import jdk.jpackage.test.Executor;
32 import jdk.jpackage.test.TKit;
33 
34 /**
35  * Tests values of environment variables altered by jpackage launcher.
36  */
37 
38 /*
39  * @test
40  * @summary Tests values of environment variables altered by jpackage launcher
41  * @library ../helpers
42  * @library /test/lib
43  * @build AppLauncherEnvTest
44  * @build jdk.jpackage.test.*
45  * @build AppLauncherEnvTest
46  * @modules jdk.jpackage/jdk.jpackage.internal
47  * @run main/othervm -Xmx512m jdk.jpackage.test.Main
48  *  --jpt-run=AppLauncherEnvTest
49  */
50 public class AppLauncherEnvTest {
51 
52     @Test
test()53     public static void test() throws Exception {
54         final String testAddDirProp = "jpackage.test.AppDir";
55 
56         JPackageCommand cmd = JPackageCommand
57                 .helloAppImage(TEST_APP_JAVA + "*Hello")
58                 .addArguments("--java-options", "-D" + testAddDirProp
59                         + "=$APPDIR");
60 
61         cmd.executeAndAssertImageCreated();
62 
63         final String envVarName = envVarName();
64 
65         final int attempts = 3;
66         final int waitBetweenAttemptsSeconds = 5;
67         List<String> output = new Executor()
68                 .saveOutput()
69                 .setExecutable(cmd.appLauncherPath().toAbsolutePath())
70                 .addArguments("--print-env-var=" + envVarName)
71                 .addArguments("--print-sys-prop=" + testAddDirProp)
72                 .addArguments("--print-sys-prop=" + "java.library.path")
73                 .executeAndRepeatUntilExitCode(0, attempts,
74                         waitBetweenAttemptsSeconds).getOutput();
75 
76         BiFunction<Integer, String, String> getValue = (idx, name) -> {
77             return  output.get(idx).substring((name + "=").length());
78         };
79 
80         final String actualEnvVarValue = getValue.apply(0, envVarName);
81         final String appDir = getValue.apply(1, testAddDirProp);
82 
83         final String expectedEnvVarValue = Optional.ofNullable(System.getenv(
84                 envVarName)).orElse("") + File.pathSeparator + appDir;
85 
86         TKit.assertEquals(expectedEnvVarValue, actualEnvVarValue, String.format(
87                 "Check value of %s env variable", envVarName));
88 
89         final String javaLibraryPath = getValue.apply(2, "java.library.path");
90         TKit.assertTrue(
91                 List.of(javaLibraryPath.split(File.pathSeparator)).contains(
92                         appDir), String.format(
93                         "Check java.library.path system property [%s] contains app dir [%s]",
94                         javaLibraryPath, appDir));
95     }
96 
envVarName()97     private static String envVarName() {
98         if (TKit.isLinux()) {
99             return "LD_LIBRARY_PATH";
100         } else if (TKit.isWindows()) {
101             return "PATH";
102         } else if (TKit.isOSX()) {
103             return "DYLD_LIBRARY_PATH";
104         } else {
105             throw new IllegalStateException();
106         }
107     }
108 
109     private static final Path TEST_APP_JAVA = TKit.TEST_SRC_ROOT.resolve(
110             "apps/PrintEnv.java");
111 }
112