Skip to content

Commit dc682b9

Browse files
author
joeretro
committed
Added XPath matcher. assertThat(xmlDoc, hasXPath("/some/thing[1]", eq("stuff"))
1 parent 3657b22 commit dc682b9

2 files changed

Lines changed: 140 additions & 0 deletions

File tree

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.hamcrest.xml;
2+
3+
import org.hamcrest.Description;
4+
import org.hamcrest.Factory;
5+
import org.hamcrest.Matcher;
6+
import org.w3c.dom.Node;
7+
8+
import javax.xml.xpath.XPath;
9+
import javax.xml.xpath.XPathConstants;
10+
import javax.xml.xpath.XPathExpression;
11+
import javax.xml.xpath.XPathExpressionException;
12+
import javax.xml.xpath.XPathFactory;
13+
14+
/**
15+
* Applies a Matcher to a given XML Node in an existing XML Node tree, specified by an XPath expression.
16+
*
17+
* @author Joe Walnes
18+
*/
19+
public class HasXPath implements Matcher<Node> {
20+
21+
private final Matcher<String> valueMatcher;
22+
private final XPathExpression compiledXPath;
23+
private final String xpathString;
24+
25+
/**
26+
* @param xPathExpression XPath expression.
27+
* @param valueMatcher Matcher to use at given XPath.
28+
* May be null to specify that the XPath must exist but the value is irrelevant.
29+
*/
30+
public HasXPath(String xPathExpression, Matcher<String> valueMatcher) {
31+
try {
32+
XPath xPath = XPathFactory.newInstance().newXPath();
33+
compiledXPath = xPath.compile(xPathExpression);
34+
this.xpathString = xPathExpression;
35+
this.valueMatcher = valueMatcher;
36+
} catch (XPathExpressionException e) {
37+
throw new IllegalArgumentException("Invalid XPath : " + xPathExpression, e);
38+
}
39+
}
40+
41+
public boolean match(Node item) {
42+
try {
43+
String result = (String) compiledXPath.evaluate(item, XPathConstants.STRING);
44+
if (result == null) {
45+
return false;
46+
} else if (valueMatcher == null) {
47+
return !result.equals("");
48+
} else {
49+
return valueMatcher.match(result);
50+
}
51+
} catch (XPathExpressionException e) {
52+
return false;
53+
}
54+
}
55+
56+
public void describeTo(Description description) {
57+
description.appendText("an XML document with XPath ").appendText(xpathString);
58+
if (valueMatcher != null) {
59+
description.appendText(" ");
60+
valueMatcher.describeTo(description);
61+
}
62+
}
63+
64+
@Factory
65+
public static Matcher<Node> hasXPath(String xPath, Matcher<String> valueMatcher) {
66+
return new HasXPath(xPath, valueMatcher);
67+
}
68+
69+
@Factory
70+
public static Matcher<Node> hasXPath(String xPath) {
71+
return hasXPath(xPath, null);
72+
}
73+
74+
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package org.hamcrest.xml;
2+
3+
import org.hamcrest.AbstractMatcherTest;
4+
import static org.hamcrest.MatcherAssert.assertThat;
5+
import static org.hamcrest.core.IsEqual.eq;
6+
import static org.hamcrest.core.IsNot.not;
7+
import static org.hamcrest.text.StringContains.containsString;
8+
import static org.hamcrest.xml.HasXPath.hasXPath;
9+
import org.w3c.dom.Document;
10+
11+
import javax.xml.parsers.DocumentBuilder;
12+
import javax.xml.parsers.DocumentBuilderFactory;
13+
import java.io.ByteArrayInputStream;
14+
15+
/**
16+
* @author Joe Walnes
17+
*/
18+
public class HasXPathTest extends AbstractMatcherTest {
19+
private Document xml;
20+
21+
protected void setUp() throws Exception {
22+
super.setUp();
23+
xml = parse(""
24+
+ "<root type='food'>\n"
25+
+ " <something id='a'><cheese>Edam</cheese></something>\n"
26+
+ " <something id='b'><cheese>Cheddar</cheese></something>\n"
27+
+ "</root>\n"
28+
);
29+
}
30+
31+
public void testAppliesMatcherToXPathInDocument() throws Exception {
32+
assertThat(xml, hasXPath("/root/something[2]/cheese", eq("Cheddar")));
33+
assertThat(xml, hasXPath("//something[1]/cheese", containsString("dam")));
34+
assertThat(xml, hasXPath("//something[2]/cheese", not(containsString("dam"))));
35+
assertThat(xml, hasXPath("/root/@type", eq("food")));
36+
assertThat(xml, hasXPath("//something[@id='b']/cheese", eq("Cheddar")));
37+
assertThat(xml, hasXPath("//something[@id='b']/cheese"));
38+
}
39+
40+
public void testFailsIfNodeIsMissing() throws Exception {
41+
assertThat(xml, not(hasXPath("/root/something[3]/cheese", eq("Cheddar"))));
42+
assertThat(xml, not(hasXPath("//something[@id='c']/cheese")));
43+
}
44+
45+
public void testThrowsIllegalArgumentExceptionIfGivenIllegalExpression() {
46+
try {
47+
hasXPath("\\g:dfgd::DSgf/root/something[2]/cheese", eq("blah"));
48+
fail("Expected exception");
49+
} catch (IllegalArgumentException expectedException) {
50+
// expected exception
51+
}
52+
}
53+
54+
public void testDescribesItself() throws Exception {
55+
assertDescription("an XML document with XPath /some/path eq(\"Cheddar\")",
56+
hasXPath("/some/path", eq("Cheddar")));
57+
assertDescription("an XML document with XPath /some/path",
58+
hasXPath("/some/path"));
59+
}
60+
61+
private Document parse(String xml) throws Exception {
62+
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
63+
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
64+
return documentBuilder.parse(new ByteArrayInputStream(xml.getBytes()));
65+
}
66+
}

0 commit comments

Comments
 (0)