1/* 2 * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 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 * @library /lib/testlibrary 27 * @modules jdk.compiler 28 * @build CompilerUtils 29 * @run testng NoInterferenceTest 30 * @summary Basic test of ServiceLoader that ensures there is no interference 31 * when there are two service interfaces of the same name in a layer 32 * or overridden in a child layer. 33 */ 34 35import java.lang.module.Configuration; 36import java.lang.module.ModuleFinder; 37import java.nio.file.Files; 38import java.nio.file.Path; 39import java.nio.file.Paths; 40import java.util.ArrayList; 41import java.util.Arrays; 42import java.util.Iterator; 43import java.util.List; 44import java.util.ServiceLoader; 45import java.util.Set; 46 47import org.testng.annotations.BeforeTest; 48import org.testng.annotations.Test; 49import static org.testng.Assert.*; 50 51public class NoInterferenceTest { 52 53 private static final String TEST_SRC = System.getProperty("test.src"); 54 private static final Path SRC_DIR = Paths.get(TEST_SRC, "modules"); 55 private static final Path MODS_DIR = Paths.get("mods"); 56 private static final List<String> MODULES = Arrays.asList("s1", "p1", "s2", "p2"); 57 58 @BeforeTest 59 void compile() throws Exception { 60 Files.createDirectory(MODS_DIR); 61 for (String name : MODULES) { 62 Path src = SRC_DIR.resolve(name); 63 Path output = Files.createDirectory(MODS_DIR.resolve(name)); 64 assertTrue(CompilerUtils.compile(src, output, "-p", MODS_DIR.toString())); 65 } 66 } 67 68 @Test 69 public void test() throws Exception { 70 ModuleFinder empty = ModuleFinder.of(); 71 ModuleFinder finder = ModuleFinder.of(MODS_DIR); 72 73 ModuleLayer bootLayer = ModuleLayer.boot(); 74 75 Configuration cf0 = bootLayer.configuration(); 76 Configuration cf1 = cf0.resolveAndBind(finder, empty, Set.of("s1", "s2")); 77 Configuration cf2 = cf1.resolveAndBind(finder, empty, Set.of("s1", "s2")); 78 79 // cf1 contains s1, p1, s2, p2 80 assertTrue(cf1.modules().size() == 4); 81 82 // cf1 contains s1, p1, s2, p2 83 assertTrue(cf2.modules().size() == 4); 84 85 ClassLoader scl = ClassLoader.getSystemClassLoader(); 86 87 ModuleLayer layer1 = bootLayer.defineModulesWithManyLoaders(cf1, scl); 88 testLayer(layer1); 89 90 ModuleLayer layer2 = layer1.defineModulesWithManyLoaders(cf2, scl); 91 testLayer(layer2); 92 } 93 94 /** 95 * Tests that the layer contains s1, p1, s2, and p2. 96 * 97 * Tests loading instances of s1/p.S and s2/p.S. 98 */ 99 private void testLayer(ModuleLayer layer) throws Exception { 100 assertTrue(layer.modules().size() == 4); 101 Module s1 = layer.findModule("s1").get(); 102 Module p1 = layer.findModule("p1").get(); 103 Module s2 = layer.findModule("s2").get(); 104 Module p2 = layer.findModule("p2").get(); 105 106 // p1 reads s1 107 assertTrue(p1.canRead(s1)); 108 assertFalse(p1.canRead(s2)); 109 110 // p2 reads s2 111 assertTrue(p2.canRead(s2)); 112 assertFalse(p2.canRead(s1)); 113 114 // iterate over implementations of s1/p.S 115 { 116 ClassLoader loader = layer.findLoader("s1"); 117 Class<?> service = loader.loadClass("p.S"); 118 119 List<?> list = collectAll(ServiceLoader.load(service, loader)); 120 assertTrue(list.size() == 1); 121 assertTrue(list.get(0).getClass().getModule() == p1); 122 123 list = collectAll(ServiceLoader.load(layer, service)); 124 assertTrue(list.size() == 1); 125 assertTrue(list.get(0).getClass().getModule() == p1); 126 } 127 128 // iterate over implementations of s2/p.S 129 { 130 ClassLoader loader = layer.findLoader("s2"); 131 Class<?> service = loader.loadClass("p.S"); 132 133 List<?> list = collectAll(ServiceLoader.load(service, loader)); 134 assertTrue(list.size() == 1); 135 assertTrue(list.get(0).getClass().getModule() == p2); 136 137 list = collectAll(ServiceLoader.load(layer, service)); 138 assertTrue(list.size() == 1); 139 assertTrue(list.get(0).getClass().getModule() == p2); 140 } 141 } 142 143 private <E> List<E> collectAll(ServiceLoader<E> loader) { 144 List<E> list = new ArrayList<>(); 145 Iterator<E> iterator = loader.iterator(); 146 while (iterator.hasNext()) { 147 list.add(iterator.next()); 148 } 149 return list; 150 } 151}