xref: /aosp_15_r20/external/apache-xml/test/tests/bugzilla/Bugzilla6329.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 import java.io.ByteArrayInputStream;
24 import org.apache.xpath.CachedXPathAPI;
25 import org.w3c.dom.*;
26 import javax.xml.parsers.*;
27 
28 /**
29  * Testlet for reproducing Bugzilla reported bugs.
30  *
31  * @author [email protected]
32  * @author [email protected]
33  */
34 public class Bugzilla6329 extends TestletImpl
35 {
36     // Initialize our classname for TestletImpl's main() method - must be updated!
37     static { thisClassName = "Bugzilla6329"; }
38 
39     /**
40      * The following program tries to select all nodes in the document using an
41      * XPath expression but the XPath misses the CDATA section.
42      * User reported output is:
43      * <PRE>
44      * Xerces-J 2.0.0
45      * Xalan Java 2.2.0
46      * 0 (DOCUMENT): [#document: null]
47      * 1 (ELEMENT): [svg: null]
48      * 2 (ATTRIBUTE): onload="thisInit()"
49      * 3 (ATTRIBUTE): width="106.786pt"
50      * 4 (ATTRIBUTE): xml:space="preserve"
51      * 5 (ATTRIBUTE): org.apache.xml.dtm.ref.dom2dtm.DOM2DTMdefaultNamespaceDeclarationNode@5b699b
52      * 6 (TEXT_NODE): [#text:
53      * ]
54      * 7 (ELEMENT): [style: null]
55      * 8 (ATTRIBUTE): type="text/css"
56      * 9 (ATTRIBUTE): xml:space=""
57      * 10 (TEXT_NODE): [#text:
58      * ]
59      * 11 (TEXT_NODE): [#text:
60      * ]
61      * </PRE>
62      * @param d (optional) Datalet to use as data point for the test.
63      */
execute(Datalet d)64     public void execute(Datalet d)
65 	{
66         // Use logger.logMsg(...) instead of System.out.println(...)
67         logger.logMsg(Logger.STATUSMSG, "Reproducing Bugzilla#6329");
68 
69         String input =
70         "<svg  width='106.786pt' xml:space='preserve' onload='thisInit()'>\n" +
71         "<style type='text/css' xml:space=''>\n" +
72         "<![CDATA[\n" +
73         "    @font-face{font-family:'RussellSquare-Oblique';src:url(Arial.cef)}\n" +
74         "]]>\n" +
75         "</style>\n" +
76         "</svg>\n";
77 
78         // Note: please avoid calling these directly, or at least use
79         //    reflection to find the classes: they do change with
80         //    different Xerces and Xalan builds! -sc
81         //logger.logMsg(logger.STATUSMSG, org.apache.xerces.impl.Version.fVersion);
82         //logger.logMsg(logger.STATUSMSG, org.apache.xalan.processor.XSLProcessorVersion.S_VERSION);
83 
84         try
85         {
86             DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
87             dbf.setNamespaceAware(true);
88 
89             DocumentBuilder db = dbf.newDocumentBuilder();
90             Document doc = db.parse(new ByteArrayInputStream(input.getBytes()));
91             CachedXPathAPI xp = new CachedXPathAPI();
92             logger.logMsg(logger.STATUSMSG, "User case: xp.selectNodeList(doc, (//. | //@* | //namespace::*))");
93             NodeList nl = xp.selectNodeList(doc, "(//. | //@* | //namespace::*)");
94 
95             for (int i = 0; i < nl.getLength(); i++)
96             {
97                 // logger.logMsg(logger.STATUSMSG, i + " parent: " + nl.item(i).getParentNode());
98                 // logger.logMsg(logger.STATUSMSG, i + " ("+org.apache.xml.security.utils.XMLUtils.getNodeTypeString(nl.item(i))+"): " + nl.item(i));
99                 logger.logMsg(logger.STATUSMSG, i + ": " + nl.item(i));
100             }
101 
102             logger.logMsg(logger.STATUSMSG, "dave case: xp.selectNodeList(doc, (//.))");
103             nl = xp.selectNodeList(doc, "(//.)");
104 
105             for (int i = 0; i < nl.getLength(); i++)
106             {
107                 // logger.logMsg(logger.STATUSMSG, i + " parent: " + nl.item(i).getParentNode());
108                 // logger.logMsg(logger.STATUSMSG, i + " ("+org.apache.xml.security.utils.XMLUtils.getNodeTypeString(nl.item(i))+"): " + nl.item(i));
109                 logger.logMsg(logger.STATUSMSG, i + ": " + nl.item(i));
110             }
111 
112 
113             logger.checkAmbiguous("Test needs manual validation! (But Joe hints it may be invalid)");
114         }
115         catch (Throwable t)
116         {
117             logger.logThrowable(logger.ERRORMSG, t, "Unexpected exception");
118             logger.checkErr("Unexpected exception: " + t.toString());
119         }
120     }
121 
122     /**
123      * <a href="http://nagoya.apache.org/bugzilla/show_bug.cgi?id=6329">
124      * Link to Bugzilla report</a>
125      * @return XPath does not catch CDATA Nodes.
126      */
getDescription()127     public String getDescription()
128     {
129         return "XPath does not catch CDATA Nodes";
130     }
131 
132 }  // end of class Bugzilla6329
133 
134