/
XpathExpectationsHelper.java
277 lines (238 loc) · 10.3 KB
/
XpathExpectationsHelper.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
/*
* Copyright 2002-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.test.util;
import java.io.ByteArrayInputStream;
import java.util.Collections;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.hamcrest.Matcher;
import org.hamcrest.MatcherAssert;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.springframework.lang.Nullable;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.util.xml.SimpleNamespaceContext;
/**
* A helper class for applying assertions via XPath expressions.
*
* @author Rossen Stoyanchev
* @since 3.2
*/
public class XpathExpectationsHelper {
private final String expression;
private final XPathExpression xpathExpression;
private final boolean hasNamespaces;
/**
* XpathExpectationsHelper constructor.
* @param expression the XPath expression
* @param namespaces the XML namespaces referenced in the XPath expression, or {@code null}
* @param args arguments to parameterize the XPath expression with using the
* formatting specifiers defined in {@link String#format(String, Object...)}
* @throws XPathExpressionException if expression compilation failed
*/
public XpathExpectationsHelper(String expression, @Nullable Map<String, String> namespaces, Object... args)
throws XPathExpressionException {
this.expression = String.format(expression, args);
this.xpathExpression = compileXpathExpression(this.expression, namespaces);
this.hasNamespaces = !CollectionUtils.isEmpty(namespaces);
}
private static XPathExpression compileXpathExpression(String expression,
@Nullable Map<String, String> namespaces) throws XPathExpressionException {
SimpleNamespaceContext namespaceContext = new SimpleNamespaceContext();
namespaceContext.setBindings(namespaces != null ? namespaces : Collections.emptyMap());
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(namespaceContext);
return xpath.compile(expression);
}
/**
* Return the compiled XPath expression.
*/
protected XPathExpression getXpathExpression() {
return this.xpathExpression;
}
/**
* Parse the content, evaluate the XPath expression as a {@link Node},
* and assert it with the given {@code Matcher<Node>}.
*/
public void assertNode(byte[] content, @Nullable String encoding, final Matcher<? super Node> matcher)
throws Exception {
Node node = evaluateXpath(content, encoding, Node.class);
MatcherAssert.assertThat("XPath " + this.expression, node, matcher);
}
/**
* Parse the content, evaluate the XPath expression as a {@link NodeList},
* and assert it with the given {@code Matcher<NodeList>}.
* @since 5.2.2
*/
public void assertNodeList(byte[] content, @Nullable String encoding, final Matcher<? super NodeList> matcher)
throws Exception {
Document document = parseXmlByteArray(content, encoding);
NodeList nodeList = evaluateXpath(document, XPathConstants.NODESET, NodeList.class);
MatcherAssert.assertThat("XPath " + this.getXpathExpression(), nodeList, matcher);
}
/**
* Apply the XPath expression and assert the resulting content exists.
* @throws Exception if content parsing or expression evaluation fails
*/
public void exists(byte[] content, @Nullable String encoding) throws Exception {
Node node = evaluateXpath(content, encoding, Node.class);
AssertionErrors.assertTrue("XPath " + this.expression + " does not exist", node != null);
}
/**
* Apply the XPath expression and assert the resulting content does not exist.
* @throws Exception if content parsing or expression evaluation fails
*/
public void doesNotExist(byte[] content, @Nullable String encoding) throws Exception {
Node node = evaluateXpath(content, encoding, Node.class);
AssertionErrors.assertTrue("XPath " + this.expression + " exists", node == null);
}
/**
* Apply the XPath expression and assert the resulting content with the
* given Hamcrest matcher.
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertNodeCount(byte[] content, @Nullable String encoding, Matcher<Integer> matcher)
throws Exception {
NodeList nodeList = evaluateXpath(content, encoding, NodeList.class);
String reason = "nodeCount for XPath " + this.expression;
MatcherAssert.assertThat(reason, nodeList != null ? nodeList.getLength() : 0, matcher);
}
/**
* Apply the XPath expression and assert the resulting content as an integer.
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertNodeCount(byte[] content, @Nullable String encoding, int expectedCount) throws Exception {
NodeList nodeList = evaluateXpath(content, encoding, NodeList.class);
AssertionErrors.assertEquals("nodeCount for XPath " + this.expression, expectedCount,
(nodeList != null ? nodeList.getLength() : 0));
}
/**
* Apply the XPath expression and assert the resulting content with the
* given Hamcrest matcher.
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertString(byte[] content, @Nullable String encoding, Matcher<? super String> matcher)
throws Exception {
String actual = evaluateXpath(content, encoding, String.class);
MatcherAssert.assertThat("XPath " + this.expression, actual, matcher);
}
/**
* Apply the XPath expression and assert the resulting content as a String.
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertString(byte[] content, @Nullable String encoding, String expectedValue) throws Exception {
String actual = evaluateXpath(content, encoding, String.class);
AssertionErrors.assertEquals("XPath " + this.expression, expectedValue, actual);
}
/**
* Apply the XPath expression and assert the resulting content with the
* given Hamcrest matcher.
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertNumber(byte[] content, @Nullable String encoding, Matcher<? super Double> matcher) throws Exception {
Double actual = evaluateXpath(content, encoding, Double.class);
MatcherAssert.assertThat("XPath " + this.expression, actual, matcher);
}
/**
* Apply the XPath expression and assert the resulting content as a Double.
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertNumber(byte[] content, @Nullable String encoding, Double expectedValue) throws Exception {
Double actual = evaluateXpath(content, encoding, Double.class);
AssertionErrors.assertEquals("XPath " + this.expression, expectedValue, actual);
}
/**
* Apply the XPath expression and assert the resulting content as a Boolean.
* @throws Exception if content parsing or expression evaluation fails
*/
public void assertBoolean(byte[] content, @Nullable String encoding, boolean expectedValue) throws Exception {
String actual = evaluateXpath(content, encoding, String.class);
AssertionErrors.assertEquals("XPath " + this.expression, expectedValue, Boolean.parseBoolean(actual));
}
/**
* Evaluate the XPath and return the resulting value.
* @param content the content to evaluate against
* @param encoding the encoding to use (optionally)
* @param targetClass the target class, one of Number, String, Boolean,
* org.w3c.Node, or NodeList
* @throws Exception if content parsing or expression evaluation fails
* @since 5.1
*/
@Nullable
public <T> T evaluateXpath(byte[] content, @Nullable String encoding, Class<T> targetClass) throws Exception {
Document document = parseXmlByteArray(content, encoding);
return evaluateXpath(document, toQName(targetClass), targetClass);
}
/**
* Parse the given XML content to a {@link Document}.
* @param xml the content to parse
* @param encoding optional content encoding, if provided as metadata (e.g. in HTTP headers)
* @return the parsed document
*/
protected Document parseXmlByteArray(byte[] xml, @Nullable String encoding) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(this.hasNamespaces);
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
InputSource inputSource = new InputSource(new ByteArrayInputStream(xml));
if (StringUtils.hasText(encoding)) {
inputSource.setEncoding(encoding);
}
return documentBuilder.parse(inputSource);
}
/**
* Apply the XPath expression to given document.
* @throws XPathExpressionException if expression evaluation failed
*/
@SuppressWarnings("unchecked")
@Nullable
protected <T> T evaluateXpath(Document document, QName evaluationType, Class<T> expectedClass)
throws XPathExpressionException {
return (T) getXpathExpression().evaluate(document, evaluationType);
}
private <T> QName toQName(Class<T> expectedClass) {
QName evaluationType;
if (Number.class.isAssignableFrom(expectedClass)) {
evaluationType = XPathConstants.NUMBER;
}
else if (CharSequence.class.isAssignableFrom(expectedClass)) {
evaluationType = XPathConstants.STRING;
}
else if (Boolean.class.isAssignableFrom(expectedClass)) {
evaluationType = XPathConstants.BOOLEAN;
}
else if (Node.class.isAssignableFrom(expectedClass)) {
evaluationType = XPathConstants.NODE;
}
else if (NodeList.class.isAssignableFrom(expectedClass)) {
evaluationType = XPathConstants.NODESET;
}
else {
throw new IllegalArgumentException("Unexpected target class " + expectedClass + ". " +
"Supported: numbers, strings, boolean, and org.w3c.Node and NodeList");
}
return evaluationType;
}
}