/** * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node contains a node * at the specified <code>xPath</code>, with any content. * <p/> * For example: * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese"))</pre> * * @param xPath * the target xpath */ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath) { return org.hamcrest.xml.HasXPath.hasXPath(xPath); }
/** * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node contains a node * at the specified <code>xPath</code> within the specified namespace context, with any content. * <p/> * For example: * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese", myNs))</pre> * * @param xPath * the target xpath * @param namespaceContext * the namespace for matching nodes */ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext) { return org.hamcrest.xml.HasXPath.hasXPath(xPath, namespaceContext); }
/** * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node has a value at the * specified <code>xPath</code> that satisfies the specified <code>valueMatcher</code>. * <p/> * For example: * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese", equalTo("Cheddar")))</pre> * * @param xPath * the target xpath * @param valueMatcher * matcher for the value at the specified xpath */ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, org.hamcrest.Matcher<java.lang.String> valueMatcher) { return org.hamcrest.xml.HasXPath.hasXPath(xPath, valueMatcher); }
/** * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node contains a node * at the specified <code>xPath</code>, with any content. * For example: * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese"))</pre> * * @param xPath * the target xpath */ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath) { return org.hamcrest.xml.HasXPath.hasXPath(xPath); }
/** * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node contains a node * at the specified <code>xPath</code>, with any content. * For example: * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese"))</pre> * * @param xPath * the target xpath */ public static Matcher<Node> hasXPath(String xPath) { return hasXPath(xPath, NO_NAMESPACE_CONTEXT); }
/** * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node has a value at the * specified <code>xPath</code>, within the specified <code>namespaceContext</code>, that satisfies * the specified <code>valueMatcher</code>. * <p/> * For example: * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese", myNs, equalTo("Cheddar")))</pre> * * @param xPath * the target xpath * @param namespaceContext * the namespace for matching nodes * @param valueMatcher * matcher for the value at the specified xpath */ public static org.hamcrest.Matcher<org.w3c.dom.Node> hasXPath(java.lang.String xPath, javax.xml.namespace.NamespaceContext namespaceContext, org.hamcrest.Matcher<java.lang.String> valueMatcher) { return org.hamcrest.xml.HasXPath.hasXPath(xPath, namespaceContext, valueMatcher); }
/** * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node has a value at the * specified <code>xPath</code> that satisfies the specified <code>valueMatcher</code>. * For example: * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese", equalTo("Cheddar")))</pre> * * @param xPath * the target xpath * @param valueMatcher * matcher for the value at the specified xpath */ public static Matcher<Node> hasXPath(String xPath, Matcher<String> valueMatcher) { return hasXPath(xPath, NO_NAMESPACE_CONTEXT, valueMatcher); }
/** * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node contains a node * at the specified <code>xPath</code>, with any content. * <p/> * For example: * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese"))</pre> * * @param xPath * the target xpath */ @Factory public static Matcher<Node> hasXPath(String xPath) { return hasXPath(xPath, NO_NAMESPACE_CONTEXT); }
/** * Creates a matcher of {@link org.w3c.dom.Node}s that matches when the examined node has a value at the * specified <code>xPath</code> that satisfies the specified <code>valueMatcher</code>. * <p/> * For example: * <pre>assertThat(xml, hasXPath("/root/something[2]/cheese", equalTo("Cheddar")))</pre> * * @param xPath * the target xpath * @param valueMatcher * matcher for the value at the specified xpath */ @Factory public static Matcher<Node> hasXPath(String xPath, Matcher<String> valueMatcher) { return hasXPath(xPath, NO_NAMESPACE_CONTEXT, valueMatcher); }
@Test public void appliesMatcherToXPathInDocument() { assertMatches(hasXPath("/root/something[2]/cheese", equalTo("Cheddar")), xml); assertMatches(hasXPath("//something[1]/cheese", containsString("dam")), xml); assertMatches(hasXPath("//something[2]/cheese", not(containsString("dam"))), xml); assertMatches(hasXPath("/root/@type", equalTo("food")), xml); assertMatches(hasXPath("//something[@id='b']/cheese", equalTo("Cheddar")), xml); assertMatches(hasXPath("//something[@id='b']/cheese"), xml); }
@Test public void throwsIllegalArgumentExceptionIfGivenIllegalExpression() { try { hasXPath("\\g:dfgd::DSgf/root/something[2]/cheese", equalTo("blah")); fail("Expected exception"); } catch (IllegalArgumentException expectedException) { // expected exception } }
@Test public void describesIncorrectNodeValueMismatch() { assertMismatchDescription("was \"Edam\"", hasXPath("//something[1]/cheese", equalTo("parmesan")), xml); }
@Test public void matchesEmptyElementInNamespace() { assertMatches(hasXPath("//cheese:emptySomething", ns), xml); }
@Test public void describesMissingNodeMismatch() { assertMismatchDescription("xpath returned no results.", hasXPath("//honky"), xml); }
@Test public void matchesEmptyElement() { assertMatches(hasXPath("//emptySomething"), xml); }
@Test public void copesWithNullsAndUnknownTypes() { Matcher<Node> matcher = hasXPath("//irrelevant"); assertNullSafe(matcher); assertUnknownTypeSafe(matcher); }