Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/jlink/multireleasejar/JLinkMRJavaBaseVersionTest.java
41152 views
1
/*
2
* Copyright (c) 2017, 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
* @bug 8177471
27
* @summary jlink should use the version from java.base.jmod to find modules
28
* @bug 8185130
29
* @summary jlink should throw error if target image and current JDK versions don't match
30
* @modules java.base/jdk.internal.module
31
* @library /test/lib
32
* @build jdk.test.lib.process.* CheckRuntimeVersion
33
* @run testng/othervm JLinkMRJavaBaseVersionTest
34
*/
35
36
import java.io.ByteArrayInputStream;
37
import java.io.File;
38
import java.io.IOException;
39
import java.lang.module.ModuleFinder;
40
import java.lang.module.ModuleReference;
41
import java.nio.file.Files;
42
import java.nio.file.Path;
43
import java.nio.file.Paths;
44
import java.util.List;
45
import java.util.jar.JarFile;
46
import java.util.spi.ToolProvider;
47
import java.util.stream.Collectors;
48
import java.util.stream.Stream;
49
50
import jdk.internal.module.ModulePath;
51
import jdk.test.lib.process.ProcessTools;
52
import org.testng.Assert;
53
import org.testng.annotations.BeforeClass;
54
import org.testng.annotations.Test;
55
56
public class JLinkMRJavaBaseVersionTest {
57
private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
58
.orElseThrow(() -> new RuntimeException("jar tool not found"));
59
private static final ToolProvider JAVAC_TOOL = ToolProvider.findFirst("javac")
60
.orElseThrow(() -> new RuntimeException("javac tool not found"));
61
private static final ToolProvider JLINK_TOOL = ToolProvider.findFirst("jlink")
62
.orElseThrow(() -> new RuntimeException("jlink tool not found"));
63
64
private static final Path javaHome = Paths.get(System.getProperty("java.home"));
65
66
// resource text for version 9
67
private byte[] resource9 = ("9 resource file").getBytes();
68
// resource text for current version
69
private byte[] resource = (Runtime.version().major() + " resource file").getBytes();
70
71
static Path getJmods() {
72
Path jmods = Paths.get(System.getProperty("java9.home", javaHome.toString())).resolve("jmods");
73
if (Files.notExists(jmods)) {
74
throw new RuntimeException(jmods + " not found");
75
}
76
return jmods;
77
}
78
79
@BeforeClass
80
public void initialize() throws IOException {
81
Path srcdir = Paths.get(System.getProperty("test.src"));
82
83
// create class files from source
84
Path base = srcdir.resolve("base");
85
Path mr9 = Paths.get("mr9");
86
javac(base, mr9, base.toString());
87
88
// current version
89
Path rt = srcdir.resolve("rt");
90
Path rtmods = Paths.get("rtmods");
91
javac(rt, rtmods, rt.toString());
92
93
// build multi-release jar file with different module-info.class
94
String[] args = {
95
"-cf", "m1.jar",
96
"-C", rtmods.resolve("m1").toString(), ".",
97
"--release ", "9",
98
"-C", mr9.resolve("m1").toString(), "."
99
100
};
101
JAR_TOOL.run(System.out, System.err, args);
102
}
103
104
private void javac(Path source, Path destination, String srcpath) throws IOException {
105
String[] args = Stream.concat(
106
Stream.of("-d", destination.toString(), "--release", "9",
107
"--module-source-path", srcpath),
108
Files.walk(source)
109
.map(Path::toString)
110
.filter(s -> s.endsWith(".java"))
111
).toArray(String[]::new);
112
int rc = JAVAC_TOOL.run(System.out, System.err, args);
113
Assert.assertEquals(rc, 0);
114
}
115
116
@Test
117
public void basicTest() throws Throwable {
118
if (Files.notExists(javaHome.resolve("lib").resolve("modules"))) {
119
// exploded image
120
return;
121
}
122
123
Runtime.Version version = targetRuntimeVersion();
124
System.out.println("Testing jlink with " + getJmods() + " of target version " + version);
125
126
// use jlink to build image from multi-release jar
127
if (jlink("m1.jar", "myimage")) {
128
return;
129
}
130
131
// validate runtime image
132
Path java = Paths.get("myimage", "bin", "java");
133
ProcessTools.executeProcess(java.toString(), "-m", "m1/p.Main");
134
135
// validate the image linked with the proper MR version
136
137
if (!version.equalsIgnoreOptional(Runtime.version())) {
138
ProcessTools.executeProcess(java.toString(), "-cp", System.getProperty("test.classes"),
139
"CheckRuntimeVersion", String.valueOf(version.major()),
140
"java.base", "m1")
141
.shouldHaveExitValue(0);
142
}
143
}
144
145
private Runtime.Version targetRuntimeVersion() {
146
ModuleReference mref = ModulePath.of(Runtime.version(), true, getJmods())
147
.find("java.base")
148
.orElseThrow(() -> new RuntimeException("java.base not found from " + getJmods()));
149
150
return Runtime.Version.parse(mref.descriptor().version().get().toString());
151
}
152
153
private boolean jlink(String jar, String image) {
154
List<String> args = List.of("--output", image,
155
"--add-modules", "m1",
156
"--module-path",
157
getJmods().toString() + File.pathSeparator + jar);
158
System.out.println("jlink " + args.stream().collect(Collectors.joining(" ")));
159
int exitCode = JLINK_TOOL.run(System.out, System.err, args.toArray(new String[0]));
160
boolean isJDK9 = System.getProperty("java9.home") != null;
161
if (isJDK9) {
162
Assert.assertNotEquals(exitCode, 0);
163
} else {
164
Assert.assertEquals(exitCode, 0);
165
}
166
return isJDK9;
167
}
168
}
169
170