xref: /aosp_15_r20/external/apache-xml/test/tests/bugzilla/Bugzilla4336.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 // REPLACE_imports needed for reproducing the bug
24 import java.io.*;
25 import org.w3c.dom.*;
26 import javax.xml.parsers.*;
27 import javax.xml.parsers.ParserConfigurationException;
28 import javax.xml.transform.TransformerException;
29 import org.xml.sax.SAXException;
30 import org.apache.xpath.XPathAPI;
31 
32 /**
33  * Testlet for reproducing Bugzilla reported bugs.
34  *
35  * @author [email protected]
36  */
37 public class Bugzilla4336 extends TestletImpl
38 {
39     // Initialize our classname for TestletImpl's main() method - must be updated!
40     static { thisClassName = "Bugzilla4336"; }
41 
42     static final String _nodeSetInput1 = "<?xml version=\"1.0\"?>\n"
43                                         + "<!DOCTYPE doc [\n"
44                                         + "<!ELEMENT doc (n+)>\n"
45                                         + "<!ELEMENT n (#PCDATA)>\n" + "]>\n"
46                                         + "<!-- full document with decl -->"
47                                         + "<doc><n>1</n></doc>";
48 
49     static final String _xpath = "(.//. | .//@* | .//namespace::*)";
50     /**
51      * Write Minimal code to reproduce your Bugzilla bug report.
52      * @param d (optional) Datalet to use as data point for the test.
53      */
execute(Datalet d)54     public void execute(Datalet d)
55 	{
56         // Use logger.logMsg(...) instead of System.out.println(...)
57         logger.logMsg(Logger.STATUSMSG, "Reproducing Bugzilla#4336: Xalan 2.2.D11 adds a strange Attribute");
58 
59         // Comment out ref to Xerces 1.x class for upcoming Xerces 2.x checkin
60         // When running via 'build bugzilla.classes bugzilla', the Xerces
61         //  version will already be reported by the test harness
62         // logger.logMsg(Logger.STATUSMSG, "Apache Xerces "
63         //              + org.apache.xerces.framework.Version.fVersion);
64         logger.logMsg(Logger.STATUSMSG, "Apache Xalan  "
65                       + org.apache.xalan.Version.getVersion());
66         try
67         {
68             DocumentBuilderFactory dfactory = DocumentBuilderFactory.newInstance();
69 
70             dfactory.setValidating(false);
71             dfactory.setNamespaceAware(true);
72 
73             DocumentBuilder db = dfactory.newDocumentBuilder();
74             Document document =
75             db.parse(new ByteArrayInputStream(_nodeSetInput1.getBytes()));
76             NodeList nl = XPathAPI.selectNodeList(document, _xpath);
77 
78             for (int i = 0; i < nl.getLength(); i++)
79             {
80                 logger.logMsg(Logger.STATUSMSG, i + " "
81                               + getNodeTypeString(nl.item(i)) + " " + nl.item(i));
82 
83                 if (nl.item(i).getNodeType() == Node.ATTRIBUTE_NODE)
84                 {
85                     Attr a = (Attr) nl.item(i);
86 
87                     logger.logMsg(Logger.STATUSMSG, i + " " + a.getNodeName() + " "
88                                        + a.getNodeValue());
89                     logger.logMsg(Logger.STATUSMSG, i + " specified " + a.getSpecified());
90                     logger.logMsg(Logger.STATUSMSG, i + " owner document: " + a.getOwnerDocument());
91                 }
92             }
93         }
94         catch (Throwable t)
95         {
96             logger.logThrowable(Logger.ERRORMSG, t, "Iterating document");
97             logger.checkFail("Iterating document threw: " + t.toString());
98         }
99    	}
100 
101     static String[] nodeTypeString = new String[]{ "", "ELEMENT", "ATTRIBUTE",
102                                       "TEXT_NODE", "CDATA_SECTION",
103                                       "ENTITY_REFERENCE", "ENTITY",
104                                       "PROCESSING_INSTRUCTION",
105                                       "COMMENT", "DOCUMENT",
106                                       "DOCUMENT_TYPE",
107                                       "DOCUMENT_FRAGMENT",
108                                       "NOTATION" };
109 
getNodeTypeString(short nodeType)110     public static String getNodeTypeString(short nodeType)
111     {
112         if ((nodeType > 0) && (nodeType < 13))
113         {
114             return nodeTypeString[nodeType];
115         } else
116         {
117             return "";
118         }
119     }
120 
getNodeTypeString(Node n)121     public static String getNodeTypeString(Node n)
122     {
123         return getNodeTypeString(n.getNodeType());
124     }
125 
126     /**
127      * <a href="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=4336">
128      * Link to Bugzilla report</a>
129      * @return Xalan 2.2.D11 adds a strange Attribute.
130      */
getDescription()131     public String getDescription()
132     {
133         return "Xalan 2.2.D11 adds a strange Attribute";
134     }
135 
136 }  // end of class Bugzilla4336
137 
138