Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/tools/jar/multiRelease/MRTestBase.java
41149 views
1
/*
2
* Copyright (c) 2017, 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
import jdk.test.lib.JDKToolFinder;
25
import jdk.test.lib.Utils;
26
import jdk.test.lib.process.OutputAnalyzer;
27
import jdk.test.lib.process.ProcessTools;
28
29
import java.io.*;
30
import java.nio.file.*;
31
import java.nio.file.attribute.BasicFileAttributes;
32
import java.util.ArrayList;
33
import java.util.Arrays;
34
import java.util.List;
35
import java.util.Map;
36
import java.util.jar.JarEntry;
37
import java.util.jar.JarFile;
38
import java.util.spi.ToolProvider;
39
import java.util.stream.Stream;
40
import java.util.zip.ZipFile;
41
42
import static org.testng.Assert.assertEquals;
43
44
public class MRTestBase {
45
46
public static final int SUCCESS = 0;
47
48
protected final String src = System.getProperty("test.src", ".");
49
protected final String usr = System.getProperty("user.dir", ".");
50
51
private static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar")
52
.orElseThrow(()
53
-> new RuntimeException("jar tool not found")
54
);
55
56
private static final ToolProvider JAVAC_TOOL = ToolProvider.findFirst("javac")
57
.orElseThrow(()
58
-> new RuntimeException("javac tool not found")
59
);
60
61
protected void compile(String test) throws Throwable {
62
Path classes = Paths.get(usr, "classes", "base");
63
Files.createDirectories(classes);
64
Path source = Paths.get(src, "data", test, "base", "version");
65
javac(classes, source.resolve("Main.java"), source.resolve("Version.java"));
66
67
classes = Paths.get(usr, "classes", "v9");
68
Files.createDirectories(classes);
69
source = Paths.get(src, "data", test, "v9", "version");
70
javac(classes, source.resolve("Version.java"));
71
72
classes = Paths.get(usr, "classes", "v10");
73
Files.createDirectories(classes);
74
source = Paths.get(src, "data", test, "v10", "version");
75
javac(classes, source.resolve("Version.java"));
76
}
77
78
protected void checkMultiRelease(String jarFile,
79
boolean expected) throws IOException {
80
try (JarFile jf = new JarFile(new File(jarFile), true,
81
ZipFile.OPEN_READ, JarFile.runtimeVersion())) {
82
assertEquals(jf.isMultiRelease(), expected);
83
}
84
}
85
86
// compares the bytes found in the jar entries with the bytes found in the
87
// corresponding data files used to create the entries
88
protected void compare(String jarfile,
89
Map<String, String[]> names) throws IOException {
90
try (JarFile jf = new JarFile(jarfile)) {
91
for (String name : names.keySet()) {
92
Path path = Paths.get("classes", names.get(name));
93
byte[] b1 = Files.readAllBytes(path);
94
byte[] b2;
95
JarEntry je = jf.getJarEntry(name);
96
try (InputStream is = jf.getInputStream(je)) {
97
b2 = is.readAllBytes();
98
}
99
assertEquals(b1, b2);
100
}
101
}
102
}
103
104
void javac(Path dest, Path... sourceFiles) throws Throwable {
105
106
List<String> commands = new ArrayList<>();
107
String opts = System.getProperty("test.compiler.opts");
108
if (!opts.isEmpty()) {
109
commands.addAll(Arrays.asList(opts.split(" +")));
110
}
111
commands.addAll(Utils.getForwardVmOptions());
112
commands.add("-d");
113
commands.add(dest.toString());
114
Stream.of(sourceFiles)
115
.map(Object::toString)
116
.forEach(x -> commands.add(x));
117
118
StringWriter sw = new StringWriter();
119
try (PrintWriter pw = new PrintWriter(sw)) {
120
int rc = JAVAC_TOOL.run(pw, pw, commands.toArray(new String[0]));
121
if(rc != 0) {
122
throw new RuntimeException(sw.toString());
123
}
124
}
125
126
}
127
128
OutputAnalyzer jarWithStdin(File stdinSource,
129
String... args) throws Throwable {
130
131
String jar = JDKToolFinder.getJDKTool("jar");
132
List<String> commands = new ArrayList<>();
133
commands.add(jar);
134
commands.addAll(Utils.getForwardVmOptions());
135
Stream.of(args).forEach(x -> commands.add(x));
136
ProcessBuilder p = new ProcessBuilder(commands);
137
if (stdinSource != null)
138
p.redirectInput(stdinSource);
139
return ProcessTools.executeCommand(p);
140
}
141
142
OutputAnalyzer jar(String... args) throws Throwable {
143
return jarWithStdin(null, args);
144
}
145
146
OutputAnalyzer jarTool(String... args) {
147
List<String> commands = new ArrayList<>();
148
commands.addAll(Utils.getForwardVmOptions());
149
Stream.of(args).forEach(x -> commands.add(x));
150
return run(JAR_TOOL, args);
151
}
152
153
OutputAnalyzer run(ToolProvider tp, String[] commands) {
154
int rc = 0;
155
StringWriter sw = new StringWriter();
156
StringWriter esw = new StringWriter();
157
158
try (PrintWriter pw = new PrintWriter(sw);
159
PrintWriter epw = new PrintWriter(esw)) {
160
rc = tp.run(pw, epw, commands);
161
}
162
return new OutputAnalyzer(sw.toString(), esw.toString(), rc);
163
}
164
}
165