58113
|
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 |
package jdk.jpackage.test;
|
|
24 |
|
|
25 |
import java.io.File;
|
|
26 |
import java.io.IOException;
|
|
27 |
import java.nio.file.Files;
|
|
28 |
import java.nio.file.Path;
|
|
29 |
import java.util.Collections;
|
|
30 |
import java.util.Enumeration;
|
|
31 |
import java.util.List;
|
|
32 |
import java.util.concurrent.atomic.AtomicInteger;
|
|
33 |
|
|
34 |
|
|
35 |
public class HelloApp {
|
|
36 |
static void addTo(JPackageCommand cmd) {
|
|
37 |
cmd.addAction(new Runnable() {
|
|
38 |
@Override
|
|
39 |
public void run() {
|
|
40 |
String mainClass = "Hello";
|
|
41 |
Path jar = cmd.inputDir().resolve("hello.jar");
|
|
42 |
new JarBuilder()
|
|
43 |
.setOutputJar(jar.toFile())
|
|
44 |
.setMainClass(mainClass)
|
|
45 |
.addSourceFile(Test.TEST_SRC_ROOT.resolve(
|
|
46 |
Path.of("apps", "image", mainClass + ".java")))
|
|
47 |
.create();
|
|
48 |
cmd.addArguments("--main-jar", jar.getFileName().toString());
|
|
49 |
cmd.addArguments("--main-class", mainClass);
|
|
50 |
}
|
|
51 |
});
|
|
52 |
|
|
53 |
if (PackageType.WINDOWS.contains(cmd.packageType())) {
|
|
54 |
cmd.addArguments("--win-console");
|
|
55 |
}
|
|
56 |
}
|
|
57 |
|
|
58 |
public static void verifyOutputFile(Path outputFile, String ... args) {
|
|
59 |
Test.assertFileExists(outputFile, true);
|
|
60 |
|
|
61 |
List<String> output = null;
|
|
62 |
try {
|
|
63 |
output = Files.readAllLines(outputFile);
|
|
64 |
} catch (IOException ex) {
|
|
65 |
throw new RuntimeException(ex);
|
|
66 |
}
|
|
67 |
|
|
68 |
final int expectedNumberOfLines = 2 + args.length;
|
|
69 |
Test.assertEquals(expectedNumberOfLines, output.size(), String.format(
|
|
70 |
"Check file [%s] contains %d text lines", outputFile,
|
|
71 |
expectedNumberOfLines));
|
|
72 |
|
|
73 |
Test.assertEquals("jpackage test application", output.get(0),
|
|
74 |
String.format(
|
|
75 |
"Check contents of the first text line in [%s] file",
|
|
76 |
outputFile));
|
|
77 |
|
|
78 |
Test.assertEquals(String.format("args.length: %d", args.length),
|
|
79 |
output.get(1), String.format(
|
|
80 |
"Check contents of the second text line in [%s] file",
|
|
81 |
outputFile));
|
|
82 |
|
|
83 |
Enumeration<String> argsEnum = Collections.enumeration(List.of(args));
|
|
84 |
AtomicInteger counter = new AtomicInteger(2);
|
|
85 |
output.stream().skip(2).sequential().forEach(line -> Test.assertEquals(
|
|
86 |
argsEnum.nextElement(), line, String.format(
|
|
87 |
"Check contents of %d text line in [%s] file",
|
|
88 |
counter.incrementAndGet(), outputFile)));
|
|
89 |
}
|
|
90 |
|
|
91 |
public static void executeAndVerifyOutput(Path helloAppLauncher,
|
|
92 |
String... defaultLauncherArgs) {
|
|
93 |
File outputFile = Test.workDir().resolve(OUTPUT_FILENAME).toFile();
|
|
94 |
new Executor()
|
|
95 |
.setDirectory(outputFile.getParentFile().toPath())
|
|
96 |
.setExecutable(helloAppLauncher.toString())
|
|
97 |
.execute()
|
|
98 |
.assertExitCodeIsZero();
|
|
99 |
|
|
100 |
verifyOutputFile(outputFile.toPath(), defaultLauncherArgs);
|
|
101 |
}
|
|
102 |
|
|
103 |
public final static String OUTPUT_FILENAME = "appOutput.txt";
|
|
104 |
}
|