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