294 public void testWithCustomLayer3() {
295 Layer bootLayer = Layer.boot();
296 Configuration cf0 = bootLayer.configuration();
297
298 // boot layer should contain "bananascript"
299 List<ScriptEngineFactory> factories
300 = collectAll(ServiceLoader.load(bootLayer, ScriptEngineFactory.class));
301 int countInBootLayer = factories.size();
302 assertTrue(countInBootLayer >= 1);
303 assertTrue(factories.stream()
304 .map(p -> p.getEngineName())
305 .filter("BananaScriptEngine"::equals)
306 .findAny()
307 .isPresent());
308
309 ClassLoader scl = ClassLoader.getSystemClassLoader();
310 Path dir = Paths.get(System.getProperty("test.classes", "."), "modules");
311 ModuleFinder finder = ModuleFinder.of(dir);
312
313 // layer1
314 Configuration cf1 = cf0.resolveRequiresAndUses(finder, ModuleFinder.of(), Set.of());
315 Layer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl);
316 assertTrue(layer1.modules().size() == 1);
317
318 // layer2
319 Configuration cf2 = cf0.resolveRequiresAndUses(finder, ModuleFinder.of(), Set.of());
320 Layer layer2 = bootLayer.defineModulesWithOneLoader(cf2, scl);
321 assertTrue(layer2.modules().size() == 1);
322
323 // layer3 with layer1 and layer2 as parents
324 Configuration cf3 = Configuration.resolveRequiresAndUses(finder,
325 List.of(cf1, cf2),
326 ModuleFinder.of(),
327 Set.of());
328 Layer layer3 = Layer.defineModulesWithOneLoader(cf3, List.of(layer1, layer2), scl).layer();
329 assertTrue(layer3.modules().size() == 1);
330
331
332 // class loaders
333 ClassLoader loader1 = layer1.findLoader("bananascript");
334 ClassLoader loader2 = layer2.findLoader("bananascript");
335 ClassLoader loader3 = layer3.findLoader("bananascript");
336 assertTrue(loader1 != loader2);
337 assertTrue(loader1 != loader3);
338 assertTrue(loader2 != loader3);
339
340 // load all factories with layer3 as the context
341 factories = collectAll(ServiceLoader.load(layer3, ScriptEngineFactory.class));
342 int count = factories.size();
343 assertTrue(count == countInBootLayer + 3);
344
396 public void testLoadNull4() {
397 ServiceLoader.load(Layer.empty(), null);
398 }
399
400 @Test(expectedExceptions = { NullPointerException.class })
401 public void testLoadNull5() {
402 ServiceLoader.loadInstalled(null);
403 }
404
405 /**
406 * Create a custom Layer by resolving the given module names. The modules
407 * are located in the {@code ${test.classes}/modules} directory.
408 */
409 private Layer createCustomLayer(String... modules) {
410 Path dir = Paths.get(System.getProperty("test.classes", "."), "modules");
411 ModuleFinder finder = ModuleFinder.of(dir);
412 Set<String> roots = new HashSet<>();
413 Collections.addAll(roots, modules);
414 Layer bootLayer = Layer.boot();
415 Configuration parent = bootLayer.configuration();
416 Configuration cf = parent.resolveRequires(finder, ModuleFinder.of(), roots);
417 ClassLoader scl = ClassLoader.getSystemClassLoader();
418 Layer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
419 assertTrue(layer.modules().size() == 1);
420 return layer;
421 }
422
423 private <E> List<E> collectAll(ServiceLoader<E> loader) {
424 List<E> list = new ArrayList<>();
425 Iterator<E> iterator = loader.iterator();
426 while (iterator.hasNext()) {
427 list.add(iterator.next());
428 }
429 return list;
430 }
431 }
432
|
294 public void testWithCustomLayer3() {
295 Layer bootLayer = Layer.boot();
296 Configuration cf0 = bootLayer.configuration();
297
298 // boot layer should contain "bananascript"
299 List<ScriptEngineFactory> factories
300 = collectAll(ServiceLoader.load(bootLayer, ScriptEngineFactory.class));
301 int countInBootLayer = factories.size();
302 assertTrue(countInBootLayer >= 1);
303 assertTrue(factories.stream()
304 .map(p -> p.getEngineName())
305 .filter("BananaScriptEngine"::equals)
306 .findAny()
307 .isPresent());
308
309 ClassLoader scl = ClassLoader.getSystemClassLoader();
310 Path dir = Paths.get(System.getProperty("test.classes", "."), "modules");
311 ModuleFinder finder = ModuleFinder.of(dir);
312
313 // layer1
314 Configuration cf1 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
315 Layer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl);
316 assertTrue(layer1.modules().size() == 1);
317
318 // layer2
319 Configuration cf2 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
320 Layer layer2 = bootLayer.defineModulesWithOneLoader(cf2, scl);
321 assertTrue(layer2.modules().size() == 1);
322
323 // layer3 with layer1 and layer2 as parents
324 Configuration cf3 = Configuration.resolveAndBind(finder,
325 List.of(cf1, cf2),
326 ModuleFinder.of(),
327 Set.of());
328 Layer layer3 = Layer.defineModulesWithOneLoader(cf3, List.of(layer1, layer2), scl).layer();
329 assertTrue(layer3.modules().size() == 1);
330
331
332 // class loaders
333 ClassLoader loader1 = layer1.findLoader("bananascript");
334 ClassLoader loader2 = layer2.findLoader("bananascript");
335 ClassLoader loader3 = layer3.findLoader("bananascript");
336 assertTrue(loader1 != loader2);
337 assertTrue(loader1 != loader3);
338 assertTrue(loader2 != loader3);
339
340 // load all factories with layer3 as the context
341 factories = collectAll(ServiceLoader.load(layer3, ScriptEngineFactory.class));
342 int count = factories.size();
343 assertTrue(count == countInBootLayer + 3);
344
396 public void testLoadNull4() {
397 ServiceLoader.load(Layer.empty(), null);
398 }
399
400 @Test(expectedExceptions = { NullPointerException.class })
401 public void testLoadNull5() {
402 ServiceLoader.loadInstalled(null);
403 }
404
405 /**
406 * Create a custom Layer by resolving the given module names. The modules
407 * are located in the {@code ${test.classes}/modules} directory.
408 */
409 private Layer createCustomLayer(String... modules) {
410 Path dir = Paths.get(System.getProperty("test.classes", "."), "modules");
411 ModuleFinder finder = ModuleFinder.of(dir);
412 Set<String> roots = new HashSet<>();
413 Collections.addAll(roots, modules);
414 Layer bootLayer = Layer.boot();
415 Configuration parent = bootLayer.configuration();
416 Configuration cf = parent.resolve(finder, ModuleFinder.of(), roots);
417 ClassLoader scl = ClassLoader.getSystemClassLoader();
418 Layer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
419 assertTrue(layer.modules().size() == 1);
420 return layer;
421 }
422
423 private <E> List<E> collectAll(ServiceLoader<E> loader) {
424 List<E> list = new ArrayList<>();
425 Iterator<E> iterator = loader.iterator();
426 while (iterator.hasNext()) {
427 list.add(iterator.next());
428 }
429 return list;
430 }
431 }
432
|