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.XMLStreamWriterTest;
  25 
  26 import java.io.ByteArrayOutputStream;
  27 
  28 import javax.xml.stream.XMLOutputFactory;
  29 import javax.xml.stream.XMLStreamWriter;
  30 
  31 import org.testng.Assert;
  32 import org.testng.annotations.Listeners;
  33 import org.testng.annotations.Test;
  34 
  35 /*
  36  * @test
  37  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
  38  * @run testng/othervm -DrunSecMngr=true stream.XMLStreamWriterTest.EmptyElementTest
  39  * @run testng/othervm stream.XMLStreamWriterTest.EmptyElementTest
  40  * @summary Test XMLStreamWriter writes namespace and attribute after writeEmptyElement.
  41  */
  42 @Listeners({jaxp.library.BasePolicy.class})
  43 public class EmptyElementTest {
  44 
  45     // expected output
  46     private static final String EXPECTED_OUTPUT = "<?xml version=\"1.0\" ?>" + "<hello xmlns=\"http://hello\">"
  47             + "<world xmlns=\"http://world\" prefixes=\"foo bar\"/>" + "</hello>";
  48 
  49     XMLStreamWriter xmlStreamWriter;
  50     ByteArrayOutputStream byteArrayOutputStream;
  51     XMLOutputFactory xmlOutputFactory;
  52 
  53     @Test
  54     public void testWriterOnLinux() throws Exception {
  55 
  56         // setup XMLStreamWriter
  57         try {
  58             byteArrayOutputStream = new ByteArrayOutputStream();
  59             xmlOutputFactory = XMLOutputFactory.newInstance();
  60             xmlOutputFactory.setProperty(xmlOutputFactory.IS_REPAIRING_NAMESPACES, new Boolean(true));
  61             xmlStreamWriter = xmlOutputFactory.createXMLStreamWriter(byteArrayOutputStream);
  62         } catch (Exception e) {
  63             System.err.println("Unexpected Exception: " + e.toString());
  64             e.printStackTrace();
  65             Assert.fail(e.toString());
  66         }
  67 
  68         // create & write a document
  69         try {
  70             xmlStreamWriter.writeStartDocument();
  71             xmlStreamWriter.writeStartElement("hello");
  72             xmlStreamWriter.writeDefaultNamespace("http://hello");
  73             xmlStreamWriter.writeEmptyElement("world");
  74             xmlStreamWriter.writeDefaultNamespace("http://world");
  75             xmlStreamWriter.writeAttribute("prefixes", "foo bar");
  76             xmlStreamWriter.writeEndElement();
  77             xmlStreamWriter.writeEndDocument();
  78             xmlStreamWriter.flush();
  79             String actualOutput = byteArrayOutputStream.toString();
  80             Assert.assertEquals(EXPECTED_OUTPUT, actualOutput);
  81         } catch (Exception e) {
  82             System.err.println("Unexpected Exception: " + e.toString());
  83             e.printStackTrace();
  84             Assert.fail(e.toString());
  85         }
  86     }
  87 }