Package org.hamcrest.xml
Class HasXPath
- All Implemented Interfaces:
Matcher<Node>
,SelfDescribing
Applies a Matcher to a given XML Node in an existing XML Node tree, specified by an XPath expression.
-
Field Summary
Modifier and TypeFieldDescriptionstatic final NamespaceContext
NullNamespaceContext
, used to document deliberate use of no namespace. -
Constructor Summary
ConstructorDescriptionHasXPath
(String xPathExpression, NamespaceContext namespaceContext, Matcher<String> valueMatcher) Constructor, best called from one of thehasXPath
static factory methods.Constructor, best called from one of thehasXPath
static factory methods. -
Method Summary
Modifier and TypeMethodDescriptionvoid
describeTo
(Description description) Generates a description of the object.Creates a matcher ofNode
s that matches when the examined node contains a node at the specifiedxPath
, with any content.hasXPath
(String xPath, 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.hasXPath
(String xPath, NamespaceContext namespaceContext, Matcher<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
.Creates a matcher ofNode
s that matches when the examined node has a value at the specifiedxPath
that satisfies the specifiedvalueMatcher
.boolean
matchesSafely
(Node item, Description mismatch) Subclasses should implement this.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 Details
-
NO_NAMESPACE_CONTEXT
NullNamespaceContext
, used to document deliberate use of no namespace.
-
-
Constructor Details
-
HasXPath
Constructor, best called from one of thehasXPath
static factory methods.- Parameters:
xPathExpression
- the target xpathvalueMatcher
- matcher for the expected value
-
HasXPath
public HasXPath(String xPathExpression, NamespaceContext namespaceContext, Matcher<String> valueMatcher) Constructor, best called from one of thehasXPath
static factory methods.- Parameters:
xPathExpression
- the target xpathnamespaceContext
- the namespace for matching nodesvalueMatcher
- matcher for the expected value
-
-
Method Details
-
matchesSafely
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<Node>
- Parameters:
item
- the item.mismatch
- the mismatch description.- Returns:
- boolean true/false depending if item matches matcher.
-
describeTo
Description copied from interface:SelfDescribing
Generates a description of the object. The description may be part of 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.
-
hasXPath
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- Returns:
- The matcher.
-
hasXPath
public static Matcher<Node> hasXPath(String xPath, NamespaceContext namespaceContext, Matcher<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- Returns:
- The matcher.
-
hasXPath
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- Returns:
- The matcher.
-
hasXPath
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- Returns:
- The matcher.
-