author | herrick |
Fri, 04 Oct 2019 18:13:05 -0400 | |
branch | JDK-8200758-branch |
changeset 58466 | 47f0d21c7e8d |
parent 58463 | 4e71249f291c |
child 58648 | 3bf53ffa9ae7 |
permissions | -rw-r--r-- |
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 |
|
58466
47f0d21c7e8d
8231882: --add-modules ALL-MODULE-PATH is not handled properly
herrick
parents:
58463
diff
changeset
|
108 |
@Parameter("ALL-MODULE-PATH") |
58416 | 109 |
@Parameter("ALL-DEFAULT") |
58466
47f0d21c7e8d
8231882: --add-modules ALL-MODULE-PATH is not handled properly
herrick
parents:
58463
diff
changeset
|
110 |
@Parameter("jdk.desktop,jdk.jartool") |
58416 | 111 |
public void testAddModules(String addModulesArg) { |
112 |
JPackageCommand cmd = new JPackageCommand() |
|
113 |
.helloAppImage("com.other/com.other.Hello"); |
|
114 |
if (!addModulesArg.isEmpty()) { |
|
115 |
cmd.addArguments("--add-modules", addModulesArg); |
|
116 |
} |
|
117 |
cmd.executeAndAssertHelloAppImageCreated(); |
|
118 |
} |
|
119 |
||
120 |
/** |
|
121 |
* Test --temp option. Doesn't make much sense for app image as temporary |
|
122 |
* directory is used only on Windows. |
|
123 |
* @throws IOException |
|
124 |
*/ |
|
125 |
// @Test |
|
126 |
public void testTemp() throws IOException { |
|
127 |
JPackageCommand cmd = new JPackageCommand().helloAppImage(); |
|
128 |
TKit.withTempDirectory("temp-root", tempDir -> { |
|
129 |
cmd.addArguments("--temp", tempDir); |
|
130 |
||
131 |
cmd.executeAndAssertHelloAppImageCreated(); |
|
132 |
||
133 |
// Check jpackage actually used the supplied directory. |
|
134 |
TKit.assertNotEquals(0, tempDir.toFile().list().length, |
|
135 |
String.format( |
|
136 |
"Check jpackage wrote some data in the supplied temporary directory [%s]", |
|
137 |
tempDir)); |
|
138 |
||
139 |
// Temporary directory should not be empty, |
|
140 |
// jpackage should exit with error. |
|
141 |
cmd.execute().assertExitCodeIs(1); |
|
142 |
}); |
|
143 |
} |
|
144 |
||
145 |
@Test |
|
146 |
public void testAtFile() throws IOException { |
|
147 |
JPackageCommand cmd = new JPackageCommand().helloAppImage(); |
|
148 |
||
149 |
// Init options file with the list of options configured |
|
150 |
// for JPackageCommand instance. |
|
151 |
final Path optionsFile = TKit.workDir().resolve("options"); |
|
152 |
Files.write(optionsFile, |
|
153 |
List.of(String.join(" ", cmd.getAllArguments()))); |
|
154 |
||
155 |
// Build app jar file. |
|
156 |
cmd.executePrerequisiteActions(); |
|
157 |
||
158 |
// Make sure output directory is empty. Normally JPackageCommand would |
|
159 |
// do this automatically. |
|
160 |
TKit.deleteDirectoryContentsRecursive(cmd.outputDir()); |
|
161 |
||
162 |
// Instead of running jpackage command through configured |
|
163 |
// JPackageCommand instance, run vanilla jpackage command with @ file. |
|
58463 | 164 |
getJPackageToolProvider() |
58416 | 165 |
.addArgument(String.format("@%s", optionsFile)) |
166 |
.execute().assertExitCodeIsZero(); |
|
167 |
||
168 |
// Verify output of jpackage command. |
|
169 |
cmd.assertImageCreated(); |
|
170 |
HelloApp.executeLauncherAndVerifyOutput(cmd); |
|
171 |
} |
|
172 |
||
173 |
@Parameter("Hello") |
|
174 |
@Parameter("com.foo/com.foo.main.Aloha") |
|
175 |
@Test |
|
176 |
public void testJLinkRuntime(String javaAppDesc) { |
|
58463 | 177 |
JPackageCommand cmd = JPackageCommand.helloAppImage(javaAppDesc); |
58416 | 178 |
|
179 |
// If `--module` parameter was set on jpackage command line, get its |
|
180 |
// value and extract module name. |
|
181 |
// E.g.: foo.bar2/foo.bar.Buz -> foo.bar2 |
|
182 |
// Note: HelloApp class manages `--module` parameter on jpackage command line |
|
183 |
final String moduleName = cmd.getArgumentValue("--module", () -> null, |
|
184 |
(v) -> v.split("/", 2)[0]); |
|
185 |
||
186 |
if (moduleName != null) { |
|
187 |
// Build module jar. |
|
188 |
cmd.executePrerequisiteActions(); |
|
189 |
} |
|
190 |
||
191 |
TKit.withTempDirectory("runtime", runtimeDir -> { |
|
192 |
TKit.deleteDirectoryRecursive(runtimeDir, String.format( |
|
193 |
"Delete [%s] output directory for jlink command", runtimeDir)); |
|
58463 | 194 |
Executor jlink = getToolProvider(JavaTool.JLINK) |
58416 | 195 |
.saveOutput(false) |
196 |
.addArguments( |
|
197 |
"--add-modules", "java.base", |
|
198 |
"--output", runtimeDir.toString(), |
|
199 |
"--strip-debug", |
|
200 |
"--no-header-files", |
|
201 |
"--no-man-pages"); |
|
202 |
||
203 |
if (moduleName != null) { |
|
204 |
jlink.addArguments("--add-modules", moduleName, "--module-path", |
|
205 |
Path.of(cmd.getArgumentValue("--module-path")).resolve( |
|
206 |
"hello.jar").toString()); |
|
207 |
} |
|
208 |
||
209 |
jlink.execute().assertExitCodeIsZero(); |
|
210 |
||
211 |
cmd.addArguments("--runtime-image", runtimeDir); |
|
212 |
cmd.executeAndAssertHelloAppImageCreated(); |
|
213 |
||
214 |
final Path appImageRuntimePath = cmd.appImage().resolve( |
|
215 |
cmd.appRuntimeDirectoryInAppImage()); |
|
216 |
||
58463 | 217 |
// |
58416 | 218 |
// This is an overkill to list modules in jlink output as we have |
219 |
// already verified that Java app is functional and thus app's runtime |
|
220 |
// is likely to be OK, but let's double check. |
|
58463 | 221 |
// |
222 |
// Filter out all first strings with whitespace. They are java |
|
223 |
// launcher output like `Picked up ...` unrelated to module names. |
|
224 |
// |
|
225 |
Pattern whitespaceChar = Pattern.compile("\\s"); |
|
58416 | 226 |
List<String> moduleList = new Executor().dumpOutput().setExecutable( |
227 |
appImageRuntimePath.resolve( |
|
228 |
JPackageCommand.relativePathInRuntime(JavaTool.JAVA))).addArguments( |
|
58463 | 229 |
"--list-modules").executeAndGetOutput().stream().dropWhile( |
230 |
s -> whitespaceChar.matcher(s).find()).sorted().collect( |
|
231 |
Collectors.toList()); |
|
58416 | 232 |
|
233 |
List<String> expectedModules = new ArrayList<>(); |
|
234 |
expectedModules.add(String.format("java.base@%s", |
|
235 |
System.getProperty("java.version"))); |
|
236 |
if (moduleName != null) { |
|
237 |
expectedModules.add(moduleName); |
|
238 |
} |
|
239 |
expectedModules = expectedModules.stream().sorted().collect( |
|
240 |
Collectors.toList()); |
|
241 |
||
242 |
TKit.assertStringListEquals(expectedModules, moduleList, |
|
243 |
String.format( |
|
244 |
"Check modules in application image runtime directory at [%s]", |
|
245 |
appImageRuntimePath)); |
|
246 |
}); |
|
247 |
} |
|
248 |
||
58463 | 249 |
private static Executor getJPackageToolProvider() { |
250 |
return getToolProvider(JavaTool.JPACKAGE); |
|
58416 | 251 |
} |
252 |
||
58463 | 253 |
private static Executor getToolProvider(JavaTool tool) { |
254 |
return new Executor() |
|
255 |
.dumpOutput().saveOutput() |
|
256 |
.setToolProvider(tool.asToolProvider()); |
|
58416 | 257 |
} |
258 |
} |