32 * @summary Basic test for ServiceLoader with a provider deployed as a module. |
32 * @summary Basic test for ServiceLoader with a provider deployed as a module. |
33 */ |
33 */ |
34 |
34 |
35 import java.lang.module.Configuration; |
35 import java.lang.module.Configuration; |
36 import java.lang.module.ModuleFinder; |
36 import java.lang.module.ModuleFinder; |
37 import java.lang.reflect.Layer; |
|
38 import java.nio.file.Files; |
37 import java.nio.file.Files; |
39 import java.nio.file.Path; |
38 import java.nio.file.Path; |
40 import java.nio.file.Paths; |
39 import java.nio.file.Paths; |
41 import java.nio.file.StandardCopyOption; |
40 import java.nio.file.StandardCopyOption; |
42 import java.util.*; |
41 import java.util.*; |
227 * Basic test of ServiceLoader.load, using the class loader for |
226 * Basic test of ServiceLoader.load, using the class loader for |
228 * a module in a custom layer as the context. |
227 * a module in a custom layer as the context. |
229 */ |
228 */ |
230 @Test |
229 @Test |
231 public void testWithCustomLayer1() { |
230 public void testWithCustomLayer1() { |
232 Layer layer = createCustomLayer("bananascript"); |
231 ModuleLayer layer = createCustomLayer("bananascript"); |
233 |
232 |
234 ClassLoader loader = layer.findLoader("bananascript"); |
233 ClassLoader loader = layer.findLoader("bananascript"); |
235 List<ScriptEngineFactory> providers |
234 List<ScriptEngineFactory> providers |
236 = collectAll(ServiceLoader.load(ScriptEngineFactory.class, loader)); |
235 = collectAll(ServiceLoader.load(ScriptEngineFactory.class, loader)); |
237 |
236 |
256 /** |
255 /** |
257 * Basic test of ServiceLoader.load using a custom Layer as the context. |
256 * Basic test of ServiceLoader.load using a custom Layer as the context. |
258 */ |
257 */ |
259 @Test |
258 @Test |
260 public void testWithCustomLayer2() { |
259 public void testWithCustomLayer2() { |
261 Layer layer = createCustomLayer("bananascript"); |
260 ModuleLayer layer = createCustomLayer("bananascript"); |
262 |
261 |
263 List<ScriptEngineFactory> factories |
262 List<ScriptEngineFactory> factories |
264 = collectAll(ServiceLoader.load(layer, ScriptEngineFactory.class)); |
263 = collectAll(ServiceLoader.load(layer, ScriptEngineFactory.class)); |
265 |
264 |
266 // should have at least 2 x bananascript |
265 // should have at least 2 x bananascript |
291 * |
290 * |
292 * ServiceLoader should locate all 4 script engine factories in DFS order. |
291 * ServiceLoader should locate all 4 script engine factories in DFS order. |
293 */ |
292 */ |
294 @Test |
293 @Test |
295 public void testWithCustomLayer3() { |
294 public void testWithCustomLayer3() { |
296 Layer bootLayer = Layer.boot(); |
295 ModuleLayer bootLayer = ModuleLayer.boot(); |
297 Configuration cf0 = bootLayer.configuration(); |
296 Configuration cf0 = bootLayer.configuration(); |
298 |
297 |
299 // boot layer should contain "bananascript" |
298 // boot layer should contain "bananascript" |
300 List<ScriptEngineFactory> factories |
299 List<ScriptEngineFactory> factories |
301 = collectAll(ServiceLoader.load(bootLayer, ScriptEngineFactory.class)); |
300 = collectAll(ServiceLoader.load(bootLayer, ScriptEngineFactory.class)); |
311 Path dir = Paths.get(System.getProperty("test.classes", "."), "modules"); |
310 Path dir = Paths.get(System.getProperty("test.classes", "."), "modules"); |
312 ModuleFinder finder = ModuleFinder.of(dir); |
311 ModuleFinder finder = ModuleFinder.of(dir); |
313 |
312 |
314 // layer1 |
313 // layer1 |
315 Configuration cf1 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of()); |
314 Configuration cf1 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of()); |
316 Layer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl); |
315 ModuleLayer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl); |
317 assertTrue(layer1.modules().size() == 1); |
316 assertTrue(layer1.modules().size() == 1); |
318 |
317 |
319 // layer2 |
318 // layer2 |
320 Configuration cf2 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of()); |
319 Configuration cf2 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of()); |
321 Layer layer2 = bootLayer.defineModulesWithOneLoader(cf2, scl); |
320 ModuleLayer layer2 = bootLayer.defineModulesWithOneLoader(cf2, scl); |
322 assertTrue(layer2.modules().size() == 1); |
321 assertTrue(layer2.modules().size() == 1); |
323 |
322 |
324 // layer3 with layer1 and layer2 as parents |
323 // layer3 with layer1 and layer2 as parents |
325 Configuration cf3 = Configuration.resolveAndBind(finder, |
324 Configuration cf3 = Configuration.resolveAndBind(finder, |
326 List.of(cf1, cf2), |
325 List.of(cf1, cf2), |
327 ModuleFinder.of(), |
326 ModuleFinder.of(), |
328 Set.of()); |
327 Set.of()); |
329 Layer layer3 = Layer.defineModulesWithOneLoader(cf3, List.of(layer1, layer2), scl).layer(); |
328 ModuleLayer layer3 |
|
329 = ModuleLayer.defineModulesWithOneLoader(cf3, List.of(layer1, layer2), scl).layer(); |
330 assertTrue(layer3.modules().size() == 1); |
330 assertTrue(layer3.modules().size() == 1); |
331 |
331 |
332 |
332 |
333 // class loaders |
333 // class loaders |
334 ClassLoader loader1 = layer1.findLoader("bananascript"); |
334 ClassLoader loader1 = layer1.findLoader("bananascript"); |
388 } |
388 } |
389 |
389 |
390 @Test(expectedExceptions = { NullPointerException.class }) |
390 @Test(expectedExceptions = { NullPointerException.class }) |
391 public void testLoadNull3() { |
391 public void testLoadNull3() { |
392 class S { } |
392 class S { } |
393 ServiceLoader.load((Layer) null, S.class); |
393 ServiceLoader.load((ModuleLayer) null, S.class); |
394 } |
394 } |
395 |
395 |
396 @Test(expectedExceptions = { NullPointerException.class }) |
396 @Test(expectedExceptions = { NullPointerException.class }) |
397 public void testLoadNull4() { |
397 public void testLoadNull4() { |
398 ServiceLoader.load(Layer.empty(), null); |
398 ServiceLoader.load(ModuleLayer.empty(), null); |
399 } |
399 } |
400 |
400 |
401 @Test(expectedExceptions = { NullPointerException.class }) |
401 @Test(expectedExceptions = { NullPointerException.class }) |
402 public void testLoadNull5() { |
402 public void testLoadNull5() { |
403 ServiceLoader.loadInstalled(null); |
403 ServiceLoader.loadInstalled(null); |
404 } |
404 } |
405 |
405 |
406 /** |
406 /** |
407 * Create a custom Layer by resolving the given module names. The modules |
407 * Create a custom layer by resolving the given module names. The modules |
408 * are located in the {@code ${test.classes}/modules} directory. |
408 * are located in the {@code ${test.classes}/modules} directory. |
409 */ |
409 */ |
410 private Layer createCustomLayer(String... modules) { |
410 private ModuleLayer createCustomLayer(String... modules) { |
411 Path dir = Paths.get(System.getProperty("test.classes", "."), "modules"); |
411 Path dir = Paths.get(System.getProperty("test.classes", "."), "modules"); |
412 ModuleFinder finder = ModuleFinder.of(dir); |
412 ModuleFinder finder = ModuleFinder.of(dir); |
413 Set<String> roots = new HashSet<>(); |
413 Set<String> roots = new HashSet<>(); |
414 Collections.addAll(roots, modules); |
414 Collections.addAll(roots, modules); |
415 Layer bootLayer = Layer.boot(); |
415 ModuleLayer bootLayer = ModuleLayer.boot(); |
416 Configuration parent = bootLayer.configuration(); |
416 Configuration parent = bootLayer.configuration(); |
417 Configuration cf = parent.resolve(finder, ModuleFinder.of(), roots); |
417 Configuration cf = parent.resolve(finder, ModuleFinder.of(), roots); |
418 ClassLoader scl = ClassLoader.getSystemClassLoader(); |
418 ClassLoader scl = ClassLoader.getSystemClassLoader(); |
419 Layer layer = bootLayer.defineModulesWithOneLoader(cf, scl); |
419 ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl); |
420 assertTrue(layer.modules().size() == 1); |
420 assertTrue(layer.modules().size() == 1); |
421 return layer; |
421 return layer; |
422 } |
422 } |
423 |
423 |
424 private <E> List<E> collectAll(ServiceLoader<E> loader) { |
424 private <E> List<E> collectAll(ServiceLoader<E> loader) { |