author | herrick |
Wed, 16 Oct 2019 10:32:08 -0400 | |
branch | JDK-8200758-branch |
changeset 58648 | 3bf53ffa9ae7 |
parent 58466 | 47f0d21c7e8d |
child 58671 | 3b578a5976df |
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; |
|
58648 | 31 |
import java.util.function.Function; |
32 |
import java.util.function.Predicate; |
|
58463 | 33 |
import java.util.regex.Pattern; |
58416 | 34 |
import java.util.stream.Collectors; |
58648 | 35 |
import java.util.stream.Stream; |
36 |
import jdk.jpackage.test.*; |
|
58416 | 37 |
import jdk.jpackage.test.Annotations.*; |
38 |
||
39 |
/* |
|
40 |
* @test |
|
41 |
* @summary jpackage basic testing |
|
42 |
* @library ../../../../helpers |
|
43 |
* @build jdk.jpackage.test.* |
|
58648 | 44 |
* @modules jdk.jpackage/jdk.jpackage.internal |
58416 | 45 |
* @compile BasicTest.java |
46 |
* @run main/othervm/timeout=360 -Xmx512m jdk.jpackage.test.Main |
|
47 |
* --jpt-run=jdk.jpackage.tests.BasicTest |
|
48 |
*/ |
|
49 |
||
58648 | 50 |
public final class BasicTest { |
58416 | 51 |
@Test |
52 |
public void testNoArgs() { |
|
53 |
List<String> output = JPackageCommand.filterOutput( |
|
58463 | 54 |
getJPackageToolProvider().executeAndGetOutput()); |
58416 | 55 |
TKit.assertStringListEquals(List.of("Usage: jpackage <mode> <options>", |
56 |
"Use jpackage --help (or -h) for a list of possible options"), |
|
57 |
output, "Check jpackage output"); |
|
58 |
} |
|
59 |
||
60 |
@Test |
|
61 |
public void testVersion() { |
|
62 |
List<String> output = JPackageCommand.filterOutput( |
|
58463 | 63 |
getJPackageToolProvider() |
58416 | 64 |
.addArgument("--version") |
65 |
.executeAndGetOutput()); |
|
66 |
TKit.assertStringListEquals(List.of(System.getProperty("java.version")), |
|
67 |
output, "Check jpackage output"); |
|
68 |
} |
|
69 |
||
70 |
@Test |
|
58648 | 71 |
public void testHelp() { |
72 |
List<String> hOutput = getJPackageToolProvider() |
|
73 |
.addArgument("-h").executeAndGetOutput(); |
|
74 |
List<String> helpOutput = getJPackageToolProvider() |
|
75 |
.addArgument("--help").executeAndGetOutput(); |
|
76 |
||
77 |
TKit.assertStringListEquals(hOutput, helpOutput, |
|
78 |
"Check -h and --help parameters produce the same output"); |
|
79 |
||
80 |
final String windowsPrefix = "--win-"; |
|
81 |
final String linuxPrefix = "--linux-"; |
|
82 |
final String osxPrefix = "--mac-"; |
|
83 |
||
84 |
final String expectedPrefix; |
|
85 |
final List<String> unexpectedPrefixes; |
|
86 |
||
87 |
if (TKit.isWindows()) { |
|
88 |
expectedPrefix = windowsPrefix; |
|
89 |
unexpectedPrefixes = List.of(osxPrefix, linuxPrefix); |
|
90 |
} else if (TKit.isLinux()) { |
|
91 |
expectedPrefix = linuxPrefix; |
|
92 |
unexpectedPrefixes = List.of(windowsPrefix, osxPrefix); |
|
93 |
} else if (TKit.isOSX()) { |
|
94 |
expectedPrefix = osxPrefix; |
|
95 |
unexpectedPrefixes = List.of(linuxPrefix, windowsPrefix); |
|
96 |
} else { |
|
97 |
throw TKit.throwUnknownPlatformError(); |
|
98 |
} |
|
99 |
||
100 |
Function<String, Predicate<String>> createPattern = (prefix) -> { |
|
101 |
return Pattern.compile("^ " + prefix).asPredicate(); |
|
102 |
}; |
|
103 |
||
104 |
Function<List<String>, Long> countStrings = (prefixes) -> { |
|
105 |
return hOutput.stream().filter( |
|
106 |
prefixes.stream().map(createPattern).reduce(x -> false, |
|
107 |
Predicate::or)).peek(TKit::trace).count(); |
|
108 |
}; |
|
109 |
||
110 |
TKit.trace("Check parameters in help text"); |
|
111 |
TKit.assertNotEquals(0, countStrings.apply(List.of(expectedPrefix)), |
|
112 |
"Check help text contains plaform specific parameters"); |
|
113 |
TKit.assertEquals(0, countStrings.apply(unexpectedPrefixes), |
|
114 |
"Check help text doesn't contain unexpected parameters"); |
|
115 |
} |
|
116 |
||
117 |
@Test |
|
118 |
public void testVerbose() { |
|
119 |
JPackageCommand cmd = JPackageCommand.helloAppImage() |
|
120 |
.setFakeRuntime().executePrerequisiteActions(); |
|
121 |
||
122 |
List<String> expectedVerboseOutputStrings = new ArrayList<>(); |
|
123 |
expectedVerboseOutputStrings.add("Creating app package:"); |
|
124 |
if (TKit.isWindows()) { |
|
125 |
expectedVerboseOutputStrings.add("Result application bundle:"); |
|
126 |
expectedVerboseOutputStrings.add( |
|
127 |
"Succeeded in building Windows Application Image package"); |
|
128 |
} else if (TKit.isLinux()) { |
|
129 |
expectedVerboseOutputStrings.add( |
|
130 |
"Succeeded in building Linux Application Image package"); |
|
131 |
} else if (TKit.isOSX()) { |
|
132 |
expectedVerboseOutputStrings.add("Preparing Info.plist:"); |
|
133 |
expectedVerboseOutputStrings.add( |
|
134 |
"Succeeded in building Mac Application Image package"); |
|
135 |
} else { |
|
136 |
TKit.throwUnknownPlatformError(); |
|
137 |
} |
|
138 |
||
139 |
TKit.deleteDirectoryContentsRecursive(cmd.outputDir()); |
|
140 |
List<String> nonVerboseOutput = cmd.createExecutor().executeAndGetOutput(); |
|
141 |
||
142 |
TKit.deleteDirectoryContentsRecursive(cmd.outputDir()); |
|
143 |
List<String> verboseOutput = cmd.createExecutor().addArgument( |
|
144 |
"--verbose").executeAndGetOutput(); |
|
145 |
||
146 |
TKit.assertTrue(nonVerboseOutput.size() < verboseOutput.size(), |
|
147 |
"Check verbose output is longer than regular"); |
|
148 |
||
149 |
expectedVerboseOutputStrings.forEach(str -> { |
|
150 |
TKit.assertTextStream(str).label("regular output") |
|
151 |
.predicate(String::contains).negate() |
|
152 |
.apply(nonVerboseOutput.stream()); |
|
153 |
}); |
|
154 |
||
155 |
expectedVerboseOutputStrings.forEach(str -> { |
|
156 |
TKit.assertTextStream(str).label("verbose output") |
|
157 |
.apply(verboseOutput.stream()); |
|
158 |
}); |
|
159 |
} |
|
160 |
||
161 |
@Test |
|
58416 | 162 |
public void testNoName() { |
163 |
final String mainClassName = "Greetings"; |
|
164 |
||
58648 | 165 |
JPackageCommand cmd = JPackageCommand.helloAppImage(mainClassName) |
58416 | 166 |
.removeArgumentWithValue("--name"); |
167 |
||
168 |
Path expectedImageDir = cmd.outputDir().resolve(mainClassName); |
|
169 |
if (TKit.isOSX()) { |
|
170 |
expectedImageDir = expectedImageDir.getParent().resolve( |
|
171 |
expectedImageDir.getFileName().toString() + ".app"); |
|
172 |
} |
|
173 |
||
174 |
cmd.executeAndAssertHelloAppImageCreated(); |
|
175 |
TKit.assertEquals(expectedImageDir.toAbsolutePath().normalize().toString(), |
|
58648 | 176 |
cmd.outputBundle().toAbsolutePath().normalize().toString(), |
58416 | 177 |
String.format( |
178 |
"Check [%s] directory is filled with application image data", |
|
179 |
expectedImageDir)); |
|
180 |
} |
|
181 |
||
182 |
@Test |
|
58648 | 183 |
// Regular app |
184 |
@Parameter("Hello") |
|
185 |
// Modular app |
|
186 |
@Parameter("com.other/com.other.Hello") |
|
187 |
public void testApp(String javaAppDesc) { |
|
188 |
JPackageCommand.helloAppImage(javaAppDesc) |
|
58416 | 189 |
.executeAndAssertHelloAppImageCreated(); |
190 |
} |
|
191 |
||
192 |
@Test |
|
58648 | 193 |
public void testWhitespaceInPaths() { |
194 |
JPackageCommand.helloAppImage("a/b c.jar:Hello") |
|
195 |
.setArgumentValue("--input", TKit.workDir().resolve("The quick brown fox")) |
|
196 |
.setArgumentValue("--dest", TKit.workDir().resolve("jumps over the lazy dog")) |
|
58416 | 197 |
.executeAndAssertHelloAppImageCreated(); |
198 |
} |
|
199 |
||
200 |
@Test |
|
58466
47f0d21c7e8d
8231882: --add-modules ALL-MODULE-PATH is not handled properly
herrick
parents:
58463
diff
changeset
|
201 |
@Parameter("ALL-MODULE-PATH") |
58416 | 202 |
@Parameter("ALL-DEFAULT") |
58648 | 203 |
@Parameter("java.desktop") |
204 |
@Parameter("java.desktop,jdk.jartool") |
|
205 |
@Parameter({ "java.desktop", "jdk.jartool" }) |
|
206 |
public void testAddModules(String... addModulesArg) { |
|
207 |
JPackageCommand cmd = JPackageCommand |
|
208 |
.helloAppImage("goodbye.jar:com.other/com.other.Hello"); |
|
209 |
Stream.of(addModulesArg).map(v -> Stream.of("--add-modules", v)).flatMap( |
|
210 |
s -> s).forEachOrdered(cmd::addArgument); |
|
58416 | 211 |
cmd.executeAndAssertHelloAppImageCreated(); |
212 |
} |
|
213 |
||
214 |
/** |
|
215 |
* Test --temp option. Doesn't make much sense for app image as temporary |
|
58648 | 216 |
* directory is used only on Windows. Test it in packaging mode. |
58416 | 217 |
* @throws IOException |
218 |
*/ |
|
58648 | 219 |
@Test |
58416 | 220 |
public void testTemp() throws IOException { |
58648 | 221 |
JPackageCommand cmd = JPackageCommand.helloAppImage() |
222 |
.removeArgumentWithValue("--package-type") |
|
223 |
.addArguments("--verbose") |
|
224 |
.setFakeRuntime(); |
|
225 |
||
58416 | 226 |
TKit.withTempDirectory("temp-root", tempDir -> { |
227 |
cmd.addArguments("--temp", tempDir); |
|
228 |
||
58648 | 229 |
cmd.execute().assertExitCodeIsZero(); |
58416 | 230 |
|
231 |
// Check jpackage actually used the supplied directory. |
|
232 |
TKit.assertNotEquals(0, tempDir.toFile().list().length, |
|
233 |
String.format( |
|
234 |
"Check jpackage wrote some data in the supplied temporary directory [%s]", |
|
235 |
tempDir)); |
|
236 |
||
237 |
// Temporary directory should not be empty, |
|
238 |
// jpackage should exit with error. |
|
239 |
cmd.execute().assertExitCodeIs(1); |
|
240 |
}); |
|
241 |
} |
|
242 |
||
243 |
@Test |
|
244 |
public void testAtFile() throws IOException { |
|
58648 | 245 |
JPackageCommand cmd = JPackageCommand.helloAppImage(); |
58416 | 246 |
|
247 |
// Init options file with the list of options configured |
|
248 |
// for JPackageCommand instance. |
|
249 |
final Path optionsFile = TKit.workDir().resolve("options"); |
|
250 |
Files.write(optionsFile, |
|
251 |
List.of(String.join(" ", cmd.getAllArguments()))); |
|
252 |
||
253 |
// Build app jar file. |
|
254 |
cmd.executePrerequisiteActions(); |
|
255 |
||
256 |
// Make sure output directory is empty. Normally JPackageCommand would |
|
257 |
// do this automatically. |
|
258 |
TKit.deleteDirectoryContentsRecursive(cmd.outputDir()); |
|
259 |
||
260 |
// Instead of running jpackage command through configured |
|
261 |
// JPackageCommand instance, run vanilla jpackage command with @ file. |
|
58463 | 262 |
getJPackageToolProvider() |
58416 | 263 |
.addArgument(String.format("@%s", optionsFile)) |
264 |
.execute().assertExitCodeIsZero(); |
|
265 |
||
266 |
// Verify output of jpackage command. |
|
267 |
cmd.assertImageCreated(); |
|
268 |
HelloApp.executeLauncherAndVerifyOutput(cmd); |
|
269 |
} |
|
270 |
||
271 |
@Parameter("Hello") |
|
272 |
@Parameter("com.foo/com.foo.main.Aloha") |
|
273 |
@Test |
|
274 |
public void testJLinkRuntime(String javaAppDesc) { |
|
58463 | 275 |
JPackageCommand cmd = JPackageCommand.helloAppImage(javaAppDesc); |
58416 | 276 |
|
277 |
// If `--module` parameter was set on jpackage command line, get its |
|
278 |
// value and extract module name. |
|
279 |
// E.g.: foo.bar2/foo.bar.Buz -> foo.bar2 |
|
280 |
// Note: HelloApp class manages `--module` parameter on jpackage command line |
|
281 |
final String moduleName = cmd.getArgumentValue("--module", () -> null, |
|
282 |
(v) -> v.split("/", 2)[0]); |
|
283 |
||
284 |
if (moduleName != null) { |
|
285 |
// Build module jar. |
|
286 |
cmd.executePrerequisiteActions(); |
|
287 |
} |
|
288 |
||
289 |
TKit.withTempDirectory("runtime", runtimeDir -> { |
|
290 |
TKit.deleteDirectoryRecursive(runtimeDir, String.format( |
|
291 |
"Delete [%s] output directory for jlink command", runtimeDir)); |
|
58463 | 292 |
Executor jlink = getToolProvider(JavaTool.JLINK) |
58416 | 293 |
.saveOutput(false) |
294 |
.addArguments( |
|
295 |
"--add-modules", "java.base", |
|
296 |
"--output", runtimeDir.toString(), |
|
297 |
"--strip-debug", |
|
298 |
"--no-header-files", |
|
299 |
"--no-man-pages"); |
|
300 |
||
301 |
if (moduleName != null) { |
|
302 |
jlink.addArguments("--add-modules", moduleName, "--module-path", |
|
303 |
Path.of(cmd.getArgumentValue("--module-path")).resolve( |
|
304 |
"hello.jar").toString()); |
|
305 |
} |
|
306 |
||
307 |
jlink.execute().assertExitCodeIsZero(); |
|
308 |
||
309 |
cmd.addArguments("--runtime-image", runtimeDir); |
|
310 |
cmd.executeAndAssertHelloAppImageCreated(); |
|
311 |
||
58648 | 312 |
final Path appImageRuntimePath = cmd.appRuntimeDirectory(); |
58416 | 313 |
|
58463 | 314 |
// |
58416 | 315 |
// This is an overkill to list modules in jlink output as we have |
316 |
// already verified that Java app is functional and thus app's runtime |
|
317 |
// is likely to be OK, but let's double check. |
|
58463 | 318 |
// |
319 |
// Filter out all first strings with whitespace. They are java |
|
320 |
// launcher output like `Picked up ...` unrelated to module names. |
|
321 |
// |
|
322 |
Pattern whitespaceChar = Pattern.compile("\\s"); |
|
58416 | 323 |
List<String> moduleList = new Executor().dumpOutput().setExecutable( |
324 |
appImageRuntimePath.resolve( |
|
325 |
JPackageCommand.relativePathInRuntime(JavaTool.JAVA))).addArguments( |
|
58463 | 326 |
"--list-modules").executeAndGetOutput().stream().dropWhile( |
327 |
s -> whitespaceChar.matcher(s).find()).sorted().collect( |
|
328 |
Collectors.toList()); |
|
58416 | 329 |
|
330 |
List<String> expectedModules = new ArrayList<>(); |
|
331 |
expectedModules.add(String.format("java.base@%s", |
|
332 |
System.getProperty("java.version"))); |
|
333 |
if (moduleName != null) { |
|
334 |
expectedModules.add(moduleName); |
|
335 |
} |
|
336 |
expectedModules = expectedModules.stream().sorted().collect( |
|
337 |
Collectors.toList()); |
|
338 |
||
339 |
TKit.assertStringListEquals(expectedModules, moduleList, |
|
340 |
String.format( |
|
341 |
"Check modules in application image runtime directory at [%s]", |
|
342 |
appImageRuntimePath)); |
|
343 |
}); |
|
344 |
} |
|
345 |
||
58463 | 346 |
private static Executor getJPackageToolProvider() { |
347 |
return getToolProvider(JavaTool.JPACKAGE); |
|
58416 | 348 |
} |
349 |
||
58463 | 350 |
private static Executor getToolProvider(JavaTool tool) { |
58648 | 351 |
return new Executor().dumpOutput().saveOutput().setToolProvider(tool); |
58416 | 352 |
} |
353 |
} |