Skip to content

Commit 57bf603

Browse files
bwhuang-usJoeWang-Java
authored andcommittedAug 1, 2022
8289948: Improve test coverage for XPath functions: Node Set Functions
Reviewed-by: joehw
1 parent 1df77ec commit 57bf603

File tree

4 files changed

+344
-45
lines changed

4 files changed

+344
-45
lines changed
 

‎test/jaxp/javax/xml/jaxp/unittest/xpath/XPathAnyTypeTest.java

+13-7
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,8 @@ public void test03(XPath xpath) throws XPathExpressionException {
9090
*/
9191
@Test(dataProvider = "document")
9292
public void test04(XPath xpath, Document doc) throws XPathExpressionException {
93-
boolean result1 = xpath.evaluateExpression("boolean(/Customers/Customer[@id=3])", doc, Boolean.class);
93+
boolean result1 = xpath.evaluateExpression("boolean" +
94+
"(/Customers/Customer[@id=\"x3\"])", doc, Boolean.class);
9495
assertTrue(result1);
9596
}
9697

@@ -121,7 +122,8 @@ public void test06(XPath xpath, Class<Number> type) throws XPathExpressionExcept
121122
*/
122123
@Test(dataProvider = "document")
123124
public void test07(XPath xpath, Document doc) throws XPathExpressionException {
124-
String result1 = xpath.evaluateExpression("string(/Customers/Customer[@id=3]/Phone/text())", doc, String.class);
125+
String result1 = xpath.evaluateExpression("string(/Customers/Customer" +
126+
"[@id=\"x3\"]/Phone/text())", doc, String.class);
125127
assertTrue(result1.equals("3333333333"));
126128
}
127129

@@ -142,7 +144,8 @@ public void test08(XPath xpath, Document doc) throws XPathExpressionException {
142144
*/
143145
@Test(dataProvider = "document")
144146
public void test09(XPath xpath, Document doc) throws XPathExpressionException {
145-
Node n = xpath.evaluateExpression("/Customers/Customer[@id=3]", doc, Node.class);
147+
Node n = xpath.evaluateExpression("/Customers/Customer[@id=\"x3\"]",
148+
doc, Node.class);
146149
assertEquals(n.getLocalName(), "Customer");
147150
}
148151

@@ -151,15 +154,17 @@ public void test09(XPath xpath, Document doc) throws XPathExpressionException {
151154
*/
152155
@Test(dataProvider = "document", expectedExceptions = IllegalArgumentException.class)
153156
public void test10(XPath xpath, Document doc) throws XPathExpressionException {
154-
File n = xpath.evaluateExpression("/Customers/Customer[@id=3]", doc, File.class);
157+
File n = xpath.evaluateExpression("/Customers/Customer[@id=\"x3\"]",
158+
doc, File.class);
155159
}
156160

157161
/*
158162
* Test return type: Any::Boolean.
159163
*/
160164
@Test(dataProvider = "document")
161165
public void test11(XPath xpath, Document doc) throws XPathExpressionException {
162-
XPathEvaluationResult<?> result = xpath.evaluateExpression("boolean(/Customers/Customer[@id=3])", doc);
166+
XPathEvaluationResult<?> result = xpath.evaluateExpression(
167+
"boolean(/Customers/Customer[@id=\"x3\"])", doc);
163168
verifyResult(result, true);
164169
}
165170

@@ -178,7 +183,7 @@ public void test12(XPath xpath, Document doc) throws XPathExpressionException {
178183
@Test(dataProvider = "document")
179184
public void test13(XPath xpath, Document doc) throws XPathExpressionException {
180185
XPathEvaluationResult<?> result = xpath.evaluateExpression(
181-
"string(/Customers/Customer[@id=3]/Phone/text())", doc, XPathEvaluationResult.class);
186+
"string(/Customers/Customer[@id=\"x3\"]/Phone/text())", doc, XPathEvaluationResult.class);
182187
verifyResult(result, "3333333333");
183188
}
184189

@@ -196,7 +201,8 @@ public void test14(XPath xpath, Document doc) throws XPathExpressionException {
196201
*/
197202
@Test(dataProvider = "document")
198203
public void test15(XPath xpath, Document doc) throws XPathExpressionException {
199-
XPathEvaluationResult<?> result = xpath.evaluateExpression("/Customers/Customer[@id=3]", doc);
204+
XPathEvaluationResult<?> result = xpath.evaluateExpression(
205+
"/Customers/Customer[@id=\"x3\"]", doc);
200206
verifyResult(result, "Customer");
201207
}
202208
}

‎test/jaxp/javax/xml/jaxp/unittest/xpath/XPathExpAnyTypeTest.java

+7-7
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public void test03(XPath xpath) throws XPathExpressionException {
7676
*/
7777
@Test(dataProvider = "document")
7878
public void test04(XPath xpath, Document doc) throws XPathExpressionException {
79-
XPathExpression exp = xpath.compile("boolean(/Customers/Customer[@id=3])");
79+
XPathExpression exp = xpath.compile("boolean(/Customers/Customer[@id=\"x3\"])");
8080
boolean result1 = exp.evaluateExpression(doc, Boolean.class);
8181
assertTrue(result1);
8282
}
@@ -99,7 +99,7 @@ public void test05(XPath xpath, Document doc) throws XPathExpressionException {
9999
*/
100100
@Test(dataProvider = "document")
101101
public void test06(XPath xpath, Document doc) throws XPathExpressionException {
102-
XPathExpression exp = xpath.compile("string(/Customers/Customer[@id=3]/Phone/text())");
102+
XPathExpression exp = xpath.compile("string(/Customers/Customer[@id=\"x3\"]/Phone/text())");
103103
String result1 = exp.evaluateExpression(doc, String.class);
104104
assertTrue(result1.equals("3333333333"));
105105
}
@@ -122,7 +122,7 @@ public void test07(XPath xpath, Document doc) throws XPathExpressionException {
122122
*/
123123
@Test(dataProvider = "document")
124124
public void test08(XPath xpath, Document doc) throws XPathExpressionException {
125-
XPathExpression exp = xpath.compile("/Customers/Customer[@id=3]");
125+
XPathExpression exp = xpath.compile("/Customers/Customer[@id=\"x3\"]");
126126
Node n = exp.evaluateExpression(doc, Node.class);
127127
assertEquals(n.getLocalName(), "Customer");
128128
}
@@ -132,7 +132,7 @@ public void test08(XPath xpath, Document doc) throws XPathExpressionException {
132132
*/
133133
@Test(dataProvider = "document", expectedExceptions = IllegalArgumentException.class)
134134
public void test09(XPath xpath, Document doc) throws XPathExpressionException {
135-
XPathExpression exp = xpath.compile("/Customers/Customer[@id=3]");
135+
XPathExpression exp = xpath.compile("/Customers/Customer[@id=\"x3\"]");
136136
File n = exp.evaluateExpression(doc, File.class);
137137
}
138138

@@ -141,7 +141,7 @@ public void test09(XPath xpath, Document doc) throws XPathExpressionException {
141141
*/
142142
@Test(dataProvider = "document")
143143
public void test10(XPath xpath, Document doc) throws XPathExpressionException {
144-
XPathExpression exp = xpath.compile("boolean(/Customers/Customer[@id=3])");
144+
XPathExpression exp = xpath.compile("boolean(/Customers/Customer[@id=\"x3\"])");
145145
XPathEvaluationResult<?> result = exp.evaluateExpression(doc);
146146
verifyResult(result, true);
147147
}
@@ -161,7 +161,7 @@ public void test11(XPath xpath, Document doc) throws XPathExpressionException {
161161
*/
162162
@Test(dataProvider = "document")
163163
public void test12(XPath xpath, Document doc) throws XPathExpressionException {
164-
XPathExpression exp = xpath.compile("string(/Customers/Customer[@id=3]/Phone/text())");
164+
XPathExpression exp = xpath.compile("string(/Customers/Customer[@id=\"x3\"]/Phone/text())");
165165
XPathEvaluationResult<?> result = exp.evaluateExpression(doc, XPathEvaluationResult.class);
166166
verifyResult(result, "3333333333");
167167
}
@@ -181,7 +181,7 @@ public void test13(XPath xpath, Document doc) throws XPathExpressionException {
181181
*/
182182
@Test(dataProvider = "document")
183183
public void test14(XPath xpath, Document doc) throws XPathExpressionException {
184-
XPathExpression exp = xpath.compile("/Customers/Customer[@id=3]");
184+
XPathExpression exp = xpath.compile("/Customers/Customer[@id=\"x3\"]");
185185
XPathEvaluationResult<?> result = exp.evaluateExpression(doc);
186186
verifyResult(result, "Customer");
187187
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
1+
/*
2+
* Copyright (c) 2022, 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+
package xpath;
24+
25+
import org.testng.Assert;
26+
import org.testng.annotations.DataProvider;
27+
import org.testng.annotations.Test;
28+
import org.w3c.dom.Document;
29+
import org.w3c.dom.Node;
30+
31+
import javax.xml.xpath.*;
32+
33+
/*
34+
* @test
35+
* @bug 8289948
36+
* @library /javax/xml/jaxp/unittest
37+
* @run testng xpath.XPathNodeSetFnTest
38+
* @summary Tests the XPath Node Set Functions
39+
*/
40+
public class XPathNodeSetFnTest extends XPathTestBase {
41+
42+
private static final Document doc = getDtdDocument();
43+
44+
/*
45+
* DataProvider for testing the id function.
46+
* Data columns:
47+
* see parameters of the test "testIdFn"
48+
*/
49+
@DataProvider(name = "idExpTestCases")
50+
public Object[][] getIdExp() {
51+
return new Object[][]{
52+
{"id('x3')", "Customer_x3"},
53+
{"id('x1 x2 x3')[3]", "Customer_x3"},
54+
{"id('x1 | x2 | x3')[3]", "Customer_x3"},
55+
{"id('x')", "Email_x"},
56+
{"id(//Customer[3]/@id)", "Customer_x3"},
57+
{"id(//*[.='123@xyz.com']/@id)", "Email_x"},
58+
};
59+
}
60+
61+
/*
62+
* DataProvider for testing the count function.
63+
* Data columns:
64+
* see parameters of the test "testCountFn"
65+
*/
66+
@DataProvider(name = "countExpTestCases")
67+
public Object[][] getCountExp() {
68+
return new Object[][]{
69+
{"count(//Customer)", CUSTOMERS},
70+
{"count(//@id)", ID_ATTRIBUTES},
71+
{"count(//Customer/@id)", CUSTOMERS},
72+
{"count(//@*)", ID_ATTRIBUTES + FOO_ID_ATTRIBUTES},
73+
{"count(//*)",
74+
ROOT + CUSTOMERS + FOO_CUSTOMERS +
75+
(CUSTOMERS + FOO_CUSTOMERS) *
76+
CUSTOMER_ELEMENTS},
77+
{"count(//*[@id])", ID_ATTRIBUTES},
78+
{"count(./*)", ROOT},
79+
{"count(//Customer[1]/following::*)",
80+
CUSTOMERS - 1 + FOO_CUSTOMERS +
81+
(CUSTOMERS - 1 + FOO_CUSTOMERS) *
82+
CUSTOMER_ELEMENTS},
83+
{"count(//Customer[1]/following-sibling::*)",
84+
CUSTOMERS - 1 + FOO_CUSTOMERS},
85+
{"count(//Customer[3]/preceding::*)",
86+
CUSTOMERS - 1 + (CUSTOMERS - 1) * CUSTOMER_ELEMENTS},
87+
{"count(//Customer[3]/preceding-sibling::*)", CUSTOMERS - 1},
88+
{"count(//Customer[1]/ancestor::*)", ROOT},
89+
{"count(//Customer[1]/ancestor-or-self::*)", ROOT + 1},
90+
{"count(//Customer[1]/descendant::*)", CUSTOMER_ELEMENTS},
91+
{"count(//Customer[1]/descendant-or-self::*)",
92+
CUSTOMER_ELEMENTS + 1},
93+
{"count(//Customer/node())",
94+
ID_ATTRIBUTES + CUSTOMERS * CUSTOMER_ELEMENTS},
95+
};
96+
}
97+
98+
/*
99+
* DataProvider for testing the position function.
100+
* Data columns:
101+
* see parameters of the test "testPositionFn"
102+
*/
103+
@DataProvider(name = "positionExpTestCases")
104+
public Object[][] getPositionExp() {
105+
return new Object[][]{
106+
{"//Customer[position()=1]", "Customer_x1"},
107+
{"//Customer[position()=last()]", "Customer_x3"},
108+
{"//Customer[position()>1 and position()<last()]",
109+
"Customer_x2"},
110+
{"//Customer[position() mod 2 =0]", "Customer_x2"},
111+
{"//Customer[last()]", "Customer_x3"},
112+
};
113+
}
114+
115+
/*
116+
* DataProvider for testing the name and local-name functions.
117+
* Data columns:
118+
* see parameters of the test "testNameFn"
119+
*/
120+
@DataProvider(name = "nameExpTestCases")
121+
public Object[][] getNameExp() {
122+
return new Object[][]{
123+
{"local-name(//Customer)", "Customer"},
124+
{"local-name(//foo:Customer)", "Customer"},
125+
{"local-name(//Customer/@id)", "id"},
126+
{"local-name(//foo:Customer/@foo:id)", "id"},
127+
{"local-name(//*[local-name()='Customer'])", "Customer"},
128+
{"namespace-uri(.)", ""},
129+
{"namespace-uri(//Customers)", ""},
130+
{"namespace-uri(//Customer)", ""},
131+
{"namespace-uri(//foo:Customer)", "foo"},
132+
{"namespace-uri(//@id)", ""},
133+
{"namespace-uri(//@foo:id)", "foo"},
134+
{"name(//*[namespace-uri()=\"foo\"])", "foo:Customer"},
135+
{"name(//Customer)", "Customer"},
136+
{"name(//foo:Customer)", "foo:Customer"},
137+
{"name(//Customer/@id)", "id"},
138+
{"name(//foo:Customer/@foo:id)", "foo:id"},
139+
{"name(//*[name()='foo:Customer'])", "foo:Customer"},
140+
};
141+
}
142+
143+
/**
144+
* Verifies that the result of evaluating the id function matches the
145+
* expected result.
146+
*
147+
* @param exp XPath expression
148+
* @param expected expected result
149+
* @throws Exception if test fails
150+
*/
151+
@Test(dataProvider = "idExpTestCases")
152+
void testIdFn(String exp, String expected) throws Exception {
153+
XPath xPath = XPathFactory.newInstance().newXPath();
154+
155+
Node node = xPath.evaluateExpression(exp, doc, Node.class);
156+
Node node2 = (Node) xPath.evaluate(exp, doc, XPathConstants.NODE);
157+
158+
Assert.assertEquals(node.getNodeName() + "_" +
159+
node.getAttributes().item(0).getNodeValue()
160+
, expected);
161+
Assert.assertEquals(node2, node);
162+
}
163+
164+
/**
165+
* Verifies that the result of evaluating the count function matches the
166+
* expected result.
167+
*
168+
* @param exp XPath expression
169+
* @param expected expected result
170+
* @throws Exception if test fails
171+
*/
172+
@Test(dataProvider = "countExpTestCases")
173+
void testCountFn(String exp, int expected) throws Exception {
174+
XPath xPath = XPathFactory.newInstance().newXPath();
175+
176+
double num = xPath.evaluateExpression(exp, doc, Double.class);
177+
double num2 = (double) xPath.evaluate(exp, doc, XPathConstants.NUMBER);
178+
179+
Assert.assertEquals(num, expected);
180+
Assert.assertEquals(num2, num);
181+
}
182+
183+
/**
184+
* Verifies that the result of evaluating the position function matches the
185+
* expected result.
186+
*
187+
* @param exp XPath expression
188+
* @param expected expected result
189+
* @throws Exception if test fails
190+
*/
191+
@Test(dataProvider = "positionExpTestCases")
192+
void testPositionFn(String exp, String expected) throws Exception {
193+
XPath xPath = XPathFactory.newInstance().newXPath();
194+
195+
Node node = xPath.evaluateExpression(exp, doc, Node.class);
196+
Node node2 = (Node) xPath.evaluate(exp, doc, XPathConstants.NODE);
197+
198+
Assert.assertEquals(node.getNodeName() + "_" +
199+
node.getAttributes().item(0).getNodeValue()
200+
, expected);
201+
Assert.assertEquals(node2, node);
202+
}
203+
204+
/**
205+
* Verifies that the result of evaluating the name and local-name functions
206+
* matches the expected result.
207+
*
208+
* @param exp XPath expression
209+
* @param expected expected result
210+
* @throws Exception if test fails
211+
*/
212+
@Test(dataProvider = "nameExpTestCases")
213+
void testNameFn(String exp, String expected) throws Exception {
214+
XPath xPath = XPathFactory.newInstance().newXPath();
215+
216+
String s = xPath.evaluateExpression(exp, doc, String.class);
217+
String s2 = (String) xPath.evaluate(exp, doc, XPathConstants.STRING);
218+
219+
Assert.assertEquals(s, expected);
220+
Assert.assertEquals(s2, s);
221+
}
222+
}

‎test/jaxp/javax/xml/jaxp/unittest/xpath/XPathTestBase.java

+102-31
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2015, 2022, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -31,11 +31,6 @@
3131
import javax.xml.parsers.DocumentBuilderFactory;
3232
import javax.xml.xpath.XPath;
3333
import javax.xml.xpath.XPathFactory;
34-
import static javax.xml.xpath.XPathConstants.BOOLEAN;
35-
import static javax.xml.xpath.XPathConstants.NUMBER;
36-
import static javax.xml.xpath.XPathConstants.STRING;
37-
import static javax.xml.xpath.XPathConstants.NODE;
38-
import static javax.xml.xpath.XPathConstants.NODESET;
3934
import javax.xml.xpath.XPathNodes;
4035
import javax.xml.xpath.XPathEvaluationResult;
4136

@@ -51,42 +46,123 @@
5146
* Base class for XPath test
5247
*/
5348
class XPathTestBase {
49+
static final String DECLARATION = "<?xml version=\"1.0\" " +
50+
"encoding=\"UTF-8\" standalone=\"yes\"?>";
5451

55-
static final String rawXML
56-
= "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"
57-
+ "<Customers>"
58-
+ " <Customer id=\"1\">"
52+
static final String DTD = """
53+
<!DOCTYPE Customers [
54+
<!ELEMENT Customers (Customer*)>
55+
<!ELEMENT Customer (Name, Phone, Email, Address)>
56+
<!ELEMENT Name (#PCDATA)>
57+
<!ELEMENT Phone (#PCDATA)>
58+
<!ELEMENT Email (#PCDATA)>
59+
<!ELEMENT Address (Street, City, State)>
60+
<!ELEMENT Street (#PCDATA)>
61+
<!ELEMENT City (#PCDATA)>
62+
<!ELEMENT State (#PCDATA)>
63+
<!ATTLIST Customer id ID #REQUIRED>
64+
<!ATTLIST Email id ID #REQUIRED>
65+
]>
66+
67+
""";
68+
69+
static final String RAW_XML
70+
= "<Customers xmlns:foo=\"foo\">"
71+
+ " <Customer id=\"x1\">"
5972
+ " <Name>name1</Name>"
6073
+ " <Phone>1111111111</Phone>"
61-
+ " <Email>123@xyz.com</Email>"
74+
+ " <Email id=\"x\">123@xyz.com</Email>"
6275
+ " <Address>"
6376
+ " <Street>1111 111st ave</Street>"
6477
+ " <City>The City</City>"
6578
+ " <State>The State</State>"
6679
+ " </Address>"
6780
+ " </Customer>"
68-
+ " <Customer id=\"2\">"
69-
+ " <Name>name1</Name>"
81+
+ " <Customer id=\"x2\">"
82+
+ " <Name>name2</Name>"
7083
+ " <Phone>2222222222</Phone>"
71-
+ " <Email>123@xyz.com</Email>"
84+
+ " <Email id=\"y\">123@xyz.com</Email>"
7285
+ " <Address>"
7386
+ " <Street>2222 222nd ave</Street>"
7487
+ " <City>The City</City>"
7588
+ " <State>The State</State>"
7689
+ " </Address>"
7790
+ " </Customer>"
78-
+ " <Customer id=\"3\">"
79-
+ " <Name>name1</Name>"
91+
+ " <Customer id=\"x3\">"
92+
+ " <Name>name3</Name>"
8093
+ " <Phone>3333333333</Phone>"
81-
+ " <Email>123@xyz.com</Email>"
94+
+ " <Email id=\"z\">123@xyz.com</Email>"
8295
+ " <Address>"
8396
+ " <Street>3333 333rd ave</Street>"
8497
+ " <City>The City</City>"
8598
+ " <State>The State</State>"
8699
+ " </Address>"
87100
+ " </Customer>"
101+
+ " <foo:Customer foo:id=\"x1\">"
102+
+ " <foo:Name>name1</foo:Name>"
103+
+ " <foo:Phone>1111111111</foo:Phone>"
104+
+ " <foo:Email foo:id=\"x\">123@xyz.com</foo:Email>"
105+
+ " <foo:Address>"
106+
+ " <foo:Street>1111 111st ave</foo:Street>"
107+
+ " <foo:City>The City</foo:City>"
108+
+ " <foo:State>The State</foo:State>"
109+
+ " </foo:Address>"
110+
+ " </foo:Customer>"
88111
+ "</Customers>";
89112

113+
// Number of root element.
114+
final int ROOT = 1;
115+
// Number of Customer elements.
116+
final int CUSTOMERS = 3;
117+
// Number of id attributes.
118+
final int ID_ATTRIBUTES = 6;
119+
// Number of child elements of Customer.
120+
final int CUSTOMER_ELEMENTS = 7;
121+
// Number of Customer in the foo namespace.
122+
final int FOO_CUSTOMERS = 1;
123+
// Number of id attributes in the foo namespace.
124+
final int FOO_ID_ATTRIBUTES = 2;
125+
126+
/**
127+
* Returns a {@link org.w3c.dom.Document} for XML with DTD.
128+
* @return a DOM Document
129+
* @throws RuntimeException if any error occurred during document
130+
* initialization.
131+
*/
132+
public static Document getDtdDocument() throws RuntimeException {
133+
return documentOf(DECLARATION + DTD + RAW_XML);
134+
}
135+
136+
/**
137+
* Returns a {@link org.w3c.dom.Document} for raw XML.
138+
* @return a DOM Document
139+
* @throws RuntimeException if any error occurred during document
140+
* initialization.
141+
*/
142+
public static Document getDocument() throws RuntimeException {
143+
return documentOf(DECLARATION + RAW_XML);
144+
}
145+
146+
/**
147+
* Returns a {@link org.w3c.dom.Document} for input XML string.
148+
* @param xml the input xml string.
149+
* @return a DOM Document.
150+
* @throws RuntimeException if any error occurred during document
151+
* initialization.
152+
*/
153+
public static Document documentOf(String xml) throws RuntimeException {
154+
try {
155+
var dBF = DocumentBuilderFactory.newInstance();
156+
dBF.setValidating(false);
157+
dBF.setNamespaceAware(true);
158+
return dBF.newDocumentBuilder().parse(
159+
new ByteArrayInputStream(xml.getBytes("UTF-8")));
160+
} catch (Exception e) {
161+
System.out.println("Exception while initializing XML document");
162+
throw new RuntimeException(e.getMessage());
163+
}
164+
}
165+
90166
void verifyResult(XPathEvaluationResult<?> result, Object expected) {
91167
switch (result.type()) {
92168
case BOOLEAN:
@@ -126,27 +202,22 @@ public Object[][] getXPath() {
126202
public Object[][] getInvalidNumericTypes() {
127203
XPath xpath = XPathFactory.newInstance().newXPath();
128204
return new Object[][]{{xpath, AtomicInteger.class},
129-
{xpath, AtomicInteger.class},
130-
{xpath, AtomicLong.class},
131-
{xpath, BigDecimal.class},
132-
{xpath, BigInteger.class},
133-
{xpath, Byte.class},
134-
{xpath, Float.class},
135-
{xpath, Short.class}
205+
{xpath, AtomicInteger.class},
206+
{xpath, AtomicLong.class},
207+
{xpath, BigDecimal.class},
208+
{xpath, BigInteger.class},
209+
{xpath, Byte.class},
210+
{xpath, Float.class},
211+
{xpath, Short.class}
136212
};
137213
}
138214

139215
/*
140216
* DataProvider: XPath and Document objects
141217
*/
142218
@DataProvider(name = "document")
143-
public Object[][] getDocument() throws Exception {
144-
DocumentBuilderFactory dBF = DocumentBuilderFactory.newInstance();
145-
dBF.setValidating(false);
146-
dBF.setNamespaceAware(true);
147-
Document doc = dBF.newDocumentBuilder().parse(
148-
new ByteArrayInputStream(rawXML.getBytes("UTF-8")));
149-
219+
public Object[][] getDocuments() throws RuntimeException {
220+
Document doc = getDocument();
150221
return new Object[][]{{XPathFactory.newInstance().newXPath(), doc}};
151222
}
152223
}

0 commit comments

Comments
 (0)
Please sign in to comment.