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.File;
  26 import java.io.FileInputStream;
  27 import java.io.FileOutputStream;
  28 import java.io.IOException;
  29 import javax.xml.parsers.DocumentBuilderFactory;
  30 import javax.xml.parsers.ParserConfigurationException;
  31 import javax.xml.transform.Transformer;
  32 import javax.xml.transform.TransformerException;
  33 import javax.xml.transform.TransformerFactory;
  34 import javax.xml.transform.dom.DOMSource;
  35 import static javax.xml.transform.ptests.TransformerTestConst.CLASS_DIR;
  36 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  37 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  38 import javax.xml.transform.stream.StreamResult;
  39 import javax.xml.transform.stream.StreamSource;
  40 import jaxp.library.JAXPFileBaseTest;
  41 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  42 import static org.testng.Assert.assertTrue;
  43 import org.testng.annotations.Test;
  44 import org.xml.sax.SAXException;
  45 
  46 
  47 /**
  48  * Here a transformer is created using DOMSource. Some specific output property
  49  * is set on transformer. Then transform(StreamSource, StreamResult) is tested.
  50  */
  51 public class TransformerTest02 extends JAXPFileBaseTest {
  52     /**
  53      * Unit test for transform(StreamSource, StreamResult).
  54      * 
  55      * @throws SAXException If any parse errors occur.
  56      * @throws IOException if the file exists but is a directory rather than
  57      *         a regular file, does not exist but cannot be created, or cannot 
  58      *         be opened for any other reason.
  59      * @throws TransformerException If an unrecoverable error occurs during the
  60      *         course of the transformation.
  61      * @throws ParserConfigurationException if a DocumentBuilder cannot be 
  62      *         created which satisfies the configuration requested.
  63      */
  64     @Test
  65     public void testcase01() throws ParserConfigurationException, SAXException, 
  66             TransformerException, IOException {
  67         String outputFile = CLASS_DIR + "transformer02.out";
  68         String goldFile = GOLDEN_DIR + "transformer02GF.out";
  69         String xsltFile = XML_DIR + "cities.xsl";
  70         String xmlFile = XML_DIR + "cities.xml";
  71 
  72         try (FileInputStream fis = new FileInputStream(xmlFile);
  73                 FileOutputStream fos = new FileOutputStream(outputFile)) {
  74             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  75             dbf.setNamespaceAware(true);
  76             DOMSource domSource = new DOMSource(dbf.newDocumentBuilder().
  77                     parse(new File(xsltFile)));
  78 
  79             Transformer transformer = TransformerFactory.newInstance().
  80                     newTransformer(domSource);
  81             StreamSource streamSource = new StreamSource(fis);
  82             StreamResult streamResult = new StreamResult(fos);
  83 
  84             transformer.setOutputProperty("indent", "no");
  85             transformer.transform(streamSource, streamResult);
  86         }
  87         assertTrue(compareWithGold(goldFile, outputFile));
  88     }
  89 }