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