author | alanb |
Fri, 10 Feb 2017 09:04:39 +0000 | |
changeset 43712 | 5dfd0950317c |
parent 42338 | a60f280f803c |
child 44545 | 83b611b88ac8 |
permissions | -rw-r--r-- |
42338 | 1 |
/* |
2 |
* Copyright (c) 2016, 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 /lib/testlibrary |
|
27 |
* @modules jdk.compiler |
|
28 |
* @build CompilerUtils |
|
29 |
* @run testng/othervm BadProvidersTest |
|
30 |
* @summary Basic test of ServiceLoader with bad provider and bad provider |
|
31 |
* factories deployed on the module path |
|
32 |
*/ |
|
33 |
||
34 |
import java.lang.module.Configuration; |
|
35 |
import java.lang.module.ModuleFinder; |
|
36 |
import java.lang.reflect.Layer; |
|
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 org.testng.annotations.Test; |
|
49 |
import org.testng.annotations.DataProvider; |
|
50 |
import static org.testng.Assert.*; |
|
51 |
||
52 |
/** |
|
53 |
* Basic test of `provides S with PF` and `provides S with P` where the provider |
|
54 |
* factory or provider |
|
55 |
*/ |
|
56 |
||
57 |
public class BadProvidersTest { |
|
58 |
||
59 |
private static final String TEST_SRC = System.getProperty("test.src"); |
|
60 |
||
61 |
private static final Path USER_DIR = Paths.get(System.getProperty("user.dir")); |
|
62 |
private static final Path SRC_DIR = Paths.get(TEST_SRC, "modules"); |
|
63 |
||
64 |
private static final Path BADFACTORIES_DIR = Paths.get(TEST_SRC, "badfactories"); |
|
65 |
private static final Path BADPROVIDERS_DIR = Paths.get(TEST_SRC, "badproviders"); |
|
66 |
||
67 |
private static final String TEST1_MODULE = "test1"; |
|
68 |
private static final String TEST2_MODULE = "test2"; |
|
69 |
||
70 |
private static final String TEST_SERVICE = "p.Service"; |
|
71 |
||
72 |
/** |
|
73 |
* Compiles a module, returning a module path with the compiled module. |
|
74 |
*/ |
|
75 |
private Path compileTest(String moduleName) throws Exception { |
|
76 |
Path dir = Files.createTempDirectory(USER_DIR, "mods"); |
|
77 |
Path output = Files.createDirectory(dir.resolve(moduleName)); |
|
78 |
boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(moduleName), output); |
|
79 |
assertTrue(compiled); |
|
80 |
return dir; |
|
81 |
} |
|
82 |
||
83 |
/** |
|
84 |
* Resolves a test module and loads it into its own layer. ServiceLoader |
|
85 |
* is then used to load all providers. |
|
86 |
*/ |
|
87 |
private List<Provider> loadProviders(Path mp, String moduleName) throws Exception { |
|
88 |
ModuleFinder finder = ModuleFinder.of(mp); |
|
89 |
||
90 |
Layer bootLayer = Layer.boot(); |
|
91 |
||
92 |
Configuration cf = bootLayer.configuration() |
|
43712
5dfd0950317c
8173393: Module system implementation refresh (2/2017)
alanb
parents:
42338
diff
changeset
|
93 |
.resolveAndBind(finder, ModuleFinder.of(), Set.of(moduleName)); |
42338 | 94 |
|
95 |
ClassLoader scl = ClassLoader.getSystemClassLoader(); |
|
96 |
||
97 |
Layer layer = Layer.boot().defineModulesWithOneLoader(cf, scl); |
|
98 |
||
99 |
Class<?> service = layer.findLoader(moduleName).loadClass(TEST_SERVICE); |
|
100 |
||
101 |
return ServiceLoader.load(layer, service) |
|
102 |
.stream() |
|
103 |
.collect(Collectors.toList()); |
|
104 |
} |
|
105 |
||
106 |
@Test |
|
107 |
public void sanityTest1() throws Exception { |
|
108 |
Path mods = compileTest(TEST1_MODULE); |
|
109 |
List<Provider> list = loadProviders(mods, TEST1_MODULE); |
|
110 |
assertTrue(list.size() == 1); |
|
111 |
||
112 |
// the provider is a singleton, enforced by the provider factory |
|
113 |
Object p1 = list.get(0).get(); |
|
114 |
Object p2 = list.get(0).get(); |
|
115 |
assertTrue(p1 != null); |
|
116 |
assertTrue(p1 == p2); |
|
117 |
} |
|
118 |
||
119 |
@Test |
|
120 |
public void sanityTest2() throws Exception { |
|
121 |
Path mods = compileTest(TEST2_MODULE); |
|
122 |
List<Provider> list = loadProviders(mods, TEST2_MODULE); |
|
123 |
assertTrue(list.size() == 1); |
|
124 |
Object p = list.get(0).get(); |
|
125 |
assertTrue(p != null); |
|
126 |
} |
|
127 |
||
128 |
||
129 |
@DataProvider(name = "badfactories") |
|
130 |
public Object[][] createBadFactories() { |
|
131 |
return new Object[][] { |
|
132 |
{ "classnotpublic", null }, |
|
133 |
{ "methodnotpublic", null }, |
|
134 |
{ "badreturntype", null }, |
|
135 |
{ "returnsnull", null }, |
|
136 |
{ "throwsexception", null }, |
|
137 |
}; |
|
138 |
} |
|
139 |
||
140 |
||
141 |
@Test(dataProvider = "badfactories", |
|
142 |
expectedExceptions = ServiceConfigurationError.class) |
|
143 |
public void testBadFactory(String testName, String ignore) throws Exception { |
|
144 |
Path mods = compileTest(TEST1_MODULE); |
|
145 |
||
146 |
// compile the bad factory |
|
147 |
Path source = BADFACTORIES_DIR.resolve(testName); |
|
148 |
Path output = Files.createTempDirectory(USER_DIR, "tmp"); |
|
149 |
boolean compiled = CompilerUtils.compile(source, output); |
|
150 |
assertTrue(compiled); |
|
151 |
||
152 |
// copy the compiled class into the module |
|
153 |
Path classFile = Paths.get("p", "ProviderFactory.class"); |
|
154 |
Files.copy(output.resolve(classFile), |
|
155 |
mods.resolve(TEST1_MODULE).resolve(classFile), |
|
156 |
StandardCopyOption.REPLACE_EXISTING); |
|
157 |
||
158 |
// load providers and instantiate each one |
|
159 |
loadProviders(mods, TEST1_MODULE).forEach(Provider::get); |
|
160 |
} |
|
161 |
||
162 |
||
163 |
@DataProvider(name = "badproviders") |
|
164 |
public Object[][] createBadProviders() { |
|
165 |
return new Object[][] { |
|
166 |
{ "notpublic", null }, |
|
167 |
{ "ctornotpublic", null }, |
|
168 |
{ "notasubtype", null }, |
|
169 |
{ "throwsexception", null } |
|
170 |
}; |
|
171 |
} |
|
172 |
||
173 |
||
174 |
@Test(dataProvider = "badproviders", |
|
175 |
expectedExceptions = ServiceConfigurationError.class) |
|
176 |
public void testBadProvider(String testName, String ignore) throws Exception { |
|
177 |
Path mods = compileTest(TEST2_MODULE); |
|
178 |
||
179 |
// compile the bad provider |
|
180 |
Path source = BADPROVIDERS_DIR.resolve(testName); |
|
181 |
Path output = Files.createTempDirectory(USER_DIR, "tmp"); |
|
182 |
boolean compiled = CompilerUtils.compile(source, output); |
|
183 |
assertTrue(compiled); |
|
184 |
||
185 |
// copy the compiled class into the module |
|
186 |
Path classFile = Paths.get("p", "Provider.class"); |
|
187 |
Files.copy(output.resolve(classFile), |
|
188 |
mods.resolve(TEST2_MODULE).resolve(classFile), |
|
189 |
StandardCopyOption.REPLACE_EXISTING); |
|
190 |
||
191 |
// load providers and instantiate each one |
|
192 |
loadProviders(mods, TEST2_MODULE).forEach(Provider::get); |
|
193 |
} |
|
194 |
||
195 |
} |
|
196 |