From 37c9d4f60105aa868f1e9f8f8f75064f810122ba 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 881e8d4853b3..084316180795 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 @@ -102,6 +102,18 @@ public void assertNode(byte[] content, @Nullable String encoding, final Matcher< 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 50143161f4f7..3930f41fe85a 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 @@ -21,6 +21,7 @@ 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; @@ -68,6 +69,17 @@ public ResultMatcher node(final Matcher matcher) { }; } + /** + * 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 fedfcfccdd31..0db427551844 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 @@ -45,6 +45,16 @@ public void nodeNoMatch() throws Exception { 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());