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