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 @Listeners({jaxp.library.FilePolicy.class})
  57 public class TransformerFactoryTest {
  58     /**
  59      * TransformerFactory implementation class name.
  60      */
  61     private static final String TRANSFORMER_FACTORY_CLASSNAME = "com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl";
  62 
  63     /**
  64      * Provide valid TransformerFactory instantiation parameters.
  65      *
  66      * @return a data provider contains TransformerFactory instantiation parameters.
  67      */
  68     @DataProvider(name = "parameters")
  69     public Object[][] getValidateParameters() {
  70         return new Object[][] { { TRANSFORMER_FACTORY_CLASSNAME, null }, { TRANSFORMER_FACTORY_CLASSNAME, this.getClass().getClassLoader() } };
  71     }
  72 
  73     /**
  74      * Test for TransformerFactory.newInstance(java.lang.String
  75      * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName
  76      * points to correct implementation of
  77      * javax.xml.transform.TransformerFactory , should return newInstance of
  78      * TransformerFactory
  79      *
  80      * @param factoryClassName
  81      * @param classLoader
  82      * @throws TransformerConfigurationException
  83      */
  84     @Test(dataProvider = "parameters")
  85     public void testNewInstance(String factoryClassName, ClassLoader classLoader) throws TransformerConfigurationException {
  86         TransformerFactory tf = TransformerFactory.newInstance(factoryClassName, classLoader);
  87         Transformer transformer = tf.newTransformer();
  88         assertNotNull(transformer);
  89     }
  90 
  91     /**
  92      * Test for TransformerFactory.newInstance(java.lang.String
  93      * factoryClassName, java.lang.ClassLoader classLoader) factoryClassName is
  94      * null , should throw TransformerFactoryConfigurationError
  95      *
  96      * @param factoryClassName
  97      * @param classLoader
  98      */
  99     @Test(expectedExceptions = TransformerFactoryConfigurationError.class, dataProvider = "new-instance-neg", dataProviderClass = JAXPDataProvider.class)
 100     public void testNewInstanceNeg(String factoryClassName, ClassLoader classLoader) {
 101         TransformerFactory.newInstance(factoryClassName, classLoader);
 102     }
 103 
 104     /**
 105      * This test case checks for the getAssociatedStylesheet method
 106      * of TransformerFactory.
 107      * The style sheet returned is then copied to an tfactory01.out
 108      * It will then be verified to see if it matches the golden files.
 109      *
 110      * @throws Exception If any errors occur.
 111      */
 112     @Test
 113     public void tfactory01() throws Exception {
 114         String outputFile = USER_DIR + "tfactory01.out";
 115         String goldFile = GOLDEN_DIR + "tfactory01GF.out";
 116         String xmlFile = XML_DIR + "TransformerFactoryTest.xml";
 117         String xmlURI = "file:///" + XML_DIR;
 118 
 119         try (FileInputStream fis = new FileInputStream(xmlFile);
 120                 FileOutputStream fos = new FileOutputStream(outputFile);) {
 121             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
 122 
 123             DocumentBuilder db = dbf.newDocumentBuilder();
 124             Document doc = db.parse(fis, xmlURI);
 125             DOMSource domSource = new DOMSource(doc);
 126             domSource.setSystemId(xmlURI);
 127             StreamResult streamResult = new StreamResult(fos);
 128             TransformerFactory tFactory = TransformerFactory.newInstance();
 129 
 130             Source s = tFactory.getAssociatedStylesheet(domSource, "screen",
 131                                            "Modern", null);
 132             Transformer t = tFactory.newTransformer();
 133             t.transform(s, streamResult);
 134         }
 135         assertTrue(compareWithGold(goldFile, outputFile));
 136     }
 137 }