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