Top Description Fields Constructors Methods
org.hamcrest.xml

public Class HasXPath

extends TypeSafeDiagnosingMatcher<Node>
Class Inheritance
Imports
org.hamcrest.Condition, .Description, .Matcher, .TypeSafeDiagnosingMatcher, org.hamcrest.core.IsAnything, org.w3c.dom.Node, javax.xml.namespace.NamespaceContext, .QName, javax.xml.xpath.*

Applies a Matcher to a given XML Node in an existing XML Node tree, specified by an XPath expression.
Authors
Joe Walnes, Steve Freeman

Field Summary

Modifier and TypeField and Description
private final XPathExpression
private final QName
public static final NamespaceContext
private static final Condition.Step<Object, String>
private final Matcher<String>
private static final IsAnything<String>
private final String

Constructor Summary

AccessConstructor and Description
public
HasXPath(String
XPath expression.
xPathExpression
,
Matcher<String>
Matcher to use at given XPath. May be null to specify that the XPath must exist but the value is irrelevant.
valueMatcher
)

public
HasXPath(String
XPath expression.
xPathExpression
,
NamespaceContext
Resolves XML namespace prefixes in the XPath expression
namespaceContext
,
Matcher<String>
Matcher to use at given XPath. May be null to specify that the XPath must exist but the value is irrelevant.
valueMatcher
)

private
HasXPath(String xPathExpression, NamespaceContext namespaceContext, Matcher<String> valueMatcher, QName mode)

Method Summary

Modifier and TypeMethod and Description
private static XPathExpression
compiledXPath(String xPathExpression, NamespaceContext namespaceContext)

public void
describeTo(Description
The description to be built or appended to.
description
)

Implements org.hamcrest.SelfDescribing.describeTo.

Generates a description of the object.
private Condition<Object>
evaluated(Node item, Description mismatch)

public static Matcher<Node>
hasXPath(String
the target xpath
xPath
,
Matcher<String>
matcher for the value at the specified xpath
valueMatcher
)

Creates a matcher of org.w3c.dom.Nodes that matches when the examined node has a value at the specified xPath that satisfies the specified valueMatcher.

public static Matcher<Node>
hasXPath(String
the target xpath
xPath
,
NamespaceContext
the namespace for matching nodes
namespaceContext
,
Matcher<String>
matcher for the value at the specified xpath
valueMatcher
)

Creates a matcher of org.w3c.dom.Nodes that matches when the examined node has a value at the specified xPath, within the specified namespaceContext, that satisfies the specified valueMatcher.

public static Matcher<Node>
hasXPath(String
the target xpath
xPath
)

Creates a matcher of org.w3c.dom.Nodes that matches when the examined node contains a node at the specified xPath, with any content.

public static Matcher<Node>
hasXPath(String
the target xpath
xPath
,
NamespaceContext
the namespace for matching nodes
namespaceContext
)

Creates a matcher of org.w3c.dom.Nodes that matches when the examined node contains a node at the specified xPath within the specified namespace context, with any content.

public boolean
matchesSafely(Node item, Description mismatch)

Implements abstract org.hamcrest.TypeSafeDiagnosingMatcher.matchesSafely.

Subclasses should implement this.
private static Condition.Step<Object, String>
Inherited from org.hamcrest.TypeSafeDiagnosingMatcher:
describeMismatchmatches

Field Detail

compiledXPathback to summary
private final XPathExpression compiledXPath
evaluationModeback to summary
private final QName evaluationMode
NO_NAMESPACE_CONTEXTback to summary
public static final NamespaceContext NO_NAMESPACE_CONTEXT
NODE_EXISTSback to summary
private static final Condition.Step<Object, String> NODE_EXISTS
valueMatcherback to summary
private final Matcher<String> valueMatcher
WITH_ANY_CONTENTback to summary
private static final IsAnything<String> WITH_ANY_CONTENT
xpathStringback to summary
private final String xpathString

Constructor Detail

HasXPathback to summary
public HasXPath(String xPathExpression, Matcher<String> valueMatcher)
Parameters
xPathExpression:String

XPath expression.

valueMatcher:Matcher<String>

Matcher to use at given XPath. May be null to specify that the XPath must exist but the value is irrelevant.

HasXPathback to summary
public HasXPath(String xPathExpression, NamespaceContext namespaceContext, Matcher<String> valueMatcher)
Parameters
xPathExpression:String

XPath expression.

namespaceContext:NamespaceContext

Resolves XML namespace prefixes in the XPath expression

valueMatcher:Matcher<String>

Matcher to use at given XPath. May be null to specify that the XPath must exist but the value is irrelevant.

HasXPathback to summary
private HasXPath(String xPathExpression, NamespaceContext namespaceContext, Matcher<String> valueMatcher, QName mode)

Method Detail

compiledXPathback to summary
private static XPathExpression compiledXPath(String xPathExpression, NamespaceContext namespaceContext)
describeToback to summary
public void describeTo(Description description)

Implements org.hamcrest.SelfDescribing.describeTo.

Doc from org.hamcrest.SelfDescribing.describeTo.

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:Description

The description to be built or appended to.

Annotations
@Override
evaluatedback to summary
private Condition<Object> evaluated(Node item, Description mismatch)
hasXPathback to summary
public static Matcher<Node> hasXPath(String xPath, Matcher<String> valueMatcher)

Creates a matcher of org.w3c.dom.Nodes that matches when the examined node has a value at the specified xPath that satisfies the specified valueMatcher. For example:

assertThat(xml, hasXPath("/root/something[2]/cheese", equalTo("Cheddar")))
Parameters
xPath:String

the target xpath

valueMatcher:Matcher<String>

matcher for the value at the specified xpath

hasXPathback to summary
public static Matcher<Node> hasXPath(String xPath, NamespaceContext namespaceContext, Matcher<String> valueMatcher)

Creates a matcher of org.w3c.dom.Nodes that matches when the examined node has a value at the specified xPath, within the specified namespaceContext, that satisfies the specified valueMatcher. For example:

assertThat(xml, hasXPath("/root/something[2]/cheese", myNs, equalTo("Cheddar")))
Parameters
xPath:String

the target xpath

namespaceContext:NamespaceContext

the namespace for matching nodes

valueMatcher:Matcher<String>

matcher for the value at the specified xpath

hasXPathback to summary
public static Matcher<Node> hasXPath(String xPath)

Creates a matcher of org.w3c.dom.Nodes that matches when the examined node contains a node at the specified xPath, with any content. For example:

assertThat(xml, hasXPath("/root/something[2]/cheese"))
Parameters
xPath:String

the target xpath

hasXPathback to summary
public static Matcher<Node> hasXPath(String xPath, NamespaceContext namespaceContext)

Creates a matcher of org.w3c.dom.Nodes that matches when the examined node contains a node at the specified xPath within the specified namespace context, with any content. For example:

assertThat(xml, hasXPath("/root/something[2]/cheese", myNs))
Parameters
xPath:String

the target xpath

namespaceContext:NamespaceContext

the namespace for matching nodes

matchesSafelyback to summary
public boolean matchesSafely(Node item, Description mismatch)

Implements abstract org.hamcrest.TypeSafeDiagnosingMatcher.matchesSafely.

Doc from org.hamcrest.TypeSafeDiagnosingMatcher.matchesSafely.

Subclasses should implement this. The item will already have been checked for the specific type and will never be null.

Annotations
@Override
nodeExistsback to summary
private static Condition.Step<Object, String> nodeExists()