Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/util/ServiceLoader/NoInterferenceTest.java
41149 views
1
/*
2
* Copyright (c) 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 jdk.compiler
28
* @build jdk.test.lib.compiler.CompilerUtils
29
* @run testng NoInterferenceTest
30
* @summary Basic test of ServiceLoader that ensures there is no interference
31
* when there are two service interfaces of the same name in a layer
32
* or overridden in a child layer.
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.util.ArrayList;
41
import java.util.Arrays;
42
import java.util.Iterator;
43
import java.util.List;
44
import java.util.ServiceLoader;
45
import java.util.Set;
46
47
import jdk.test.lib.compiler.CompilerUtils;
48
49
import org.testng.annotations.BeforeTest;
50
import org.testng.annotations.Test;
51
import static org.testng.Assert.*;
52
53
public class NoInterferenceTest {
54
55
private static final String TEST_SRC = System.getProperty("test.src");
56
private static final Path SRC_DIR = Paths.get(TEST_SRC, "modules");
57
private static final Path MODS_DIR = Paths.get("mods");
58
private static final List<String> MODULES = Arrays.asList("s1", "p1", "s2", "p2");
59
60
@BeforeTest
61
void compile() throws Exception {
62
Files.createDirectory(MODS_DIR);
63
for (String name : MODULES) {
64
Path src = SRC_DIR.resolve(name);
65
Path output = Files.createDirectory(MODS_DIR.resolve(name));
66
assertTrue(CompilerUtils.compile(src, output, "-p", MODS_DIR.toString()));
67
}
68
}
69
70
@Test
71
public void test() throws Exception {
72
ModuleFinder empty = ModuleFinder.of();
73
ModuleFinder finder = ModuleFinder.of(MODS_DIR);
74
75
ModuleLayer bootLayer = ModuleLayer.boot();
76
77
Configuration cf0 = bootLayer.configuration();
78
Configuration cf1 = cf0.resolveAndBind(finder, empty, Set.of("s1", "s2"));
79
Configuration cf2 = cf1.resolveAndBind(finder, empty, Set.of("s1", "s2"));
80
81
// cf1 contains s1, p1, s2, p2
82
assertTrue(cf1.modules().size() == 4);
83
84
// cf1 contains s1, p1, s2, p2
85
assertTrue(cf2.modules().size() == 4);
86
87
ClassLoader scl = ClassLoader.getSystemClassLoader();
88
89
ModuleLayer layer1 = bootLayer.defineModulesWithManyLoaders(cf1, scl);
90
testLayer(layer1);
91
92
ModuleLayer layer2 = layer1.defineModulesWithManyLoaders(cf2, scl);
93
testLayer(layer2);
94
}
95
96
/**
97
* Tests that the layer contains s1, p1, s2, and p2.
98
*
99
* Tests loading instances of s1/p.S and s2/p.S.
100
*/
101
private void testLayer(ModuleLayer layer) throws Exception {
102
assertTrue(layer.modules().size() == 4);
103
Module s1 = layer.findModule("s1").get();
104
Module p1 = layer.findModule("p1").get();
105
Module s2 = layer.findModule("s2").get();
106
Module p2 = layer.findModule("p2").get();
107
108
// p1 reads s1
109
assertTrue(p1.canRead(s1));
110
assertFalse(p1.canRead(s2));
111
112
// p2 reads s2
113
assertTrue(p2.canRead(s2));
114
assertFalse(p2.canRead(s1));
115
116
// iterate over implementations of s1/p.S
117
{
118
ClassLoader loader = layer.findLoader("s1");
119
Class<?> service = loader.loadClass("p.S");
120
121
List<?> list = collectAll(ServiceLoader.load(service, loader));
122
assertTrue(list.size() == 1);
123
assertTrue(list.get(0).getClass().getModule() == p1);
124
125
list = collectAll(ServiceLoader.load(layer, service));
126
assertTrue(list.size() == 1);
127
assertTrue(list.get(0).getClass().getModule() == p1);
128
}
129
130
// iterate over implementations of s2/p.S
131
{
132
ClassLoader loader = layer.findLoader("s2");
133
Class<?> service = loader.loadClass("p.S");
134
135
List<?> list = collectAll(ServiceLoader.load(service, loader));
136
assertTrue(list.size() == 1);
137
assertTrue(list.get(0).getClass().getModule() == p2);
138
139
list = collectAll(ServiceLoader.load(layer, service));
140
assertTrue(list.size() == 1);
141
assertTrue(list.get(0).getClass().getModule() == p2);
142
}
143
}
144
145
private <E> List<E> collectAll(ServiceLoader<E> loader) {
146
List<E> list = new ArrayList<>();
147
Iterator<E> iterator = loader.iterator();
148
while (iterator.hasNext()) {
149
list.add(iterator.next());
150
}
151
return list;
152
}
153
}
154
155