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 * @modules java.scripting
27 * @library modules /lib/testlibrary
28 * @build bananascript/*
29 * @build JarUtils
30 * @compile classpath/pearscript/org/pear/PearScriptEngineFactory.java
31 * classpath/pearscript/org/pear/PearScript.java
32 * @run testng/othervm ModulesTest
33 * @summary Basic test for ServiceLoader with a provider deployed as a module.
34 */
35
36 import java.lang.module.Configuration;
37 import java.lang.module.ModuleFinder;
38 import java.nio.file.Files;
39 import java.nio.file.Path;
40 import java.nio.file.Paths;
41 import java.nio.file.StandardCopyOption;
42 import java.util.ArrayList;
43 import java.util.Collections;
44 import java.util.HashSet;
45 import java.util.Iterator;
46 import java.util.List;
47 import java.util.Optional;
48 import java.util.ServiceLoader;
49 import java.util.ServiceLoader.Provider;
50 import java.util.Set;
51 import java.util.stream.Collectors;
52 import javax.script.ScriptEngineFactory;
53
54 import org.testng.annotations.Test;
55 import org.testng.annotations.BeforeTest;
56 import static org.testng.Assert.*;
57
58 /**
59 * Basic test for ServiceLoader. The test make use of two service providers:
60 * 1. BananaScriptEngine - a ScriptEngineFactory deployed as a module on the
61 * module path. It implementations a singleton via the public static
62 * provider method.
63 * 2. PearScriptEngine - a ScriptEngineFactory deployed on the class path
64 * with a service configuration file.
65 */
66
67 public class ModulesTest {
68
69 // Copy the services configuration file for "pearscript" into place.
70 @BeforeTest
71 public void setup() throws Exception {
219 .collect(Collectors.toSet());
220 assertFalse(names.contains("BananaScriptEngine"));
221 assertFalse(names.contains("PearScriptEngine"));
222
223 // stream
224 names = ServiceLoader.load(ScriptEngineFactory.class, pcl)
225 .stream()
226 .map(Provider::get)
227 .map(ScriptEngineFactory::getEngineName)
228 .collect(Collectors.toSet());
229 assertFalse(names.contains("BananaScriptEngine"));
230 assertFalse(names.contains("PearScriptEngine"));
231 }
232
233 /**
234 * Basic test of ServiceLoader.load where the service provider module is an
235 * automatic module.
236 */
237 @Test
238 public void testWithAutomaticModule() throws Exception {
239 Path classes = Paths.get(System.getProperty("test.classes"));
240 Path jar = Files.createTempDirectory("lib").resolve("pearscript.jar");
241 JarUtils.createJarFile(jar, classes, "META-INF", "org");
242
243 ModuleFinder finder = ModuleFinder.of(jar);
244 ModuleLayer bootLayer = ModuleLayer.boot();
245 Configuration parent = bootLayer.configuration();
246 Configuration cf = parent.resolveAndBind(finder, ModuleFinder.of(), Set.of());
247 assertTrue(cf.modules().size() == 1);
248
249 ClassLoader scl = ClassLoader.getSystemClassLoader();
250 ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
251 assertTrue(layer.modules().size() == 1);
252
253 ClassLoader loader = layer.findLoader("pearscript");
254 ScriptEngineFactory factory;
255
256 // load using the class loader as context
257 factory = ServiceLoader.load(ScriptEngineFactory.class, loader)
258 .findFirst()
259 .orElse(null);
260 assertNotNull(factory);
336 *
337 * ServiceLoader should locate all 4 script engine factories in DFS order.
338 */
339 @Test
340 public void testWithCustomLayer3() {
341 ModuleLayer bootLayer = ModuleLayer.boot();
342 Configuration cf0 = bootLayer.configuration();
343
344 // boot layer should contain "bananascript"
345 List<ScriptEngineFactory> factories
346 = collectAll(ServiceLoader.load(bootLayer, ScriptEngineFactory.class));
347 int countInBootLayer = factories.size();
348 assertTrue(countInBootLayer >= 1);
349 assertTrue(factories.stream()
350 .map(p -> p.getEngineName())
351 .filter("BananaScriptEngine"::equals)
352 .findAny()
353 .isPresent());
354
355 ClassLoader scl = ClassLoader.getSystemClassLoader();
356 Path dir = Paths.get(System.getProperty("test.classes", "."), "modules");
357 ModuleFinder finder = ModuleFinder.of(dir);
358
359 // layer1
360 Configuration cf1 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
361 ModuleLayer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl);
362 assertTrue(layer1.modules().size() == 1);
363
364 // layer2
365 Configuration cf2 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
366 ModuleLayer layer2 = bootLayer.defineModulesWithOneLoader(cf2, scl);
367 assertTrue(layer2.modules().size() == 1);
368
369 // layer3 with layer1 and layer2 as parents
370 Configuration cf3 = Configuration.resolveAndBind(finder,
371 List.of(cf1, cf2),
372 ModuleFinder.of(),
373 Set.of());
374 ModuleLayer layer3
375 = ModuleLayer.defineModulesWithOneLoader(cf3, List.of(layer1, layer2), scl).layer();
376 assertTrue(layer3.modules().size() == 1);
377
434 }
435
436 @Test(expectedExceptions = { NullPointerException.class })
437 public void testLoadNull3() {
438 class S { }
439 ServiceLoader.load((ModuleLayer) null, S.class);
440 }
441
442 @Test(expectedExceptions = { NullPointerException.class })
443 public void testLoadNull4() {
444 ServiceLoader.load(ModuleLayer.empty(), null);
445 }
446
447 @Test(expectedExceptions = { NullPointerException.class })
448 public void testLoadNull5() {
449 ServiceLoader.loadInstalled(null);
450 }
451
452 /**
453 * Create a custom layer by resolving the given module names. The modules
454 * are located in the {@code ${test.classes}/modules} directory.
455 */
456 private ModuleLayer createCustomLayer(String... modules) {
457 Path dir = Paths.get(System.getProperty("test.classes", "."), "modules");
458 ModuleFinder finder = ModuleFinder.of(dir);
459 Set<String> roots = new HashSet<>();
460 Collections.addAll(roots, modules);
461 ModuleLayer bootLayer = ModuleLayer.boot();
462 Configuration parent = bootLayer.configuration();
463 Configuration cf = parent.resolve(finder, ModuleFinder.of(), roots);
464 ClassLoader scl = ClassLoader.getSystemClassLoader();
465 ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
466 assertTrue(layer.modules().size() == 1);
467 return layer;
468 }
469
470 private <E> List<E> collectAll(ServiceLoader<E> loader) {
471 List<E> list = new ArrayList<>();
472 Iterator<E> iterator = loader.iterator();
473 while (iterator.hasNext()) {
474 list.add(iterator.next());
475 }
476 return list;
477 }
478 }
479
|
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 * @modules java.scripting
27 * @library modules /lib/testlibrary
28 * @build bananascript/*
29 * @build JarUtils
30 * @compile classpath/pearscript/org/pear/PearScriptEngineFactory.java
31 * classpath/pearscript/org/pear/PearScript.java
32 * @run testng/othervm ModulesTest
33 * @summary Basic test for ServiceLoader with a provider deployed as a module.
34 */
35
36 import java.io.File;
37 import java.lang.module.Configuration;
38 import java.lang.module.ModuleFinder;
39 import java.nio.file.Files;
40 import java.nio.file.Path;
41 import java.nio.file.Paths;
42 import java.nio.file.StandardCopyOption;
43 import java.util.ArrayList;
44 import java.util.Collections;
45 import java.util.HashSet;
46 import java.util.Iterator;
47 import java.util.List;
48 import java.util.Optional;
49 import java.util.ServiceLoader;
50 import java.util.ServiceLoader.Provider;
51 import java.util.Set;
52 import java.util.stream.Collectors;
53 import java.util.stream.Stream;
54 import javax.script.ScriptEngineFactory;
55
56 import org.testng.annotations.Test;
57 import org.testng.annotations.BeforeTest;
58 import static org.testng.Assert.*;
59
60 /**
61 * Basic test for ServiceLoader. The test make use of two service providers:
62 * 1. BananaScriptEngine - a ScriptEngineFactory deployed as a module on the
63 * module path. It implementations a singleton via the public static
64 * provider method.
65 * 2. PearScriptEngine - a ScriptEngineFactory deployed on the class path
66 * with a service configuration file.
67 */
68
69 public class ModulesTest {
70
71 // Copy the services configuration file for "pearscript" into place.
72 @BeforeTest
73 public void setup() throws Exception {
221 .collect(Collectors.toSet());
222 assertFalse(names.contains("BananaScriptEngine"));
223 assertFalse(names.contains("PearScriptEngine"));
224
225 // stream
226 names = ServiceLoader.load(ScriptEngineFactory.class, pcl)
227 .stream()
228 .map(Provider::get)
229 .map(ScriptEngineFactory::getEngineName)
230 .collect(Collectors.toSet());
231 assertFalse(names.contains("BananaScriptEngine"));
232 assertFalse(names.contains("PearScriptEngine"));
233 }
234
235 /**
236 * Basic test of ServiceLoader.load where the service provider module is an
237 * automatic module.
238 */
239 @Test
240 public void testWithAutomaticModule() throws Exception {
241 Path here = Paths.get("");
242 Path jar = Files.createTempDirectory(here, "lib").resolve("pearscript.jar");
243 Path classes = Paths.get(System.getProperty("test.classes"));
244
245 JarUtils.createJarFile(jar, classes, "META-INF", "org");
246
247 ModuleFinder finder = ModuleFinder.of(jar);
248 ModuleLayer bootLayer = ModuleLayer.boot();
249 Configuration parent = bootLayer.configuration();
250 Configuration cf = parent.resolveAndBind(finder, ModuleFinder.of(), Set.of());
251 assertTrue(cf.modules().size() == 1);
252
253 ClassLoader scl = ClassLoader.getSystemClassLoader();
254 ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
255 assertTrue(layer.modules().size() == 1);
256
257 ClassLoader loader = layer.findLoader("pearscript");
258 ScriptEngineFactory factory;
259
260 // load using the class loader as context
261 factory = ServiceLoader.load(ScriptEngineFactory.class, loader)
262 .findFirst()
263 .orElse(null);
264 assertNotNull(factory);
340 *
341 * ServiceLoader should locate all 4 script engine factories in DFS order.
342 */
343 @Test
344 public void testWithCustomLayer3() {
345 ModuleLayer bootLayer = ModuleLayer.boot();
346 Configuration cf0 = bootLayer.configuration();
347
348 // boot layer should contain "bananascript"
349 List<ScriptEngineFactory> factories
350 = collectAll(ServiceLoader.load(bootLayer, ScriptEngineFactory.class));
351 int countInBootLayer = factories.size();
352 assertTrue(countInBootLayer >= 1);
353 assertTrue(factories.stream()
354 .map(p -> p.getEngineName())
355 .filter("BananaScriptEngine"::equals)
356 .findAny()
357 .isPresent());
358
359 ClassLoader scl = ClassLoader.getSystemClassLoader();
360 ModuleFinder finder = ModuleFinder.of(testModulePath());
361
362 // layer1
363 Configuration cf1 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
364 ModuleLayer layer1 = bootLayer.defineModulesWithOneLoader(cf1, scl);
365 assertTrue(layer1.modules().size() == 1);
366
367 // layer2
368 Configuration cf2 = cf0.resolveAndBind(finder, ModuleFinder.of(), Set.of());
369 ModuleLayer layer2 = bootLayer.defineModulesWithOneLoader(cf2, scl);
370 assertTrue(layer2.modules().size() == 1);
371
372 // layer3 with layer1 and layer2 as parents
373 Configuration cf3 = Configuration.resolveAndBind(finder,
374 List.of(cf1, cf2),
375 ModuleFinder.of(),
376 Set.of());
377 ModuleLayer layer3
378 = ModuleLayer.defineModulesWithOneLoader(cf3, List.of(layer1, layer2), scl).layer();
379 assertTrue(layer3.modules().size() == 1);
380
437 }
438
439 @Test(expectedExceptions = { NullPointerException.class })
440 public void testLoadNull3() {
441 class S { }
442 ServiceLoader.load((ModuleLayer) null, S.class);
443 }
444
445 @Test(expectedExceptions = { NullPointerException.class })
446 public void testLoadNull4() {
447 ServiceLoader.load(ModuleLayer.empty(), null);
448 }
449
450 @Test(expectedExceptions = { NullPointerException.class })
451 public void testLoadNull5() {
452 ServiceLoader.loadInstalled(null);
453 }
454
455 /**
456 * Create a custom layer by resolving the given module names. The modules
457 * are located on the test module path ({@code ${test.module.path}}).
458 */
459 private ModuleLayer createCustomLayer(String... modules) {
460 ModuleFinder finder = ModuleFinder.of(testModulePath());
461 Set<String> roots = new HashSet<>();
462 Collections.addAll(roots, modules);
463 ModuleLayer bootLayer = ModuleLayer.boot();
464 Configuration parent = bootLayer.configuration();
465 Configuration cf = parent.resolve(finder, ModuleFinder.of(), roots);
466 ClassLoader scl = ClassLoader.getSystemClassLoader();
467 ModuleLayer layer = bootLayer.defineModulesWithOneLoader(cf, scl);
468 assertTrue(layer.modules().size() == 1);
469 return layer;
470 }
471
472 private Path[] testModulePath() {
473 String mp = System.getProperty("test.module.path");
474 return Stream.of(mp.split(File.pathSeparator))
475 .map(Paths::get)
476 .toArray(Path[]::new);
477 }
478
479 private <E> List<E> collectAll(ServiceLoader<E> loader) {
480 List<E> list = new ArrayList<>();
481 Iterator<E> iterator = loader.iterator();
482 while (iterator.hasNext()) {
483 list.add(iterator.next());
484 }
485 return list;
486 }
487 }
488
|