8156119: Update ServiceProviderTest for XMLReaderFactory
Reviewed-by: joehw
This commit is contained in:
parent
1e67fc1de6
commit
0537b18b12
@ -38,7 +38,7 @@ import org.testng.annotations.Test;
|
||||
* @library /javax/xml/jaxp/libs
|
||||
* @build jdk.testlibrary.*
|
||||
* @run testng BasicModularXMLParserTest
|
||||
* @bug 8078820
|
||||
* @bug 8078820 8156119
|
||||
* @summary Tests JAXP lib can instantiate the following interfaces
|
||||
* with customized provider module on boot layer
|
||||
*
|
||||
@ -51,6 +51,7 @@ import org.testng.annotations.Test;
|
||||
* javax.xml.transform.TransformerFactory
|
||||
* javax.xml.validation.SchemaFactory
|
||||
* javax.xml.xpath.XPathFactory
|
||||
* org.xml.sax.XMLReader
|
||||
*/
|
||||
|
||||
@Test
|
||||
|
@ -50,7 +50,7 @@ import jdk.testlibrary.CompilerUtils;
|
||||
* @library /javax/xml/jaxp/libs
|
||||
* @build jdk.testlibrary.*
|
||||
* @run testng LayerModularXMLParserTest
|
||||
* @bug 8078820
|
||||
* @bug 8078820 8156119
|
||||
* @summary Tests JAXP lib works with layer and TCCL
|
||||
*/
|
||||
|
||||
@ -75,7 +75,7 @@ public class LayerModularXMLParserTest {
|
||||
* services provided by provider2
|
||||
*/
|
||||
private static final String[] services2 = { "javax.xml.datatype.DatatypeFactory",
|
||||
"javax.xml.stream.XMLEventFactory" };
|
||||
"javax.xml.stream.XMLEventFactory", "org.xml.sax.XMLReader" };
|
||||
|
||||
/*
|
||||
* Compiles all modules used by the test
|
||||
|
@ -0,0 +1,122 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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.
|
||||
*/
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
import static org.testng.Assert.assertTrue;
|
||||
|
||||
import java.io.File;
|
||||
import java.net.URL;
|
||||
import java.net.URLClassLoader;
|
||||
import java.nio.file.Files;
|
||||
import java.nio.file.Path;
|
||||
import java.nio.file.Paths;
|
||||
|
||||
import jdk.testlibrary.CompilerUtils;
|
||||
|
||||
import org.testng.Assert;
|
||||
import org.testng.annotations.BeforeTest;
|
||||
import org.testng.annotations.Test;
|
||||
import org.xml.sax.XMLReader;
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
/*
|
||||
* @test
|
||||
* @library /javax/xml/jaxp/libs
|
||||
* @build jdk.testlibrary.*
|
||||
* @run testng XMLReaderFactoryTest
|
||||
* @bug 8152912 8015099 8156119
|
||||
* @summary Tests XMLReaderFactory can work as ServiceLoader compliant, as well as backward compatible
|
||||
*/
|
||||
|
||||
@Test
|
||||
public class XMLReaderFactoryTest {
|
||||
private static final String TEST_SRC = System.getProperty("test.src");
|
||||
|
||||
private static final Path SRC_DIR = Paths.get(TEST_SRC, "src").resolve("xmlprovider3");
|
||||
private static final Path CLASSES_DIR = Paths.get("classes");
|
||||
private static final Path LEGACY_DIR = CLASSES_DIR.resolve("legacy");
|
||||
private static final Path SERVICE_DIR = CLASSES_DIR.resolve("service");
|
||||
|
||||
// resources to copy to the class path
|
||||
private static final String LEGACY_SERVICE_FILE = "legacy/META-INF/services/org.xml.sax.driver";
|
||||
private static final String SERVICE_FILE = "service/META-INF/services/org.xml.sax.XMLReader";
|
||||
|
||||
/*
|
||||
* Compile class and copy service files
|
||||
*/
|
||||
@BeforeTest
|
||||
public void setup() throws Exception {
|
||||
setup(LEGACY_DIR, LEGACY_SERVICE_FILE);
|
||||
setup(SERVICE_DIR, SERVICE_FILE);
|
||||
}
|
||||
|
||||
private void setup(Path dest, String serviceFile) throws Exception {
|
||||
Files.createDirectories(dest);
|
||||
assertTrue(CompilerUtils.compile(SRC_DIR, dest));
|
||||
|
||||
Path file = Paths.get(serviceFile.replace('/', File.separatorChar));
|
||||
Path source = SRC_DIR.resolve(file);
|
||||
Path target = CLASSES_DIR.resolve(file);
|
||||
Files.createDirectories(target.getParent());
|
||||
Files.copy(source, target);
|
||||
|
||||
}
|
||||
|
||||
public void testService() throws Exception {
|
||||
ClassLoader clBackup = Thread.currentThread().getContextClassLoader();
|
||||
try {
|
||||
URL[] classUrls = { SERVICE_DIR.toUri().toURL() };
|
||||
URLClassLoader loader = new URLClassLoader(classUrls, ClassLoader.getSystemClassLoader().getParent());
|
||||
|
||||
// set TCCL and try locating the provider
|
||||
Thread.currentThread().setContextClassLoader(loader);
|
||||
XMLReader reader = XMLReaderFactory.createXMLReader();
|
||||
assertEquals(reader.getClass().getName(), "xp3.XMLReaderImpl");
|
||||
} finally {
|
||||
Thread.currentThread().setContextClassLoader(clBackup);
|
||||
}
|
||||
}
|
||||
|
||||
public void testLegacy() throws Exception {
|
||||
ClassLoader clBackup = Thread.currentThread().getContextClassLoader();
|
||||
try {
|
||||
URL[] classUrls = { LEGACY_DIR.toUri().toURL() };
|
||||
URLClassLoader loader = new URLClassLoader(classUrls, ClassLoader.getSystemClassLoader().getParent());
|
||||
|
||||
// set TCCL and try locating the provider
|
||||
Thread.currentThread().setContextClassLoader(loader);
|
||||
XMLReader reader1 = XMLReaderFactory.createXMLReader();
|
||||
assertEquals(reader1.getClass().getName(), "xp3.XMLReaderImpl");
|
||||
|
||||
// now point to a random URL
|
||||
Thread.currentThread().setContextClassLoader(
|
||||
new URLClassLoader(new URL[0], ClassLoader.getSystemClassLoader().getParent()));
|
||||
// ClassNotFoundException if also trying to load class of reader1, which
|
||||
// would be the case before 8152912
|
||||
XMLReader reader2 = XMLReaderFactory.createXMLReader();
|
||||
assertEquals(reader2.getClass().getName(), "com.sun.org.apache.xerces.internal.parsers.SAXParser");
|
||||
} finally {
|
||||
Thread.currentThread().setContextClassLoader(clBackup);
|
||||
}
|
||||
}
|
||||
}
|
@ -31,6 +31,8 @@ import java.lang.System;
|
||||
import java.util.Iterator;
|
||||
import java.util.ServiceLoader;
|
||||
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
public class XMLFactoryHelper {
|
||||
/*
|
||||
* instantiate a xml factory by reflection e.g.
|
||||
@ -41,7 +43,9 @@ public class XMLFactoryHelper {
|
||||
try {
|
||||
// set thread context class loader to module class loader
|
||||
Thread.currentThread().setContextClassLoader(XMLFactoryHelper.class.getClassLoader());
|
||||
if (serviceName.equals("javax.xml.validation.SchemaFactory"))
|
||||
if (serviceName.equals("org.xml.sax.XMLReader"))
|
||||
return XMLReaderFactory.createXMLReader();
|
||||
else if (serviceName.equals("javax.xml.validation.SchemaFactory"))
|
||||
return Class.forName(serviceName).getMethod("newInstance", String.class)
|
||||
.invoke(null, W3C_XML_SCHEMA_NS_URI);
|
||||
else
|
||||
|
@ -30,6 +30,8 @@ import java.util.HashSet;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.xml.sax.helpers.XMLReaderFactory;
|
||||
|
||||
public class Main {
|
||||
/*
|
||||
* @param args, the names of provider modules, which have been loaded
|
||||
@ -69,7 +71,9 @@ public class Main {
|
||||
*/
|
||||
private static Object instantiateXMLService(String serviceName) {
|
||||
try {
|
||||
if (serviceName.equals("javax.xml.validation.SchemaFactory"))
|
||||
if (serviceName.equals("org.xml.sax.XMLReader"))
|
||||
return XMLReaderFactory.createXMLReader();
|
||||
else if (serviceName.equals("javax.xml.validation.SchemaFactory"))
|
||||
return Class.forName(serviceName).getMethod("newInstance", String.class)
|
||||
.invoke(null, W3C_XML_SCHEMA_NS_URI);
|
||||
else
|
||||
@ -102,6 +106,7 @@ public class Main {
|
||||
"javax.xml.parsers.DocumentBuilderFactory", "javax.xml.parsers.SAXParserFactory",
|
||||
"javax.xml.stream.XMLEventFactory", "javax.xml.stream.XMLInputFactory",
|
||||
"javax.xml.stream.XMLOutputFactory", "javax.xml.transform.TransformerFactory",
|
||||
"javax.xml.validation.SchemaFactory", "javax.xml.xpath.XPathFactory" };
|
||||
"javax.xml.validation.SchemaFactory", "javax.xml.xpath.XPathFactory",
|
||||
"org.xml.sax.XMLReader"};
|
||||
|
||||
}
|
||||
|
@ -26,4 +26,5 @@ module xmlprovider2 {
|
||||
|
||||
provides javax.xml.datatype.DatatypeFactory with xp2.DatatypeFactoryImpl;
|
||||
provides javax.xml.stream.XMLEventFactory with xp2.XMLEventFactoryImpl;
|
||||
provides org.xml.sax.XMLReader with xp2.XMLReaderImpl;
|
||||
}
|
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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.
|
||||
*/
|
||||
|
||||
package xp2;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.DTDHandler;
|
||||
import org.xml.sax.EntityResolver;
|
||||
import org.xml.sax.ErrorHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.SAXNotRecognizedException;
|
||||
import org.xml.sax.SAXNotSupportedException;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
public class XMLReaderImpl implements XMLReader {
|
||||
|
||||
@Override
|
||||
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFeature(String name, boolean value) throws SAXNotRecognizedException,
|
||||
SAXNotSupportedException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProperty(String name, Object value) throws SAXNotRecognizedException,
|
||||
SAXNotSupportedException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEntityResolver(EntityResolver resolver) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityResolver getEntityResolver() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDTDHandler(DTDHandler handler) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DTDHandler getDTDHandler() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContentHandler(ContentHandler handler) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContentHandler getContentHandler() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setErrorHandler(ErrorHandler handler) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ErrorHandler getErrorHandler() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(InputSource input) throws IOException, SAXException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(String systemId) throws IOException, SAXException {
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1 @@
|
||||
xp3.XMLReaderImpl
|
@ -0,0 +1 @@
|
||||
xp3.XMLReaderImpl
|
@ -0,0 +1,106 @@
|
||||
/*
|
||||
* Copyright (c) 2016, 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.
|
||||
*/
|
||||
|
||||
package xp3;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import org.xml.sax.ContentHandler;
|
||||
import org.xml.sax.DTDHandler;
|
||||
import org.xml.sax.EntityResolver;
|
||||
import org.xml.sax.ErrorHandler;
|
||||
import org.xml.sax.InputSource;
|
||||
import org.xml.sax.SAXException;
|
||||
import org.xml.sax.SAXNotRecognizedException;
|
||||
import org.xml.sax.SAXNotSupportedException;
|
||||
import org.xml.sax.XMLReader;
|
||||
|
||||
public class XMLReaderImpl implements XMLReader {
|
||||
|
||||
@Override
|
||||
public boolean getFeature(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setFeature(String name, boolean value) throws SAXNotRecognizedException,
|
||||
SAXNotSupportedException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getProperty(String name) throws SAXNotRecognizedException, SAXNotSupportedException {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setProperty(String name, Object value) throws SAXNotRecognizedException,
|
||||
SAXNotSupportedException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setEntityResolver(EntityResolver resolver) {
|
||||
// TODO Auto-generated method stub
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public EntityResolver getEntityResolver() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setDTDHandler(DTDHandler handler) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public DTDHandler getDTDHandler() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setContentHandler(ContentHandler handler) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ContentHandler getContentHandler() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void setErrorHandler(ErrorHandler handler) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public ErrorHandler getErrorHandler() {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(InputSource input) throws IOException, SAXException {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void parse(String systemId) throws IOException, SAXException {
|
||||
}
|
||||
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user