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