From 6db8306e4642a0541774272080fad4c6f0baa5ad Mon Sep 17 00:00:00 2001 From: Pat Turner Date: Thu, 22 Nov 2018 19:26:09 +0000 Subject: [PATCH] XpathResultMatcher supports Hamcrest Matcher NodeList Use when xpath result is XPathConstants.NODESET --- .../test/util/XpathExpectationsHelper.java | 12 ++++++++++++ .../test/web/servlet/result/XpathResultMatchers.java | 12 ++++++++++++ .../web/servlet/result/XpathResultMatchersTests.java | 10 ++++++++++ 3 files changed, 34 insertions(+) diff --git a/spring-test/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java b/spring-test/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java index 6c5a61cd3f..4f2e9ab9e4 100644 --- a/spring-test/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java +++ b/spring-test/src/main/java/org/springframework/test/util/XpathExpectationsHelper.java @@ -103,6 +103,18 @@ public class XpathExpectationsHelper { MatcherAssert.assertThat("XPath " + this.expression, node, matcher); } + /** + * Parse the content, evaluate the XPath expression as a {@link NodeList}, + * and assert it with the given {@code Matcher}. + */ + public void assertNodeList(byte[] content, @Nullable String encoding, final Matcher matcher) + throws Exception { + + Document document = parseXmlByteArray(content, encoding); + NodeList nodeList = evaluateXpath(document, XPathConstants.NODESET, NodeList.class); + MatcherAssert.assertThat("XPath " + this.getXpathExpression(), nodeList, matcher); + } + /** * Apply the XPath expression and assert the resulting content exists. * @throws Exception if content parsing or expression evaluation fails diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/XpathResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/XpathResultMatchers.java index 15eebbfcae..2f195f0b00 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/XpathResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/XpathResultMatchers.java @@ -22,6 +22,7 @@ import javax.xml.xpath.XPathExpressionException; import org.hamcrest.Matcher; import org.w3c.dom.Node; +import org.w3c.dom.NodeList; import org.springframework.lang.Nullable; import org.springframework.mock.web.MockHttpServletResponse; @@ -69,6 +70,17 @@ public class XpathResultMatchers { }; } + /** + * Evaluate the XPath and assert the {@link NodeList} content found with the + * given Hamcrest {@link Matcher}. + */ + public ResultMatcher nodeList(final Matcher matcher) { + return result -> { + MockHttpServletResponse response = result.getResponse(); + this.xpathHelper.assertNodeList(response.getContentAsByteArray(), getDefinedEncoding(response), matcher); + }; + } + /** * Get the response encoding if explicitly defined in the response, {code null} otherwise. */ diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/XpathResultMatchersTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/XpathResultMatchersTests.java index f62ca5e4e2..7cbff19f40 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/XpathResultMatchersTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/XpathResultMatchersTests.java @@ -48,6 +48,16 @@ public class XpathResultMatchersTests { new XpathResultMatchers("/foo/bar", null).node(Matchers.nullValue()).match(getStubMvcResult())); } + @Test + public void nodeList() throws Exception { + new XpathResultMatchers("/foo/bar", null).nodeList(Matchers.notNullValue()).match(getStubMvcResult()); + } + + @Test(expected = AssertionError.class) + public void nodeListNoMatch() throws Exception { + new XpathResultMatchers("/foo/bar", null).nodeList(Matchers.nullValue()).match(getStubMvcResult()); + } + @Test public void exists() throws Exception { new XpathResultMatchers("/foo/bar", null).exists().match(getStubMvcResult()); -- GitLab