Class 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.
    Author:
    Joe Walnes, Steve Freeman
    • Field Summary

      Fields 
      Modifier and Type Field Description
      static javax.xml.namespace.NamespaceContext NO_NAMESPACE_CONTEXT  
    • Constructor Summary

      Constructors 
      Constructor Description
      HasXPath​(java.lang.String xPathExpression, javax.xml.namespace.NamespaceContext namespaceContext, Matcher<java.lang.String> valueMatcher)  
      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
      void describeTo​(Description description)
      Generates a description of the object.
      static Matcher<org.w3c.dom.Node> hasXPath​(java.lang.String xPath)
      Creates a matcher of Nodes that matches when the examined node contains a node at the specified xPath, with any content.
      static Matcher<org.w3c.dom.Node> hasXPath​(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext)
      Creates a matcher of Nodes that matches when the examined node contains a node at the specified xPath 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 of Nodes that matches when the examined node has a value at the specified xPath, within the specified namespaceContext, that satisfies the specified valueMatcher.
      static Matcher<org.w3c.dom.Node> hasXPath​(java.lang.String xPath, Matcher<java.lang.String> valueMatcher)
      Creates a matcher of Nodes that matches when the examined node has a value at the specified xPath that satisfies the specified valueMatcher.
      boolean matchesSafely​(org.w3c.dom.Node item, Description mismatch)
      Subclasses should implement this.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Field Detail

      • NO_NAMESPACE_CONTEXT

        public static final javax.xml.namespace.NamespaceContext NO_NAMESPACE_CONTEXT
    • 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 expression
        valueMatcher - Matcher to use at given XPath. May be null to specify that the XPath must exist but the value is irrelevant.
    • Method Detail

      • 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.
      • hasXPath

        public static Matcher<org.w3c.dom.Node> hasXPath​(java.lang.String xPath,
                                                         Matcher<java.lang.String> valueMatcher)
        Creates a matcher of 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 - the target xpath
        valueMatcher - 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 of 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 - the target xpath
        namespaceContext - the namespace for matching nodes
        valueMatcher - matcher for the value at the specified xpath
      • hasXPath

        public static Matcher<org.w3c.dom.Node> hasXPath​(java.lang.String xPath)
        Creates a matcher of 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 - the target xpath
      • hasXPath

        public static Matcher<org.w3c.dom.Node> hasXPath​(java.lang.String xPath,
                                                         javax.xml.namespace.NamespaceContext namespaceContext)
        Creates a matcher of 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 - the target xpath
        namespaceContext - the namespace for matching nodes