From 6db8306e4642a0541774272080fad4c6f0baa5ad Mon Sep 17 00:00:00 2001 From: Pat Turner Date: Thu, 22 Nov 2018 19:26:09 +0000 Subject: [PATCH 1/2] 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 6c5a61cd3f2..4f2e9ab9e46 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 15eebbfcae9..2f195f0b002 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 f62ca5e4e2d..7cbff19f409 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()); From 347f16c8ac82339cb4c8f4e30705a9a770c9f873 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Mon, 11 Nov 2019 15:03:52 +0000 Subject: [PATCH 2/2] Polishing See gh-2023 --- .../test/util/XpathExpectationsHelper.java | 25 ++++++++++--------- .../servlet/result/XpathResultMatchers.java | 21 ++++++++-------- .../result/XpathResultMatchersTests.java | 21 ++++++++-------- 3 files changed, 35 insertions(+), 32 deletions(-) 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 4f2e9ab9e46..028d8b06c5c 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 @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2019 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -103,17 +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); - } + /** + * Parse the content, evaluate the XPath expression as a {@link NodeList}, + * and assert it with the given {@code Matcher}. + * @since 5.2.2 + */ + 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. 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 2f195f0b002..0010ec9dabe 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 @@ -70,16 +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); - }; - } + /** + * Evaluate the XPath and assert the {@link NodeList} content found with the + * given Hamcrest {@link Matcher}. + * @since 5.2.2 + */ + 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 7cbff19f409..7a61272472a 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 @@ -43,7 +43,7 @@ public class XpathResultMatchersTests { } @Test - public void nodeNoMatch() throws Exception { + public void nodeNoMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new XpathResultMatchers("/foo/bar", null).node(Matchers.nullValue()).match(getStubMvcResult())); } @@ -53,9 +53,10 @@ public class XpathResultMatchersTests { 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 nodeListNoMatch() { + assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> + new XpathResultMatchers("/foo/bar", null).nodeList(Matchers.nullValue()).match(getStubMvcResult())); } @Test @@ -64,7 +65,7 @@ public class XpathResultMatchersTests { } @Test - public void existsNoMatch() throws Exception { + public void existsNoMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new XpathResultMatchers("/foo/Bar", null).exists().match(getStubMvcResult())); } @@ -75,7 +76,7 @@ public class XpathResultMatchersTests { } @Test - public void doesNotExistNoMatch() throws Exception { + public void doesNotExistNoMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new XpathResultMatchers("/foo/bar", null).doesNotExist().match(getStubMvcResult())); } @@ -86,7 +87,7 @@ public class XpathResultMatchersTests { } @Test - public void nodeCountNoMatch() throws Exception { + public void nodeCountNoMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new XpathResultMatchers("/foo/bar", null).nodeCount(1).match(getStubMvcResult())); } @@ -97,7 +98,7 @@ public class XpathResultMatchersTests { } @Test - public void stringNoMatch() throws Exception { + public void stringNoMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new XpathResultMatchers("/foo/bar[1]", null).string("112").match(getStubMvcResult())); } @@ -108,7 +109,7 @@ public class XpathResultMatchersTests { } @Test - public void numberNoMatch() throws Exception { + public void numberNoMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new XpathResultMatchers("/foo/bar[1]", null).number(111.1).match(getStubMvcResult())); } @@ -119,7 +120,7 @@ public class XpathResultMatchersTests { } @Test - public void booleanValueNoMatch() throws Exception { + public void booleanValueNoMatch() { assertThatExceptionOfType(AssertionError.class).isThrownBy(() -> new XpathResultMatchers("/foo/bar[2]", null).booleanValue(false).match(getStubMvcResult())); }