DefaultFactoryWrapperTest.java revision 1045:427ce6a2c674
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 24import static javax.xml.XMLConstants.W3C_XML_SCHEMA_NS_URI; 25import static org.testng.Assert.assertEquals; 26import static org.testng.Assert.assertSame; 27 28import java.io.StringReader; 29import java.io.StringWriter; 30import java.lang.reflect.Layer; 31import java.lang.reflect.Module; 32 33import javax.xml.datatype.DatatypeFactory; 34import javax.xml.parsers.DocumentBuilderFactory; 35import javax.xml.parsers.SAXParserFactory; 36import javax.xml.stream.XMLEventFactory; 37import javax.xml.stream.XMLInputFactory; 38import javax.xml.stream.XMLOutputFactory; 39import javax.xml.transform.TransformerFactory; 40import javax.xml.validation.SchemaFactory; 41import javax.xml.xpath.XPathFactory; 42 43import org.testng.annotations.DataProvider; 44import org.testng.annotations.Test; 45 46/* 47 * @test 48 * @library src/DefaultFactoryWrapperTest 49 * @build xmlwrapperprovider/* 50 * @run testng/othervm --add-modules=xmlwrapperprovider DefaultFactoryWrapperTest 51 * @bug 8169948 8169778 52 * @summary test customized provider wraps the built-in system-default implementation of JAXP factories 53 */ 54public class DefaultFactoryWrapperTest { 55 private static final Module XML_MODULE = Layer.boot().findModule("java.xml").get(); 56 57 private static final String PROVIDER_PACKAGE = "xwp"; 58 59 /* 60 * Return JAXP factory and corresponding factory function. 61 */ 62 @DataProvider(name = "jaxpFactories") 63 public Object[][] jaxpFactories() throws Exception { 64 return new Object[][] { 65 { DocumentBuilderFactory.newInstance(), (Produce)factory -> ((DocumentBuilderFactory)factory).newDocumentBuilder() }, 66 { SAXParserFactory.newInstance(), (Produce)factory -> ((SAXParserFactory)factory).newSAXParser() }, 67 { SchemaFactory.newInstance(W3C_XML_SCHEMA_NS_URI), (Produce)factory -> ((SchemaFactory)factory).newSchema() }, 68 { TransformerFactory.newInstance(), (Produce)factory -> ((TransformerFactory)factory).newTransformer() }, 69 { XMLEventFactory.newInstance(), (Produce)factory -> ((XMLEventFactory)factory).createStartDocument() }, 70 { XMLInputFactory.newInstance(), (Produce)factory -> ((XMLInputFactory)factory).createXMLEventReader(new StringReader("")) }, 71 { XMLOutputFactory.newInstance(), (Produce)factory -> ((XMLOutputFactory)factory).createXMLEventWriter(new StringWriter()) }, 72 { XPathFactory.newInstance(), (Produce)factory -> ((XPathFactory)factory).newXPath() }, 73 { DatatypeFactory.newInstance(), (Produce)factory -> ((DatatypeFactory)factory).newXMLGregorianCalendar() } 74 }; 75 } 76 77 /* 78 * Verify the factory comes from customized provider, and produces a built-in type. 79 */ 80 @Test(dataProvider = "jaxpFactories") 81 public void testFactory(Object factory, Produce<Object, Object> p) throws Exception { 82 assertEquals(factory.getClass().getPackageName(), PROVIDER_PACKAGE); 83 assertSame(p.produce(factory).getClass().getModule(), XML_MODULE); 84 } 85 86 @FunctionalInterface 87 public interface Produce<T, R> { 88 R produce(T t) throws Exception; 89 } 90} 91