diff --git a/spring-test/src/main/java/org/springframework/test/web/client/match/XpathRequestMatchers.java b/spring-test/src/main/java/org/springframework/test/web/client/match/XpathRequestMatchers.java
index 3bc7080380..0fb49b608c 100644
--- a/spring-test/src/main/java/org/springframework/test/web/client/match/XpathRequestMatchers.java
+++ b/spring-test/src/main/java/org/springframework/test/web/client/match/XpathRequestMatchers.java
@@ -16,7 +16,6 @@
package org.springframework.test.web.client.match;
-import java.io.IOException;
import java.util.Map;
import javax.xml.xpath.XPathExpressionException;
@@ -31,11 +30,15 @@ import org.springframework.test.util.XpathExpectationsHelper;
import org.springframework.test.web.client.RequestMatcher;
/**
- * Factory methods for request content {@code RequestMatcher}'s using an XPath
- * expression. An instance of this class is typically accessed via
- * {@code RequestMatchers.xpath(..)}.
+ * Factory methods for request content {@code RequestMatcher} implementations
+ * that use an XPath expression.
+ *
+ *
An instance of this class is typically accessed via
+ * {@link MockRestRequestMatchers#xpath(String, Object...)} or
+ * {@link MockRestRequestMatchers#xpath(String, Map, Object...)}.
*
* @author Rossen Stoyanchev
+ * @author Sam Brannen
* @since 3.2
*/
public class XpathRequestMatchers {
@@ -46,16 +49,16 @@ public class XpathRequestMatchers {
/**
- * Class constructor, not for direct instantiation. Use
- * {@link MockRestRequestMatchers#xpath(String, Object...)} or
+ * Class constructor, not for direct instantiation.
+ *
Use {@link MockRestRequestMatchers#xpath(String, Object...)} or
* {@link MockRestRequestMatchers#xpath(String, Map, Object...)}.
* @param expression the XPath expression
* @param namespaces the XML namespaces referenced in the XPath expression, or {@code null}
- * @param args arguments to parameterize the XPath expression with using the
+ * @param args arguments to parameterize the XPath expression with, using the
* formatting specifiers defined in {@link String#format(String, Object...)}
* @throws XPathExpressionException if expression compilation failed
*/
- protected XpathRequestMatchers(String expression, @Nullable Map namespaces, Object ... args)
+ XpathRequestMatchers(String expression, @Nullable Map namespaces, Object... args)
throws XPathExpressionException {
this.xpathHelper = new XpathExpectationsHelper(expression, namespaces, args);
@@ -66,36 +69,24 @@ public class XpathRequestMatchers {
* Apply the XPath and assert it with the given {@code Matcher}.
*/
public RequestMatcher node(Matcher super Node> matcher) {
- return new AbstractXpathRequestMatcher() {
- @Override
- protected void matchInternal(MockClientHttpRequest request) throws Exception {
- xpathHelper.assertNode(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
- }
- };
+ return (XpathRequestMatcher) request ->
+ this.xpathHelper.assertNode(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
}
/**
* Assert that content exists at the given XPath.
*/
public RequestMatcher exists() {
- return new AbstractXpathRequestMatcher() {
- @Override
- protected void matchInternal(MockClientHttpRequest request) throws Exception {
- xpathHelper.exists(request.getBodyAsBytes(), DEFAULT_ENCODING);
- }
- };
+ return (XpathRequestMatcher) request ->
+ this.xpathHelper.exists(request.getBodyAsBytes(), DEFAULT_ENCODING);
}
/**
* Assert that content does not exist at the given XPath.
*/
public RequestMatcher doesNotExist() {
- return new AbstractXpathRequestMatcher() {
- @Override
- protected void matchInternal(MockClientHttpRequest request) throws Exception {
- xpathHelper.doesNotExist(request.getBodyAsBytes(), DEFAULT_ENCODING);
- }
- };
+ return (XpathRequestMatcher) request ->
+ this.xpathHelper.doesNotExist(request.getBodyAsBytes(), DEFAULT_ENCODING);
}
/**
@@ -103,104 +94,76 @@ public class XpathRequestMatchers {
* {@code Matcher}.
*/
public RequestMatcher nodeCount(Matcher matcher) {
- return new AbstractXpathRequestMatcher() {
- @Override
- protected void matchInternal(MockClientHttpRequest request) throws Exception {
- xpathHelper.assertNodeCount(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
- }
- };
+ return (XpathRequestMatcher) request ->
+ this.xpathHelper.assertNodeCount(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
}
/**
* Apply the XPath and assert the number of nodes found.
*/
public RequestMatcher nodeCount(int expectedCount) {
- return new AbstractXpathRequestMatcher() {
- @Override
- protected void matchInternal(MockClientHttpRequest request) throws Exception {
- xpathHelper.assertNodeCount(request.getBodyAsBytes(), DEFAULT_ENCODING, expectedCount);
- }
- };
+ return (XpathRequestMatcher) request ->
+ this.xpathHelper.assertNodeCount(request.getBodyAsBytes(), DEFAULT_ENCODING, expectedCount);
}
/**
* Apply the XPath and assert the String content found with the given matcher.
*/
public RequestMatcher string(Matcher super String> matcher) {
- return new AbstractXpathRequestMatcher() {
- @Override
- protected void matchInternal(MockClientHttpRequest request) throws Exception {
- xpathHelper.assertString(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
- }
- };
+ return (XpathRequestMatcher) request ->
+ this.xpathHelper.assertString(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
}
/**
* Apply the XPath and assert the String content found.
*/
- public RequestMatcher string(String value) {
- return new AbstractXpathRequestMatcher() {
- @Override
- protected void matchInternal(MockClientHttpRequest request) throws Exception {
- xpathHelper.assertString(request.getBodyAsBytes(), DEFAULT_ENCODING, value);
- }
- };
+ public RequestMatcher string(String content) {
+ return (XpathRequestMatcher) request ->
+ this.xpathHelper.assertString(request.getBodyAsBytes(), DEFAULT_ENCODING, content);
}
/**
* Apply the XPath and assert the number found with the given matcher.
*/
public RequestMatcher number(Matcher super Double> matcher) {
- return new AbstractXpathRequestMatcher() {
- @Override
- protected void matchInternal(MockClientHttpRequest request) throws Exception {
- xpathHelper.assertNumber(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
- }
- };
+ return (XpathRequestMatcher) request ->
+ this.xpathHelper.assertNumber(request.getBodyAsBytes(), DEFAULT_ENCODING, matcher);
}
/**
- * Apply the XPath and assert the number of nodes found.
+ * Apply the XPath and assert the number value found.
*/
public RequestMatcher number(Double value) {
- return new AbstractXpathRequestMatcher() {
- @Override
- protected void matchInternal(MockClientHttpRequest request) throws Exception {
- xpathHelper.assertNumber(request.getBodyAsBytes(), DEFAULT_ENCODING, value);
- }
- };
+ return (XpathRequestMatcher) request ->
+ this.xpathHelper.assertNumber(request.getBodyAsBytes(), DEFAULT_ENCODING, value);
}
/**
* Apply the XPath and assert the boolean value found.
*/
public RequestMatcher booleanValue(Boolean value) {
- return new AbstractXpathRequestMatcher() {
- @Override
- protected void matchInternal(MockClientHttpRequest request) throws Exception {
- xpathHelper.assertBoolean(request.getBodyAsBytes(), DEFAULT_ENCODING, value);
- }
- };
+ return (XpathRequestMatcher) request ->
+ this.xpathHelper.assertBoolean(request.getBodyAsBytes(), DEFAULT_ENCODING, value);
}
/**
- * Abstract base class for XPath {@link RequestMatcher}'s.
+ * Functional interface for XPath {@link RequestMatcher} implementations.
*/
- private abstract static class AbstractXpathRequestMatcher implements RequestMatcher {
+ @FunctionalInterface
+ private interface XpathRequestMatcher extends RequestMatcher {
@Override
- public final void match(ClientHttpRequest request) throws IOException, AssertionError {
+ default void match(ClientHttpRequest request) {
try {
- MockClientHttpRequest mockRequest = (MockClientHttpRequest) request;
- matchInternal(mockRequest);
+ matchInternal((MockClientHttpRequest) request);
}
catch (Exception ex) {
throw new AssertionError("Failed to parse XML request content", ex);
}
}
- protected abstract void matchInternal(MockClientHttpRequest request) throws Exception;
+ void matchInternal(MockClientHttpRequest request) throws Exception;
}
}