Class XhtmlMatchers


  • public final class XhtmlMatchers
    extends Object
    Convenient set of matchers for XHTML/XML content.

    For example:

     MatcherAssert.assertThat(
       "<root><a>hello</a></root>",
       XhtmlMatchers.hasXPath("/root/a[.='hello']")
     );
    Since:
    0.2.6
    • Method Detail

      • xhtml

        public static <T> Source xhtml​(T xhtml)
        Makes XHTML source presentable for testing.

        Useful method for assertions in unit tests. For example:

         MatcherAssert.assertThat(
           XhtmlMatchers.xhtml(dom_xml_element),
           XhtmlMatchers.hasXPath("/root/data")
         );

        The method understands different input types differently. For example, an InputStream will be read as a UTF-8 document, Reader will be read as a document, a Source will be used "as is", Node will be printed as a text, etc. The goal is to make any input type presentable as an XML document, as much as it is possible.

        Type Parameters:
        T - Type of source
        Parameters:
        xhtml - The source of data
        Returns:
        Renderable source
        Since:
        0.4.10
      • hasXPath

        public static <T> org.hamcrest.Matcher<T> hasXPath​(String query)
        Matches content against XPath query.
        Type Parameters:
        T - Type of XML content provided
        Parameters:
        query - The query
        Returns:
        Matcher suitable for JUnit/Hamcrest matching
      • hasXPath

        public static <T> org.hamcrest.Matcher<T> hasXPath​(String query,
                                                           Object... namespaces)
        Matches content against XPath query, with custom namespaces.

        Every namespace from the namespaces list will be assigned to its own prefix, in order of appearance. Start with 1. For example:

         MatcherAssert.assert(
           "<foo xmlns='my-namespace'></foo>",
           XhtmlMatchers.hasXPath("/ns1:foo", "my-namespace")
         );
        Type Parameters:
        T - Type of XML content provided
        Parameters:
        query - The query
        namespaces - List of namespaces
        Returns:
        Matcher suitable for JUnit/Hamcrest matching
      • hasXPath

        public static <T> org.hamcrest.Matcher<T> hasXPath​(String query,
                                                           NamespaceContext ctx)
        Matches content against XPath query, with custom context.
        Type Parameters:
        T - Type of XML content provided
        Parameters:
        query - The query
        ctx - The context
        Returns:
        Matcher suitable for JUnit/Hamcrest matching
      • hasXPaths

        public static <T> org.hamcrest.Matcher<T> hasXPaths​(String... xpaths)
        Matches content against list of XPaths.
        Type Parameters:
        T - Type of XML content provided
        Parameters:
        xpaths - The query
        Returns:
        Matcher suitable for JUnit/Hamcrest matching