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
FieldsModifier and TypeFieldDescriptionstatic final NamespaceContextNullNamespaceContext, used to document deliberate use of no namespace. -
Constructor Summary
ConstructorsConstructorDescriptionHasXPath(String xPathExpression, NamespaceContext namespaceContext, Matcher<String> valueMatcher) Constructor, best called from one of thehasXPathstatic factory methods.Constructor, best called from one of thehasXPathstatic factory methods. -
Method Summary
Modifier and TypeMethodDescriptionvoiddescribeTo(Description description) Generates a description of the object.Creates a matcher ofNodes that matches when the examined node contains a node at the specifiedxPath, with any content.hasXPath(String xPath, NamespaceContext namespaceContext) Creates a matcher ofNodes that matches when the examined node contains a node at the specifiedxPathwithin the specified namespace context, with any content.hasXPath(String xPath, NamespaceContext namespaceContext, Matcher<String> valueMatcher) Creates a matcher ofNodes that matches when the examined node has a value at the specifiedxPath, within the specifiednamespaceContext, that satisfies the specifiedvalueMatcher.Creates a matcher ofNodes that matches when the examined node has a value at the specifiedxPaththat satisfies the specifiedvalueMatcher.booleanmatchesSafely(Node item, Description mismatch) Subclasses should implement this.Methods inherited from class org.hamcrest.TypeSafeDiagnosingMatcher
describeMismatch, matchesMethods 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 thehasXPathstatic 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 thehasXPathstatic factory methods.- Parameters:
xPathExpression- the target xpathnamespaceContext- the namespace for matching nodesvalueMatcher- matcher for the expected value
-
-
Method Details
-
matchesSafely
Description copied from class:TypeSafeDiagnosingMatcherSubclasses should implement this. The item will already have been checked for the specific type and will never be null.- Specified by:
matchesSafelyin classTypeSafeDiagnosingMatcher<Node>- Parameters:
item- the item.mismatch- the mismatch description.- Returns:
- boolean true/false depending if item matches matcher.
-
describeTo
Description copied from interface:SelfDescribingGenerates 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 ofNodes that matches when the examined node has a value at the specifiedxPaththat 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 ofNodes 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 ofNodes 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 ofNodes that matches when the examined node contains a node at the specifiedxPathwithin 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.
-