Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/net/URLClassLoader/closetest/CloseTest.java
41153 views
1
/*
2
* Copyright (c) 2009, 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
/*
25
* @test
26
* @bug 4167874
27
* @modules java.logging
28
* jdk.httpserver
29
* jdk.compiler
30
* @library ../../../../com/sun/net/httpserver
31
* /test/lib
32
* @build jdk.test.lib.compiler.CompilerUtils
33
* jdk.test.lib.util.FileUtils
34
* jdk.test.lib.util.JarUtils
35
* jdk.test.lib.Platform
36
* FileServerHandler
37
* @run main/othervm CloseTest
38
* @summary URL-downloaded jar files can consume all available file descriptors
39
*/
40
41
import java.io.File;
42
import java.io.IOException;
43
import java.lang.reflect.Method;
44
import java.net.URLClassLoader;
45
import java.net.InetAddress;
46
import java.net.InetSocketAddress;
47
import java.net.URL;
48
import java.nio.file.Files;
49
import java.nio.file.Path;
50
import java.nio.file.Paths;
51
52
import jdk.test.lib.compiler.CompilerUtils;
53
import jdk.test.lib.net.URIBuilder;
54
import jdk.test.lib.util.JarUtils;
55
56
import com.sun.net.httpserver.HttpContext;
57
import com.sun.net.httpserver.HttpServer;
58
59
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
60
61
public class CloseTest extends Common {
62
private static final String WORK_DIR = System.getProperty("user.dir")
63
+ "/";
64
//
65
// needs two jar files test1.jar and test2.jar with following structure
66
//
67
// com/foo/TestClass
68
// com/foo/TestClass1
69
// com/foo/Resource1
70
// com/foo/Resource2
71
//
72
// and a directory hierarchy with the same structure/contents
73
74
public static void main(String args[]) throws Exception {
75
setup();
76
77
startHttpServer(WORK_DIR + "serverRoot/");
78
79
String testjar = WORK_DIR + "test.jar";
80
copyFile(WORK_DIR + "test1.jar", testjar);
81
test(testjar, 1);
82
83
// repeat test with different implementation
84
// of test.jar (whose TestClass.getValue() returns 2
85
copyFile(WORK_DIR + "test2.jar", testjar);
86
test(testjar, 2);
87
88
// repeat test using a directory of files
89
String testdir = WORK_DIR + "testdir/";
90
rm_minus_rf(new File(testdir));
91
copyDir(WORK_DIR + "test1/", testdir);
92
test(testdir, 1);
93
94
testdir = WORK_DIR + "testdir/";
95
rm_minus_rf(new File(testdir));
96
copyDir(WORK_DIR + "test2/", testdir);
97
test(testdir, 2);
98
getHttpServer().stop(3);
99
}
100
101
// create a loader on jarfile (or directory), plus a http loader
102
// load a class , then look for a resource
103
// also load a class from http loader
104
// then close the loader
105
// check further new classes/resources cannot be loaded
106
// check jar (or dir) can be deleted
107
// check existing classes can be loaded
108
// check boot classes can be loaded
109
110
static void test(String name, int expectedValue) throws Exception {
111
112
URL url = new URL("file", null, name);
113
URL url2 = getServerURL();
114
System.out.println("Doing tests with URL: " + url + " and " + url2);
115
URL[] urls = new URL[2];
116
urls[0] = url;
117
urls[1] = url2;
118
URLClassLoader loader = new URLClassLoader(urls);
119
Class testclass = loadClass("com.foo.TestClass", loader, true);
120
Class class2 = loadClass("Test", loader, true); // from http
121
class2.newInstance();
122
Object test = testclass.newInstance();
123
Method method = testclass.getDeclaredMethods()[0]; // int getValue();
124
int res = (Integer) method.invoke(test);
125
126
if (res != expectedValue) {
127
throw new RuntimeException("wrong value from getValue() [" + res +
128
"/" + expectedValue + "]");
129
}
130
131
// should find /resource1
132
URL u1 = loader.findResource("com/foo/Resource1");
133
if (u1 == null) {
134
throw new RuntimeException("can't find com/foo/Resource1 in test1.jar");
135
}
136
loader.close();
137
138
// should NOT find /resource2 even though it is in jar
139
URL u2 = loader.findResource("com/foo/Resource2");
140
if (u2 != null) {
141
throw new RuntimeException("com/foo/Resource2 unexpected in test1.jar");
142
}
143
144
// load tests
145
loadClass("com.foo.TestClass1", loader, false);
146
loadClass("com.foo.TestClass", loader, true);
147
loadClass("java.util.ArrayList", loader, true);
148
149
// now check we can delete the path
150
rm_minus_rf(new File(name));
151
System.out.println(" ... OK");
152
}
153
154
static HttpServer httpServer;
155
156
static HttpServer getHttpServer() {
157
return httpServer;
158
}
159
160
static URL getServerURL() throws Exception {
161
int port = httpServer.getAddress().getPort();
162
return URIBuilder.newBuilder()
163
.scheme("http")
164
.loopback()
165
.port(port)
166
.path("/")
167
.toURL();
168
}
169
170
static void startHttpServer(String docroot) throws Exception {
171
httpServer = HttpServer.create(
172
new InetSocketAddress(InetAddress.getLoopbackAddress(), 0),
173
10);
174
HttpContext ctx = httpServer.createContext(
175
"/", new FileServerHandler(docroot)
176
);
177
httpServer.start();
178
}
179
180
/**
181
* Prepare jars files for the tests
182
*/
183
private static void setup () throws IOException {
184
String[] tests = new String[]{"test1", "test2"};
185
Path workDir = Paths.get(WORK_DIR);
186
Path testSrc = Paths.get(System.getProperty("test.src"));
187
for (String test : tests) {
188
Path testSrcDir = testSrc.resolve(test);
189
Path testTargetDir = workDir.resolve(test);
190
// Compile sources for corresponding test
191
CompilerUtils.compile(testSrcDir, testTargetDir);
192
// Copy all resources
193
Path packages = Paths.get("com", "foo");
194
Path copySrcDir = testSrcDir.resolve(packages);
195
Path copyTargetDir = testTargetDir.resolve(packages);
196
Files.createDirectories(copyTargetDir);
197
Path res1 = Paths.get("Resource1");
198
Path res2 = Paths.get("Resource2");
199
Files.copy(copySrcDir.resolve(res1), copyTargetDir.resolve(res1),
200
REPLACE_EXISTING);
201
Files.copy(copySrcDir.resolve(res2), copyTargetDir.resolve(res2),
202
REPLACE_EXISTING);
203
// Create jar
204
JarUtils.createJarFile(workDir.resolve(test + ".jar"), testTargetDir);
205
}
206
207
// Copy and compile server test class
208
Path serverRoot = Paths.get("serverRoot");
209
Path targetDir = workDir.resolve(serverRoot);
210
Path file = Paths.get("Test.java");
211
Files.createDirectories(targetDir);
212
Files.copy(testSrc.resolve(serverRoot).resolve(file),
213
targetDir.resolve(file), REPLACE_EXISTING);
214
CompilerUtils.compile(targetDir, targetDir);
215
}
216
}
217
218