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