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