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 javax.xml.stream.XMLOutputFactory;
  27 import javax.xml.stream.XMLStreamException;
  28 import javax.xml.stream.XMLStreamWriter;
  29 
  30 import org.testng.Assert;
  31 import org.testng.annotations.Test;
  32 
  33 /*
  34  * @bug 6394074
  35  * @summary Test XMLStreamWriter namespace prefix with writeDefaultNamespace.
  36  */
  37 public class UnprefixedNameTest {
  38 
  39     @Test
  40     public void testUnboundPrefix() throws Exception {
  41 
  42         try {
  43             XMLOutputFactory xof = XMLOutputFactory.newInstance();
  44             XMLStreamWriter w = xof.createXMLStreamWriter(System.out);
  45             // here I'm trying to write
  46             // <bar xmlns="foo" />
  47             w.writeStartDocument();
  48             w.writeStartElement("foo", "bar");
  49             w.writeDefaultNamespace("foo");
  50             w.writeCharacters("---");
  51             w.writeEndElement();
  52             w.writeEndDocument();
  53             w.close();
  54 
  55             // Unexpected success
  56             String FAIL_MSG = "Unexpected success.  Expected: " + "XMLStreamException - " + "if the namespace URI has not been bound to a prefix "
  57                     + "and javax.xml.stream.isPrefixDefaulting has not been " + "set to true";
  58             System.err.println(FAIL_MSG);
  59             Assert.fail(FAIL_MSG);
  60         } catch (XMLStreamException xmlStreamException) {
  61             // Expected Exception
  62             System.out.println("Expected XMLStreamException: " + xmlStreamException.toString());
  63         }
  64     }
  65 
  66     @Test
  67     public void testBoundPrefix() throws Exception {
  68 
  69         try {
  70             XMLOutputFactory xof = XMLOutputFactory.newInstance();
  71             XMLStreamWriter w = xof.createXMLStreamWriter(System.out);
  72             // here I'm trying to write
  73             // <bar xmlns="foo" />
  74             w.writeStartDocument();
  75             w.writeStartElement("foo", "bar", "http://namespace");
  76             w.writeCharacters("---");
  77             w.writeEndElement();
  78             w.writeEndDocument();
  79             w.close();
  80 
  81             // Expected success
  82             System.out.println("Expected success.");
  83         } catch (Exception exception) {
  84             // Unexpected Exception
  85             String FAIL_MSG = "Unexpected Exception: " + exception.toString();
  86             System.err.println(FAIL_MSG);
  87             Assert.fail(FAIL_MSG);
  88         }
  89     }
  90 
  91     @Test
  92     public void testRepairingPrefix() throws Exception {
  93 
  94         try {
  95 
  96             // repair namespaces
  97             // use new XMLOutputFactory as changing its property settings
  98             XMLOutputFactory xof = XMLOutputFactory.newInstance();
  99             xof.setProperty(xof.IS_REPAIRING_NAMESPACES, new Boolean(true));
 100             XMLStreamWriter w = xof.createXMLStreamWriter(System.out);
 101 
 102             // here I'm trying to write
 103             // <bar xmlns="foo" />
 104             w.writeStartDocument();
 105             w.writeStartElement("foo", "bar");
 106             w.writeDefaultNamespace("foo");
 107             w.writeCharacters("---");
 108             w.writeEndElement();
 109             w.writeEndDocument();
 110             w.close();
 111 
 112             // Expected success
 113             System.out.println("Expected success.");
 114         } catch (Exception exception) {
 115             // Unexpected Exception
 116             String FAIL_MSG = "Unexpected Exception: " + exception.toString();
 117             System.err.println(FAIL_MSG);
 118             Assert.fail(FAIL_MSG);
 119         }
 120     }
 121 }