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 
  24 package stream.XMLOutputFactoryTest;
  25 
  26 import static jaxp.library.JAXPTestUtilities.getSystemProperty;
  27 
  28 import java.io.ByteArrayOutputStream;
  29 
  30 import javax.xml.stream.XMLEventFactory;
  31 import javax.xml.stream.XMLEventWriter;
  32 import javax.xml.stream.XMLOutputFactory;
  33 import javax.xml.stream.XMLStreamWriter;
  34 import javax.xml.transform.stax.StAXResult;
  35 import javax.xml.transform.stream.StreamResult;
  36 
  37 import org.testng.Assert;
  38 import org.testng.annotations.Listeners;
  39 import org.testng.annotations.Test;
  40 
  41 /*
  42  * @test
  43  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  44  * @run testng/othervm -DrunSecMngr=true stream.XMLOutputFactoryTest.StreamResultTest
  45  * @run testng/othervm stream.XMLOutputFactoryTest.StreamResultTest
  46  * @summary Test create XMLWriter with variant Result.
  47  */
  48 @Listeners({jaxp.library.BasePolicy.class})
  49 public class StreamResultTest {
  50 
  51     @Test
  52     public void testStreamResult() {
  53         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>";
  54         try {
  55             XMLOutputFactory ofac = XMLOutputFactory.newInstance();
  56             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  57             StreamResult sr = new StreamResult(buffer);
  58             XMLStreamWriter writer = ofac.createXMLStreamWriter(sr);
  59             writer.writeStartDocument("1.0");
  60             writer.writeStartElement("root");
  61             writer.writeEndElement();
  62             writer.writeEndDocument();
  63             writer.close();
  64             Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
  65         } catch (Exception e) {
  66             e.printStackTrace();
  67             Assert.fail(e.toString());
  68         }
  69     }
  70 
  71     @Test
  72     public void testStreamWriterWithStAXResultNStreamWriter() {
  73         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"?><root></root>";
  74 
  75         try {
  76             XMLOutputFactory ofac = XMLOutputFactory.newInstance();
  77             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
  78             XMLStreamWriter writer = ofac.createXMLStreamWriter(buffer);
  79             StAXResult res = new StAXResult(writer);
  80             writer = ofac.createXMLStreamWriter(res);
  81             writer.writeStartDocument("1.0");
  82             writer.writeStartElement("root");
  83             writer.writeEndElement();
  84             writer.writeEndDocument();
  85             writer.close();
  86             Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
  87         } catch (Exception e) {
  88             e.printStackTrace();
  89             Assert.fail(e.toString());
  90         }
  91     }
  92 
  93     @Test
  94     public void testEventWriterWithStAXResultNStreamWriter() {
  95         String encoding = "";
  96         if (getSystemProperty("file.encoding").equals("UTF-8")) {
  97             encoding = " encoding=\"UTF-8\"";
  98         }
  99         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";
 100 
 101         try {
 102             XMLOutputFactory ofac = XMLOutputFactory.newInstance();
 103             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 104             XMLStreamWriter swriter = ofac.createXMLStreamWriter(buffer);
 105             StAXResult res = new StAXResult(swriter);
 106             XMLEventWriter writer = ofac.createXMLEventWriter(res);
 107 
 108             XMLEventFactory efac = XMLEventFactory.newInstance();
 109             writer.add(efac.createStartDocument(null, "1.0"));
 110             writer.add(efac.createStartElement("", "", "root"));
 111             writer.add(efac.createEndElement("", "", "root"));
 112             writer.add(efac.createEndDocument());
 113             writer.close();
 114 
 115             Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
 116         } catch (Exception e) {
 117             e.printStackTrace();
 118             Assert.fail(e.toString());
 119         }
 120     }
 121 
 122     @Test
 123     public void testEventWriterWithStAXResultNEventWriter() {
 124         String encoding = "";
 125         if (getSystemProperty("file.encoding").equals("UTF-8")) {
 126             encoding = " encoding=\"UTF-8\"";
 127         }
 128         final String EXPECTED_OUTPUT = "<?xml version=\"1.0\"" + encoding + "?><root></root>";
 129 
 130         try {
 131             XMLOutputFactory ofac = XMLOutputFactory.newInstance();
 132             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 133             XMLEventWriter writer = ofac.createXMLEventWriter(buffer);
 134             StAXResult res = new StAXResult(writer);
 135             writer = ofac.createXMLEventWriter(res);
 136 
 137             XMLEventFactory efac = XMLEventFactory.newInstance();
 138             writer.add(efac.createStartDocument(null, "1.0"));
 139             writer.add(efac.createStartElement("", "", "root"));
 140             writer.add(efac.createEndElement("", "", "root"));
 141             writer.add(efac.createEndDocument());
 142             writer.close();
 143 
 144             Assert.assertEquals(buffer.toString(), EXPECTED_OUTPUT);
 145         } catch (Exception e) {
 146             e.printStackTrace();
 147             Assert.fail(e.toString());
 148         }
 149     }
 150 
 151     @Test
 152     public void testStreamWriterWithStAXResultNEventWriter() throws Exception {
 153         try {
 154             XMLOutputFactory ofac = XMLOutputFactory.newInstance();
 155             ByteArrayOutputStream buffer = new ByteArrayOutputStream();
 156             XMLEventWriter writer = ofac.createXMLEventWriter(buffer);
 157             StAXResult res = new StAXResult(writer);
 158             XMLStreamWriter swriter = ofac.createXMLStreamWriter(res);
 159             Assert.fail("Expected an Exception as XMLStreamWriter can't be created " + "with a StAXResult which has EventWriter.");
 160         } catch (Exception e) {
 161             System.out.println(e.toString());
 162         }
 163     }
 164 }
 165