1 /*
   2  * Copyright (c) 2003, 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 test.gaptest;
  25 
  26 import static jaxp.library.JAXPTestUtilities.setSystemProperty;
  27 import static jaxp.library.JAXPTestUtilities.clearSystemProperty;
  28 
  29 import static org.testng.Assert.assertTrue;
  30 
  31 import java.io.IOException;
  32 import java.io.StringReader;
  33 import java.io.StringWriter;
  34 
  35 import javax.xml.parsers.ParserConfigurationException;
  36 import javax.xml.parsers.SAXParserFactory;
  37 import javax.xml.transform.Transformer;
  38 import javax.xml.transform.TransformerConfigurationException;
  39 import javax.xml.transform.TransformerException;
  40 import javax.xml.transform.TransformerFactory;
  41 import javax.xml.transform.sax.SAXSource;
  42 import javax.xml.transform.sax.SAXTransformerFactory;
  43 import javax.xml.transform.stream.StreamResult;
  44 
  45 import org.testng.annotations.AfterClass;
  46 import org.testng.annotations.BeforeClass;
  47 import org.testng.annotations.Listeners;
  48 import org.testng.annotations.Test;
  49 import org.xml.sax.InputSource;
  50 import org.xml.sax.SAXException;
  51 import org.xml.sax.helpers.XMLFilterImpl;
  52 
  53 /*
  54  * @test
  55  * @bug 4515660
  56  * @library /javax/xml/jaxp/libs
  57  * @run testng/othervm -DrunSecMngr=true test.gaptest.Bug4515660
  58  * @run testng/othervm test.gaptest.Bug4515660
  59  * @summary verify property org.xml.sax.driver is used by SAXTransformerFactory
  60  */
  61 @Test(singleThreaded = true)
  62 @Listeners({jaxp.library.BasePolicy.class})
  63 public class Bug4515660 {
  64 
  65     @BeforeClass
  66     public void setSaxDrier() {
  67         setSystemProperty("org.xml.sax.driver", ReaderStub.class.getName());
  68     }
  69 
  70     @AfterClass
  71     public void clearSaxDrier() {
  72         clearSystemProperty("org.xml.sax.driver");
  73     }
  74 
  75     @Test
  76     public void testTransformer() throws TransformerException {
  77         String xml = "<?xml version='1.0'?><root/>";
  78         ReaderStub.used = false;
  79 
  80         TransformerFactory transFactory = TransformerFactory.newInstance();
  81         Transformer transformer = transFactory.newTransformer();
  82         InputSource in = new InputSource(new StringReader(xml));
  83         SAXSource source = new SAXSource(in);
  84         StreamResult result = new StreamResult(new StringWriter());
  85 
  86         transformer.transform(source, result);
  87 
  88         assertTrue(ReaderStub.used);
  89 
  90     }
  91 
  92     @Test
  93     public void testSAXTransformerFactory() throws TransformerConfigurationException {
  94         final String xsl = "<?xml version='1.0'?>\n" + "<xsl:stylesheet" + " xmlns:xsl='http://www.w3.org/1999/XSL/Transform'" + " version='1.0'>\n"
  95                 + "   <xsl:template match='/'>Hello World!</xsl:template>\n" + "</xsl:stylesheet>\n";
  96 
  97         ReaderStub.used = false;
  98 
  99         TransformerFactory transFactory = TransformerFactory.newInstance();
 100         assertTrue(transFactory.getFeature(SAXTransformerFactory.FEATURE));
 101 
 102         InputSource in = new InputSource(new StringReader(xsl));
 103         SAXSource source = new SAXSource(in);
 104 
 105         transFactory.newTransformer(source);
 106         assertTrue(ReaderStub.used);
 107 
 108     }
 109 
 110     public static class ReaderStub extends XMLFilterImpl {
 111         static boolean used = false;
 112 
 113         public ReaderStub() throws ParserConfigurationException, SAXException {
 114             super();
 115             super.setParent(SAXParserFactory.newInstance().newSAXParser().getXMLReader());
 116             used = true;
 117         }
 118 
 119         public void parse(InputSource input) throws SAXException, IOException {
 120             used = true;
 121             super.parse(input);
 122         }
 123 
 124         public void parse(String systemId) throws SAXException, IOException {
 125             used = true;
 126             super.parse(systemId);
 127         }
 128     }
 129 
 130 }
 131