001/*
002 * Copyright 2002-2015 the original author or authors.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *      https://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 */
016
017package org.springframework.test.web.servlet.result;
018
019import java.io.OutputStream;
020import java.io.PrintWriter;
021import java.io.StringWriter;
022import java.io.Writer;
023
024import org.apache.commons.logging.Log;
025import org.apache.commons.logging.LogFactory;
026
027import org.springframework.test.web.servlet.MvcResult;
028import org.springframework.test.web.servlet.ResultHandler;
029import org.springframework.util.CollectionUtils;
030
031/**
032 * Static factory methods for {@link ResultHandler}-based result actions.
033 *
034 * <h3>Eclipse Users</h3>
035 * <p>Consider adding this class as a Java editor favorite. To navigate to
036 * this setting, open the Preferences and type "favorites".
037 *
038 * @author Rossen Stoyanchev
039 * @author Sam Brannen
040 * @since 3.2
041 */
042public abstract class MockMvcResultHandlers {
043
044        private static final Log logger = LogFactory.getLog("org.springframework.test.web.servlet.result");
045
046
047        /**
048         * Log {@link MvcResult} details as a {@code DEBUG} log message via
049         * Apache Commons Logging using the log category
050         * {@code org.springframework.test.web.servlet.result}.
051         * @since 4.2
052         * @see #print()
053         * @see #print(OutputStream)
054         * @see #print(Writer)
055         */
056        public static ResultHandler log() {
057                return new LoggingResultHandler();
058        }
059
060        /**
061         * Print {@link MvcResult} details to the "standard" output stream.
062         * @see System#out
063         * @see #print(OutputStream)
064         * @see #print(Writer)
065         * @see #log()
066         */
067        public static ResultHandler print() {
068                return print(System.out);
069        }
070
071        /**
072         * Print {@link MvcResult} details to the supplied {@link OutputStream}.
073         * @since 4.2
074         * @see #print()
075         * @see #print(Writer)
076         * @see #log()
077         */
078        public static ResultHandler print(OutputStream stream) {
079                return new PrintWriterPrintingResultHandler(new PrintWriter(stream, true));
080        }
081
082        /**
083         * Print {@link MvcResult} details to the supplied {@link Writer}.
084         * @since 4.2
085         * @see #print()
086         * @see #print(OutputStream)
087         * @see #log()
088         */
089        public static ResultHandler print(Writer writer) {
090                return new PrintWriterPrintingResultHandler(new PrintWriter(writer, true));
091        }
092
093
094        /**
095         * A {@link PrintingResultHandler} that writes to a {@link PrintWriter}.
096         */
097        private static class PrintWriterPrintingResultHandler extends PrintingResultHandler {
098
099                public PrintWriterPrintingResultHandler(final PrintWriter writer) {
100                        super(new ResultValuePrinter() {
101                                @Override
102                                public void printHeading(String heading) {
103                                        writer.println();
104                                        writer.println(String.format("%s:", heading));
105                                }
106                                @Override
107                                public void printValue(String label, Object value) {
108                                        if (value != null && value.getClass().isArray()) {
109                                                value = CollectionUtils.arrayToList(value);
110                                        }
111                                        writer.println(String.format("%17s = %s", label, value));
112                                }
113                        });
114                }
115        }
116
117
118        /**
119         * A {@link ResultHandler} that logs {@link MvcResult} details at
120         * {@code DEBUG} level via Apache Commons Logging.
121         *
122         * <p>Delegates to a {@link PrintWriterPrintingResultHandler} for
123         * building the log message.
124         *
125         * @since 4.2
126         */
127        private static class LoggingResultHandler implements ResultHandler {
128
129                @Override
130                public void handle(MvcResult result) throws Exception {
131                        if (logger.isDebugEnabled()) {
132                                StringWriter stringWriter = new StringWriter();
133                                ResultHandler printingResultHandler =
134                                                new PrintWriterPrintingResultHandler(new PrintWriter(stringWriter));
135                                printingResultHandler.handle(result);
136                                logger.debug("MvcResult details:\n" + stringWriter);
137                        }
138                }
139        }
140
141}