diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultHandlers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultHandlers.java index beb832ae940..1728324d4ba 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultHandlers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/MockMvcResultHandlers.java @@ -24,9 +24,10 @@ import org.springframework.util.CollectionUtils; * Static, factory methods for {@link ResultHandler}-based result actions. * *
Eclipse users: consider adding this class as a Java editor - * favorite. To navigate, open the Preferences and type "favorites". + * favorite. To navigate to this setting, open the Preferences and type "favorites". * * @author Rossen Stoyanchev + * @author Sam Brannen * @since 3.2 */ public abstract class MockMvcResultHandlers { @@ -49,14 +50,14 @@ public abstract class MockMvcResultHandlers { @Override public void printHeading(String heading) { System.out.println(); - System.out.println(String.format("%20s:", heading)); + System.out.println(String.format("%s:", heading)); } @Override public void printValue(String label, Object value) { if (value != null && value.getClass().isArray()) { value = CollectionUtils.arrayToList(value); } - System.out.println(String.format("%20s = %s", label, value)); + System.out.println(String.format("%17s = %s", label, value)); } }); } diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java index 9dba5b1c474..3398f128b14 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/PrintingResultHandler.java @@ -18,8 +18,11 @@ package org.springframework.test.web.servlet.result; import java.util.Enumeration; import java.util.Map; + +import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; +import org.springframework.core.style.ToStringCreator; import org.springframework.http.HttpHeaders; import org.springframework.mock.web.MockHttpServletRequest; import org.springframework.mock.web.MockHttpServletResponse; @@ -27,6 +30,7 @@ import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultHandler; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; +import org.springframework.util.ObjectUtils; import org.springframework.validation.BindingResult; import org.springframework.validation.Errors; import org.springframework.web.method.HandlerMethod; @@ -37,10 +41,12 @@ import org.springframework.web.servlet.support.RequestContextUtils; /** * Result handler that prints {@link MvcResult} details to the "standard" output - * stream. An instance of this class is typically accessed via + * stream. + *
An instance of this class is typically accessed via
* {@link MockMvcResultHandlers#print()}.
*
* @author Rossen Stoyanchev
+ * @author Sam Brannen
* @since 3.2
*/
public class PrintingResultHandler implements ResultHandler {
@@ -57,7 +63,7 @@ public class PrintingResultHandler implements ResultHandler {
}
/**
- * @return the result value printer.
+ * @return the result value printer
*/
protected ResultValuePrinter getPrinter() {
return this.printer;
@@ -198,7 +204,7 @@ public class PrintingResultHandler implements ResultHandler {
* Print "output" flash attributes.
*/
protected void printFlashMap(FlashMap flashMap) throws Exception {
- if (flashMap == null) {
+ if (ObjectUtils.isEmpty(flashMap)) {
this.printer.printValue("Attributes", null);
}
else {
@@ -220,7 +226,31 @@ public class PrintingResultHandler implements ResultHandler {
this.printer.printValue("Body", response.getContentAsString());
this.printer.printValue("Forwarded URL", response.getForwardedUrl());
this.printer.printValue("Redirected URL", response.getRedirectedUrl());
- this.printer.printValue("Cookies", response.getCookies());
+ printCookies(response.getCookies());
+ }
+
+ /**
+ * Print the supplied cookies in a human-readable form, assuming the
+ * {@link Cookie} implementation does not provide its own {@code toString()}.
+ * @since 4.2
+ */
+ private void printCookies(Cookie[] cookies) {
+ String[] cookieStrings = new String[cookies.length];
+ for (int i = 0; i < cookies.length; i++) {
+ Cookie cookie = cookies[i];
+ cookieStrings[i] = new ToStringCreator(cookie)
+ .append("name", cookie.getName())
+ .append("value", cookie.getValue())
+ .append("comment", cookie.getComment())
+ .append("domain", cookie.getDomain())
+ .append("maxAge", cookie.getMaxAge())
+ .append("path", cookie.getPath())
+ .append("secure", cookie.getSecure())
+ .append("version", cookie.getVersion())
+ .append("httpOnly", cookie.isHttpOnly())
+ .toString();
+ }
+ this.printer.printValue("Cookies", cookieStrings);
}
protected final HttpHeaders getResponseHeaders(MockHttpServletResponse response) {
diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java
index 752cb6b9277..4de65c53d53 100644
--- a/spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java
+++ b/spring-test/src/test/java/org/springframework/test/web/servlet/result/PrintingResultHandlerTests.java
@@ -1,5 +1,5 @@
/*
- * Copyright 2002-2014 the original author or authors.
+ * Copyright 2002-2015 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.
@@ -19,9 +19,9 @@ package org.springframework.test.web.servlet.result;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
+
import javax.servlet.http.Cookie;
-import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
@@ -45,33 +45,27 @@ import static org.junit.Assert.*;
* Tests for {@link PrintingResultHandler}.
*
* @author Rossen Stoyanchev
+ * @author Sam Brannen
*/
public class PrintingResultHandlerTests {
- private TestPrintingResultHandler handler;
+ private final TestPrintingResultHandler handler = new TestPrintingResultHandler();
- private MockHttpServletRequest request;
+ private final MockHttpServletRequest request = new MockHttpServletRequest("GET", "/") {
+ @Override
+ public boolean isAsyncStarted() {
+ return false;
+ }
+ };
- private MockHttpServletResponse response;
+ private final MockHttpServletResponse response = new MockHttpServletResponse();
- private StubMvcResult mvcResult;
+ private final StubMvcResult mvcResult = new StubMvcResult(this.request, null, null,
+ null, null, null, this.response);
- @Before
- public void setup() {
- this.handler = new TestPrintingResultHandler();
- this.request = new MockHttpServletRequest("GET", "/") {
- @Override
- public boolean isAsyncStarted() {
- return false;
- }
- };
- this.response = new MockHttpServletResponse();
- this.mvcResult = new StubMvcResult(this.request, null, null, null, null, null, this.response);
- }
-
@Test
- public void testPrintRequest() throws Exception {
+ public void printRequest() throws Exception {
this.request.addParameter("param", "paramValue");
this.request.addHeader("header", "headerValue");
@@ -91,7 +85,15 @@ public class PrintingResultHandlerTests {
@Test
@SuppressWarnings("deprecation")
- public void testPrintResponse() throws Exception {
+ public void printResponse() throws Exception {
+ Cookie enigmaCookie = new Cookie("enigma", "42");
+ enigmaCookie.setComment("This is a comment");
+ enigmaCookie.setHttpOnly(true);
+ enigmaCookie.setMaxAge(1234);
+ enigmaCookie.setDomain(".example.com");
+ enigmaCookie.setPath("/crumbs");
+ enigmaCookie.setSecure(true);
+
this.response.setStatus(400, "error");
this.response.addHeader("header", "headerValue");
this.response.setContentType("text/plain");
@@ -99,6 +101,7 @@ public class PrintingResultHandlerTests {
this.response.setForwardedUrl("redirectFoo");
this.response.sendRedirect("/redirectFoo");
this.response.addCookie(new Cookie("cookie", "cookieValue"));
+ this.response.addCookie(enigmaCookie);
this.handler.handle(this.mvcResult);
@@ -107,17 +110,30 @@ public class PrintingResultHandlerTests {
headers.setContentType(MediaType.TEXT_PLAIN);
headers.setLocation(new URI("/redirectFoo"));
- assertValue("MockHttpServletResponse", "Status", this.response.getStatus());
- assertValue("MockHttpServletResponse", "Error message", response.getErrorMessage());
- assertValue("MockHttpServletResponse", "Headers", headers);
- assertValue("MockHttpServletResponse", "Content type", this.response.getContentType());
- assertValue("MockHttpServletResponse", "Body", this.response.getContentAsString());
- assertValue("MockHttpServletResponse", "Forwarded URL", this.response.getForwardedUrl());
- assertValue("MockHttpServletResponse", "Redirected URL", this.response.getRedirectedUrl());
+ String heading = "MockHttpServletResponse";
+ assertValue(heading, "Status", this.response.getStatus());
+ assertValue(heading, "Error message", response.getErrorMessage());
+ assertValue(heading, "Headers", headers);
+ assertValue(heading, "Content type", this.response.getContentType());
+ assertValue(heading, "Body", this.response.getContentAsString());
+ assertValue(heading, "Forwarded URL", this.response.getForwardedUrl());
+ assertValue(heading, "Redirected URL", this.response.getRedirectedUrl());
+
+ Map