Browse Source

Polishing

pull/941/head
Juergen Hoeller 10 years ago
parent
commit
9652430f01
  1. 13
      spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultHandlers.java
  2. 72
      spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java

13
spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultHandlers.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2012 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -31,10 +31,6 @@ import org.springframework.util.CollectionUtils;
*/ */
public abstract class MockMvcResultHandlers { public abstract class MockMvcResultHandlers {
private MockMvcResultHandlers() {
}
/** /**
* Print {@link MvcResult} details to the "standard" output stream. * Print {@link MvcResult} details to the "standard" output stream.
*/ */
@ -43,18 +39,18 @@ public abstract class MockMvcResultHandlers {
} }
/** An {@link PrintingResultHandler} that writes to the "standard" output stream */ /**
* A {@link PrintingResultHandler} that writes to the "standard" output stream.
*/
private static class ConsolePrintingResultHandler extends PrintingResultHandler { private static class ConsolePrintingResultHandler extends PrintingResultHandler {
public ConsolePrintingResultHandler() { public ConsolePrintingResultHandler() {
super(new ResultValuePrinter() { super(new ResultValuePrinter() {
@Override @Override
public void printHeading(String heading) { public void printHeading(String heading) {
System.out.println(); System.out.println();
System.out.println(String.format("%20s:", heading)); System.out.println(String.format("%20s:", heading));
} }
@Override @Override
public void printValue(String label, Object value) { public void printValue(String label, Object value) {
if (value != null && value.getClass().isArray()) { if (value != null && value.getClass().isArray()) {
@ -65,4 +61,5 @@ public abstract class MockMvcResultHandlers {
}); });
} }
} }
} }

72
spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultMatchers.java

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2013 the original author or authors. * Copyright 2002-2015 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -28,13 +28,15 @@ import org.springframework.util.AntPathMatcher;
import static org.springframework.test.util.AssertionErrors.*; import static org.springframework.test.util.AssertionErrors.*;
/** /**
* Static, factory methods for {@link ResultMatcher}-based result actions. * Static factory methods for {@link ResultMatcher}-based result actions.
* *
* <p><strong>Eclipse users:</strong> consider adding this class as a Java editor * <h3>Eclipse Users</h3>
* favorite. To navigate, open the Preferences and type "favorites". * <p>Consider adding this class as a Java editor favorite. To navigate to
* this setting, open the Preferences and type "favorites".
* *
* @author Rossen Stoyanchev * @author Rossen Stoyanchev
* @author Brian Clozel * @author Brian Clozel
* @author Sam Brannen
* @since 3.2 * @since 3.2
*/ */
public abstract class MockMvcResultMatchers { public abstract class MockMvcResultMatchers {
@ -42,9 +44,6 @@ public abstract class MockMvcResultMatchers {
private static final AntPathMatcher pathMatcher = new AntPathMatcher(); private static final AntPathMatcher pathMatcher = new AntPathMatcher();
private MockMvcResultMatchers() {
}
/** /**
* Access to request-related assertions. * Access to request-related assertions.
*/ */
@ -82,12 +81,11 @@ public abstract class MockMvcResultMatchers {
/** /**
* Asserts the request was forwarded to the given URL. * Asserts the request was forwarded to the given URL.
* This methods accepts only exact matches. * <p>This methods accepts only exact matches.
* @param expectedUrl the exact URL expected * @param expectedUrl the exact URL expected
*/ */
public static ResultMatcher forwardedUrl(final String expectedUrl) { public static ResultMatcher forwardedUrl(final String expectedUrl) {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) { public void match(MvcResult result) {
assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl()); assertEquals("Forwarded URL", expectedUrl, result.getResponse().getForwardedUrl());
@ -97,14 +95,14 @@ public abstract class MockMvcResultMatchers {
/** /**
* Asserts the request was forwarded to the given URL. * Asserts the request was forwarded to the given URL.
* This methods accepts {@link org.springframework.util.AntPathMatcher} expressions. * <p>This methods accepts {@link org.springframework.util.AntPathMatcher}
* expressions.
* @param urlPattern an AntPath expression to match against * @param urlPattern an AntPath expression to match against
* @see org.springframework.util.AntPathMatcher
* @since 4.0 * @since 4.0
* @see org.springframework.util.AntPathMatcher
*/ */
public static ResultMatcher forwardedUrlPattern(final String urlPattern) { public static ResultMatcher forwardedUrlPattern(final String urlPattern) {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) { public void match(MvcResult result) {
assertTrue("AntPath expression", pathMatcher.isPattern(urlPattern)); assertTrue("AntPath expression", pathMatcher.isPattern(urlPattern));
@ -116,12 +114,11 @@ public abstract class MockMvcResultMatchers {
/** /**
* Asserts the request was redirected to the given URL. * Asserts the request was redirected to the given URL.
* This methods accepts only exact matches. * <p>This methods accepts only exact matches.
* @param expectedUrl the exact URL expected * @param expectedUrl the exact URL expected
*/ */
public static ResultMatcher redirectedUrl(final String expectedUrl) { public static ResultMatcher redirectedUrl(final String expectedUrl) {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) { public void match(MvcResult result) {
assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl()); assertEquals("Redirected URL", expectedUrl, result.getResponse().getRedirectedUrl());
@ -131,14 +128,14 @@ public abstract class MockMvcResultMatchers {
/** /**
* Asserts the request was redirected to the given URL. * Asserts the request was redirected to the given URL.
* This methods accepts {@link org.springframework.util.AntPathMatcher} expressions. * <p>This method accepts {@link org.springframework.util.AntPathMatcher}
* expressions.
* @param expectedUrl an AntPath expression to match against * @param expectedUrl an AntPath expression to match against
* @see org.springframework.util.AntPathMatcher * @see org.springframework.util.AntPathMatcher
* @since 4.0 * @since 4.0
*/ */
public static ResultMatcher redirectedUrlPattern(final String expectedUrl) { public static ResultMatcher redirectedUrlPattern(final String expectedUrl) {
return new ResultMatcher() { return new ResultMatcher() {
@Override @Override
public void match(MvcResult result) { public void match(MvcResult result) {
assertTrue("AntPath expression",pathMatcher.isPattern(expectedUrl)); assertTrue("AntPath expression",pathMatcher.isPattern(expectedUrl));
@ -170,13 +167,13 @@ public abstract class MockMvcResultMatchers {
} }
/** /**
* Access to response body assertions using a <a * Access to response body assertions using a
* href="http://goessner.net/articles/JsonPath/">JSONPath</a> expression to * <a href="https://github.com/jayway/JsonPath">JsonPath</a> expression
* inspect a specific subset of the body. The JSON path expression can be a * to inspect a specific subset of the body.
* parameterized string using formatting specifiers as defined in * <p>The JSON path expression can be a parameterized string using
* formatting specifiers as defined in
* {@link String#format(String, Object...)}. * {@link String#format(String, Object...)}.
* * @param expression the JSON path expression, optionally parameterized with arguments
* @param expression the JSON path optionally parameterized with arguments
* @param args arguments to parameterize the JSON path expression with * @param args arguments to parameterize the JSON path expression with
*/ */
public static JsonPathResultMatchers jsonPath(String expression, Object ... args) { public static JsonPathResultMatchers jsonPath(String expression, Object ... args) {
@ -184,11 +181,10 @@ public abstract class MockMvcResultMatchers {
} }
/** /**
* Access to response body assertions using a <a * Access to response body assertions using a
* href="http://goessner.net/articles/JsonPath/">JSONPath</a> expression to * <a href="https://github.com/jayway/JsonPath">JsonPath</a> expression
* inspect a specific subset of the body and a Hamcrest match for asserting * to inspect a specific subset of the body and a Hamcrest matcher for
* the value found at the JSON path. * asserting the value found at the JSON path.
*
* @param expression the JSON path expression * @param expression the JSON path expression
* @param matcher a matcher for the value expected at the JSON path * @param matcher a matcher for the value expected at the JSON path
*/ */
@ -197,12 +193,11 @@ public abstract class MockMvcResultMatchers {
} }
/** /**
* Access to response body assertions using an XPath to inspect a specific * Access to response body assertions using an XPath expression to
* subset of the body. The XPath expression can be a parameterized string * inspect a specific subset of the body.
* using formatting specifiers as defined in * <p>The XPath expression can be a parameterized string using formatting
* {@link String#format(String, Object...)}. * specifiers as defined in {@link String#format(String, Object...)}.
* * @param expression the XPath expression, optionally parameterized with arguments
* @param expression the XPath optionally parameterized with arguments
* @param args arguments to parameterize the XPath expression with * @param args arguments to parameterize the XPath expression with
*/ */
public static XpathResultMatchers xpath(String expression, Object... args) throws XPathExpressionException { public static XpathResultMatchers xpath(String expression, Object... args) throws XPathExpressionException {
@ -210,12 +205,11 @@ public abstract class MockMvcResultMatchers {
} }
/** /**
* Access to response body assertions using an XPath to inspect a specific * Access to response body assertions using an XPath expression to
* subset of the body. The XPath expression can be a parameterized string * inspect a specific subset of the body.
* using formatting specifiers as defined in * <p>The XPath expression can be a parameterized string using formatting
* {@link String#format(String, Object...)}. * specifiers as defined in {@link String#format(String, Object...)}.
* * @param expression the XPath expression, optionally parameterized with arguments
* @param expression the XPath optionally parameterized with arguments
* @param namespaces namespaces referenced in the XPath expression * @param namespaces namespaces referenced in the XPath expression
* @param args arguments to parameterize the XPath expression with * @param args arguments to parameterize the XPath expression with
*/ */

Loading…
Cancel
Save