/* * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions.
*/
/** * @test * @modules java.scripting * @library modules /test/lib * @build bananascript/* * @build jdk.test.lib.util.JarUtils * @compile classpath/pearscript/org/pear/PearScriptEngineFactory.java * classpath/pearscript/org/pear/PearScript.java * @run testng/othervm ModulesTest * @summary Basic test for ServiceLoader with a provider deployed as a module.
*/
/** * Basic test for ServiceLoader. The test make use of two service providers: * 1. BananaScriptEngine - a ScriptEngineFactory deployed as a module on the * module path. It implementations a singleton via the public static * provider method. * 2. PearScriptEngine - a ScriptEngineFactory deployed on the class path * with a service configuration file.
*/
/** * Basic test of iterator() to ensure that providers located as modules * and on the class path are found.
*/
@Test publicvoid testIterator() {
ServiceLoader<ScriptEngineFactory> loader
= ServiceLoader.load(ScriptEngineFactory.class);
Set<String> names = collectAll(loader)
.stream()
.map(ScriptEngineFactory::getEngineName)
.collect(Collectors.toSet());
assertTrue(names.contains("BananaScriptEngine"));
assertTrue(names.contains("PearScriptEngine"));
}
/** * Basic test of iterator() to test iteration order. Providers deployed * as named modules should be found before providers deployed on the class * path.
*/
@Test publicvoid testIteratorOrder() {
ServiceLoader<ScriptEngineFactory> loader
= ServiceLoader.load(ScriptEngineFactory.class); boolean foundUnnamed = false; for (ScriptEngineFactory factory : collectAll(loader)) { if (factory.getClass().getModule().isNamed()) { if (foundUnnamed) {
assertTrue(false, "Named module element after unnamed");
}
} else {
foundUnnamed = true;
}
}
}
/** * Basic test of the public static provider method. BananaScriptEngine * defines a provider method that returns the same instance.
*/
@Test publicvoid testSingleton() {
Optional<Provider<ScriptEngineFactory>> oprovider
= ServiceLoader.load(ScriptEngineFactory.class)
.stream()
.filter(p -> p.type().getName().equals("org.banana.BananaScriptEngineFactory"))
.findFirst();
assertTrue(oprovider.isPresent());
Provider<ScriptEngineFactory> provider = oprovider.get();
/** * Basic test of stream() to ensure that elements for providers in named * modules come before elements for providers in unnamed modules.
*/
@Test publicvoid testStreamOrder() {
List<Class<?>> types = ServiceLoader.load(ScriptEngineFactory.class)
.stream()
.map(Provider::type)
.collect(Collectors.toList());
boolean foundUnnamed = false; for (Class<?> factoryClass : types) { if (factoryClass.getModule().isNamed()) { if (foundUnnamed) {
assertTrue(false, "Named module element after unnamed");
}
} else {
foundUnnamed = true;
}
}
}
class S { }
assertFalse(ServiceLoader.load(S.class).findFirst().isPresent());
}
/** * Basic test ServiceLoader.load specifying the platform class loader. * The providers on the module path and class path should not be located.
*/
@Test publicvoid testWithPlatformClassLoader() {
ClassLoader pcl = ClassLoader.getPlatformClassLoader();
/** * Basic test of ServiceLoader.load where the service provider module is an * automatic module.
*/
@Test publicvoid testWithAutomaticModule() throws Exception {
Path here = Paths.get("");
Path jar = Files.createTempDirectory(here, "lib").resolve("pearscript.jar");
Path classes = Paths.get(System.getProperty("test.classes"));
// load using the class loader as context
factory = ServiceLoader.load(ScriptEngineFactory.class, loader)
.findFirst()
.orElse(null);
assertNotNull(factory);
assertTrue(factory.getClass().getClassLoader() == loader);
// load using the layer as context
factory = ServiceLoader.load(layer, ScriptEngineFactory.class)
.findFirst()
.orElse(null);
assertNotNull(factory);
assertTrue(factory.getClass().getClassLoader() == loader);
}
/** * Basic test of ServiceLoader.load, using the class loader for * a module in a custom layer as the context.
*/
@Test publicvoid testWithCustomLayer1() {
ModuleLayer layer = createCustomLayer("bananascript");
// should have at least 2 x bananascript + pearscript
assertTrue(providers.size() >= 3);
// first element should be the provider in the custom layer
ScriptEngineFactory factory = providers.get(0);
assertTrue(factory.getClass().getClassLoader() == loader);
assertTrue(factory.getClass().getModule().getLayer() == layer);
assertTrue(factory.getEngineName().equals("BananaScriptEngine"));
// remainder should be the boot layer
providers.remove(0);
Set<String> names = providers.stream()
.map(ScriptEngineFactory::getEngineName)
.collect(Collectors.toSet());
assertTrue(names.contains("BananaScriptEngine"));
assertTrue(names.contains("PearScriptEngine"));
}
/** * Basic test of ServiceLoader.load using a custom Layer as the context.
*/
@Test publicvoid testWithCustomLayer2() {
ModuleLayer layer = createCustomLayer("bananascript");
// should have at least 2 x bananascript
assertTrue(factories.size() >= 2);
// first element should be the provider in the custom layer
ScriptEngineFactory factory = factories.get(0);
assertTrue(factory.getClass().getModule().getLayer() == layer);
assertTrue(factory.getEngineName().equals("BananaScriptEngine"));
// remainder should be the boot layer
factories.remove(0);
Set<String> names = factories.stream()
.map(ScriptEngineFactory::getEngineName)
.collect(Collectors.toSet());
assertTrue(names.contains("BananaScriptEngine"));
assertFalse(names.contains("PearScriptEngine"));
}
/** * Basic test of ServiceLoader.load with a tree of layers. * * Test scenario: * - boot layer contains "bananascript", maybe other script engines * - layer1, with boot layer as parent, contains "bananascript" * - layer2, with boot layer as parent, contains "bananascript" * - layer3, with layer1 ad layer as parents, contains "bananascript" * * ServiceLoader should locate all 4 script engine factories in DFS order.
*/
@Test publicvoid testWithCustomLayer3() {
ModuleLayer bootLayer = ModuleLayer.boot();
Configuration cf0 = bootLayer.configuration();
// load all factories with layer3 as the context
factories = collectAll(ServiceLoader.load(layer3, ScriptEngineFactory.class)); int count = factories.size();
assertTrue(count == countInBootLayer + 3);
// the ordering should be layer3, layer1, boot layer, layer2
// boot layer "bananascript" and maybe other factories int last = count -1; boolean found = false; for (int i=2; i<last; i++) {
factory = factories.get(i);
assertTrue(factory.getClass().getModule().getLayer() == bootLayer); if (factory.getEngineName().equals("BananaScriptEngine")) {
assertFalse(found);
found = true;
}
}
assertTrue(found);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.