Package org.xmlunit.matchers
Class EvaluateXPathMatcher
- java.lang.Object
-
- org.hamcrest.BaseMatcher<java.lang.Object>
-
- org.xmlunit.matchers.EvaluateXPathMatcher
-
- All Implemented Interfaces:
org.hamcrest.Matcher<java.lang.Object>
,org.hamcrest.SelfDescribing
public class EvaluateXPathMatcher extends org.hamcrest.BaseMatcher<java.lang.Object>
This HamcrestMatcher
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));
- Since:
- XMLUnit 2.1.0
-
-
Field Summary
Fields Modifier and Type Field Description private javax.xml.parsers.DocumentBuilderFactory
dbf
private java.util.Map<java.lang.String,java.lang.String>
prefix2Uri
private org.hamcrest.Matcher<java.lang.String>
valueMatcher
private java.lang.String
xPath
private javax.xml.xpath.XPathFactory
xpf
-
Constructor Summary
Constructors Constructor Description EvaluateXPathMatcher(java.lang.String xPath, org.hamcrest.Matcher<java.lang.String> valueMatcher)
Creates aEvaluateXPathMatcher
instance with the associated XPath expression and the value matcher corresponding to the XPath evaluation.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
describeMismatch(java.lang.Object object, org.hamcrest.Description mismatchDescription)
void
describeTo(org.hamcrest.Description description)
static EvaluateXPathMatcher
hasXPath(java.lang.String xPath, org.hamcrest.Matcher<java.lang.String> valueMatcher)
Creates a matcher that matches when the examined XML input has a value at the specifiedxPath
that satisfies the specifiedvalueMatcher
.boolean
matches(java.lang.Object object)
EvaluateXPathMatcher
withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)
Sets theDocumentBuilderFactory
to use when creating aDocument
from the XML input.EvaluateXPathMatcher
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.EvaluateXPathMatcher
withXPathFactory(javax.xml.xpath.XPathFactory f)
Sets theXPathFactory
to use.private java.lang.String
xPathEvaluate(java.lang.Object input)
Evaluates the provided XML input to the configuredxPath
field XPath expression.
-
-
-
Field Detail
-
xPath
private final java.lang.String xPath
-
valueMatcher
private final org.hamcrest.Matcher<java.lang.String> valueMatcher
-
dbf
private javax.xml.parsers.DocumentBuilderFactory dbf
-
xpf
private javax.xml.xpath.XPathFactory xpf
-
prefix2Uri
private java.util.Map<java.lang.String,java.lang.String> prefix2Uri
-
-
Constructor Detail
-
EvaluateXPathMatcher
public EvaluateXPathMatcher(java.lang.String xPath, org.hamcrest.Matcher<java.lang.String> valueMatcher)
Creates aEvaluateXPathMatcher
instance with the associated XPath expression and the value matcher corresponding to the XPath evaluation.- Parameters:
xPath
- xPath expressionvalueMatcher
- matcher for the value at the specified xpath
-
-
Method Detail
-
hasXPath
public static EvaluateXPathMatcher hasXPath(java.lang.String xPath, org.hamcrest.Matcher<java.lang.String> valueMatcher)
Creates a matcher that matches when the examined XML input has a value at the specifiedxPath
that satisfies the specifiedvalueMatcher
.For example:
assertThat(xml, hasXPath("//fruits/fruit/@name", equalTo("apple"))
- Parameters:
xPath
- the target xpathvalueMatcher
- matcher for the value at the specified xpath- Returns:
- the xpath matcher
-
withDocumentBuilderFactory
public EvaluateXPathMatcher withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)
Sets theDocumentBuilderFactory
to use when creating aDocument
from the XML input.- Since:
- XMLUnit 2.6.0
-
withXPathFactory
public EvaluateXPathMatcher withXPathFactory(javax.xml.xpath.XPathFactory f)
Sets theXPathFactory
to use.- Since:
- XMLUnit 2.6.1
-
matches
public boolean matches(java.lang.Object object)
-
describeTo
public void describeTo(org.hamcrest.Description description)
-
describeMismatch
public void describeMismatch(java.lang.Object object, org.hamcrest.Description mismatchDescription)
- Specified by:
describeMismatch
in interfaceorg.hamcrest.Matcher<java.lang.Object>
- Overrides:
describeMismatch
in classorg.hamcrest.BaseMatcher<java.lang.Object>
-
withNamespaceContext
public EvaluateXPathMatcher 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.- Parameters:
prefix2Uri
- prefix2Uri maps from prefix to namespace URI. It is used to resolve XML namespace prefixes in the XPath expression
-
xPathEvaluate
private java.lang.String xPathEvaluate(java.lang.Object input)
Evaluates the provided XML input to the configuredxPath
field XPath expression.- Parameters:
input
- an XML input- Returns:
- the result of the XPath evaluation
-
-