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