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