Skip to content

Commit 44f137f

Browse files
author
Alexey Semenyuk
committedAug 11, 2021
8271170: Add unit test for what jpackage app launcher puts in the environment
Reviewed-by: almatvee, herrick
1 parent cd2dbe5 commit 44f137f

File tree

7 files changed

+214
-130
lines changed

7 files changed

+214
-130
lines changed
 
+54
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.util.List;
25+
import java.util.ArrayList;
26+
27+
public class PrintEnv {
28+
29+
public static void main(String[] args) {
30+
List<String> lines = printArgs(args);
31+
lines.forEach(System.out::println);
32+
}
33+
34+
private static List<String> printArgs(String[] args) {
35+
List<String> lines = new ArrayList<>();
36+
37+
for (String arg : args) {
38+
if (arg.startsWith(PRINT_ENV_VAR)) {
39+
String name = arg.substring(PRINT_ENV_VAR.length());
40+
lines.add(name + "=" + System.getenv(name));
41+
} else if (arg.startsWith(PRINT_SYS_PROP)) {
42+
String name = arg.substring(PRINT_SYS_PROP.length());
43+
lines.add(name + "=" + System.getProperty(name));
44+
} else {
45+
throw new IllegalArgumentException();
46+
}
47+
}
48+
49+
return lines;
50+
}
51+
52+
private final static String PRINT_ENV_VAR = "--print-env-var=";
53+
private final static String PRINT_SYS_PROP = "--print-sys-prop=";
54+
}

‎test/jdk/tools/jpackage/apps/installer/Hello.java

-113
This file was deleted.

‎test/jdk/tools/jpackage/helpers/jdk/jpackage/test/HelloApp.java

+13-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -56,6 +56,8 @@ public final class HelloApp {
5656
}
5757

5858
private JarBuilder prepareSources(Path srcDir) throws IOException {
59+
final String srcClassName = appDesc.srcClassName();
60+
5961
final String qualifiedClassName = appDesc.className();
6062

6163
final String className = qualifiedClassName.substring(
@@ -83,9 +85,9 @@ private JarBuilder prepareSources(Path srcDir) throws IOException {
8385
// Add package directive and replace class name in java source file.
8486
// Works with simple test Hello.java.
8587
// Don't expect too much from these regexps!
86-
Pattern classNameRegex = Pattern.compile("\\bHello\\b");
88+
Pattern classNameRegex = Pattern.compile("\\b" + srcClassName + "\\b");
8789
Pattern classDeclaration = Pattern.compile(
88-
"(^.*\\bclass\\s+)\\bHello\\b(.*$)");
90+
"(^.*\\bclass\\s+)\\b" + srcClassName + "\\b(.*$)");
8991
Pattern importDirective = Pattern.compile(
9092
"(?<=import (?:static )?+)[^;]+");
9193
AtomicBoolean classDeclared = new AtomicBoolean();
@@ -97,7 +99,8 @@ private JarBuilder prepareSources(Path srcDir) throws IOException {
9799
System.lineSeparator(), line);
98100
});
99101

