1 /* 2 * Copyright (c) 2014, 2015, 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 parsers; 25 26 import java.io.StringReader; 27 import java.io.StringWriter; 28 29 import javax.xml.parsers.SAXParser; 30 import javax.xml.parsers.SAXParserFactory; 31 import javax.xml.transform.Transformer; 32 import javax.xml.transform.TransformerFactory; 33 import javax.xml.transform.sax.SAXSource; 34 import javax.xml.transform.stream.StreamResult; 35 36 import org.testng.Assert; 37 import org.testng.annotations.Test; 38 import org.xml.sax.InputSource; 39 import org.xml.sax.helpers.DefaultHandler; 40 41 /* 42 * @bug 6594813 43 * @summary Test SAXParser output is wellformed with name space. 44 */ 45 public class Bug6594813 { 46 47 public Bug6594813(String name) { 48 } 49 50 private static final String TESTXML = "<?xml version='1.0' ?>\n" 51 + "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ns1='http://faulttestservice.org/wsdl'>\n" 52 + "<soapenv:Body>\n" + "<soapenv:Fault xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/'>\n" + "<faultcode>\n" 53 + "soapenv:Server</faultcode>\n" + "<faultstring>\n" + "com.sun.ts.tests.jaxws.sharedwebservices.faultservice.DummyException</faultstring>\n" 54 + "<detail>\n" + "<ns1:DummyException>\n" + "<dummyField1>\n" + "dummyString1</dummyField1>\n" + "<dummyField2>\n" + "dummyString2</dummyField2>\n" 55 + "</ns1:DummyException>\n" + "</detail>\n" + "</soapenv:Fault>\n" + "</soapenv:Body>\n" + "</soapenv:Envelope>\n"; 56 57 // simplest XML to re-declare same prefix/namespace mappings 58 private static final String SIMPLE_TESTXML = "<?xml version='1.0' ?>\n" + "<prefix:ElementName xmlns:prefix='URI'>\n" 59 + "<prefix:ElementName xmlns:prefix='URI'>\n" + "</prefix:ElementName>\n" + "</prefix:ElementName>\n"; 60 61 private String runTransform(SAXParser sp) throws Exception { 62 // Run identity transform using SAX parser 63 SAXSource src = new SAXSource(sp.getXMLReader(), new InputSource(new StringReader(TESTXML))); 64 Transformer transformer = TransformerFactory.newInstance().newTransformer(); 65 StringWriter sw = new StringWriter(); 66 transformer.transform(src, new StreamResult(sw)); 67 68 String result = sw.getBuffer().toString(); 69 // System.out.println(result); 70 return result; 71 } 72 73 private void checkWellFormedness(String xml) throws Exception { 74 SAXParserFactory spf = SAXParserFactory.newInstance(); 75 spf.setNamespaceAware(true); // Same as default 76 spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true); 77 SAXParser sp = spf.newSAXParser(); 78 79 // Re-parse output to make sure that it is well formed 80 sp.parse(new InputSource(new StringReader(xml)), new DefaultHandler()); 81 } 82 83 /** 84 * Test an identity transform of an XML document with NS decls using a 85 * non-ns-aware parser. Output result to a StreamSource. Set ns-awareness to 86 * FALSE and prefixes to FALSE. 87 */ 88 @Test 89 public void testXMLNoNsAwareStreamResult1() { 90 try { 91 // Create SAX parser *without* enabling ns 92 SAXParserFactory spf = SAXParserFactory.newInstance(); 93 spf.setNamespaceAware(false); // Same as default 94 spf.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 95 SAXParser sp = spf.newSAXParser(); 96 97 // Make sure that the output is well formed 98 String xml = runTransform(sp); 99 checkWellFormedness(xml); 100 } catch (Throwable ex) { 101 Assert.fail(ex.toString()); 102 } 103 } 104 105 /** 106 * Test an identity transform of an XML document with NS decls using a 107 * non-ns-aware parser. Output result to a StreamSource. Set ns-awareness to 108 * FALSE and prefixes to TRUE. 109 */ 110 @Test 111 public void testXMLNoNsAwareStreamResult2() { 112 try { 113 // Create SAX parser *without* enabling ns 114 SAXParserFactory spf = SAXParserFactory.newInstance(); 115 spf.setNamespaceAware(false); // Same as default 116 spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true); 117 SAXParser sp = spf.newSAXParser(); 118 119 // Make sure that the output is well formed 120 String xml = runTransform(sp); 121 checkWellFormedness(xml); 122 } catch (Throwable ex) { 123 Assert.fail(ex.toString()); 124 } 125 } 126 127 /** 128 * Test an identity transform of an XML document with NS decls using a 129 * non-ns-aware parser. Output result to a StreamSource. Set ns-awareness to 130 * TRUE and prefixes to FALSE. 131 */ 132 @Test 133 public void testXMLNoNsAwareStreamResult3() { 134 try { 135 // Create SAX parser *without* enabling ns 136 SAXParserFactory spf = SAXParserFactory.newInstance(); 137 spf.setNamespaceAware(true); // Same as default 138 spf.setFeature("http://xml.org/sax/features/namespace-prefixes", false); 139 SAXParser sp = spf.newSAXParser(); 140 141 // Make sure that the output is well formed 142 String xml = runTransform(sp); 143 checkWellFormedness(xml); 144 } catch (Throwable ex) { 145 Assert.fail(ex.toString()); 146 } 147 } 148 149 /** 150 * Test an identity transform of an XML document with NS decls using a 151 * non-ns-aware parser. Output result to a StreamSource. Set ns-awareness to 152 * TRUE and prefixes to TRUE. 153 */ 154 @Test 155 public void testXMLNoNsAwareStreamResult4() { 156 try { 157 // Create SAX parser *without* enabling ns 158 SAXParserFactory spf = SAXParserFactory.newInstance(); 159 spf.setNamespaceAware(true); // Same as default 160 spf.setFeature("http://xml.org/sax/features/namespace-prefixes", true); 161 SAXParser sp = spf.newSAXParser(); 162 163 // Make sure that the output is well formed 164 String xml = runTransform(sp); 165 checkWellFormedness(xml); 166 } catch (Throwable ex) { 167 Assert.fail(ex.toString()); 168 } 169 } 170 171 }