001/*
002 * Copyright 2002-2018 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.mock.web;
018
019import java.io.IOException;
020import java.io.PrintWriter;
021import java.io.Writer;
022
023import javax.servlet.http.HttpServletResponse;
024import javax.servlet.jsp.JspWriter;
025
026import org.springframework.lang.Nullable;
027
028/**
029 * Mock implementation of the {@link javax.servlet.jsp.JspWriter} class.
030 * Only necessary for testing applications when testing custom JSP tags.
031 *
032 * @author Juergen Hoeller
033 * @since 2.5
034 */
035public class MockJspWriter extends JspWriter {
036
037        private final HttpServletResponse response;
038
039        @Nullable
040        private PrintWriter targetWriter;
041
042
043        /**
044         * Create a MockJspWriter for the given response,
045         * using the response's default Writer.
046         * @param response the servlet response to wrap
047         */
048        public MockJspWriter(HttpServletResponse response) {
049                this(response, null);
050        }
051
052        /**
053         * Create a MockJspWriter for the given plain Writer.
054         * @param targetWriter the target Writer to wrap
055         */
056        public MockJspWriter(Writer targetWriter) {
057                this(null, targetWriter);
058        }
059
060        /**
061         * Create a MockJspWriter for the given response.
062         * @param response the servlet response to wrap
063         * @param targetWriter the target Writer to wrap
064         */
065        public MockJspWriter(@Nullable HttpServletResponse response, @Nullable Writer targetWriter) {
066                super(DEFAULT_BUFFER, true);
067                this.response = (response != null ? response : new MockHttpServletResponse());
068                if (targetWriter instanceof PrintWriter) {
069                        this.targetWriter = (PrintWriter) targetWriter;
070                }
071                else if (targetWriter != null) {
072                        this.targetWriter = new PrintWriter(targetWriter);
073                }
074        }
075
076        /**
077         * Lazily initialize the target Writer.
078         */
079        protected PrintWriter getTargetWriter() throws IOException {
080                if (this.targetWriter == null) {
081                        this.targetWriter = this.response.getWriter();
082                }
083                return this.targetWriter;
084        }
085
086
087        @Override
088        public void clear() throws IOException {
089                if (this.response.isCommitted()) {
090                        throw new IOException("Response already committed");
091                }
092                this.response.resetBuffer();
093        }
094
095        @Override
096        public void clearBuffer() throws IOException {
097        }
098
099        @Override
100        public void flush() throws IOException {
101                this.response.flushBuffer();
102        }
103
104        @Override
105        public void close() throws IOException {
106                flush();
107        }
108
109        @Override
110        public int getRemaining() {
111                return Integer.MAX_VALUE;
112        }
113
114        @Override
115        public void newLine() throws IOException {
116                getTargetWriter().println();
117        }
118
119        @Override
120        public void write(char[] value, int offset, int length) throws IOException {
121                getTargetWriter().write(value, offset, length);
122        }
123
124        @Override
125        public void print(boolean value) throws IOException {
126                getTargetWriter().print(value);
127        }
128
129        @Override
130        public void print(char value) throws IOException {
131                getTargetWriter().print(value);
132        }
133
134        @Override
135        public void print(char[] value) throws IOException {
136                getTargetWriter().print(value);
137        }
138
139        @Override
140        public void print(double value) throws IOException {
141                getTargetWriter().print(value);
142        }
143
144        @Override
145        public void print(float value) throws IOException {
146                getTargetWriter().print(value);
147        }
148
149        @Override
150        public void print(int value) throws IOException {
151                getTargetWriter().print(value);
152        }
153
154        @Override
155        public void print(long value) throws IOException {
156                getTargetWriter().print(value);
157        }
158
159        @Override
160        public void print(Object value) throws IOException {
161                getTargetWriter().print(value);
162        }
163
164        @Override
165        public void print(String value) throws IOException {
166                getTargetWriter().print(value);
167        }
168
169        @Override
170        public void println() throws IOException {
171                getTargetWriter().println();
172        }
173
174        @Override
175        public void println(boolean value) throws IOException {
176                getTargetWriter().println(value);
177        }
178
179        @Override
180        public void println(char value) throws IOException {
181                getTargetWriter().println(value);
182        }
183
184        @Override
185        public void println(char[] value) throws IOException {
186                getTargetWriter().println(value);
187        }
188
189        @Override
190        public void println(double value) throws IOException {
191                getTargetWriter().println(value);
192        }
193
194        @Override
195        public void println(float value) throws IOException {
196                getTargetWriter().println(value);
197        }
198
199        @Override
200        public void println(int value) throws IOException {
201                getTargetWriter().println(value);
202        }
203
204        @Override
205        public void println(long value) throws IOException {
206                getTargetWriter().println(value);
207        }
208
209        @Override
210        public void println(Object value) throws IOException {
211                getTargetWriter().println(value);
212        }
213
214        @Override
215        public void println(String value) throws IOException {
216                getTargetWriter().println(value);
217        }
218
219}