public class HasXPathMatcher
extends org.hamcrest.BaseMatcher<java.lang.Object>
Matcher
verifies whether the provided XPath expression corresponds to at least
one element in the provided object.
All types which are supported by Input.from(Object)
can be used as input for the object
against the matcher is evaluated.
Simple Example
final String xml = "<a><b attr=\"abc\"></b></a>"; assertThat(xml, hasXPath("//a/b/@attr")); assertThat(xml, not(hasXPath("//a/b/c")));
Example with namespace mapping
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<feed xmlns=\"http://www.w3.org/2005/Atom\">" + " <title>title</title>" + " <entry>" + " <title>title1</title>" + " <id>id1</id>" + " </entry>" + "</feed>"; HashMap<String, String> prefix2Uri = new HashMap<String, String>(); prefix2Uri.put("atom", "http://www.w3.org/2005/Atom"); assertThat(xmlRootElement, hasXPath("//atom:feed/atom:entry/atom:id").withNamespaceContext(prefix2Uri));
Modifier and Type | Field and Description |
---|---|
private javax.xml.parsers.DocumentBuilderFactory |
dbf |
private java.util.Map<java.lang.String,java.lang.String> |
prefix2Uri |
private java.lang.String |
xPath |
private javax.xml.xpath.XPathFactory |
xpf |
Constructor and Description |
---|
HasXPathMatcher(java.lang.String xPath)
Creates a
HasXPathMatcher instance with the associated XPath expression. |
Modifier and Type | Method and Description |
---|---|
void |
describeMismatch(java.lang.Object item,
org.hamcrest.Description mismatchDescription) |
void |
describeTo(org.hamcrest.Description description) |
static HasXPathMatcher |
hasXPath(java.lang.String xPath)
Creates a matcher that matches when the examined XML input has at least one node
corresponding to the specified
xPath . |
boolean |
matches(java.lang.Object object) |
HasXPathMatcher |
withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)
Sets the
DocumentBuilderFactory to use when creating a
Document from the XML input. |
HasXPathMatcher |
withNamespaceContext(java.util.Map<java.lang.String,java.lang.String> prefix2Uri)
Utility method used for creating a namespace context mapping to be used in XPath matching.
|
HasXPathMatcher |
withXPathFactory(javax.xml.xpath.XPathFactory f)
Sets the
XPathFactory to use. |
private java.lang.String xPath
private javax.xml.parsers.DocumentBuilderFactory dbf
private javax.xml.xpath.XPathFactory xpf
private java.util.Map<java.lang.String,java.lang.String> prefix2Uri
public HasXPathMatcher(java.lang.String xPath)
HasXPathMatcher
instance with the associated XPath expression.xPath
- xPath expressionpublic HasXPathMatcher withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)
DocumentBuilderFactory
to use when creating a
Document
from the XML input.public HasXPathMatcher withXPathFactory(javax.xml.xpath.XPathFactory f)
XPathFactory
to use.public boolean matches(java.lang.Object object)
public void describeTo(org.hamcrest.Description description)
public void describeMismatch(java.lang.Object item, org.hamcrest.Description mismatchDescription)
describeMismatch
in interface org.hamcrest.Matcher<java.lang.Object>
describeMismatch
in class org.hamcrest.BaseMatcher<java.lang.Object>
public static HasXPathMatcher hasXPath(java.lang.String xPath)
xPath
.
For example:
assertThat(xml, hasXPath("/root/cars[0]/audi"))
xPath
- the target xpathpublic HasXPathMatcher withNamespaceContext(java.util.Map<java.lang.String,java.lang.String> prefix2Uri)
prefix2Uri
- prefix2Uri maps from prefix to namespace URI. It is used to resolve
XML namespace prefixes in the XPath expression