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