100-
Files.write(srcFile, Files.readAllLines(HELLO_JAVA).stream().map(line -> {
102+
Files.write(srcFile,
103+
Files.readAllLines(appDesc.srcJavaPath()).stream().map(line -> {
101104
Matcher m;
102105
if (classDeclared.getPlain()) {
103106
if ((m = classNameRegex.matcher(line)).find()) {
@@ -144,13 +147,14 @@ void addTo(JPackageCommand cmd) {
144147
return cmd.getArgumentValue("--module-path", cmd::inputDir, Path::of);
145148
};
146149

147-
if (moduleName == null && CLASS_NAME.equals(qualifiedClassName)) {
150+
if (moduleName == null && CLASS_NAME.equals(qualifiedClassName)
151+
&& HELLO_JAVA.equals(appDesc.srcJavaPath())) {
148152
// Use Hello.java as is.
149153
cmd.addPrerequisiteAction((self) -> {
150154
if (self.inputDir() != null) {
151155
Path jarFile = self.inputDir().resolve(appDesc.jarFileName());
152156
createJarBuilder().setOutputJar(jarFile).addSourceFile(
153-
HELLO_JAVA).create();
157+
appDesc.srcJavaPath()).create();
154158
}
155159
});
156160
} else if (appDesc.jmodFileName() != null) {
@@ -159,7 +163,7 @@ void addTo(JPackageCommand cmd) {
159163
createBundle(appDesc, getModulePath.get());
160164
});
161165
} else {
162-
// Modular app in .jar file
166+
// Modular/non-modular app in .jar file
163167
cmd.addPrerequisiteAction(unused -> {
164168
final Path jarFile;
165169
if (moduleName == null) {
@@ -195,7 +199,7 @@ void addTo(JPackageCommand cmd) {
195199
}
196200

197201
static JavaAppDesc createDefaltAppDesc() {
198-
return new JavaAppDesc().setClassName(CLASS_NAME).setBundleFileName("hello.jar");
202+
return new JavaAppDesc().setSrcJavaPath(HELLO_JAVA).setClassName(CLASS_NAME).setBundleFileName("hello.jar");
199203
}
200204

201205
static void verifyOutputFile(Path outputFile, List<String> args,
@@ -462,7 +466,7 @@ public static AppOutputVerifier assertApp(Path helloAppLauncher) {
462466
private final JavaAppDesc appDesc;
463467

464468
private static final Path HELLO_JAVA = TKit.TEST_SRC_ROOT.resolve(
465-
"apps/image/Hello.java");
469+
"apps/Hello.java");
466470

467471
private final static String CLASS_NAME = HELLO_JAVA.getFileName().toString().split(
468472
"\\.", 2)[0];

‎test/jdk/tools/jpackage/helpers/jdk/jpackage/test/JavaAppDesc.java

+29-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2019, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -30,6 +30,11 @@ public final class JavaAppDesc {
3030
public JavaAppDesc() {
3131
}
3232

33+
public JavaAppDesc setSrcJavaPath(Path v) {
34+
srcJavaPath = v;
35+
return this;
36+
}
37+
3338
public JavaAppDesc setClassName(String v) {
3439
qualifiedClassName = v;
3540
return this;
@@ -55,6 +60,15 @@ public JavaAppDesc setWithMainClass(boolean v) {
5560
return this;
5661
}
5762

63+
public Path srcJavaPath() {
64+
return srcJavaPath;
65+
}
66+
67+
public String srcClassName() {
68+
String fname = srcJavaPath().getFileName().toString();
69+
return fname.substring(0, fname.lastIndexOf('.'));
70+
}
71+
5872
public String className() {
5973
return qualifiedClassName;
6074
}
@@ -113,6 +127,9 @@ public boolean isWithMainClass() {
113127
@Override
114128
public String toString() {
115129
StringBuilder sb = new StringBuilder();
130+
if (srcJavaPath != null) {
131+
sb.append(srcJavaPath.toString()).append('*');
132+
}
116133
if (bundleFileName != null) {
117134
sb.append(bundleFileName).append(':');
118135
}
@@ -135,7 +152,7 @@ public String toString() {
135152
* Create Java application description form encoded string value.
136153
*
137154
* Syntax of encoded Java application description is
138-
* [(jar_file|jmods_file|exploded_jmods_file):][module_name/]qualified_class_name[!][@module_version].
155+
* [src_java_file*][(jar_file|jmods_file|exploded_jmods_file):][module_name/]qualified_class_name[!][@module_version].
139156
*
140157
* E.g.: `duke.jar:com.other/com.other.foo.bar.Buz!@3.7` encodes modular
141158
* application. Module name is `com.other`. Main class is
@@ -168,8 +185,16 @@ public static JavaAppDesc parse(final String javaAppDesc) {
168185
return desc;
169186
}
170187

188+
String srcJavaPathAndOther = Functional.identity(() -> {
189+
String[] components = javaAppDesc.split("\\*", 2);
190+
if (components.length == 2) {
191+
desc.setSrcJavaPath(Path.of(components[0]));
192+
}
193+
return components[components.length - 1];
194+
}).get();
195+
171196
String moduleNameAndOther = Functional.identity(() -> {
172-
String[] components = javaAppDesc.split(":", 2);
197+
String[] components = srcJavaPathAndOther.split(":", 2);
173198
if (components.length == 2) {
174199
desc.setBundleFileName(components[0]);
175200
}
@@ -206,6 +231,7 @@ public static JavaAppDesc parse(final String javaAppDesc) {
206231
return desc;
207232
}
208233

234+
private Path srcJavaPath;
209235
private String qualifiedClassName;
210236
private String moduleName;
211237
private String bundleFileName;

‎test/jdk/tools/jpackage/share/AddLauncherTest.java

+7-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2018, 2020, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2018, 2021, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -185,9 +185,13 @@ public void testMainLauncherIsModular(boolean mainLauncherIsModular) {
185185
cmd.ignoreDefaultRuntime(true); // because of --add-modules
186186
}
187187

188+
final String expectedMod = JavaAppDesc.parse(modularAppDesc.toString())
189+
.setBundleFileName(null)
190+
.setSrcJavaPath(null)
191+
.toString();
192+
188193
new AdditionalLauncher("ModularAppLauncher")
189-
.addRawProperties(Map.entry("module", JavaAppDesc.parse(
190-
modularAppDesc.toString()).setBundleFileName(null).toString()))
194+
.addRawProperties(Map.entry("module", expectedMod))
191195
.addRawProperties(Map.entry("main-jar", ""))
192196
.applyTo(cmd);
193197

@@ -208,8 +212,6 @@ public void testMainLauncherIsModular(boolean mainLauncherIsModular) {
208212
String moduleValue = cfg.getValue("Application", "app.mainmodule");
209213
String mainClass = null;
210214
String classpath = null;
211-
String expectedMod = JavaAppDesc.parse(
212-
modularAppDesc.toString()).setBundleFileName(null).toString();
213215
TKit.assertEquals(expectedMod, moduleValue,
214216
String.format("Check value of app.mainmodule=[%s]" +
215217
"in ModularAppLauncher cfg file is as expected", expectedMod));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
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
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+
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+
}

0 commit comments

Comments
 (0)
Please sign in to comment.