Skip to content

fixed Matcher.hasXpath against multiple element #337

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 39 additions & 6 deletions hamcrest/src/main/java/org/hamcrest/xml/HasXPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,18 @@
import org.hamcrest.TypeSafeDiagnosingMatcher;
import org.hamcrest.core.IsAnything;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import javax.xml.xpath.*;

import java.util.ArrayList;
import java.util.List;

import static javax.xml.xpath.XPathConstants.NODESET;
import static javax.xml.xpath.XPathConstants.STRING;
import static org.hamcrest.Condition.matched;
import static org.hamcrest.Condition.notMatched;
import static org.hamcrest.Condition.*;

/**
* Applies a Matcher to a given XML Node in an existing XML Node tree, specified by an XPath expression.
Expand Down Expand Up @@ -58,9 +62,22 @@ private HasXPath(String xPathExpression, NamespaceContext namespaceContext, Matc

@Override
public boolean matchesSafely(Node item, Description mismatch) {
return evaluated(item, mismatch)
.and(NODE_EXISTS)
.matching(valueMatcher);
if (this.evaluationMode == NODESET)
{
List<Condition<Object>> match_list = evaluatedList(item, mismatch);

for (Condition<Object> match: match_list) {
if (match.and(NODE_EXISTS).matching(valueMatcher)) {
return true;
}
}
return false;
}
else {
return evaluated(item, mismatch)
.and(NODE_EXISTS)
.matching(valueMatcher);
}
}

@Override
Expand All @@ -80,6 +97,22 @@ private Condition<Object> evaluated(Node item, Description mismatch) {
return notMatched();
}

private List<Condition<Object>> evaluatedList(Node item, Description mismatch) {
List<Condition<Object>> match_list = new ArrayList<>();
try {
NodeList list = (NodeList) compiledXPath.evaluate(item, evaluationMode);
for (int i = 0; i < list.getLength(); i++) {
Node node = list.item(i);
match_list.add((matched((Object)node.getTextContent(), mismatch)));
}
return match_list;
} catch (XPathExpressionException e) {
mismatch.appendText(e.getMessage());
}
match_list.add(notMatched());
return match_list;
}

private static Condition.Step<Object, String> nodeExists() {
return new Condition.Step<Object, String>() {
@Override
Expand Down Expand Up @@ -136,7 +169,7 @@ public static Matcher<Node> hasXPath(String xPath, Matcher<String> valueMatcher)
* matcher for the value at the specified xpath
*/
public static Matcher<Node> hasXPath(String xPath, NamespaceContext namespaceContext, Matcher<String> valueMatcher) {
return new HasXPath(xPath, namespaceContext, valueMatcher, STRING);
return new HasXPath(xPath, namespaceContext, valueMatcher, NODESET);
}

/**
Expand Down
35 changes: 35 additions & 0 deletions hamcrest/src/test/java/org/hamcrest/xml/HasXPathTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,41 @@ public Iterator<String> getPrefixes(String namespaceURI) {
assertMatches(hasXPath("//something[@id='b']/cheese"), xml);
}

/*
Test whether the multiple elements case works or not
*/
@Test public void
appliesMatcherToXPathInDocumentWithMultipleElement() {
Document multipleBookXml = parse("<books>\n"
+ "<book>\n"
+ "<isbn>A-ISBN</isbn>\n"
+ "</book>\n"
+ "<book>\n"
+ "<isbn>B-ISBN</isbn>\n"
+ "<name>B-Book></name>\n"
+ "</book>\n"
+ "</books>\n"
);

assertMatches(hasXPath("/books/book/isbn", equalTo("B-ISBN")), multipleBookXml);
assertMatches(hasXPath("/books/book/isbn", equalTo("A-ISBN")), multipleBookXml);
}

/*
Test whether the single element case still works or not
*/
@Test public void
appliesMatcherToPathInDocumentWithSingleElement() {
Document multipleBookXml = parse("<books>\n"
+ "<book>\n"
+ "<isbn>A-ISBN</isbn>\n"
+ "</book>\n"
+ "</books>\n"
);

assertMatches(hasXPath("/books/book/isbn", equalTo("A-ISBN")), multipleBookXml);
}

@Test public void
matchesEmptyElement() {
assertMatches(hasXPath("//emptySomething"), xml);
Expand Down