xref: /aosp_15_r20/external/apache-xml/test/tests/bugzilla/Bugzilla1251.java (revision 1212f9a0ffdc28482b8821715d2222bf16dc14e2)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the  "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 // Common Qetest / Xalan testing imports
19 import org.apache.qetest.Datalet;
20 import org.apache.qetest.Logger;
21 import org.apache.qetest.TestletImpl;
22 
23 // imports needed for reproducing the bug
24 import javax.xml.transform.ErrorListener;
25 import javax.xml.transform.Transformer;
26 import javax.xml.transform.TransformerException;
27 import javax.xml.transform.sax.SAXTransformerFactory;
28 import javax.xml.transform.sax.TransformerHandler;
29 import javax.xml.transform.sax.SAXResult;
30 import javax.xml.transform.stream.StreamSource;
31 import org.xml.sax.Attributes;
32 import org.xml.sax.SAXException;
33 import org.xml.sax.helpers.AttributesImpl;
34 import org.xml.sax.helpers.DefaultHandler;
35 
36 /**
37  * Testlet for reproducing Bugzilla reported bugs.
38  *
39  * Reported-by: [email protected]
40  */
41 public class Bugzilla1251 extends TestletImpl
42 {
43     // Initialize our classname for TestletImpl's main() method - must be updated!
44     static { thisClassName = "Bugzilla1251"; }
45 
46     /**
47      * Reproduce a Bugzilla bug report.
48      * @param d (optional) Datalet to use as data point for the test.
49      */
execute(Datalet d)50     public void execute(Datalet d)
51 	{
52         // Use logger.logMsg(...) instead of System.out.println(...)
53         logger.logMsg(Logger.STATUSMSG, "Reproducing Bugzilla#1251: TransformerHandler with SAXResult mishandles exceptions");
54 
55         try {
56             logger.logMsg(Logger.STATUSMSG, "Using an identity transformer should work...");
57             SAXTransformerFactory f = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
58             TransformerHandler th = f.newTransformerHandler();
59             th.setResult(new SAXResult(new Bugzilla1251Handler(logger, "CH1")));
60             Transformer t = th.getTransformer();
61             t.setErrorListener(new Bugzilla1251Handler(logger, "EL1"));
62             th.startDocument();
63             th.startElement("","foo","foo",new AttributesImpl());
64             th.endElement("","foo","foo");
65             th.endDocument();
66         } catch(Throwable t) {
67             logger.checkFail("Should not have thrown exception!");
68             logger.logThrowable(Logger.ERRORMSG, t, "Should not have thrown exception!");
69         }
70 
71         try {
72             logger.logMsg(Logger.STATUSMSG, "But using a real transformer does not...");
73             SAXTransformerFactory f = (SAXTransformerFactory)SAXTransformerFactory.newInstance();
74             TransformerHandler th = f.newTransformerHandler(new StreamSource("identity.xsl"));
75             th.setResult(new SAXResult(new Bugzilla1251Handler(logger, "CH2")));
76             Transformer t = th.getTransformer();
77             t.setErrorListener(new Bugzilla1251Handler(logger, "EL2"));
78             th.startDocument();
79             th.startElement("","foo","foo",new AttributesImpl());
80             th.endElement("","foo","foo");
81             th.endDocument();
82         } catch(Throwable t) {
83             logger.checkFail("Should not have thrown exception!");
84             logger.logThrowable(Logger.ERRORMSG, t, "Should not have thrown exception!");
85         }
86 	}
87 
88     /**
89      * <a href="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=1251">
90      * Link to Bugzilla</a>
91      * @return TransformerHandler with SAXResult mishandles exceptions.
92      */
getDescription()93     public String getDescription()
94     {
95         return "TransformerHandler with SAXResult mishandles exceptions";
96     }
97 
98     class Bugzilla1251Handler extends DefaultHandler implements ErrorListener {
99 
100         private Logger m_logger = null;
101         private String m_id = null; // Not strictly needed, I've over-instrumented this a bit -sc
102         private int m_ctr = 0;
Bugzilla1251Handler(Logger l, String id)103         public Bugzilla1251Handler(Logger l, String id) {
104             m_logger = l;
105             m_id = id;
106         }
107 
108         // Moved to separate class: for main() method, see Bugzilla1251.execute()
startElement(String namespaceURI,String localName,String qName,Attributes atts)109             public void startElement(String namespaceURI,String localName,String qName,Attributes atts) throws SAXException {
110                 m_logger.logMsg(Logger.STATUSMSG, "Entering(" + m_id + ") startElement("
111                                 + namespaceURI + ", "
112                                 + localName + ", "
113                                 + qName + ", "
114                                 + ") and throwing an Exception..");
115                 m_ctr++;
116                 if (m_ctr > 1)
117                     m_logger.checkFail("Entered(" + m_id + ") startElement more than once! " + m_ctr);
118 
119                 // This is really what's being tested: if a Handler
120                 //  throws an exception, does Xalan propagate it
121                 //  to the correct places?
122                 throw new SAXException("Should stop processing");
123             }
124 
warning(TransformerException e)125             public void warning(TransformerException e) throws TransformerException
126             {
127                 m_logger.checkPass("Entering(" + m_id + ") warning()");
128                 m_logger.logThrowable(Logger.WARNINGMSG, e, "Entering(" + m_id + ") warning()");
129                 //throw e;
130             }
131 
error(TransformerException e)132             public void error(TransformerException e) throws TransformerException {
133                 m_logger.checkPass("Entering(" + m_id + ") error()");
134                 m_logger.logThrowable(Logger.WARNINGMSG, e, "Entering(" + m_id + ") error()");
135                 //throw e;
136             }
137 
fatalError(TransformerException e)138             public void fatalError(TransformerException e) throws TransformerException {
139                 m_logger.checkPass("Entering(" + m_id + ") fatalError()");
140                 m_logger.logThrowable(Logger.WARNINGMSG, e, "Entering(" + m_id + ") fatalError()");
141                 //throw e;
142             }
143     }
144 
145 }  // end of class BugzillaTestlet
146 
147