1 /* 2 * Copyright (c) 2016, 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 import static org.testng.Assert.assertSame; 25 import static org.testng.Assert.assertTrue; 26 27 import java.lang.ClassLoader; 28 import java.lang.String; 29 import java.lang.System; 30 import java.lang.module.Configuration; 31 import java.lang.module.ModuleFinder; 32 import java.lang.reflect.Layer; 33 import java.lang.reflect.Method; 34 import java.lang.reflect.Module; 35 import java.nio.file.Paths; 36 import java.nio.file.Path; 37 import java.util.Collections; 38 import java.util.Iterator; 39 import java.util.ServiceLoader; 40 import java.util.Set; 41 42 import org.testng.annotations.BeforeTest; 43 import org.testng.annotations.Test; 44 45 import jdk.testlibrary.CompilerUtils; 46 47 /* 48 * @test 49 * @library /javax/xml/jaxp/libs 50 * @build jdk.testlibrary.* 51 * @run testng LayerModularXMLParserTest 52 * @bug 8078820 8156119 53 * @summary Tests JAXP lib works with layer and TCCL 54 */ 55 56 @Test 57 public class LayerModularXMLParserTest { 58 59 private static final String TEST_SRC = System.getProperty("test.src"); 60 61 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src"); 62 private static final Path MOD_DIR1 = Paths.get("mod1"); 63 private static final Path MOD_DIR2 = Paths.get("mod2"); 64 65 /* 66 * services provided by provider1 67 */ 68 private static final String[] services1 = { "javax.xml.parsers.DocumentBuilderFactory", 69 "javax.xml.parsers.SAXParserFactory", "javax.xml.stream.XMLInputFactory", 70 "javax.xml.stream.XMLOutputFactory", "javax.xml.transform.TransformerFactory", 71 "javax.xml.validation.SchemaFactory", "javax.xml.xpath.XPathFactory" }; 72 73 /* 74 * services provided by provider2 75 */ 76 private static final String[] services2 = { "javax.xml.datatype.DatatypeFactory", 77 "javax.xml.stream.XMLEventFactory", "org.xml.sax.XMLReader" }; 78 79 /* 80 * Compiles all modules used by the test 81 */ 82 @BeforeTest 83 public void compileAll() throws Exception { 84 assertTrue(CompilerUtils.compile(SRC_DIR.resolve("xmlprovider1"), MOD_DIR1.resolve("xmlprovider1"))); 85 assertTrue(CompilerUtils.compile(SRC_DIR.resolve("xmlprovider2"), MOD_DIR2.resolve("xmlprovider2"))); 86 assertTrue(CompilerUtils.compile(SRC_DIR.resolve("test"), MOD_DIR1.resolve("test"))); 87 assertTrue(CompilerUtils.compile(SRC_DIR.resolve("test"), MOD_DIR2.resolve("test"))); 88 } 89 90 /* 91 * layer 1 is created on top of boot layer, layer1 includes module provider1. 92 * 93 * Instantiate each XML service, verify the services provided by provider1 94 * are loaded from layer 1, the other services are loaded from boot layer 95 */ 96 public void testOneLayer() throws Exception { 97 ModuleFinder finder1 = ModuleFinder.of(MOD_DIR1); 98 Configuration cf1 = Layer.boot().configuration() 99 .resolveAndBind(finder1, ModuleFinder.of(), Set.of("test")); 100 ClassLoader scl = ClassLoader.getSystemClassLoader(); 101 Layer layer1 = Layer.boot().defineModulesWithManyLoaders(cf1, scl); 102 ClassLoader cl1 = layer1.findLoader("test"); 103 104 Method m = cl1.loadClass("test.XMLFactoryHelper").getMethod("instantiateXMLService", String.class); 105 for (String service : services1) { 106 Object o = m.invoke(null, service); 107 Layer providerLayer = o.getClass().getModule().getLayer(); 108 assertSame(providerLayer, layer1); 109 } 110 111 for (String service : services2) { 112 Object o = m.invoke(null, service); 113 Layer providerLayer = o.getClass().getModule().getLayer(); 114 assertSame(providerLayer, Layer.boot()); 115 } 116 117 } 118 119 /* 120 * layer 1 is created on top of boot layer, layer 1 includes module provider1. 121 * layer 2 is created on top of layer 1, layer 2 includes module provider2. 122 * 123 * Instantiate each XML service, verify the services provided by provider1 124 * are loaded from layer 1, the services provided by provider2 are loaded from layer 2 125 */ 126 public void testTwoLayer() throws Exception { 127 ModuleFinder finder1 = ModuleFinder.of(MOD_DIR1); 128 Configuration cf1 = Layer.boot().configuration() 129 .resolveAndBind(finder1, ModuleFinder.of(), Set.of("test")); 130 ClassLoader scl = ClassLoader.getSystemClassLoader(); 131 Layer layer1 = Layer.boot().defineModulesWithManyLoaders(cf1, scl); 132 133 ModuleFinder finder2 = ModuleFinder.of(MOD_DIR2); 134 Configuration cf2 = cf1.resolveAndBind(finder2, ModuleFinder.of(), Set.of("test")); 135 Layer layer2 = layer1.defineModulesWithOneLoader(cf2, layer1.findLoader("test")); 136 ClassLoader cl2 = layer2.findLoader("test"); 137 138 Method m = cl2.loadClass("test.XMLFactoryHelper").getMethod("instantiateXMLService", String.class); 139 for (String service : services1) { 140 Object o = m.invoke(null, service); 141 Layer providerLayer = o.getClass().getModule().getLayer(); 142 assertSame(providerLayer, layer1); 143 } 144 145 for (String service : services2) { 146 Object o = m.invoke(null, service); 147 Layer providerLayer = o.getClass().getModule().getLayer(); 148 assertSame(providerLayer, layer2); 149 } 150 151 } 152 153 /* 154 * layer 1 is created on top of boot layer, layer 1 includes module provider1 and provider2. 155 * layer 2 is created on top of layer 1, layer 2 includes module provider2. 156 * 157 * Instantiate each XML service, verify the services provided by provider1 158 * are loaded from layer 1, the services provided by provider2 are loaded from layer 2 159 */ 160 public void testTwoLayerWithDuplicate() throws Exception { 161 ModuleFinder finder1 = ModuleFinder.of(MOD_DIR1, MOD_DIR2); 162 Configuration cf1 = Layer.boot().configuration() 163 .resolveAndBind(finder1, ModuleFinder.of(), Set.of("test")); 164 ClassLoader scl = ClassLoader.getSystemClassLoader(); 165 Layer layer1 = Layer.boot().defineModulesWithManyLoaders(cf1, scl); 166 167 ModuleFinder finder2 = ModuleFinder.of(MOD_DIR2); 168 Configuration cf2 = cf1.resolveAndBind(finder2, ModuleFinder.of(), Set.of("test")); 169 Layer layer2 = layer1.defineModulesWithOneLoader(cf2, layer1.findLoader("test")); 170 ClassLoader cl2 = layer2.findLoader("test"); 171 172 Method m = cl2.loadClass("test.XMLFactoryHelper").getMethod("instantiateXMLService", String.class); 173 for (String service : services1) { 174 Object o = m.invoke(null, service); 175 Layer providerLayer = o.getClass().getModule().getLayer(); 176 assertSame(providerLayer, layer1); 177 } 178 179 for (String service : services2) { 180 Object o = m.invoke(null, service); 181 Layer providerLayer = o.getClass().getModule().getLayer(); 182 assertSame(providerLayer, layer2); 183 } 184 185 } 186 }