Introduce LoggingResultHandler in Spring MVC Test

Prior to this commit, the Spring MVC Test framework only provided
support for printing debug information about the MvcResult to STDOUT.

This commit introduces support for logging `MvcResult` details at
`DEBUG` level via the Apache Commons Logging API. In addition, this
commit introduces additional `print(..)` variants for printing debug
information to custom output streams and writers.

Specifically, `MockMvcResultHandlers` has been augmented with the
following new static methods:

 - `log()`
 - `print(OutputStream)`
 - `print(Writer)`

Issue: SPR-13171
This commit is contained in:
Sam Brannen
2015-06-27 21:53:19 +02:00
parent 895d43a2b3
commit 693dcba867
6 changed files with 138 additions and 32 deletions

View File

@@ -16,6 +16,14 @@
package org.springframework.test.web.servlet.result;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.io.Writer;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.ResultHandler;
import org.springframework.util.CollectionUtils;
@@ -32,35 +40,100 @@ import org.springframework.util.CollectionUtils;
*/
public abstract class MockMvcResultHandlers {
private static final Log logger = LogFactory.getLog(MockMvcResultHandlers.class.getPackage().getName());
/**
* Log {@link MvcResult} details as a {@code DEBUG} log message via
* Apache Commons Logging using the log category
* {@code org.springframework.test.web.servlet.result}.
* @since 4.2
* @see #print()
* @see #print(OutputStream)
* @see #print(Writer)
*/
public static ResultHandler log() {
return new LoggingResultHandler();
}
/**
* Print {@link MvcResult} details to the "standard" output stream.
* @see System#out
* @see #print(OutputStream)
* @see #print(Writer)
* @see #log()
*/
public static ResultHandler print() {
return new ConsolePrintingResultHandler();
return print(System.out);
}
/**
* Print {@link MvcResult} details to the supplied {@link OutputStream}.
* @since 4.2
* @see #print()
* @see #print(Writer)
* @see #log()
*/
public static ResultHandler print(OutputStream stream) {
return new PrintWriterPrintingResultHandler(new PrintWriter(stream, true));
}
/**
* Print {@link MvcResult} details to the supplied {@link Writer}.
* @since 4.2
* @see #print()
* @see #print(OutputStream)
* @see #log()
*/
public static ResultHandler print(Writer writer) {
return new PrintWriterPrintingResultHandler(new PrintWriter(writer, true));
}
/**
* An {@link PrintingResultHandler} that writes to the "standard" output stream
* A {@link PrintingResultHandler} that writes to a {@link PrintWriter}.
*/
private static class ConsolePrintingResultHandler extends PrintingResultHandler {
private static class PrintWriterPrintingResultHandler extends PrintingResultHandler {
public ConsolePrintingResultHandler() {
PrintWriterPrintingResultHandler(final PrintWriter writer) {
super(new ResultValuePrinter() {
@Override
public void printHeading(String heading) {
System.out.println();
System.out.println(String.format("%s:", heading));
writer.println();
writer.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("%17s = %s", label, value));
writer.println(String.format("%17s = %s", label, value));
}
});
}
}
/**
* A {@link ResultHandler} that logs {@link MvcResult} details at
* {@code DEBUG} level via Apache Commons Logging.
*
* <p>Delegates to a {@link PrintWriterPrintingResultHandler} for
* building the log message.
* @since 4.2
*/
private static class LoggingResultHandler implements ResultHandler {
private final StringWriter stringWriter = new StringWriter();
private final ResultHandler printingResultHandler = new PrintWriterPrintingResultHandler(
new PrintWriter(stringWriter, true));
@Override
public void handle(MvcResult result) throws Exception {
this.printingResultHandler.handle(result);
logger.debug("MvcResult details:\n" + this.stringWriter);
}
}
}

View File

@@ -40,10 +40,13 @@ import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.support.RequestContextUtils;
/**
* Result handler that prints {@link MvcResult} details to the "standard" output
* stream.
* <p>An instance of this class is typically accessed via
* {@link MockMvcResultHandlers#print()}.
* Result handler that prints {@link MvcResult} details to a given output
* stream &mdash; for example: {@code System.out}, {@code System.err}, a
* custom {@code java.io.PrintWriter}, etc.
*
* <p>An instance of this class is typically accessed via one of the
* {@link MockMvcResultHandlers#print print} or {@link MockMvcResultHandlers#log log}
* methods in {@link MockMvcResultHandlers}.
*
* @author Rossen Stoyanchev
* @author Sam Brannen
@@ -70,7 +73,7 @@ public class PrintingResultHandler implements ResultHandler {
}
/**
* Print {@link MvcResult} details to the "standard" output stream.
* Print {@link MvcResult} details.
*/
@Override
public final void handle(MvcResult result) throws Exception {