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