Package org.hamcrest.xml
Class HasXPath
- java.lang.Object
-
- org.hamcrest.BaseMatcher<T>
-
- org.hamcrest.TypeSafeDiagnosingMatcher<org.w3c.dom.Node>
-
- org.hamcrest.xml.HasXPath
-
- All Implemented Interfaces:
Matcher<org.w3c.dom.Node>
,SelfDescribing
public class HasXPath extends TypeSafeDiagnosingMatcher<org.w3c.dom.Node>
Applies a Matcher to a given XML Node in an existing XML Node tree, specified by an XPath expression.
-
-
Field Summary
Fields Modifier and Type Field Description private javax.xml.xpath.XPathExpression
compiledXPath
private javax.xml.namespace.QName
evaluationMode
static javax.xml.namespace.NamespaceContext
NO_NAMESPACE_CONTEXT
private static Condition.Step<java.lang.Object,java.lang.String>
NODE_EXISTS
private Matcher<java.lang.String>
valueMatcher
private static IsAnything<java.lang.String>
WITH_ANY_CONTENT
private java.lang.String
xpathString
-
Constructor Summary
Constructors Modifier Constructor Description HasXPath(java.lang.String xPathExpression, javax.xml.namespace.NamespaceContext namespaceContext, Matcher<java.lang.String> valueMatcher)
private
HasXPath(java.lang.String xPathExpression, javax.xml.namespace.NamespaceContext namespaceContext, Matcher<java.lang.String> valueMatcher, javax.xml.namespace.QName mode)
HasXPath(java.lang.String xPathExpression, Matcher<java.lang.String> valueMatcher)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description private static javax.xml.xpath.XPathExpression
compiledXPath(java.lang.String xPathExpression, javax.xml.namespace.NamespaceContext namespaceContext)
void
describeTo(Description description)
Generates a description of the object.private Condition<java.lang.Object>
evaluated(org.w3c.dom.Node item, Description mismatch)
static Matcher<org.w3c.dom.Node>
hasXPath(java.lang.String xPath)
Creates a matcher ofNode
s that matches when the examined node contains a node at the specifiedxPath
, with any content.static Matcher<org.w3c.dom.Node>
hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext)
Creates a matcher ofNode
s that matches when the examined node contains a node at the specifiedxPath
within the specified namespace context, with any content.static Matcher<org.w3c.dom.Node>
hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, Matcher<java.lang.String> valueMatcher)
Creates a matcher ofNode
s that matches when the examined node has a value at the specifiedxPath
, within the specifiednamespaceContext
, that satisfies the specifiedvalueMatcher
.static Matcher<org.w3c.dom.Node>
hasXPath(java.lang.String xPath, Matcher<java.lang.String> valueMatcher)
Creates a matcher ofNode
s that matches when the examined node has a value at the specifiedxPath
that satisfies the specifiedvalueMatcher
.boolean
matchesSafely(org.w3c.dom.Node item, Description mismatch)
Subclasses should implement this.private static Condition.Step<java.lang.Object,java.lang.String>
nodeExists()
-
Methods inherited from class org.hamcrest.TypeSafeDiagnosingMatcher
describeMismatch, matches
-
Methods inherited from class org.hamcrest.BaseMatcher
_dont_implement_Matcher___instead_extend_BaseMatcher_, isNotNull, toString
-
-
-
-
Field Detail
-
NO_NAMESPACE_CONTEXT
public static final javax.xml.namespace.NamespaceContext NO_NAMESPACE_CONTEXT
-
WITH_ANY_CONTENT
private static final IsAnything<java.lang.String> WITH_ANY_CONTENT
-
NODE_EXISTS
private static final Condition.Step<java.lang.Object,java.lang.String> NODE_EXISTS
-
valueMatcher
private final Matcher<java.lang.String> valueMatcher
-
compiledXPath
private final javax.xml.xpath.XPathExpression compiledXPath
-
xpathString
private final java.lang.String xpathString
-
evaluationMode
private final javax.xml.namespace.QName evaluationMode
-
-
Constructor Detail
-
HasXPath
public HasXPath(java.lang.String xPathExpression, Matcher<java.lang.String> valueMatcher)
- Parameters:
xPathExpression
- XPath expression.valueMatcher
- Matcher to use at given XPath. May be null to specify that the XPath must exist but the value is irrelevant.
-
HasXPath
public HasXPath(java.lang.String xPathExpression, javax.xml.namespace.NamespaceContext namespaceContext, Matcher<java.lang.String> valueMatcher)
- Parameters:
xPathExpression
- XPath expression.namespaceContext
- Resolves XML namespace prefixes in the XPath expressionvalueMatcher
- Matcher to use at given XPath. May be null to specify that the XPath must exist but the value is irrelevant.
-
HasXPath
private HasXPath(java.lang.String xPathExpression, javax.xml.namespace.NamespaceContext namespaceContext, Matcher<java.lang.String> valueMatcher, javax.xml.namespace.QName mode)
-
-
Method Detail
-
matchesSafely
public boolean matchesSafely(org.w3c.dom.Node item, Description mismatch)
Description copied from class:TypeSafeDiagnosingMatcher
Subclasses should implement this. The item will already have been checked for the specific type and will never be null.- Specified by:
matchesSafely
in classTypeSafeDiagnosingMatcher<org.w3c.dom.Node>
-
describeTo
public void describeTo(Description description)
Description copied from interface:SelfDescribing
Generates a description of the object. The description may be part of a a description of a larger object of which this is just a component, so it should be worded appropriately.- Parameters:
description
- The description to be built or appended to.
-
evaluated
private Condition<java.lang.Object> evaluated(org.w3c.dom.Node item, Description mismatch)
-
nodeExists
private static Condition.Step<java.lang.Object,java.lang.String> nodeExists()
-
compiledXPath
private static javax.xml.xpath.XPathExpression compiledXPath(java.lang.String xPathExpression, javax.xml.namespace.NamespaceContext namespaceContext)
-
hasXPath
public static Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, Matcher<java.lang.String> valueMatcher)
Creates a matcher ofNode
s that matches when the examined node has a value at the specifiedxPath
that satisfies the specifiedvalueMatcher
. For example:assertThat(xml, hasXPath("/root/something[2]/cheese", equalTo("Cheddar")))
- Parameters:
xPath
- the target xpathvalueMatcher
- matcher for the value at the specified xpath
-
hasXPath
public static Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, Matcher<java.lang.String> valueMatcher)
Creates a matcher ofNode
s that matches when the examined node has a value at the specifiedxPath
, within the specifiednamespaceContext
, that satisfies the specifiedvalueMatcher
. For example:assertThat(xml, hasXPath("/root/something[2]/cheese", myNs, equalTo("Cheddar")))
- Parameters:
xPath
- the target xpathnamespaceContext
- the namespace for matching nodesvalueMatcher
- matcher for the value at the specified xpath
-
hasXPath
public static Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath)
Creates a matcher ofNode
s that matches when the examined node contains a node at the specifiedxPath
, with any content. For example:assertThat(xml, hasXPath("/root/something[2]/cheese"))
- Parameters:
xPath
- the target xpath
-
hasXPath
public static Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext)
Creates a matcher ofNode
s that matches when the examined node contains a node at the specifiedxPath
within the specified namespace context, with any content. For example:assertThat(xml, hasXPath("/root/something[2]/cheese", myNs))
- Parameters:
xPath
- the target xpathnamespaceContext
- the namespace for matching nodes
-
-