TransformerFactoryTest.java revision 1038:ccad0993fc67
1/*
2 * Copyright (c) 2003, 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 */
23package javax.xml.transform.ptests;
24
25import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
26import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
27import static jaxp.library.JAXPTestUtilities.USER_DIR;
28import static jaxp.library.JAXPTestUtilities.compareWithGold;
29import static org.testng.Assert.assertNotNull;
30import static org.testng.Assert.assertTrue;
31import static org.testng.Assert.assertNotSame;
32import static org.testng.Assert.assertSame;
33import static org.testng.Assert.assertEquals;
34
35import java.io.FileInputStream;
36import java.io.FileOutputStream;
37
38import javax.xml.parsers.DocumentBuilder;
39import javax.xml.parsers.DocumentBuilderFactory;
40import javax.xml.transform.Source;
41import javax.xml.transform.Transformer;
42import javax.xml.transform.TransformerConfigurationException;
43import javax.xml.transform.TransformerFactory;
44import javax.xml.transform.TransformerFactoryConfigurationError;
45import javax.xml.transform.dom.DOMSource;
46import javax.xml.transform.stream.StreamResult;
47
48import jaxp.library.JAXPDataProvider;
49
50import org.testng.annotations.DataProvider;
51import org.testng.annotations.Listeners;
52import org.testng.annotations.Test;
53import org.w3c.dom.Document;
54
55/**
56 * Class containing the test cases for TransformerFactory API's
57 * getAssociatedStyleSheet method and TransformerFactory.newInstance(factoryClassName , classLoader).
58 */
59/*
60 * @test
61 * @bug 8169778
62 * @library /javax/xml/jaxp/libs
63 * @run testng/othervm -DrunSecMngr=true javax.xml.transform.ptests.TransformerFactoryTest
64 * @run testng/othervm javax.xml.transform.ptests.TransformerFactoryTest
65 */
66@Listeners({jaxp.library.FilePolicy.class})
67public class TransformerFactoryTest {
68    /**
69     * TransformerFactory builtin system-default implementation class name.
70     */
71    private static final String DEFAULT_IMPL_CLASS =
72       "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
73
74    /**
75     * TransformerFactory implementation class name.
76     */
77    private static final String TRANSFORMER_FACTORY_CLASSNAME = DEFAULT_IMPL_CLASS;
78
79    /**
80     * Provide valid TransformerFactory instantiation parameters.
81     *
82     * @return a data provider contains TransformerFactory instantiation parameters.
83     */
84    @DataProvider(name = "parameters")
85    public Object[][] getValidateParameters() {
86        return new Object[][] { { TRANSFORMER_FACTORY_CLASSNAME, null }, { TRANSFORMER_FACTORY_CLASSNAME, this.getClass().getClassLoader() } };
87    }
88
89    /**
90     * Test if newDefaultInstance() method returns an instance
91     * of the expected factory.
92     * @throws Exception If any errors occur.
93     */
94    @Test
95    public void testDefaultInstance() throws Exception {
96        TransformerFactory tf1 = TransformerFactory.newDefaultInstance();
97        TransformerFactory tf2 = TransformerFactory.newInstance();
98        assertNotSame(tf1, tf2, "same instance returned:");
99        assertSame(tf1.getClass(), tf2.getClass(),
100                  "unexpected class mismatch for newDefaultInstance():");
101        assertEquals(tf1.getClass().getName(), DEFAULT_IMPL_CLASS);
102    }
103
104    /**
105     * Test for TransformerFactory.newInstance(java.lang.String
106     * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName
107     * points to correct implementation of
108     * javax.xml.transform.TransformerFactory , should return newInstance of
109     * TransformerFactory
110     *
111     * @param factoryClassName
112     * @param classLoader
113     * @throws TransformerConfigurationException
114     */
115    @Test(dataProvider = "parameters")
116    public void testNewInstance(String factoryClassName, ClassLoader classLoader) throws TransformerConfigurationException {
117        TransformerFactory tf = TransformerFactory.newInstance(factoryClassName, classLoader);
118        Transformer transformer = tf.newTransformer();
119        assertNotNull(transformer);
120    }
121
122    /**
123     * Test for TransformerFactory.newInstance(java.lang.String
124     * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName is
125     * null , should throw TransformerFactoryConfigurationError
126     *
127     * @param factoryClassName
128     * @param classLoader
129     */
130    @Test(expectedExceptions = TransformerFactoryConfigurationError.class, dataProvider = "new-instance-neg", dataProviderClass = JAXPDataProvider.class)
131    public void testNewInstanceNeg(String factoryClassName, ClassLoader classLoader) {
132        TransformerFactory.newInstance(factoryClassName, classLoader);
133    }
134
135    /**
136     * This test case checks for the getAssociatedStylesheet method
137     * of TransformerFactory.
138     * The style sheet returned is then copied to an tfactory01.out
139     * It will then be verified to see if it matches the golden files.
140     *
141     * @throws Exception If any errors occur.
142     */
143    @Test
144    public void tfactory01() throws Exception {
145        String outputFile = USER_DIR + "tfactory01.out";
146        String goldFile = GOLDEN_DIR + "tfactory01GF.out";
147        String xmlFile = XML_DIR + "TransformerFactoryTest.xml";
148        String xmlURI = "file:///" + XML_DIR;
149
150        try (FileInputStream fis = new FileInputStream(xmlFile);
151                FileOutputStream fos = new FileOutputStream(outputFile);) {
152            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
153
154            DocumentBuilder db = dbf.newDocumentBuilder();
155            Document doc = db.parse(fis, xmlURI);
156            DOMSource domSource = new DOMSource(doc);
157            domSource.setSystemId(xmlURI);
158            StreamResult streamResult = new StreamResult(fos);
159            TransformerFactory tFactory = TransformerFactory.newInstance();
160
161            Source s = tFactory.getAssociatedStylesheet(domSource, "screen",
162                                           "Modern", null);
163            Transformer t = tFactory.newTransformer();
164            t.transform(s, streamResult);
165        }
166        assertTrue(compareWithGold(goldFile, outputFile));
167    }
168}
169