ReaderToWriterTest.java revision 968:874082a9b565
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
24package stream.XMLEventWriterTest;
25
26import java.io.ByteArrayOutputStream;
27import java.io.File;
28import java.io.FileInputStream;
29import java.io.FileNotFoundException;
30import java.io.FileOutputStream;
31import java.io.InputStream;
32import java.io.OutputStream;
33
34import javax.xml.stream.XMLEventFactory;
35import javax.xml.stream.XMLEventReader;
36import javax.xml.stream.XMLEventWriter;
37import javax.xml.stream.XMLInputFactory;
38import javax.xml.stream.XMLOutputFactory;
39import javax.xml.stream.XMLStreamException;
40import javax.xml.stream.events.XMLEvent;
41
42import org.testng.Assert;
43import org.testng.annotations.Listeners;
44import org.testng.annotations.Test;
45
46/*
47 * @test
48 * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
49 * @run testng/othervm -DrunSecMngr=true stream.XMLEventWriterTest.ReaderToWriterTest
50 * @run testng/othervm stream.XMLEventWriterTest.ReaderToWriterTest
51 * @summary Test XMLEventWriter.
52 */
53@Listeners({jaxp.library.FilePolicy.class})
54public class ReaderToWriterTest {
55
56    private static final XMLEventFactory XML_EVENT_FACTORY = XMLEventFactory.newInstance();
57    private static final XMLInputFactory XML_INPUT_FACTORY = XMLInputFactory.newInstance();
58    private static final XMLOutputFactory XML_OUTPUT_FACTORY = XMLOutputFactory.newInstance();
59
60    private static final String INPUT_FILE = "W2JDLR4002TestService.wsdl.data";
61    private static final String OUTPUT_FILE = "Encoded.wsdl";
62
63    /**
64     * Unit test for writing namespaces when namespaceURI == null.
65     */
66    @Test
67    public void testWriteNamespace() {
68
69        /** Platform default encoding. */
70        final String DEFAULT_CHARSET = java.nio.charset.Charset.defaultCharset().name();
71        System.out.println("DEFAULT_CHARSET = " + DEFAULT_CHARSET);
72
73        final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" encoding=\"" + DEFAULT_CHARSET + "\"?><prefix:root xmlns=\"\" xmlns:null=\"\"></prefix:root>";
74        final String EXPECTED_OUTPUT_NO_ENCODING = "<?xml version=\"1.0\"?><prefix:root xmlns=\"\" xmlns:null=\"\"></prefix:root>";
75
76        // new Writer
77        XMLEventWriter xmlEventWriter = null;
78        ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
79        try {
80            xmlEventWriter = XML_OUTPUT_FACTORY.createXMLEventWriter(byteArrayOutputStream);
81        } catch (XMLStreamException xmlStreamException) {
82            xmlStreamException.printStackTrace();
83            Assert.fail(xmlStreamException.toString());
84        }
85
86        try {
87            // start a valid event stream
88            XMLEvent startDocumentEvent = XML_EVENT_FACTORY.createStartDocument(DEFAULT_CHARSET);
89            XMLEvent startElementEvent = XML_EVENT_FACTORY.createStartElement("prefix", "http://example.com", "root");
90            xmlEventWriter.add(startDocumentEvent);
91            xmlEventWriter.add(startElementEvent);
92
93            // try using a null default namespaceURI
94            XMLEvent namespaceEvent = XML_EVENT_FACTORY.createNamespace(null);
95            xmlEventWriter.add(namespaceEvent);
96
97            // try using a null prefix'd namespaceURI
98            XMLEvent namespacePrefixEvent = XML_EVENT_FACTORY.createNamespace("null", null);
99            xmlEventWriter.add(namespacePrefixEvent);
100
101            // close event stream
102            XMLEvent endElementEvent = XML_EVENT_FACTORY.createEndElement("prefix", "http://example.com", "root");
103            XMLEvent endDocumentEvent = XML_EVENT_FACTORY.createEndDocument();
104            xmlEventWriter.add(endElementEvent);
105            xmlEventWriter.add(endDocumentEvent);
106            xmlEventWriter.flush();
107        } catch (XMLStreamException xmlStreamException) {
108            xmlStreamException.printStackTrace();
109            Assert.fail(xmlStreamException.toString());
110        }
111
112        // get XML document as String
113        String actualOutput = byteArrayOutputStream.toString();
114
115        // is output as expected?
116        if (!actualOutput.equals(EXPECTED_OUTPUT) && !actualOutput.equals(EXPECTED_OUTPUT_NO_ENCODING)) {
117            Assert.fail("Expected: " + EXPECTED_OUTPUT + ", actual: " + actualOutput);
118        }
119    }
120
121    /**
122     * Test: 6419687 NPE in XMLEventWriterImpl.
123     */
124    @Test
125    public void testCR6419687() {
126
127        try {
128            InputStream in = getClass().getResourceAsStream("ReaderToWriterTest.wsdl");
129            OutputStream out = new FileOutputStream("ReaderToWriterTest-out.xml");
130
131            XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
132            XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(out, "UTF-8");
133            while (reader.hasNext()) {
134                XMLEvent event = reader.nextEvent();
135                writer.add(event);
136            }
137            reader.close();
138            writer.close();
139        } catch (XMLStreamException xmlStreamException) {
140            xmlStreamException.printStackTrace();
141            Assert.fail(xmlStreamException.toString());
142        } catch (FileNotFoundException fileNotFoundException) {
143            fileNotFoundException.printStackTrace();
144            Assert.fail(fileNotFoundException.toString());
145        }
146    }
147
148    /*
149     * Reads UTF-16 encoding file and writes it to UTF-8 encoded format.
150     */
151    @Test
152    public void testUTF8Encoding() {
153        try {
154            InputStream in = util.BOMInputStream.createStream("UTF-16BE", this.getClass().getResourceAsStream(INPUT_FILE));
155            OutputStream out = new FileOutputStream(OUTPUT_FILE);
156
157            XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
158            XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(out, "UTF-8");
159
160            writeEvents(reader, writer);
161            checkOutput(OUTPUT_FILE);
162
163        } catch (Exception e) {
164            e.printStackTrace();
165            Assert.fail("Exception occured: " + e.getMessage());
166        } finally {
167            File file = new File(OUTPUT_FILE);
168            if (file.exists())
169                file.delete();
170        }
171    }
172
173    private void writeEvents(XMLEventReader reader, XMLEventWriter writer) throws XMLStreamException {
174        while (reader.hasNext()) {
175            XMLEvent event = reader.nextEvent();
176            writer.add(event);
177        }
178        reader.close();
179        writer.close();
180    }
181
182    private void checkOutput(String output) throws Exception {
183        InputStream in = new FileInputStream(output);
184        XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
185        while (reader.hasNext()) {
186            reader.next();
187        }
188        reader.close();
189    }
190
191    /*
192     * Reads UTF-16 encoding file and writes it with default encoding.
193     */
194    @Test
195    public void testNoEncoding() {
196        try {
197            InputStream in = util.BOMInputStream.createStream("UTF-16BE", this.getClass().getResourceAsStream(INPUT_FILE));
198            OutputStream out = new FileOutputStream(OUTPUT_FILE);
199
200            XMLEventReader reader = XML_INPUT_FACTORY.createXMLEventReader(in);
201            XMLEventWriter writer = XML_OUTPUT_FACTORY.createXMLEventWriter(out);
202
203            writeEvents(reader, writer);
204            checkOutput(OUTPUT_FILE);
205
206        } catch (Exception e) {
207            e.printStackTrace();
208            Assert.fail("Exception occured: " + e.getMessage());
209        } finally {
210            File file = new File(OUTPUT_FILE);
211            if (file.exists())
212                file.delete();
213        }
214    }
215
216}
217
218