Package org.xmlunit.matchers
Class HasXPathMatcher
- java.lang.Object
-
- org.hamcrest.BaseMatcher<java.lang.Object>
-
- org.xmlunit.matchers.HasXPathMatcher
-
- All Implemented Interfaces:
org.hamcrest.Matcher<java.lang.Object>,org.hamcrest.SelfDescribing
public class HasXPathMatcher extends org.hamcrest.BaseMatcher<java.lang.Object>This HamcrestMatcherverifies 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));- Since:
- XMLUnit 2.1.0
-
-
Field Summary
Fields Modifier and Type Field Description private javax.xml.parsers.DocumentBuilderFactorydbfprivate java.util.Map<java.lang.String,java.lang.String>prefix2Uriprivate java.lang.StringxPathprivate javax.xml.xpath.XPathFactoryxpf
-
Constructor Summary
Constructors Constructor Description HasXPathMatcher(java.lang.String xPath)Creates aHasXPathMatcherinstance with the associated XPath expression.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description voiddescribeMismatch(java.lang.Object item, org.hamcrest.Description mismatchDescription)voiddescribeTo(org.hamcrest.Description description)static HasXPathMatcherhasXPath(java.lang.String xPath)Creates a matcher that matches when the examined XML input has at least one node corresponding to the specifiedxPath.booleanmatches(java.lang.Object object)HasXPathMatcherwithDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)Sets theDocumentBuilderFactoryto use when creating aDocumentfrom the XML input.HasXPathMatcherwithNamespaceContext(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.HasXPathMatcherwithXPathFactory(javax.xml.xpath.XPathFactory f)Sets theXPathFactoryto use.
-
-
-
Constructor Detail
-
HasXPathMatcher
public HasXPathMatcher(java.lang.String xPath)
Creates aHasXPathMatcherinstance with the associated XPath expression.- Parameters:
xPath- xPath expression
-
-
Method Detail
-
withDocumentBuilderFactory
public HasXPathMatcher withDocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory f)
Sets theDocumentBuilderFactoryto use when creating aDocumentfrom the XML input.- Since:
- XMLUnit 2.6.0
-
withXPathFactory
public HasXPathMatcher withXPathFactory(javax.xml.xpath.XPathFactory f)
Sets theXPathFactoryto 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 item, org.hamcrest.Description mismatchDescription)- Specified by:
describeMismatchin interfaceorg.hamcrest.Matcher<java.lang.Object>- Overrides:
describeMismatchin classorg.hamcrest.BaseMatcher<java.lang.Object>
-
hasXPath
public 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 specifiedxPath.For example:
assertThat(xml, hasXPath("/root/cars[0]/audi"))- Parameters:
xPath- the target xpath- Returns:
- the xpath matcher
-
withNamespaceContext
public 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.- Parameters:
prefix2Uri- prefix2Uri maps from prefix to namespace URI. It is used to resolve XML namespace prefixes in the XPath expression
-
-