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