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