author | herrick |
Wed, 16 Oct 2019 10:32:08 -0400 | |
branch | JDK-8200758-branch |
changeset 58648 | 3bf53ffa9ae7 |
parent 58416 | f09bf58c1f17 |
child 58696 | 61c44899b4eb |
permissions | -rw-r--r-- |
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; |
|
58416 | 29 |
import java.util.ArrayList; |
58113 | 30 |
import java.util.List; |
58416 | 31 |
import java.util.concurrent.atomic.AtomicBoolean; |
32 |
import java.util.regex.Matcher; |
|
33 |
import java.util.regex.Pattern; |
|
34 |
import java.util.stream.Collectors; |
|
35 |
import jdk.jpackage.test.Functional.ThrowingFunction; |
|
36 |
import jdk.jpackage.test.Functional.ThrowingSupplier; |
|
58113 | 37 |
|
38 |
public class HelloApp { |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
39 |
|
58648 | 40 |
HelloApp(JavaAppDesc appDesc) { |
41 |
if (appDesc == null) { |
|
42 |
this.appDesc = createDefaltAppDesc(); |
|
43 |
} else { |
|
44 |
this.appDesc = appDesc; |
|
45 |
} |
|
58416 | 46 |
} |
47 |
||
48 |
private JarBuilder prepareSources(Path srcDir) throws IOException { |
|
58648 | 49 |
final String qualifiedClassName = appDesc.className(); |
50 |
||
58416 | 51 |
final String className = qualifiedClassName.substring( |
52 |
qualifiedClassName.lastIndexOf('.') + 1); |
|
58648 | 53 |
final String packageName = appDesc.packageName(); |
58416 | 54 |
|
55 |
final Path srcFile = srcDir.resolve(Path.of(String.join( |
|
56 |
File.separator, qualifiedClassName.split("\\.")) + ".java")); |
|
57 |
Files.createDirectories(srcFile.getParent()); |
|
58 |
||
59 |
JarBuilder jarBuilder = createJarBuilder().addSourceFile(srcFile); |
|
58648 | 60 |
final String moduleName = appDesc.moduleName(); |
58416 | 61 |
if (moduleName != null) { |
62 |
Path moduleInfoFile = srcDir.resolve("module-info.java"); |
|
63 |
TKit.createTextFile(moduleInfoFile, List.of( |
|
64 |
String.format("module %s {", moduleName), |
|
65 |
String.format(" exports %s;", packageName), |
|
66 |
"}" |
|
67 |
)); |
|
68 |
jarBuilder.addSourceFile(moduleInfoFile); |
|
58648 | 69 |
jarBuilder.setModuleVersion(appDesc.moduleVersion()); |
58416 | 70 |
} |
71 |
||
72 |
// Add package directive and replace class name in java source file. |
|
73 |
// Works with simple test Hello.java. |
|
74 |
// Don't expect too much from these regexps! |
|
75 |
Pattern classDeclaration = Pattern.compile("(^.*\\bclass\\s+)Hello(.*$)"); |
|
76 |
Pattern importDirective = Pattern.compile( |
|
77 |
"(?<=import (?:static )?+)[^;]+"); |
|
78 |
AtomicBoolean classDeclared = new AtomicBoolean(); |
|
79 |
AtomicBoolean packageInserted = new AtomicBoolean(packageName == null); |
|
80 |
||
81 |
var packageInserter = Functional.identityFunction((line) -> { |
|
82 |
packageInserted.setPlain(true); |
|
83 |
return String.format("package %s;%s%s", packageName, |
|
84 |
System.lineSeparator(), line); |
|
85 |
}); |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
86 |
|
58416 | 87 |
Files.write(srcFile, Files.readAllLines(HELLO_JAVA).stream().map(line -> { |
88 |
if (classDeclared.getPlain()) { |
|
89 |
return line; |
|
90 |
} |
|
91 |
||
92 |
Matcher m; |
|
93 |
if (!packageInserted.getPlain() && importDirective.matcher(line).find()) { |
|
94 |
line = packageInserter.apply(line); |
|
95 |
} else if ((m = classDeclaration.matcher(line)).find()) { |
|
96 |
classDeclared.setPlain(true); |
|
97 |
line = m.group(1) + className + m.group(2); |
|
98 |
if (!packageInserted.getPlain()) { |
|
99 |
line = packageInserter.apply(line); |
|
100 |
} |
|
101 |
} |
|
102 |
return line; |
|
103 |
}).collect(Collectors.toList())); |
|
104 |
||
105 |
return jarBuilder; |
|
106 |
} |
|
107 |
||
108 |
private JarBuilder createJarBuilder() { |
|
58648 | 109 |
JarBuilder builder = new JarBuilder(); |
110 |
if (appDesc.jarWithMainClass()) { |
|
111 |
builder.setMainClass(appDesc.className()); |
|
112 |
} |
|
113 |
return builder; |
|
58416 | 114 |
} |
115 |
||
58648 | 116 |
void addTo(JPackageCommand cmd) { |
117 |
final String moduleName = appDesc.moduleName(); |
|
118 |
final String jarFileName = appDesc.jarFileName(); |
|
119 |
final String qualifiedClassName = appDesc.className(); |
|
120 |
||
121 |
if (moduleName != null && appDesc.packageName() == null) { |
|
58416 | 122 |
throw new IllegalArgumentException(String.format( |
123 |
"Module [%s] with default package", moduleName)); |
|
124 |
} |
|
125 |
||
126 |
if (moduleName == null && CLASS_NAME.equals(qualifiedClassName)) { |
|
127 |
// Use Hello.java as is. |
|
128 |
cmd.addAction((self) -> { |
|
129 |
File jarFile = self.inputDir().resolve(jarFileName).toFile(); |
|
130 |
createJarBuilder().setOutputJar(jarFile).addSourceFile( |
|
131 |
HELLO_JAVA).create(); |
|
132 |
}); |
|
133 |
} else { |
|
134 |
cmd.addAction((self) -> { |
|
135 |
final Path jarFile; |
|
136 |
if (moduleName == null) { |
|
137 |
jarFile = self.inputDir().resolve(jarFileName); |
|
138 |
} else { |
|
139 |
// `--module-path` option should be set by the moment |
|
140 |
// when this action is being executed. |
|
141 |
jarFile = Path.of(self.getArgumentValue("--module-path", |
|
142 |
() -> self.inputDir().toString()), jarFileName); |
|
143 |
Files.createDirectories(jarFile.getParent()); |
|
144 |
} |
|
145 |
||
146 |
TKit.withTempDirectory("src", |
|
147 |
workDir -> prepareSources(workDir).setOutputJar( |
|
148 |
jarFile.toFile()).create()); |
|
149 |
}); |
|
150 |
} |
|
151 |
||
152 |
if (moduleName == null) { |
|
153 |
cmd.addArguments("--main-jar", jarFileName); |
|
154 |
cmd.addArguments("--main-class", qualifiedClassName); |
|
155 |
} else { |
|
156 |
cmd.addArguments("--module-path", TKit.workDir().resolve( |
|
157 |
"input-modules")); |
|
158 |
cmd.addArguments("--module", String.join("/", moduleName, |
|
159 |
qualifiedClassName)); |
|
160 |
// For modular app assume nothing will go in input directory and thus |
|
161 |
// nobody will create input directory, so remove corresponding option |
|
162 |
// from jpackage command line. |
|
163 |
cmd.removeArgumentWithValue("--input"); |
|
164 |
} |
|
165 |
if (TKit.isWindows()) { |
|
58113 | 166 |
cmd.addArguments("--win-console"); |
167 |
} |
|
168 |
} |
|
169 |
||
58648 | 170 |
static JavaAppDesc createDefaltAppDesc() { |
171 |
return new JavaAppDesc().setClassName(CLASS_NAME).setJarFileName( |
|
172 |
"hello.jar"); |
|
58416 | 173 |
} |
174 |
||
175 |
static void verifyOutputFile(Path outputFile, List<String> args) { |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
176 |
if (!outputFile.isAbsolute()) { |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
177 |
verifyOutputFile(outputFile.toAbsolutePath().normalize(), args); |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
178 |
return; |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
179 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
180 |
|
58416 | 181 |
TKit.assertFileExists(outputFile); |
58113 | 182 |
|
58416 | 183 |
List<String> contents = ThrowingSupplier.toSupplier( |
184 |
() -> Files.readAllLines(outputFile)).get(); |
|
58113 | 185 |
|
58416 | 186 |
List<String> expected = new ArrayList<>(List.of( |
187 |
"jpackage test application", |
|
188 |
String.format("args.length: %d", args.size()) |
|
189 |
)); |
|
190 |
expected.addAll(args); |
|
58113 | 191 |
|
58416 | 192 |
TKit.assertStringListEquals(expected, contents, String.format( |
193 |
"Check contents of [%s] file", outputFile)); |
|
58113 | 194 |
} |
195 |
||
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
196 |
public static void executeLauncherAndVerifyOutput(JPackageCommand cmd) { |
58648 | 197 |
final Path launcherPath = cmd.appLauncherPath(); |
198 |
if (!cmd.isFakeRuntime(String.format("Not running [%s] launcher", |
|
199 |
launcherPath))) { |
|
200 |
executeAndVerifyOutput(launcherPath, cmd.getAllArgumentValues( |
|
201 |
"--arguments")); |
|
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
202 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
203 |
} |
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
204 |
|
58113 | 205 |
public static void executeAndVerifyOutput(Path helloAppLauncher, |
206 |
String... defaultLauncherArgs) { |
|
58416 | 207 |
executeAndVerifyOutput(helloAppLauncher, List.of(defaultLauncherArgs)); |
208 |
} |
|
209 |
||
210 |
public static void executeAndVerifyOutput(Path helloAppLauncher, |
|
211 |
List<String> defaultLauncherArgs) { |
|
212 |
// Output file will be created in the current directory. |
|
58648 | 213 |
Path outputFile = TKit.workDir().resolve(OUTPUT_FILENAME); |
58416 | 214 |
ThrowingFunction.toFunction(Files::deleteIfExists).apply(outputFile); |
58113 | 215 |
new Executor() |
58416 | 216 |
.setDirectory(outputFile.getParent()) |
217 |
.setExecutable(helloAppLauncher) |
|
58113 | 218 |
.execute() |
219 |
.assertExitCodeIsZero(); |
|
220 |
||
58416 | 221 |
verifyOutputFile(outputFile, defaultLauncherArgs); |
58113 | 222 |
} |
223 |
||
58301
e0efb29609bd
8225249 : LinuxDebBundler and LinuxRpmBundler should share more code
herrick
parents:
58115
diff
changeset
|
224 |
final static String OUTPUT_FILENAME = "appOutput.txt"; |
58416 | 225 |
|
58648 | 226 |
private final JavaAppDesc appDesc; |
58416 | 227 |
|
228 |
private static final Path HELLO_JAVA = TKit.TEST_SRC_ROOT.resolve( |
|
229 |
"apps/image/Hello.java"); |
|
230 |
||
231 |
private final static String CLASS_NAME = HELLO_JAVA.getFileName().toString().split( |
|
232 |
"\\.", 2)[0]; |
|
58113 | 233 |
} |