1 /*
   2  * Copyright (c) 2013, 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 /**
  25  * @test 
  26  * @bug 8021148
  27  * @summary test that JAXPSAXParser works even if referenced directly 
  28  * @run main/othervm JAXPSAXParserTest
  29  */
  30 import java.io.StringReader;
  31 import java.io.StringWriter;
  32 import javax.xml.transform.Result;
  33 import javax.xml.transform.Transformer;
  34 import javax.xml.transform.TransformerFactory;
  35 import javax.xml.transform.stream.StreamResult;
  36 import javax.xml.transform.stream.StreamSource;
  37 
  38 /**
  39  * test that JAXPSAXParser works even if referenced directly as
  40  * NetBeans did. **Note that JAXPSAXParser is an internal implementation, this
  41  * may therefore change.
  42  * 
  43  * @author huizhe.wang@oracle.com
  44  */
  45 public class JAXPSAXParserTest extends TestBase {
  46 
  47     public JAXPSAXParserTest(String name) {
  48         super(name);
  49     }
  50 
  51     /**
  52      * @param args the command line arguments
  53      */
  54     public static void main(String[] args) {
  55         JAXPSAXParserTest test = new JAXPSAXParserTest("JAXP 1.5 regression");
  56         test.setUp();
  57         test.testTransform();
  58         test.tearDown();
  59     }
  60 
  61     public final void testTransform() {
  62         String data =
  63                 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
  64                 + "<r>\n"
  65                 + "    <e/>\n"
  66                 + "</r>\n";
  67         String IDENTITY_XSLT_WITH_INDENT = // #5064280 workaround
  68                 "<xsl:stylesheet version='1.0' "
  69                 + "xmlns:xsl='http://www.w3.org/1999/XSL/Transform' "
  70                 + "xmlns:xalan='http://xml.apache.org/xslt' "
  71                 + "exclude-result-prefixes='xalan'>"
  72                 + "<xsl:output method='xml' indent='yes' xalan:indent-amount='4'/>"
  73                 + "<xsl:template match='@*|node()'>"
  74                 + "<xsl:copy>"
  75                 + "<xsl:apply-templates select='@*|node()'/>"
  76                 + "</xsl:copy>"
  77                 + "</xsl:template>"
  78                 + "</xsl:stylesheet>";
  79         try {
  80             //Skip the default XMLReader
  81             System.setProperty("org.xml.sax.driver", "com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser");
  82 
  83             StringWriter sw = new StringWriter();
  84             TransformerFactory tf = TransformerFactory.newInstance();
  85             Transformer t = tf.newTransformer(new StreamSource(new StringReader(IDENTITY_XSLT_WITH_INDENT)));
  86             Result result = new StreamResult(sw);
  87             t.transform(new StreamSource(new StringReader(data)), result);
  88             success("JAXPSAXParserTest passed");
  89         } catch (Exception e) {
  90             /**
  91              * JAXPSAXParser throws NullPointerException since the jaxp 1.5 security 
  92              * manager is not initialized when JAXPSAXParser is instantiated using
  93              * the default constructor.
  94             */
  95             fail(e.toString());
  96         } finally {
  97             System.clearProperty("org.xml.sax.driver");
  98         }
  99     }
 100 }