/* * Copyright (c) 2003, 2015, 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 javax.xml.transform.ptests; import java.io.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.*; import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR; import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR; import javax.xml.transform.stream.*; import jaxp.library.JAXPFileBaseTest; import static jaxp.library.JAXPTestUtilities.USER_DIR; import static jaxp.library.JAXPTestUtilities.compareWithGold; import static org.testng.Assert.assertNotNull; import static org.testng.Assert.assertTrue; import org.testng.annotations.DataProvider; import org.testng.annotations.Test; import org.w3c.dom.*; /** * Class containing the test cases for TransformerFactory API's * getAssociatedStyleSheet method and TransformerFactory.newInstance(factoryClassName , classLoader). */ public class TransformerFactoryTest extends JAXPFileBaseTest { private static final String TRANSFORMER_FACTORY_CLASSNAME = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl"; @DataProvider(name = "parameters") public Object[][] getValidateParameters() { return new Object[][] { { TRANSFORMER_FACTORY_CLASSNAME, null }, { TRANSFORMER_FACTORY_CLASSNAME, this.getClass().getClassLoader() } }; } /* * test for TransformerFactory.newInstance(java.lang.String * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName * points to correct implementation of * javax.xml.transform.TransformerFactory , should return newInstance of * TransformerFactory */ @Test(dataProvider = "parameters") public void testNewInstance(String factoryClassName, ClassLoader classLoader) throws TransformerConfigurationException { TransformerFactory tf = TransformerFactory.newInstance(factoryClassName, classLoader); Transformer transformer = tf.newTransformer(); assertNotNull(transformer); } @DataProvider(name = "invalid-parameters") public Object[][] getInvalidateParameters() { return new Object[][] { { null, null }, { null, this.getClass().getClassLoader() } }; } /* * test for TransformerFactory.newInstance(java.lang.String * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName is * null , should throw TransformerFactoryConfigurationError */ @Test(expectedExceptions = TransformerFactoryConfigurationError.class, dataProvider = "invalid-parameters") public void testNewInstanceNeg(String factoryClassName, ClassLoader classLoader) { TransformerFactory.newInstance(factoryClassName, classLoader); } /** * This test case checks for the getAssociatedStylesheet method * of TransformerFactory. * The style sheet returned is then copied to an tfactory01.out * It will then be verified to see if it matches the golden files. * * @throws Exception If any errors occur. */ @Test public void tfactory01() throws Exception { String outputFile = USER_DIR + "tfactory01.out"; String goldFile = GOLDEN_DIR + "tfactory01GF.out"; String xmlFile = XML_DIR + "TransformerFactoryTest.xml"; String xmlURI = "file:///" + XML_DIR; try (FileInputStream fis = new FileInputStream(xmlFile); FileOutputStream fos = new FileOutputStream(outputFile);) { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db = dbf.newDocumentBuilder(); Document doc = db.parse(fis, xmlURI); DOMSource domSource = new DOMSource(doc); domSource.setSystemId(xmlURI); StreamResult streamResult = new StreamResult(fos); TransformerFactory tFactory = TransformerFactory.newInstance(); Source s = tFactory.getAssociatedStylesheet(domSource, "screen", "Modern", null); Transformer t = tFactory.newTransformer(); t.transform(s, streamResult); } assertTrue(compareWithGold(goldFile, outputFile)); } }