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