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 import java.io.FileOutputStream;
  27 import javax.xml.parsers.*;
  28 import javax.xml.transform.*;
  29 import javax.xml.transform.dom.*;
  30 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  31 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  32 import javax.xml.transform.stream.*;
  33 import jaxp.library.JAXPFileBaseTest;
  34 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  35 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  36 import static org.testng.Assert.assertTrue;
  37 import org.testng.annotations.Test;
  38 import org.w3c.dom.*;
  39 
  40 /**
  41  * Class containing the test cases for TransformerFactory API's
  42  * getAssociatedStyleSheet method.
  43  */
  44 public class TransformerFactoryTest extends JAXPFileBaseTest {
  45     /**
  46      * This test case checks for the getAssociatedStylesheet method
  47      * of TransformerFactory.
  48      * The style sheet returned is then copied to an tfactory01.out
  49      * It will then be verified to see if it matches the golden files.
  50      * 
  51      * @throws Exception If any errors occur.
  52      */
  53     @Test
  54     public void tfactory01() throws Exception {
  55         String outputFile = USER_DIR + "tfactory01.out";
  56         String goldFile = GOLDEN_DIR + "tfactory01GF.out";
  57         String xmlFile = XML_DIR + "TransformerFactoryTest.xml";
  58         String xmlURI = "file:///" + XML_DIR;
  59 
  60         try (FileInputStream fis = new FileInputStream(xmlFile);
  61                 FileOutputStream fos = new FileOutputStream(outputFile);) {
  62             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  63 
  64             DocumentBuilder db = dbf.newDocumentBuilder();
  65             Document doc = db.parse(fis, xmlURI);
  66             DOMSource domSource = new DOMSource(doc);
  67             domSource.setSystemId(xmlURI);
  68             StreamResult streamResult = new StreamResult(fos);
  69             TransformerFactory tFactory = TransformerFactory.newInstance();
  70 
  71             Source s = tFactory.getAssociatedStylesheet(domSource, "screen",
  72                                            "Modern", null);
  73             Transformer t = tFactory.newTransformer();
  74             t.transform(s, streamResult);
  75         }
  76         assertTrue(compareWithGold(goldFile, outputFile));
  77     }
  78 }