Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/ServiceLoader/basic/ServiceLoaderBasicTest.java
41153 views
1
/*
2
* Copyright (c) 2018, 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 4640520 6354623 7198496
27
* @summary Unit test for java.util.ServiceLoader
28
* @library /test/lib
29
* @build jdk.test.lib.process.*
30
* jdk.test.lib.util.JarUtils
31
* Basic Load FooService FooProvider1 FooProvider2 FooProvider3 BarProvider
32
* @run testng ServiceLoaderBasicTest
33
*/
34
35
36
import java.io.File;
37
import java.nio.file.Files;
38
import java.nio.file.Path;
39
import java.util.ArrayList;
40
import java.util.List;
41
42
import jdk.test.lib.JDKToolFinder;
43
import jdk.test.lib.Utils;
44
import jdk.test.lib.process.ProcessTools;
45
import jdk.test.lib.util.JarUtils;
46
47
import org.testng.annotations.BeforeClass;
48
import org.testng.annotations.DataProvider;
49
import org.testng.annotations.Test;
50
51
import static java.nio.file.StandardOpenOption.CREATE;
52
import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
53
import static java.util.Arrays.asList;
54
55
public class ServiceLoaderBasicTest {
56
57
private static final String METAINFO = "META-INF/services/FooService";
58
private static final Path XTEST_CONFIG = Path.of("x.test").resolve(METAINFO);
59
private static final Path XMETA_CONFIG = Path.of("x.meta").resolve(METAINFO);
60
private static final Path P2JAR = Path.of("p2.jar");
61
private static final Path P2DUPJAR = Path.of("p2dup.jar");
62
private static final Path P3JAR = Path.of("x.ext", "p3.jar");
63
64
private static final String XTEST = File.pathSeparator + "x.test";
65
private static final String XMETA = File.pathSeparator + "x.meta";
66
private static final String P2 = File.pathSeparator + P2JAR.toString();
67
private static final String P2DUP = File.pathSeparator + P2DUPJAR.toString();
68
private static final String P3 = File.pathSeparator + P3JAR.toString();
69
70
private static final String XTEST_CP = Utils.TEST_CLASS_PATH + XTEST;
71
private static final String P2_CP = Utils.TEST_CLASS_PATH + P2;
72
private static final String P2DUP_CP = P2_CP + P2DUP;
73
private static final String P3P2_CP = Utils.TEST_CLASS_PATH + P3 + P2;
74
private static final String XTESTP2_CP = XTEST_CP + P2;
75
private static final String P3XTEST_CP = Utils.TEST_CLASS_PATH + P3 + XTEST;
76
private static final String P3XTESTP2_CP = P3XTEST_CP + P2;
77
private static final String XMETA_CP = Utils.TEST_CLASS_PATH + XMETA;
78
private static final String XMETAXTEST_CP = XMETA_CP + XTEST;
79
private static final String XTESTXMETA_CP = XTEST_CP + XMETA;
80
private static final String XTESTXMETAP2_CP = XTESTXMETA_CP + P2;
81
82
@BeforeClass
83
public void initialize() throws Exception {
84
createProviderConfig(XTEST_CONFIG, "FooProvider1");
85
createProviderConfig(XMETA_CONFIG, "FooProvider42");
86
createJar(P2JAR, "FooProvider2", List.of("FooProvider2"));
87
createJar(P3JAR, "FooProvider3", List.of("FooProvider3", "FooService"));
88
Files.copy(P2JAR, P2DUPJAR, REPLACE_EXISTING);
89
}
90
91
@DataProvider
92
public Object[][] testCases() {
93
return new Object[][]{
94
// CLI options, Test, Runtime arguments
95
// Success cases
96
{List.of("-cp", XTESTP2_CP, "Basic")},
97
{List.of("-cp", XTEST_CP, "Load", "FooProvider1")},
98
{List.of("-cp", P2_CP, "Load", "FooProvider2")},
99
{List.of("-cp", P2DUP_CP, "Load", "FooProvider2")},
100
{List.of("-cp", P3P2_CP, "Load", "FooProvider3", "FooProvider2")},
101
{List.of("-cp", XTESTP2_CP, "Load", "FooProvider1", "FooProvider2")},
102
{List.of("-cp", P3XTEST_CP, "Load", "FooProvider3", "FooProvider1")},
103
{List.of("-cp", P3XTESTP2_CP, "Load", "FooProvider3",
104
"FooProvider1",
105
"FooProvider2")},
106
// Failures followed by successes
107
{List.of("-cp", XTESTXMETA_CP, "Load", "FooProvider1", "fail")},
108
{List.of("-cp", XMETAXTEST_CP, "Load", "fail", "FooProvider1")},
109
{List.of("-cp", XTESTXMETAP2_CP, "Load", "FooProvider1", "fail", "FooProvider2")}
110
};
111
}
112
113
@DataProvider
114
public Object[][] negativeTestCases() {
115
return new Object[][]{
116
{"blah blah"},
117
{"9234"},
118
{"X!"},
119
{"BarProvider"},
120
{"FooProvider42"}
121
};
122
}
123
124
@Test(dataProvider = "testCases")
125
public void testProvider(List<String> args) throws Throwable {
126
runJava(args);
127
}
128
129
@Test(dataProvider = "negativeTestCases")
130
public void testBadProvider(String providerName) throws Throwable {
131
Files.write(XMETA_CONFIG, providerName.getBytes());
132
runJava(List.of("-cp", XMETA_CP, "Load", "fail"));
133
}
134
135
private void runJava(List<String> opts) throws Throwable {
136
List<String> cmds = new ArrayList<>();
137
cmds.add(JDKToolFinder.getJDKTool("java"));
138
cmds.addAll(asList(Utils.getTestJavaOpts()));
139
cmds.addAll(opts);
140
141
ProcessTools.executeCommand(cmds.stream()
142
.filter(t -> !t.isEmpty())
143
.toArray(String[]::new))
144
.shouldHaveExitValue(0);
145
}
146
147
private void createProviderConfig(Path config, String providerName) throws Exception {
148
Files.createDirectories(config.getParent());
149
Files.write(config, providerName.getBytes(), CREATE);
150
}
151
152
private void createJar(Path jar, String provider, List<String> files) throws Exception {
153
Path xdir = Path.of(provider);
154
createProviderConfig(xdir.resolve(METAINFO), provider);
155
156
for (String f : files) {
157
Path source = Path.of(Utils.TEST_CLASSES, f + ".class");
158
Path target = xdir.resolve(source.getFileName());
159
Files.copy(source, target, REPLACE_EXISTING);
160
}
161
JarUtils.createJarFile(jar, xdir);
162
}
163
164
}
165
166