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