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.assertEquals; 25 import static org.testng.Assert.assertTrue; 26 27 import java.io.File; 28 import java.net.URL; 29 import java.net.URLClassLoader; 30 import java.nio.file.Files; 31 import java.nio.file.Path; 32 import java.nio.file.Paths; 33 34 import jdk.testlibrary.CompilerUtils; 35 36 import org.testng.Assert; 37 import org.testng.annotations.BeforeTest; 38 import org.testng.annotations.Test; 39 import org.xml.sax.XMLReader; 40 import org.xml.sax.helpers.XMLReaderFactory; 41 42 /* 43 * @test 44 * @library /javax/xml/jaxp/libs 45 * @build jdk.testlibrary.* 46 * @run testng XMLReaderFactoryTest 47 * @bug 8152912 8015099 8156119 48 * @summary Tests XMLReaderFactory can work as ServiceLoader compliant, as well as backward compatible 49 */ 50 51 @Test 52 public class XMLReaderFactoryTest { 53 private static final String TEST_SRC = System.getProperty("test.src"); 54 55 private static final Path SRC_DIR = Paths.get(TEST_SRC, "src").resolve("xmlprovider3"); 56 private static final Path CLASSES_DIR = Paths.get("classes"); 57 private static final Path LEGACY_DIR = CLASSES_DIR.resolve("legacy"); 58 private static final Path SERVICE_DIR = CLASSES_DIR.resolve("service"); 59 60 // resources to copy to the class path 61 private static final String LEGACY_SERVICE_FILE = "legacy/META-INF/services/org.xml.sax.driver"; 62 private static final String SERVICE_FILE = "service/META-INF/services/org.xml.sax.XMLReader"; 63 64 /* 65 * Compile class and copy service files 66 */ 67 @BeforeTest 68 public void setup() throws Exception { 69 setup(LEGACY_DIR, LEGACY_SERVICE_FILE); 70 setup(SERVICE_DIR, SERVICE_FILE); 71 } 72 73 private void setup(Path dest, String serviceFile) throws Exception { 74 Files.createDirectories(dest); 75 assertTrue(CompilerUtils.compile(SRC_DIR, dest)); 76 77 Path file = Paths.get(serviceFile.replace('/', File.separatorChar)); 78 Path source = SRC_DIR.resolve(file); 79 Path target = CLASSES_DIR.resolve(file); 80 Files.createDirectories(target.getParent()); 81 Files.copy(source, target); 82 83 } 84 85 public void testService() throws Exception { 86 ClassLoader clBackup = Thread.currentThread().getContextClassLoader(); 87 try { 88 URL[] classUrls = { SERVICE_DIR.toUri().toURL() }; 89 URLClassLoader loader = new URLClassLoader(classUrls, ClassLoader.getSystemClassLoader().getParent()); 90 91 // set TCCL and try locating the provider 92 Thread.currentThread().setContextClassLoader(loader); 93 XMLReader reader = XMLReaderFactory.createXMLReader(); 94 assertEquals(reader.getClass().getName(), "xp3.XMLReaderImpl"); 95 } finally { 96 Thread.currentThread().setContextClassLoader(clBackup); 97 } 98 } 99 100 public void testLegacy() throws Exception { 101 ClassLoader clBackup = Thread.currentThread().getContextClassLoader(); 102 try { 103 URL[] classUrls = { LEGACY_DIR.toUri().toURL() }; 104 URLClassLoader loader = new URLClassLoader(classUrls, ClassLoader.getSystemClassLoader().getParent()); 105 106 // set TCCL and try locating the provider 107 Thread.currentThread().setContextClassLoader(loader); 108 XMLReader reader1 = XMLReaderFactory.createXMLReader(); 109 assertEquals(reader1.getClass().getName(), "xp3.XMLReaderImpl"); 110 111 // now point to a random URL 112 Thread.currentThread().setContextClassLoader( 113 new URLClassLoader(new URL[0], ClassLoader.getSystemClassLoader().getParent())); 114 // ClassNotFoundException if also trying to load class of reader1, which 115 // would be the case before 8152912 116 XMLReader reader2 = XMLReaderFactory.createXMLReader(); 117 assertEquals(reader2.getClass().getName(), "com.sun.org.apache.xerces.internal.parsers.SAXParser"); 118 } finally { 119 Thread.currentThread().setContextClassLoader(clBackup); 120 } 121 } 122 }