1 /*
2  * Copyright (c) 2015, 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 java.io.File;
27 
28 import javax.xml.xpath.XPath;
29 import javax.xml.xpath.XPathExpression;
30 import javax.xml.xpath.XPathExpressionException;
31 import javax.xml.xpath.XPathNodes;
32 import javax.xml.xpath.XPathEvaluationResult;
33 
34 import static org.testng.Assert.assertEquals;
35 import static org.testng.Assert.assertTrue;
36 
37 import org.testng.annotations.Listeners;
38 import org.testng.annotations.Test;
39 import org.w3c.dom.Document;
40 import org.w3c.dom.Node;
41 
42 /*
43  * @test
44  * @bug 8054196
45  * @library /javax/xml/jaxp/libs /javax/xml/jaxp/unittest
46  * @run testng/othervm -DrunSecMngr=true xpath.XPathExpAnyTypeTest
47  * @run testng/othervm xpath.XPathExpAnyTypeTest
48  * @summary Test for the project XPath: support any type. This test covers the new
49  * evaluateExpression methods of XPathExpression.
50  */
51 @Listeners({jaxp.library.BasePolicy.class})
52 public class XPathExpAnyTypeTest extends XPathTestBase {
53 
54     /*
55      * Check that NPE is thrown when the class type is null.
56      */
57     @Test(dataProvider = "xpath", expectedExceptions = NullPointerException.class)
test02(XPath xpath)58     public void test02(XPath xpath) throws XPathExpressionException {
59         XPathExpression exp = xpath.compile("1+1");
60         double result = exp.evaluateExpression((Object)null, null);
61     }
62 
63     /*
64      * Parameter item can be null when the expression does not depends on the
65      * context.
66      */
67     @Test(dataProvider = "xpath")
test03(XPath xpath)68     public void test03(XPath xpath) throws XPathExpressionException {
69         XPathExpression exp = xpath.compile("1+1");
70         int result = exp.evaluateExpression((Object)null, Integer.class);
71         assertTrue(result == 2);
72     }
73 
74     /*
75      * Test return type: boolean.
76      */
77     @Test(dataProvider = "document")
test04(XPath xpath, Document doc)78     public void test04(XPath xpath, Document doc) throws XPathExpressionException {
79         XPathExpression exp = xpath.compile("boolean(/Customers/Customer[@id=3])");
80         boolean result1 = exp.evaluateExpression(doc, Boolean.class);
81         assertTrue(result1);
82     }
83 
84     /*
85      * Test return type: numeric.
86      */
87     @Test(dataProvider = "document")
test05(XPath xpath, Document doc)88     public void test05(XPath xpath, Document doc) throws XPathExpressionException {
89         XPathExpression exp = xpath.compile("count(/Customers/Customer)");
90         double result1 = exp.evaluateExpression(doc, Double.class);
91         assertTrue(result1 == 3.0);
92 
93         int result2 = exp.evaluateExpression(doc, Integer.class);
94         assertTrue(result2 == 3);
95     }
96 
97     /*
98      * Test return type: String.
99      */
100     @Test(dataProvider = "document")
test06(XPath xpath, Document doc)101     public void test06(XPath xpath, Document doc) throws XPathExpressionException {
102         XPathExpression exp = xpath.compile("string(/Customers/Customer[@id=3]/Phone/text())");
103         String result1 = exp.evaluateExpression(doc, String.class);
104         assertTrue(result1.equals("3333333333"));
105     }
106 
107     /*
108      * Test return type: NodeSet.
109      */
110     @Test(dataProvider = "document")
test07(XPath xpath, Document doc)111     public void test07(XPath xpath, Document doc) throws XPathExpressionException {
112         XPathExpression exp = xpath.compile("/Customers/Customer");
113         XPathNodes nodes = exp.evaluateExpression(doc, XPathNodes.class);
114         assertTrue(nodes.size() == 3);
115         for (Node n : nodes) {
116             assertEquals(n.getLocalName(), "Customer");
117         }
118     }
119 
120     /*
121      * Test return type: Node.
122      */
123     @Test(dataProvider = "document")
test08(XPath xpath, Document doc)124     public void test08(XPath xpath, Document doc) throws XPathExpressionException {
125         XPathExpression exp = xpath.compile("/Customers/Customer[@id=3]");
126         Node n = exp.evaluateExpression(doc, Node.class);
127         assertEquals(n.getLocalName(), "Customer");
128     }
129 
130     /*
131      * Test return type: Unsupported type.
132      */
133     @Test(dataProvider = "document", expectedExceptions = IllegalArgumentException.class)
test09(XPath xpath, Document doc)134     public void test09(XPath xpath, Document doc) throws XPathExpressionException {
135         XPathExpression exp = xpath.compile("/Customers/Customer[@id=3]");
136         File n = exp.evaluateExpression(doc, File.class);
137     }
138 
139     /*
140      * Test return type: Any::Boolean.
141      */
142     @Test(dataProvider = "document")
test10(XPath xpath, Document doc)143     public void test10(XPath xpath, Document doc) throws XPathExpressionException {
144         XPathExpression exp = xpath.compile("boolean(/Customers/Customer[@id=3])");
145         XPathEvaluationResult<?> result = exp.evaluateExpression(doc);
146         verifyResult(result, true);
147     }
148 
149     /*
150      * Test return type: Any::Number.
151      */
152     @Test(dataProvider = "document")
test11(XPath xpath, Document doc)153     public void test11(XPath xpath, Document doc) throws XPathExpressionException {
154         XPathExpression exp = xpath.compile("count(/Customers/Customer)");
155         XPathEvaluationResult<?> result = exp.evaluateExpression(doc);
156         verifyResult(result, 3.0);
157     }
158 
159     /*
160      * Test return type: Any::String.
161      */
162     @Test(dataProvider = "document")
test12(XPath xpath, Document doc)163     public void test12(XPath xpath, Document doc) throws XPathExpressionException {
164         XPathExpression exp = xpath.compile("string(/Customers/Customer[@id=3]/Phone/text())");
165         XPathEvaluationResult<?> result = exp.evaluateExpression(doc, XPathEvaluationResult.class);
166         verifyResult(result, "3333333333");
167     }
168 
169     /*
170      * Test return type: Any::Nodeset.
171      */
172     @Test(dataProvider = "document")
test13(XPath xpath, Document doc)173     public void test13(XPath xpath, Document doc) throws XPathExpressionException {
174         XPathExpression exp = xpath.compile("/Customers/Customer");
175         XPathEvaluationResult<?> result = exp.evaluateExpression(doc);
176         verifyResult(result, "Customer");
177     }
178 
179     /*
180      * Test return type: Any::Node.
181      */
182     @Test(dataProvider = "document")
test14(XPath xpath, Document doc)183     public void test14(XPath xpath, Document doc) throws XPathExpressionException {
184         XPathExpression exp = xpath.compile("/Customers/Customer[@id=3]");
185         XPathEvaluationResult<?> result = exp.evaluateExpression(doc);
186         verifyResult(result, "Customer");
187     }
188 
189 }
190