1 /*
2  * Copyright (c) 2014, 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 xpath;
25 
26 import javax.xml.namespace.NamespaceContext;
27 import javax.xml.parsers.DocumentBuilderFactory;
28 import javax.xml.parsers.ParserConfigurationException;
29 import javax.xml.xpath.XPath;
30 import javax.xml.xpath.XPathConstants;
31 import javax.xml.xpath.XPathExpressionException;
32 import javax.xml.xpath.XPathFactory;
33 import org.testng.annotations.DataProvider;
34 import org.testng.annotations.Listeners;
35 import org.testng.annotations.Test;
36 import org.w3c.dom.Document;
37 import org.w3c.dom.Node;
38 
39 /*
40  * @test
41  * @bug 6376058
42  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
43  * @run testng/othervm -DrunSecMngr=true xpath.XPathTest
44  * @run testng/othervm xpath.XPathTest
45  * @summary Test XPath functions. See details for each test.
46  */
47 @Listeners({jaxp.library.BasePolicy.class})
48 public class XPathTest {
49 
50     /*
51       @bug 6211561
52      * Verifies the specification for XPath and XPathExpression:
53      * If a null value is provided for item (the context),
54      * the expression must have no dependency on the context.
55     */
56     @Test(dataProvider = "noContextDependency")
testNoContextDependency1(String expression, Object item)57     public void testNoContextDependency1(String expression, Object item) throws XPathExpressionException {
58         XPath xPath = XPathFactory.newInstance().newXPath();
59         xPath.evaluate(expression, item, XPathConstants.STRING);
60     }
61 
62     @Test(dataProvider = "noContextDependency")
testNoContextDependency2(String expression, Object item)63     public void testNoContextDependency2(String expression, Object item) throws XPathExpressionException {
64         XPath xPath = XPathFactory.newInstance().newXPath();
65         xPath.evaluateExpression(expression, item, String.class);
66     }
67 
68     /*
69       @bug 6211561
70      * Verifies the specification for XPath and XPathExpression:
71      * If a null value is provided for item (the context) that the operation
72      * depends on, XPathExpressionException will be thrown
73     */
74     @Test(dataProvider = "hasContextDependency", expectedExceptions = XPathExpressionException.class)
testHasContextDependency1(String expression, Object item)75     public void testHasContextDependency1(String expression, Object item) throws XPathExpressionException {
76         XPath xPath = XPathFactory.newInstance().newXPath();
77         xPath.evaluate(expression, item, XPathConstants.STRING);
78     }
79 
80     @Test(dataProvider = "hasContextDependency", expectedExceptions = XPathExpressionException.class)
testHasContextDependency2(String expression, Object item)81     public void testHasContextDependency2(String expression, Object item) throws XPathExpressionException {
82         XPath xPath = XPathFactory.newInstance().newXPath();
83         xPath.evaluateExpression(expression, item, String.class);
84     }
85 
86     /*
87       @bug 6376058
88       Verifies that XPath.getNamespaceContext() is supported.
89     */
90     @Test
testNamespaceContext()91     public void testNamespaceContext() {
92         XPathFactory xPathFactory = XPathFactory.newInstance();
93         XPath xPath = xPathFactory.newXPath();
94         NamespaceContext namespaceContext = xPath.getNamespaceContext();
95     }
96 
97     /*
98      * DataProvider: the expression has no dependency on the context
99      */
100     @DataProvider(name = "noContextDependency")
getExpressionContext()101     public Object[][] getExpressionContext() throws Exception {
102         return new Object[][]{
103             {"1+1", (Node)null},
104             {"5 mod 2", (Node)null},
105             {"8 div 2", (Node)null},
106             {"/node", getEmptyDocument()}
107         };
108     }
109 
110     /*
111      * DataProvider: the expression has dependency on the context, but the context
112      * is null.
113      */
114     @DataProvider(name = "hasContextDependency")
getExpressionContext1()115     public Object[][] getExpressionContext1() throws Exception {
116         return new Object[][]{
117             {"/node", (Node)null},
118             {"//@lang", (Node)null},
119             {"bookstore//book", (Node)null},
120             {"/bookstore/book[last()]", (Node)null},
121             {"//title[@lang='en']", (Node)null},
122             {"/bookstore/book[price>9.99]", (Node)null},
123             {"/bookstore/book[price>8.99 and price<9.99]", (Node)null},
124             {"/bookstore/*", (Node)null},
125             {"//title[@*]", (Node)null},
126             {"//title | //price", (Node)null},
127             {"//book/title | //book/price", (Node)null},
128             {"/bookstore/book/title | //price", (Node)null},
129             {"child::book", (Node)null},
130             {"child::text()", (Node)null},
131             {"child::*/child::price", (Node)null}
132         };
133     }
134 
135     /**
136      * Returns an empty {@link org.w3c.dom.Document}.
137      * @return a DOM Document, null in case of Exception
138      */
getEmptyDocument()139     public Document getEmptyDocument() {
140         try {
141             return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
142         } catch (ParserConfigurationException e) {
143             return null;
144         }
145     }
146 }
147