public class EvaluateXPathMatcher extends org.hamcrest.BaseMatcher<Object>
Matcher
verifies whether the evaluation of the provided XPath expression
corresponds to the value matcher specified for the provided input XML object.
All types which are supported by Input.from(Object)
can be used as input for the XML object
against the matcher is evaluated.
Simple Example
final String xml = "<a><b attr=\"abc\"></b></a>"; assertThat(xml, hasXPath("//a/b/@attr", equalTo("abc"))); assertThat(xml, hasXPath("count(//a/b/c)", equalTo("0")));
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(xml, hasXPath("//atom:feed/atom:entry/atom:id/text()", equalTo("id1")) .withNamespaceContext(prefix2Uri));
Constructor and Description |
---|
EvaluateXPathMatcher(String xPath,
org.hamcrest.Matcher<String> valueMatcher)
Creates a
EvaluateXPathMatcher instance with the associated XPath expression and
the value matcher corresponding to the XPath evaluation. |
Modifier and Type | Method and Description |
---|---|
void |
describeMismatch(Object object,
org.hamcrest.Description mismatchDescription) |
void |
describeTo(org.hamcrest.Description description) |
static EvaluateXPathMatcher |
hasXPath(String xPath,
org.hamcrest.Matcher<String> valueMatcher)
Creates a matcher that matches when the examined XML input has a value at the
specified
xPath that satisfies the specified valueMatcher . |
boolean |
matches(Object object) |
EvaluateXPathMatcher |
withDocumentBuilderFactory(DocumentBuilderFactory f)
Sets the
DocumentBuilderFactory to use when creating a
Document from the XML input. |
EvaluateXPathMatcher |
withNamespaceContext(Map<String,String> prefix2Uri)
Utility method used for creating a namespace context mapping to be used in XPath matching.
|
EvaluateXPathMatcher |
withXPathFactory(XPathFactory f)
Sets the
XPathFactory to use. |
public EvaluateXPathMatcher(String xPath, org.hamcrest.Matcher<String> valueMatcher)
EvaluateXPathMatcher
instance with the associated XPath expression and
the value matcher corresponding to the XPath evaluation.xPath
- xPath expressionvalueMatcher
- matcher for the value at the specified xpathpublic static EvaluateXPathMatcher hasXPath(String xPath, org.hamcrest.Matcher<String> valueMatcher)
xPath
that satisfies the specified valueMatcher
.
For example:
assertThat(xml, hasXPath("//fruits/fruit/@name", equalTo("apple"))
xPath
- the target xpathvalueMatcher
- matcher for the value at the specified xpathpublic EvaluateXPathMatcher withDocumentBuilderFactory(DocumentBuilderFactory f)
DocumentBuilderFactory
to use when creating a
Document
from the XML input.public EvaluateXPathMatcher withXPathFactory(XPathFactory f)
XPathFactory
to use.public boolean matches(Object object)
public void describeTo(org.hamcrest.Description description)
public void describeMismatch(Object object, org.hamcrest.Description mismatchDescription)
public EvaluateXPathMatcher withNamespaceContext(Map<String,String> prefix2Uri)
prefix2Uri
- prefix2Uri maps from prefix to namespace URI. It is used to resolve
XML namespace prefixes in the XPath expressionCopyright © 2001–2020 XMLUnit. All rights reserved.