Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/spi/URLStreamHandlerProvider/Basic.java
41155 views
1
/*
2
* Copyright (c) 2015, 2021, 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 java.io.File;
25
import java.io.FileWriter;
26
import java.io.Reader;
27
import java.io.IOException;
28
import java.io.InputStream;
29
import java.io.InputStreamReader;
30
import java.io.SequenceInputStream;
31
import java.io.StringWriter;
32
import java.io.Writer;
33
import java.nio.file.Files;
34
import java.nio.file.Path;
35
import java.nio.file.Paths;
36
import java.util.ArrayList;
37
import java.util.Collection;
38
import java.util.Collections;
39
import java.util.List;
40
import java.util.function.Consumer;
41
import java.util.stream.Collectors;
42
import java.util.stream.Stream;
43
import javax.tools.JavaCompiler;
44
import javax.tools.JavaFileObject;
45
import javax.tools.StandardJavaFileManager;
46
import javax.tools.StandardLocation;
47
import javax.tools.ToolProvider;
48
import jdk.test.lib.util.FileUtils;
49
import jdk.test.lib.JDKToolFinder;
50
import static java.lang.String.format;
51
import static java.util.Arrays.asList;
52
53
/*
54
* @test
55
* @bug 8064924
56
* @modules jdk.compiler
57
* @summary Basic test for URLStreamHandlerProvider
58
* @library /test/lib
59
* @build jdk.test.lib.Platform
60
* jdk.test.lib.util.FileUtils
61
* jdk.test.lib.JDKToolFinder
62
* @compile Basic.java Child.java
63
* @run main Basic
64
*/
65
66
public class Basic {
67
68
static final Path TEST_SRC = Paths.get(System.getProperty("test.src", "."));
69
static final Path TEST_CLASSES = Paths.get(System.getProperty("test.classes", "."));
70
71
public static void main(String[] args) throws Throwable {
72
unknownProtocol("foo", UNKNOWN);
73
unknownProtocol("bar", UNKNOWN);
74
viaProvider("baz", KNOWN);
75
viaProvider("bert", KNOWN);
76
viaProvider("ernie", UNKNOWN, "-Djava.security.manager");
77
viaProvider("curly", UNKNOWN, "-Djava.security.manager");
78
viaProvider("larry", KNOWN, "-Djava.security.manager",
79
"-Djava.security.policy=" + TEST_SRC + File.separator + "basic.policy");
80
viaProvider("moe", KNOWN, "-Djava.security.manager",
81
"-Djava.security.policy=" + TEST_SRC + File.separator + "basic.policy");
82
viaBadProvider("tom", SCE);
83
viaBadProvider("jerry", SCE);
84
}
85
86
static final String SECURITY_MANAGER_DEPRECATED
87
= "WARNING: The Security Manager is deprecated and will be removed in a future release."
88
+ System.getProperty("line.separator");
89
static final Consumer<Result> KNOWN = r -> {
90
if (r.exitValue != 0 ||
91
(!r.output.isEmpty() && !r.output.equals(SECURITY_MANAGER_DEPRECATED)))
92
throw new RuntimeException(r.output);
93
};
94
static final Consumer<Result> UNKNOWN = r -> {
95
if (r.exitValue == 0 ||
96
!r.output.contains("java.net.MalformedURLException: unknown protocol")) {
97
throw new RuntimeException("exitValue: "+ r.exitValue + ", output:[" +r.output +"]");
98
}
99
};
100
static final Consumer<Result> SCE = r -> {
101
if (r.exitValue == 0 ||
102
!r.output.contains("java.util.ServiceConfigurationError")) {
103
throw new RuntimeException("exitValue: "+ r.exitValue + ", output:[" +r.output +"]");
104
}
105
};
106
107
static void unknownProtocol(String protocol, Consumer<Result> resultChecker) {
108
System.out.println("\nTesting " + protocol);
109
Result r = java(Collections.emptyList(), asList(TEST_CLASSES),
110
"Child", protocol);
111
resultChecker.accept(r);
112
}
113
114
static void viaProvider(String protocol, Consumer<Result> resultChecker,
115
String... sysProps)
116
throws Exception
117
{
118
viaProviderWithTemplate(protocol, resultChecker,
119
TEST_SRC.resolve("provider.template"),
120
sysProps);
121
}
122
123
static void viaBadProvider(String protocol, Consumer<Result> resultChecker,
124
String... sysProps)
125
throws Exception
126
{
127
viaProviderWithTemplate(protocol, resultChecker,
128
TEST_SRC.resolve("bad.provider.template"),
129
sysProps);
130
}
131
132
static void viaProviderWithTemplate(String protocol,
133
Consumer<Result> resultChecker,
134
Path template, String... sysProps)
135
throws Exception
136
{
137
System.out.println("\nTesting " + protocol);
138
Path testRoot = Paths.get("URLStreamHandlerProvider-" + protocol);
139
if (Files.exists(testRoot))
140
FileUtils.deleteFileTreeWithRetry(testRoot);
141
Files.createDirectory(testRoot);
142
143
Path srcPath = Files.createDirectory(testRoot.resolve("src"));
144
Path srcClass = createProvider(protocol, template, srcPath);
145
146
Path build = Files.createDirectory(testRoot.resolve("build"));
147
javac(build, srcClass);
148
createServices(build, protocol);
149
Path testJar = testRoot.resolve("test.jar");
150
jar(testJar, build);
151
152
List<String> props = new ArrayList<>();
153
for (String p : sysProps)
154
props.add(p);
155
156
Result r = java(props, asList(testJar, TEST_CLASSES),
157
"Child", protocol);
158
159
resultChecker.accept(r);
160
}
161
162
static String platformPath(String p) { return p.replace("/", File.separator); }
163
static String binaryName(String name) { return name.replace(".", "/"); }
164
165
static final String SERVICE_IMPL_PREFIX = "net.java.openjdk.test";
166
167
static void createServices(Path dst, String protocol) throws IOException {
168
Path services = Files.createDirectories(dst.resolve("META-INF")
169
.resolve("services"));
170
171
final String implName = SERVICE_IMPL_PREFIX + "." + protocol + ".Provider";
172
Path s = services.resolve("java.net.spi.URLStreamHandlerProvider");
173
FileWriter fw = new FileWriter(s.toFile());
174
try {
175
fw.write(implName);
176
} finally {
177
fw.close();
178
}
179
}
180
181
static Path createProvider(String protocol, Path srcTemplate, Path dst)
182
throws IOException
183
{
184
String pkg = SERVICE_IMPL_PREFIX + "." + protocol;
185
Path classDst = dst.resolve(platformPath(binaryName(pkg)));
186
Files.createDirectories(classDst);
187
Path classPath = classDst.resolve("Provider.java");
188
189
List<String> lines = Files.lines(srcTemplate)
190
.map(s -> s.replaceAll("\\$package", pkg))
191
.map(s -> s.replaceAll("\\$protocol", protocol))
192
.collect(Collectors.toList());
193
Files.write(classPath, lines);
194
195
return classPath;
196
}
197
198
static void jar(Path jarName, Path jarRoot) { String jar = getJDKTool("jar");
199
ProcessBuilder p = new ProcessBuilder(jar, "cf", jarName.toString(),
200
"-C", jarRoot.toString(), ".");
201
quickFail(run(p));
202
}
203
204
static void javac(Path dest, Path... sourceFiles) throws IOException {
205
JavaCompiler compiler = ToolProvider.getSystemJavaCompiler();
206
try (StandardJavaFileManager fileManager =
207
compiler.getStandardFileManager(null, null, null)) {
208
209
List<File> files = Stream.of(sourceFiles)
210
.map(p -> p.toFile())
211
.collect(Collectors.toList());
212
List<File> dests = Stream.of(dest)
213
.map(p -> p.toFile())
214
.collect(Collectors.toList());
215
Iterable<? extends JavaFileObject> compilationUnits =
216
fileManager.getJavaFileObjectsFromFiles(files);
217
fileManager.setLocation(StandardLocation.CLASS_OUTPUT, dests);
218
JavaCompiler.CompilationTask task =
219
compiler.getTask(null, fileManager, null, null, null, compilationUnits);
220
boolean passed = task.call();
221
if (!passed)
222
throw new RuntimeException("Error compiling " + files);
223
}
224
}
225
226
static void quickFail(Result r) {
227
if (r.exitValue != 0)
228
throw new RuntimeException(r.output);
229
}
230
231
static Result java(List<String> sysProps, Collection<Path> classpath,
232
String classname, String arg) {
233
String java = getJDKTool("java");
234
235
List<String> commands = new ArrayList<>();
236
commands.add(java);
237
for (String prop : sysProps)
238
commands.add(prop);
239
240
String cp = classpath.stream()
241
.map(Path::toString)
242
.collect(Collectors.joining(File.pathSeparator));
243
commands.add("-cp");
244
commands.add(cp);
245
commands.add(classname);
246
commands.add(arg);
247
248
return run(new ProcessBuilder(commands));
249
}
250
251
static Result run(ProcessBuilder pb) {
252
Process p = null;
253
System.out.println("running: " + pb.command());
254
try {
255
p = pb.start();
256
} catch (IOException e) {
257
throw new RuntimeException(
258
format("Couldn't start process '%s'", pb.command()), e);
259
}
260
261
String output;
262
try {
263
output = toString(p.getInputStream(), p.getErrorStream());
264
} catch (IOException e) {
265
throw new RuntimeException(
266
format("Couldn't read process output '%s'", pb.command()), e);
267
}
268
269
try {
270
p.waitFor();
271
} catch (InterruptedException e) {
272
throw new RuntimeException(
273
format("Process hasn't finished '%s'", pb.command()), e);
274
}
275
276
return new Result(p.exitValue(), output);
277
}
278
279
static final String DEFAULT_IMAGE_BIN = System.getProperty("java.home")
280
+ File.separator + "bin" + File.separator;
281
282
static String getJDKTool(String name) {
283
try {
284
return JDKToolFinder.getJDKTool(name);
285
} catch (Exception x) {
286
return DEFAULT_IMAGE_BIN + name;
287
}
288
}
289
290
static String toString(InputStream... src) throws IOException {
291
StringWriter dst = new StringWriter();
292
Reader concatenated =
293
new InputStreamReader(
294
new SequenceInputStream(
295
Collections.enumeration(asList(src))));
296
copy(concatenated, dst);
297
return dst.toString();
298
}
299
300
static void copy(Reader src, Writer dst) throws IOException {
301
int len;
302
char[] buf = new char[1024];
303
try {
304
while ((len = src.read(buf)) != -1)
305
dst.write(buf, 0, len);
306
} finally {
307
try {
308
src.close();
309
} catch (IOException ignored1) {
310
} finally {
311
try {
312
dst.close();
313
} catch (IOException ignored2) {
314
}
315
}
316
}
317
}
318
319
static class Result {
320
final int exitValue;
321
final String output;
322
323
private Result(int exitValue, String output) {
324
this.exitValue = exitValue;
325
this.output = output;
326
}
327
}
328
}
329
330