Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/jlink/basic/BasicTest.java
41152 views
1
/*
2
* Copyright (c) 2015, 2018, 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
/*
25
* @test
26
* @summary Basic test of jlink to create jmods and images
27
* @author Andrei Eremeev
28
* @library /test/lib
29
* @modules java.base/jdk.internal.module
30
* jdk.jlink
31
* jdk.compiler
32
* @build jdk.test.lib.process.ProcessTools
33
* jdk.test.lib.process.OutputAnalyzer
34
* jdk.test.lib.compiler.CompilerUtils
35
* jdk.test.lib.util.JarUtils
36
* @run main BasicTest
37
*/
38
39
import java.io.File;
40
import java.io.PrintWriter;
41
import java.nio.file.Files;
42
import java.nio.file.Path;
43
import java.nio.file.Paths;
44
import java.util.ArrayList;
45
import java.util.Collections;
46
import java.util.List;
47
import java.util.spi.ToolProvider;
48
49
import jdk.test.lib.compiler.CompilerUtils;
50
import jdk.test.lib.process.OutputAnalyzer;
51
import jdk.test.lib.process.ProcessTools;
52
import jdk.test.lib.util.JarUtils;
53
54
public class BasicTest {
55
static final ToolProvider JMOD_TOOL = ToolProvider.findFirst("jmod")
56
.orElseThrow(() ->
57
new RuntimeException("jmod tool not found")
58
);
59
60
static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
61
.orElseThrow(() ->
62
new RuntimeException("jlink tool not found")
63
);
64
65
private final String TEST_MODULE = "test";
66
private final Path jdkHome = Paths.get(System.getProperty("test.jdk"));
67
private final Path jdkMods = jdkHome.resolve("jmods");
68
private final Path testSrc = Paths.get(System.getProperty("test.src"));
69
private final Path src = testSrc.resolve("src").resolve(TEST_MODULE);
70
private final Path classes = Paths.get("classes");
71
private final Path jmods = Paths.get("jmods");
72
private final Path jars = Paths.get("jars");
73
74
public static void main(String[] args) throws Throwable {
75
new BasicTest().run();
76
}
77
78
public void run() throws Throwable {
79
if (Files.notExists(jdkMods)) {
80
return;
81
}
82
83
if (!CompilerUtils.compile(src, classes)) {
84
throw new AssertionError("Compilation failure. See log.");
85
}
86
87
Files.createDirectories(jmods);
88
Files.createDirectories(jars);
89
Path jarfile = jars.resolve("test.jar");
90
JarUtils.createJarFile(jarfile, classes);
91
92
Path image = Paths.get("mysmallimage");
93
runJmod(jarfile.toString(), TEST_MODULE, true);
94
runJlink(image, TEST_MODULE, "--compress", "2", "--launcher", "foo=" + TEST_MODULE);
95
execute(image, "foo");
96
97
Files.delete(jmods.resolve(TEST_MODULE + ".jmod"));
98
99
image = Paths.get("myimage");
100
runJmod(classes.toString(), TEST_MODULE, true);
101
runJlink(image, TEST_MODULE, "--launcher", "bar=" + TEST_MODULE);
102
execute(image, "bar");
103
Files.delete(jmods.resolve(TEST_MODULE + ".jmod"));
104
105
image = Paths.get("myimage2");
106
runJmod(classes.toString(), TEST_MODULE, false /* no ModuleMainClass! */);
107
// specify main class in --launcher command line
108
runJlink(image, TEST_MODULE, "--launcher", "bar2=" + TEST_MODULE + "/jdk.test.Test");
109
execute(image, "bar2");
110
Files.delete(jmods.resolve(TEST_MODULE + ".jmod"));
111
112
image = Paths.get("myadder");
113
runJmod(classes.toString(), TEST_MODULE, false /* no ModuleMainClass! */);
114
// specify main class in --launcher command line
115
runJlink(image, TEST_MODULE, "--launcher", "adder=" + TEST_MODULE + "/jdk.test.Adder");
116
addAndCheck(image, "adder");
117
}
118
119
private void addAndCheck(Path image, String scriptName) throws Throwable {
120
String cmd = image.resolve("bin").resolve(scriptName).toString();
121
OutputAnalyzer analyzer;
122
if (System.getProperty("os.name").startsWith("Windows")) {
123
analyzer = ProcessTools.executeProcess("sh.exe", cmd, "12", "8", "7", "--", "foo bar");
124
} else {
125
analyzer = ProcessTools.executeProcess(cmd, "12", "8", "7", "--", "foo bar");
126
}
127
if (analyzer.getExitValue() != 27) {
128
throw new AssertionError("Image invocation failed: expected 27, rc=" + analyzer.getExitValue());
129
}
130
// last argument contains space and should be properly quoted.
131
analyzer.stdoutShouldContain("Num args: 5");
132
}
133
134
private void execute(Path image, String scriptName) throws Throwable {
135
String cmd = image.resolve("bin").resolve(scriptName).toString();
136
OutputAnalyzer analyzer;
137
if (System.getProperty("os.name").startsWith("Windows")) {
138
analyzer = ProcessTools.executeProcess("sh.exe", cmd, "1", "2", "3");
139
} else {
140
analyzer = ProcessTools.executeProcess(cmd, "1", "2", "3");
141
}
142
if (analyzer.getExitValue() != 0) {
143
throw new AssertionError("Image invocation failed: rc=" + analyzer.getExitValue());
144
}
145
}
146
147
private void runJlink(Path image, String modName, String... options) {
148
List<String> args = new ArrayList<>();
149
Collections.addAll(args,
150
"--module-path", jdkMods + File.pathSeparator + jmods,
151
"--add-modules", modName,
152
"--output", image.toString());
153
Collections.addAll(args, options);
154
155
PrintWriter pw = new PrintWriter(System.out);
156
int rc = JLINK_TOOL.run(pw, pw, args.toArray(new String[args.size()]));
157
if (rc != 0) {
158
throw new AssertionError("Jlink failed: rc = " + rc);
159
}
160
}
161
162
private void runJmod(String cp, String modName, boolean main) {
163
int rc;
164
if (main) {
165
rc = JMOD_TOOL.run(System.out, System.out, new String[] {
166
"create",
167
"--class-path", cp,
168
"--module-version", "1.0",
169
"--main-class", "jdk.test.Test",
170
jmods.resolve(modName + ".jmod").toString()
171
});
172
} else {
173
rc = JMOD_TOOL.run(System.out, System.out, new String[] {
174
"create",
175
"--class-path", cp,
176
"--module-version", "1.0",
177
jmods.resolve(modName + ".jmod").toString(),
178
});
179
}
180
181
if (rc != 0) {
182
throw new AssertionError("Jmod failed: rc = " + rc);
183
}
184
}
185
}
186
187