58416
|
1 |
/*
|
|
2 |
* Copyright (c) 2019, 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 |
package jdk.jpackage.tests;
|
|
25 |
|
|
26 |
import java.io.IOException;
|
|
27 |
import java.nio.file.Files;
|
|
28 |
import java.nio.file.Path;
|
|
29 |
import java.util.List;
|
|
30 |
import java.util.ArrayList;
|
58463
|
31 |
import java.util.regex.Pattern;
|
58416
|
32 |
import java.util.stream.Collectors;
|
|
33 |
import jdk.jpackage.test.Executor;
|
|
34 |
import jdk.jpackage.test.JPackageCommand;
|
|
35 |
import jdk.jpackage.test.TKit;
|
|
36 |
import jdk.jpackage.test.JavaTool;
|
|
37 |
import jdk.jpackage.test.HelloApp;
|
|
38 |
import jdk.jpackage.test.Annotations.*;
|
|
39 |
|
|
40 |
/*
|
|
41 |
* @test
|
|
42 |
* @summary jpackage basic testing
|
|
43 |
* @library ../../../../helpers
|
|
44 |
* @build jdk.jpackage.test.*
|
|
45 |
* @modules jdk.jpackage
|
|
46 |
* @compile BasicTest.java
|
|
47 |
* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main
|
|
48 |
* --jpt-run=jdk.jpackage.tests.BasicTest
|
|
49 |
*/
|
|
50 |
|
|
51 |
public class BasicTest {
|
|
52 |
@Test
|
|
53 |
public void testNoArgs() {
|
|
54 |
List<String> output = JPackageCommand.filterOutput(
|
58463
|
55 |
getJPackageToolProvider().executeAndGetOutput());
|
58416
|
56 |
TKit.assertStringListEquals(List.of("Usage: jpackage <mode> <options>",
|
|
57 |
"Use jpackage --help (or -h) for a list of possible options"),
|
|
58 |
output, "Check jpackage output");
|
|
59 |
}
|
|
60 |
|
|
61 |
@Test
|
|
62 |
public void testVersion() {
|
|
63 |
List<String> output = JPackageCommand.filterOutput(
|
58463
|
64 |
getJPackageToolProvider()
|
58416
|
65 |
.addArgument("--version")
|
|
66 |
.executeAndGetOutput());
|
|
67 |
TKit.assertStringListEquals(List.of(System.getProperty("java.version")),
|
|
68 |
output, "Check jpackage output");
|
|
69 |
}
|
|
70 |
|
|
71 |
@Test
|
|
72 |
public void testNoName() {
|
|
73 |
final String mainClassName = "Greetings";
|
|
74 |
|
|
75 |
JPackageCommand cmd = new JPackageCommand()
|
|
76 |
.helloAppImage(mainClassName)
|
|
77 |
.removeArgumentWithValue("--name");
|
|
78 |
|
|
79 |
Path expectedImageDir = cmd.outputDir().resolve(mainClassName);
|
|
80 |
if (TKit.isOSX()) {
|
|
81 |
expectedImageDir = expectedImageDir.getParent().resolve(
|
|
82 |
expectedImageDir.getFileName().toString() + ".app");
|
|
83 |
}
|
|
84 |
|
|
85 |
cmd.executeAndAssertHelloAppImageCreated();
|
|
86 |
TKit.assertEquals(expectedImageDir.toAbsolutePath().normalize().toString(),
|
|
87 |
cmd.appImage().toAbsolutePath().normalize().toString(),
|
|
88 |
String.format(
|
|
89 |
"Check [%s] directory is filled with application image data",
|
|
90 |
expectedImageDir));
|
|
91 |
}
|
|
92 |
|
|
93 |
@Test
|
|
94 |
public void testApp() {
|
|
95 |
new JPackageCommand()
|
|
96 |
.helloAppImage()
|
|
97 |
.executeAndAssertHelloAppImageCreated();
|
|
98 |
}
|
|
99 |
|
|
100 |
@Test
|
|
101 |
public void testModularApp() {
|
|
102 |
new JPackageCommand()
|
|
103 |
.helloAppImage("com.other/com.other.Hello")
|
|
104 |
.executeAndAssertHelloAppImageCreated();
|
|
105 |
}
|
|
106 |
|
|
107 |
@Test
|
|
108 |
// @Parameter("ALL-MODULE-PATH") ; This test fails
|
|
109 |
@Parameter("ALL-DEFAULT")
|
|
110 |
public void testAddModules(String addModulesArg) {
|
|
111 |
JPackageCommand cmd = new JPackageCommand()
|
|
112 |
.helloAppImage("com.other/com.other.Hello");
|
|
113 |
if (!addModulesArg.isEmpty()) {
|
|
114 |
cmd.addArguments("--add-modules", addModulesArg);
|
|
115 |
}
|
|
116 |
cmd.executeAndAssertHelloAppImageCreated();
|
|
117 |
}
|
|
118 |
|
|
119 |
/**
|
|
120 |
* Test --temp option. Doesn't make much sense for app image as temporary
|
|
121 |
* directory is used only on Windows.
|
|
122 |
* @throws IOException
|
|
123 |
*/
|
|
124 |
// @Test
|
|
125 |
public void testTemp() throws IOException {
|
|
126 |
JPackageCommand cmd = new JPackageCommand().helloAppImage();
|
|
127 |
TKit.withTempDirectory("temp-root", tempDir -> {
|
|
128 |
cmd.addArguments("--temp", tempDir);
|
|
129 |
|
|
130 |
cmd.executeAndAssertHelloAppImageCreated();
|
|
131 |
|
|
132 |
// Check jpackage actually used the supplied directory.
|
|
133 |
TKit.assertNotEquals(0, tempDir.toFile().list().length,
|
|
134 |
String.format(
|
|
135 |
"Check jpackage wrote some data in the supplied temporary directory [%s]",
|
|
136 |
tempDir));
|
|
137 |
|
|
138 |
// Temporary directory should not be empty,
|
|
139 |
// jpackage should exit with error.
|
|
140 |
cmd.execute().assertExitCodeIs(1);
|
|
141 |
});
|
|
142 |
}
|
|
143 |
|
|
144 |
@Test
|
|
145 |
public void testAtFile() throws IOException {
|
|
146 |
JPackageCommand cmd = new JPackageCommand().helloAppImage();
|
|
147 |
|
|
148 |
// Init options file with the list of options configured
|
|
149 |
// for JPackageCommand instance.
|
|
150 |
final Path optionsFile = TKit.workDir().resolve("options");
|
|
151 |
Files.write(optionsFile,
|
|
152 |
List.of(String.join(" ", cmd.getAllArguments())));
|
|
153 |
|
|
154 |
// Build app jar file.
|
|
155 |
cmd.executePrerequisiteActions();
|
|
156 |
|
|
157 |
// Make sure output directory is empty. Normally JPackageCommand would
|
|
158 |
// do this automatically.
|
|
159 |
TKit.deleteDirectoryContentsRecursive(cmd.outputDir());
|
|
160 |
|
|
161 |
// Instead of running jpackage command through configured
|
|
162 |
// JPackageCommand instance, run vanilla jpackage command with @ file.
|
58463
|
163 |
getJPackageToolProvider()
|
58416
|
164 |
.addArgument(String.format("@%s", optionsFile))
|
|
165 |
.execute().assertExitCodeIsZero();
|
|
166 |
|
|
167 |
// Verify output of jpackage command.
|
|
168 |
cmd.assertImageCreated();
|
|
169 |
HelloApp.executeLauncherAndVerifyOutput(cmd);
|
|
170 |
}
|
|
171 |
|
|
172 |
@Parameter("Hello")
|
|
173 |
@Parameter("com.foo/com.foo.main.Aloha")
|
|
174 |
@Test
|
|
175 |
public void testJLinkRuntime(String javaAppDesc) {
|
58463
|
176 |
JPackageCommand cmd = JPackageCommand.helloAppImage(javaAppDesc);
|
58416
|
177 |
|
|
178 |
// If `--module` parameter was set on jpackage command line, get its
|
|
179 |
// value and extract module name.
|
|
180 |
// E.g.: foo.bar2/foo.bar.Buz -> foo.bar2
|
|
181 |
// Note: HelloApp class manages `--module` parameter on jpackage command line
|
|
182 |
final String moduleName = cmd.getArgumentValue("--module", () -> null,
|
|
183 |
(v) -> v.split("/", 2)[0]);
|
|
184 |
|
|
185 |
if (moduleName != null) {
|
|
186 |
// Build module jar.
|
|
187 |
cmd.executePrerequisiteActions();
|
|
188 |
}
|
|
189 |
|
|
190 |
TKit.withTempDirectory("runtime", runtimeDir -> {
|
|
191 |
TKit.deleteDirectoryRecursive(runtimeDir, String.format(
|
|
192 |
"Delete [%s] output directory for jlink command", runtimeDir));
|
58463
|
193 |
Executor jlink = getToolProvider(JavaTool.JLINK)
|
58416
|
194 |
.saveOutput(false)
|
|
195 |
.addArguments(
|
|
196 |
"--add-modules", "java.base",
|
|
197 |
"--output", runtimeDir.toString(),
|
|
198 |
"--strip-debug",
|
|
199 |
"--no-header-files",
|
|
200 |
"--no-man-pages");
|
|
201 |
|
|
202 |
if (moduleName != null) {
|
|
203 |
jlink.addArguments("--add-modules", moduleName, "--module-path",
|
|
204 |
Path.of(cmd.getArgumentValue("--module-path")).resolve(
|
|
205 |
"hello.jar").toString());
|
|
206 |
}
|
|
207 |
|
|
208 |
jlink.execute().assertExitCodeIsZero();
|
|
209 |
|
|
210 |
cmd.addArguments("--runtime-image", runtimeDir);
|
|
211 |
cmd.executeAndAssertHelloAppImageCreated();
|
|
212 |
|
|
213 |
final Path appImageRuntimePath = cmd.appImage().resolve(
|
|
214 |
cmd.appRuntimeDirectoryInAppImage());
|
|
215 |
|
58463
|
216 |
//
|
58416
|
217 |
// This is an overkill to list modules in jlink output as we have
|
|
218 |
// already verified that Java app is functional and thus app's runtime
|
|
219 |
// is likely to be OK, but let's double check.
|
58463
|
220 |
//
|
|
221 |
// Filter out all first strings with whitespace. They are java
|
|
222 |
// launcher output like `Picked up ...` unrelated to module names.
|
|
223 |
//
|
|
224 |
Pattern whitespaceChar = Pattern.compile("\\s");
|
58416
|
225 |
List<String> moduleList = new Executor().dumpOutput().setExecutable(
|
|
226 |
appImageRuntimePath.resolve(
|
|
227 |
JPackageCommand.relativePathInRuntime(JavaTool.JAVA))).addArguments(
|
58463
|
228 |
"--list-modules").executeAndGetOutput().stream().dropWhile(
|
|
229 |
s -> whitespaceChar.matcher(s).find()).sorted().collect(
|
|
230 |
Collectors.toList());
|
58416
|
231 |
|
|
232 |
List<String> expectedModules = new ArrayList<>();
|
|
233 |
expectedModules.add(String.format("java.base@%s",
|
|
234 |
System.getProperty("java.version")));
|
|
235 |
if (moduleName != null) {
|
|
236 |
expectedModules.add(moduleName);
|
|
237 |
}
|
|
238 |
expectedModules = expectedModules.stream().sorted().collect(
|
|
239 |
Collectors.toList());
|
|
240 |
|
|
241 |
TKit.assertStringListEquals(expectedModules, moduleList,
|
|
242 |
String.format(
|
|
243 |
"Check modules in application image runtime directory at [%s]",
|
|
244 |
appImageRuntimePath));
|
|
245 |
});
|
|
246 |
}
|
|
247 |
|
58463
|
248 |
private static Executor getJPackageToolProvider() {
|
|
249 |
return getToolProvider(JavaTool.JPACKAGE);
|
58416
|
250 |
}
|
|
251 |
|
58463
|
252 |
private static Executor getToolProvider(JavaTool tool) {
|
|
253 |
return new Executor()
|
|
254 |
.dumpOutput().saveOutput()
|
|
255 |
.setToolProvider(tool.asToolProvider());
|
58416
|
256 |
}
|
|
257 |
}
|