Path: blob/master/test/jdk/java/util/ServiceLoader/NoInterferenceTest.java
41149 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @library /test/lib26* @modules jdk.compiler27* @build jdk.test.lib.compiler.CompilerUtils28* @run testng NoInterferenceTest29* @summary Basic test of ServiceLoader that ensures there is no interference30* when there are two service interfaces of the same name in a layer31* or overridden in a child layer.32*/3334import java.lang.module.Configuration;35import java.lang.module.ModuleFinder;36import java.nio.file.Files;37import java.nio.file.Path;38import java.nio.file.Paths;39import java.util.ArrayList;40import java.util.Arrays;41import java.util.Iterator;42import java.util.List;43import java.util.ServiceLoader;44import java.util.Set;4546import jdk.test.lib.compiler.CompilerUtils;4748import org.testng.annotations.BeforeTest;49import org.testng.annotations.Test;50import static org.testng.Assert.*;5152public class NoInterferenceTest {5354private static final String TEST_SRC = System.getProperty("test.src");55private static final Path SRC_DIR = Paths.get(TEST_SRC, "modules");56private static final Path MODS_DIR = Paths.get("mods");57private static final List<String> MODULES = Arrays.asList("s1", "p1", "s2", "p2");5859@BeforeTest60void compile() throws Exception {61Files.createDirectory(MODS_DIR);62for (String name : MODULES) {63Path src = SRC_DIR.resolve(name);64Path output = Files.createDirectory(MODS_DIR.resolve(name));65assertTrue(CompilerUtils.compile(src, output, "-p", MODS_DIR.toString()));66}67}6869@Test70public void test() throws Exception {71ModuleFinder empty = ModuleFinder.of();72ModuleFinder finder = ModuleFinder.of(MODS_DIR);7374ModuleLayer bootLayer = ModuleLayer.boot();7576Configuration cf0 = bootLayer.configuration();77Configuration cf1 = cf0.resolveAndBind(finder, empty, Set.of("s1", "s2"));78Configuration cf2 = cf1.resolveAndBind(finder, empty, Set.of("s1", "s2"));7980// cf1 contains s1, p1, s2, p281assertTrue(cf1.modules().size() == 4);8283// cf1 contains s1, p1, s2, p284assertTrue(cf2.modules().size() == 4);8586ClassLoader scl = ClassLoader.getSystemClassLoader();8788ModuleLayer layer1 = bootLayer.defineModulesWithManyLoaders(cf1, scl);89testLayer(layer1);9091ModuleLayer layer2 = layer1.defineModulesWithManyLoaders(cf2, scl);92testLayer(layer2);93}9495/**96* Tests that the layer contains s1, p1, s2, and p2.97*98* Tests loading instances of s1/p.S and s2/p.S.99*/100private void testLayer(ModuleLayer layer) throws Exception {101assertTrue(layer.modules().size() == 4);102Module s1 = layer.findModule("s1").get();103Module p1 = layer.findModule("p1").get();104Module s2 = layer.findModule("s2").get();105Module p2 = layer.findModule("p2").get();106107// p1 reads s1108assertTrue(p1.canRead(s1));109assertFalse(p1.canRead(s2));110111// p2 reads s2112assertTrue(p2.canRead(s2));113assertFalse(p2.canRead(s1));114115// iterate over implementations of s1/p.S116{117ClassLoader loader = layer.findLoader("s1");118Class<?> service = loader.loadClass("p.S");119120List<?> list = collectAll(ServiceLoader.load(service, loader));121assertTrue(list.size() == 1);122assertTrue(list.get(0).getClass().getModule() == p1);123124list = collectAll(ServiceLoader.load(layer, service));125assertTrue(list.size() == 1);126assertTrue(list.get(0).getClass().getModule() == p1);127}128129// iterate over implementations of s2/p.S130{131ClassLoader loader = layer.findLoader("s2");132Class<?> service = loader.loadClass("p.S");133134List<?> list = collectAll(ServiceLoader.load(service, loader));135assertTrue(list.size() == 1);136assertTrue(list.get(0).getClass().getModule() == p2);137138list = collectAll(ServiceLoader.load(layer, service));139assertTrue(list.size() == 1);140assertTrue(list.get(0).getClass().getModule() == p2);141}142}143144private <E> List<E> collectAll(ServiceLoader<E> loader) {145List<E> list = new ArrayList<>();146Iterator<E> iterator = loader.iterator();147while (iterator.hasNext()) {148list.add(iterator.next());149}150return list;151}152}153154155