1 /*
   2  * Copyright (c) 2020, 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 
  24 package transform;
  25 
  26 import java.io.ByteArrayOutputStream;
  27 import java.io.OutputStream;
  28 import java.io.Reader;
  29 import java.io.StringReader;
  30 import javax.xml.parsers.SAXParser;
  31 import javax.xml.parsers.SAXParserFactory;
  32 import javax.xml.stream.XMLEventWriter;
  33 import javax.xml.stream.XMLOutputFactory;
  34 import javax.xml.stream.XMLStreamWriter;
  35 import javax.xml.transform.Transformer;
  36 import javax.xml.transform.TransformerFactory;
  37 import javax.xml.transform.stax.StAXResult;
  38 import javax.xml.transform.stream.StreamSource;
  39 import org.testng.annotations.BeforeClass;
  40 import org.testng.annotations.Test;
  41 import org.xml.sax.InputSource;
  42 import org.xml.sax.helpers.DefaultHandler;
  43 
  44 /*
  45  * @test
  46  * @bug 8238183
  47  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  48  * @run testng/othervm transform.ResultTest
  49  * @summary Verifies that the output of a transformation is well-formed when
  50  *          StAXResult is used.
  51  */
  52 public class ResultTest {
  53     // The XML contains a comment before the root element
  54     final static String XML =
  55             "<?xml version=\"1.0\" ?>\n"
  56             + "<!--comment-->\n"
  57             + "<root />\n";
  58     static Transformer T;
  59 
  60     @BeforeClass
  61     public void setup() {
  62         try {
  63             T = TransformerFactory.newInstance().newTransformer();
  64         } catch (Exception e) {
  65             throw new RuntimeException(e.getMessage());
  66         }
  67     }
  68 
  69     /**
  70      * Transforms the XML using a StAXResult with a StreamWriter.
  71      * @throws Exception if the process fails
  72      */
  73     @Test
  74     public void testStreamWriter() throws Exception {
  75         try (Reader reader = new StringReader(XML);
  76                 OutputStream out = new ByteArrayOutputStream()) {
  77             XMLOutputFactory factory = XMLOutputFactory.newFactory();
  78             XMLStreamWriter writer = factory.createXMLStreamWriter(out);
  79             StreamSource source = new StreamSource(reader);
  80             StAXResult result = new StAXResult(writer);
  81             T.transform(source, result);
  82             // verify the output is well-formed
  83             parseXML(out.toString());
  84         }
  85     }
  86 
  87     /**
  88      * Transforms the XML using a StAXResult with a EventWriter.
  89      * @throws Exception if the process fails
  90      */
  91     @Test
  92     public void testEventWriter() throws Exception {
  93         try (Reader reader = new StringReader(XML);
  94                 OutputStream out = new ByteArrayOutputStream()) {
  95             XMLOutputFactory ofactory = XMLOutputFactory.newInstance();
  96             XMLEventWriter writer = ofactory.createXMLEventWriter(out);
  97             StAXResult result = new StAXResult(writer);
  98             StreamSource source = new StreamSource(reader);
  99             T.transform(source, result);
 100             parseXML(out.toString());
 101         }
 102     }
 103 
 104     // parses the xml to verify that it's well-formed
 105     private void parseXML(String xml) throws Exception {
 106         SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
 107         parser.parse(new InputSource(new StringReader(xml)), new DefaultHandler());
 108     }
 109 }