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.File;
  26 import java.io.FileInputStream;
  27 import java.io.FileOutputStream;
  28 import javax.xml.parsers.DocumentBuilderFactory;
  29 import javax.xml.transform.Transformer;
  30 import javax.xml.transform.TransformerFactory;
  31 import javax.xml.transform.dom.DOMSource;
  32 import static javax.xml.transform.ptests.TransformerTestConst.GOLDEN_DIR;
  33 import static javax.xml.transform.ptests.TransformerTestConst.XML_DIR;
  34 import javax.xml.transform.stream.StreamResult;
  35 import javax.xml.transform.stream.StreamSource;
  36 import jaxp.library.JAXPFileBaseTest;
  37 import static jaxp.library.JAXPTestUtilities.USER_DIR;
  38 import static jaxp.library.JAXPTestUtilities.compareWithGold;
  39 import static org.testng.Assert.assertTrue;
  40 import org.testng.annotations.Test;
  41 
  42 
  43 /**
  44  * Here a transformer is created using DOMSource. Some specific output property
  45  * is set on transformer. Then transform(StreamSource, StreamResult) is tested.
  46  */
  47 public class TransformerTest02 extends JAXPFileBaseTest {
  48     /**
  49      * Unit test for transform(StreamSource, StreamResult).
  50      * 
  51      * @throws Exception If any errors occur.
  52      */
  53     @Test
  54     public void testcase01() throws Exception {
  55         String outputFile = USER_DIR + "transformer02.out";
  56         String goldFile = GOLDEN_DIR + "transformer02GF.out";
  57         String xsltFile = XML_DIR + "cities.xsl";
  58         String xmlFile = XML_DIR + "cities.xml";
  59 
  60         try (FileInputStream fis = new FileInputStream(xmlFile);
  61                 FileOutputStream fos = new FileOutputStream(outputFile)) {
  62             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  63             dbf.setNamespaceAware(true);
  64             DOMSource domSource = new DOMSource(dbf.newDocumentBuilder().
  65                     parse(new File(xsltFile)));
  66 
  67             Transformer transformer = TransformerFactory.newInstance().
  68                     newTransformer(domSource);
  69             StreamSource streamSource = new StreamSource(fis);
  70             StreamResult streamResult = new StreamResult(fos);
  71 
  72             transformer.setOutputProperty("indent", "no");
  73             transformer.transform(streamSource, streamResult);
  74         }
  75         assertTrue(compareWithGold(goldFile, outputFile));
  76     }
  77 }