Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/lang/Package/GetPackages.java
41149 views
1
/*
2
* Copyright (c) 2015, 2020, 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 Test Package object is local to each ClassLoader.
27
* There can be one Package object of "foo" name defined by
28
* different class loader.
29
* @compile Foo.java
30
* @run testng GetPackages
31
*/
32
33
import java.io.IOException;
34
import java.io.UncheckedIOException;
35
import java.nio.file.Files;
36
import java.nio.file.Path;
37
import java.nio.file.Paths;
38
import java.util.Arrays;
39
import java.lang.reflect.*;
40
41
import org.testng.annotations.DataProvider;
42
import org.testng.annotations.Test;
43
import static org.testng.Assert.*;
44
45
public class GetPackages {
46
final TestClassLoader loader;
47
final Class<?> fooClass;
48
/*
49
* Each TestClassLoader defines a "foo.Foo" class which has a unique
50
* Package object of "foo" regardless whether its ancestor class loader
51
* defines a package "foo" or not.
52
*/
53
GetPackages(TestClassLoader loader) throws ClassNotFoundException {
54
this.loader = loader;
55
this.fooClass = loader.loadClass("foo.Foo");
56
}
57
58
/** For TestNG */
59
public GetPackages() {
60
loader = null;
61
fooClass = null;
62
}
63
64
/*
65
* Check package "foo" defined locally in the TestClassLoader
66
* as well as its ancestors.
67
*/
68
void checkPackage() throws ClassNotFoundException {
69
// Name of an unnamed package is empty string
70
assertEquals(this.getClass().getPackage().getName(), "");
71
72
assertEquals(fooClass.getClassLoader(), loader);
73
74
Package p = loader.getDefinedPackage("foo");
75
assertEquals(p.getName(), "foo");
76
assertEquals(p, loader.getPackage("foo"));
77
78
if (loader.getParent() != null) {
79
Package p2 = ((TestClassLoader)loader.getParent()).getDefinedPackage("foo");
80
assertTrue(p != p2);
81
}
82
83
long count = Arrays.stream(loader.getDefinedPackages())
84
.map(Package::getName)
85
.filter(pn -> pn.equals("foo"))
86
.count();
87
assertEquals(count, 1);
88
}
89
90
/*
91
* Check the number of package "foo" defined to this class loader and
92
* its ancestors
93
*/
94
Package[] getPackagesFromLoader() {
95
return loader.packagesInClassLoaderChain();
96
}
97
98
/*
99
* Package.getPackages is caller-sensitve. Call through Foo class
100
* to find all Packages visible to this TestClassLoader and its ancestors
101
*/
102
Package[] getPackagesFromFoo() throws Exception {
103
Method m = fooClass.getMethod("getPackages");
104
return (Package[])m.invoke(null);
105
}
106
107
private static long numFooPackages(Package[] pkgs) throws Exception {
108
return Arrays.stream(pkgs)
109
.filter(p -> p.getName().equals("foo"))
110
.count();
111
}
112
113
@DataProvider(name = "loaders")
114
public static Object[][] testLoaders() {
115
TestClassLoader loader1 = new TestClassLoader(null);
116
TestClassLoader loader2 = new TestClassLoader(loader1);
117
TestClassLoader loader3 = new TestClassLoader(loader2);
118
119
// Verify the number of expected Package object of "foo" visible
120
// to the class loader
121
return new Object[][] {
122
{ loader1, 1 },
123
{ loader2, 2 },
124
{ loader3, 3 }
125
};
126
}
127
128
@Test(dataProvider = "loaders")
129
public static void test(TestClassLoader loader, long expected) throws Exception {
130
GetPackages test = new GetPackages(loader);
131
// check package "foo" existence
132
test.checkPackage();
133
134
assertEquals(numFooPackages(test.getPackagesFromLoader()), expected);
135
assertEquals(numFooPackages(test.getPackagesFromFoo()), expected);
136
}
137
}
138
139
class TestClassLoader extends ClassLoader {
140
public TestClassLoader() {
141
super();
142
}
143
144
public TestClassLoader(ClassLoader parent) {
145
super(parent);
146
}
147
148
public Package getPackage(String pn) {
149
return super.getPackage(pn);
150
}
151
152
public Package[] packagesInClassLoaderChain() {
153
return super.getPackages();
154
}
155
156
@Override
157
protected Class<?> findClass(String name) throws ClassNotFoundException {
158
Path p = Paths.get(System.getProperty("test.classes", "."));
159
160
try {
161
byte[] bb = Files.readAllBytes(p.resolve("foo/Foo.class"));
162
return defineClass(name, bb, 0, bb.length);
163
} catch (IOException e) {
164
throw new UncheckedIOException(e);
165
}
166
}
167
@Override
168
protected Class<?> loadClass(String cn, boolean resolve) throws ClassNotFoundException {
169
if (!cn.equals("foo.Foo"))
170
return super.loadClass(cn, resolve);
171
return findClass(cn);
172
}
173
}
174
175