Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/ServiceLoader/BadProvidersTest.java
41152 views
1
/*
2
* Copyright (c) 2016, 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
* @library /test/lib
27
* @modules java.base/jdk.internal.org.objectweb.asm
28
* jdk.compiler
29
* @build jdk.test.lib.compiler.CompilerUtils
30
* @run testng/othervm BadProvidersTest
31
* @summary Basic test of ServiceLoader with bad provider and bad provider
32
* factories deployed on the module path
33
*/
34
35
import java.lang.module.Configuration;
36
import java.lang.module.ModuleFinder;
37
import java.nio.file.Files;
38
import java.nio.file.Path;
39
import java.nio.file.Paths;
40
import java.nio.file.StandardCopyOption;
41
import java.util.List;
42
import java.util.ServiceConfigurationError;
43
import java.util.ServiceLoader;
44
import java.util.ServiceLoader.Provider;
45
import java.util.Set;
46
import java.util.stream.Collectors;
47
48
import jdk.internal.org.objectweb.asm.ClassWriter;
49
import jdk.internal.org.objectweb.asm.MethodVisitor;
50
import static jdk.internal.org.objectweb.asm.Opcodes.*;
51
52
import jdk.test.lib.compiler.CompilerUtils;
53
54
import org.testng.annotations.Test;
55
import org.testng.annotations.DataProvider;
56
import static org.testng.Assert.*;
57
58
/**
59
* Basic test of `provides S with PF` and `provides S with P` where the provider
60
* factory or provider
61
*/
62
63
public class BadProvidersTest {
64
65
private static final String TEST_SRC = System.getProperty("test.src");
66
67
private static final Path USER_DIR = Paths.get(System.getProperty("user.dir"));
68
private static final Path SRC_DIR = Paths.get(TEST_SRC, "modules");
69
70
private static final Path BADFACTORIES_DIR = Paths.get(TEST_SRC, "badfactories");
71
private static final Path BADPROVIDERS_DIR = Paths.get(TEST_SRC, "badproviders");
72
73
private static final String TEST1_MODULE = "test1";
74
private static final String TEST2_MODULE = "test2";
75
76
private static final String TEST_SERVICE = "p.Service";
77
78
/**
79
* Compiles a module, returning a module path with the compiled module.
80
*/
81
private Path compileTest(String moduleName) throws Exception {
82
Path dir = Files.createTempDirectory(USER_DIR, "mods");
83
Path output = Files.createDirectory(dir.resolve(moduleName));
84
boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(moduleName), output);
85
assertTrue(compiled);
86
return dir;
87
}
88
89
/**
90
* Resolves a test module and loads it into its own layer. ServiceLoader
91
* is then used to load all providers.
92
*/
93
private List<Provider> loadProviders(Path mp, String moduleName) throws Exception {
94
ModuleFinder finder = ModuleFinder.of(mp);
95
96
ModuleLayer bootLayer = ModuleLayer.boot();
97
98
Configuration cf = bootLayer.configuration()
99
.resolveAndBind(finder, ModuleFinder.of(), Set.of(moduleName));
100
101
ClassLoader scl = ClassLoader.getSystemClassLoader();
102
103
ModuleLayer layer = ModuleLayer.boot().defineModulesWithOneLoader(cf, scl);
104
105
Class<?> service = layer.findLoader(moduleName).loadClass(TEST_SERVICE);
106
107
return ServiceLoader.load(layer, service)
108
.stream()
109
.collect(Collectors.toList());
110
}
111
112
@Test
113
public void sanityTest1() throws Exception {
114
Path mods = compileTest(TEST1_MODULE);
115
List<Provider> list = loadProviders(mods, TEST1_MODULE);
116
assertTrue(list.size() == 1);
117
118
// the provider is a singleton, enforced by the provider factory
119
Object p1 = list.get(0).get();
120
Object p2 = list.get(0).get();
121
assertTrue(p1 != null);
122
assertTrue(p1 == p2);
123
}
124
125
@Test
126
public void sanityTest2() throws Exception {
127
Path mods = compileTest(TEST2_MODULE);
128
List<Provider> list = loadProviders(mods, TEST2_MODULE);
129
assertTrue(list.size() == 1);
130
Object p = list.get(0).get();
131
assertTrue(p != null);
132
}
133
134
135
@DataProvider(name = "badfactories")
136
public Object[][] createBadFactories() {
137
return new Object[][] {
138
{ "classnotpublic", null },
139
{ "methodnotpublic", null },
140
{ "badreturntype", null },
141
{ "returnsnull", null },
142
{ "throwsexception", null },
143
};
144
}
145
146
147
@Test(dataProvider = "badfactories",
148
expectedExceptions = ServiceConfigurationError.class)
149
public void testBadFactory(String testName, String ignore) throws Exception {
150
Path mods = compileTest(TEST1_MODULE);
151
152
// compile the bad factory
153
Path source = BADFACTORIES_DIR.resolve(testName);
154
Path output = Files.createTempDirectory(USER_DIR, "tmp");
155
boolean compiled = CompilerUtils.compile(source, output);
156
assertTrue(compiled);
157
158
// copy the compiled class into the module
159
Path classFile = Paths.get("p", "ProviderFactory.class");
160
Files.copy(output.resolve(classFile),
161
mods.resolve(TEST1_MODULE).resolve(classFile),
162
StandardCopyOption.REPLACE_EXISTING);
163
164
// load providers and instantiate each one
165
loadProviders(mods, TEST1_MODULE).forEach(Provider::get);
166
}
167
168
169
@DataProvider(name = "badproviders")
170
public Object[][] createBadProviders() {
171
return new Object[][] {
172
{ "notpublic", null },
173
{ "ctornotpublic", null },
174
{ "notasubtype", null },
175
{ "throwsexception", null }
176
};
177
}
178
179
180
@Test(dataProvider = "badproviders",
181
expectedExceptions = ServiceConfigurationError.class)
182
public void testBadProvider(String testName, String ignore) throws Exception {
183
Path mods = compileTest(TEST2_MODULE);
184
185
// compile the bad provider
186
Path source = BADPROVIDERS_DIR.resolve(testName);
187
Path output = Files.createTempDirectory(USER_DIR, "tmp");
188
boolean compiled = CompilerUtils.compile(source, output);
189
assertTrue(compiled);
190
191
// copy the compiled class into the module
192
Path classFile = Paths.get("p", "Provider.class");
193
Files.copy(output.resolve(classFile),
194
mods.resolve(TEST2_MODULE).resolve(classFile),
195
StandardCopyOption.REPLACE_EXISTING);
196
197
// load providers and instantiate each one
198
loadProviders(mods, TEST2_MODULE).forEach(Provider::get);
199
}
200
201
202
/**
203
* Test a service provider that defines more than one no-args
204
* public static "provider" method.
205
*/
206
@Test(expectedExceptions = ServiceConfigurationError.class)
207
public void testWithTwoFactoryMethods() throws Exception {
208
Path mods = compileTest(TEST1_MODULE);
209
210
ClassWriter cw = new ClassWriter(ClassWriter.COMPUTE_MAXS
211
+ ClassWriter.COMPUTE_FRAMES);
212
cw.visit(V9,
213
ACC_PUBLIC + ACC_SUPER,
214
"p/ProviderFactory",
215
null,
216
"java/lang/Object",
217
null);
218
219
// public static p.Service provider()
220
MethodVisitor mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC,
221
"provider",
222
"()Lp/Service;",
223
null,
224
null);
225
mv.visitTypeInsn(NEW, "p/ProviderFactory$1");
226
mv.visitInsn(DUP);
227
mv.visitMethodInsn(INVOKESPECIAL,
228
"p/ProviderFactory$1",
229
"<init>", "()V",
230
false);
231
mv.visitInsn(ARETURN);
232
mv.visitMaxs(0, 0);
233
mv.visitEnd();
234
235
// public static p.ProviderFactory$1 provider()
236
mv = cw.visitMethod(ACC_PUBLIC + ACC_STATIC,
237
"provider",
238
"()Lp/ProviderFactory$1;",
239
null,
240
null);
241
mv.visitTypeInsn(NEW, "p/ProviderFactory$1");
242
mv.visitInsn(DUP);
243
mv.visitMethodInsn(INVOKESPECIAL,
244
"p/ProviderFactory$1",
245
"<init>",
246
"()V",
247
false);
248
mv.visitInsn(ARETURN);
249
mv.visitMaxs(0, 0);
250
mv.visitEnd();
251
252
cw.visitEnd();
253
254
// write the class bytes into the compiled module directory
255
Path classFile = mods.resolve(TEST1_MODULE)
256
.resolve("p")
257
.resolve("ProviderFactory.class");
258
Files.write(classFile, cw.toByteArray());
259
260
// load providers and instantiate each one
261
loadProviders(mods, TEST1_MODULE).forEach(Provider::get);
262
}
263
264
}
265
266
267