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