001 package com.mockrunner.mock.web;
002
003 import java.io.IOException;
004 import java.io.PrintWriter;
005 import java.io.StringWriter;
006 import java.io.Writer;
007
008 import javax.servlet.http.HttpServletResponse;
009 import javax.servlet.jsp.JspWriter;
010
011 import com.mockrunner.base.NestedApplicationException;
012
013 /**
014 * Mock implementation of <code>JspWriter</code>.
015 * Collects the output data.
016 */
017 public class MockJspWriter extends JspWriter
018 {
019 private PrintWriter printWriter;
020 private Writer writer;
021 private HttpServletResponse response;
022 private boolean providedWriter;
023
024 public MockJspWriter()
025 {
026 super(0, true);
027 this.writer = new StringWriter();
028 printWriter = new PrintWriter(writer);
029 response = null;
030 providedWriter = false;
031 }
032
033 public MockJspWriter(HttpServletResponse response) throws IOException
034 {
035 super(0, true);
036 this.writer = response.getWriter();
037 this.response = response;
038 printWriter = new PrintWriter(writer);
039 providedWriter = false;
040 }
041
042 public MockJspWriter(Writer writer)
043 {
044 super(0, true);
045 this.writer = writer;
046 printWriter = new PrintWriter(writer);
047 response = null;
048 providedWriter = true;
049 }
050
051 /**
052 * Returns the output.
053 * @return the output
054 */
055 public String getOutputAsString()
056 {
057 try
058 {
059 flush();
060 if(!providedWriter)
061 {
062 if(null == response)
063 {
064 return ((StringWriter)writer).toString();
065 }
066 if(response instanceof MockHttpServletResponse)
067 {
068 return ((MockHttpServletResponse)response).getOutputStreamContent();
069 }
070 }
071 return "";
072 }
073 catch(IOException exc)
074 {
075 throw new NestedApplicationException(exc);
076 }
077 }
078
079 /**
080 * Delegates to {@link #getOutputAsString}
081 */
082 public String toString()
083 {
084 return getOutputAsString();
085 }
086
087 /**
088 * Clears the output. This method throws an <code>IOException</code>,
089 * if a <code>Writer</code> was provided according to spec.
090 */
091 public void clear() throws IOException
092 {
093 if(!providedWriter)
094 {
095 clearWriter();
096 }
097 else
098 {
099 throw new IOException("Illegal call if writer is provided.");
100 }
101 }
102
103 /**
104 * Clears the output. This method does nothing,
105 * if a <code>Writer</code> was provided according to spec.
106 */
107 public void clearBuffer() throws IOException
108 {
109 if(!providedWriter)
110 {
111 clearWriter();
112 }
113 }
114
115 private void clearWriter() throws IOException
116 {
117 if(null == response)
118 {
119 this.writer = new StringWriter();
120 printWriter = new PrintWriter(writer);
121 }
122 else
123 {
124 flush();
125 response.resetBuffer();
126 }
127 }
128
129 public void close() throws IOException
130 {
131 flush();
132 printWriter.close();
133 }
134
135 public int getRemaining()
136 {
137 return 0;
138 }
139
140 public void flush() throws IOException
141 {
142 printWriter.flush();
143 }
144
145 public void newLine() throws IOException
146 {
147 print(System.getProperty("line.separator"));
148 }
149
150 public void print(boolean value) throws IOException
151 {
152 printWriter.print(value);
153 }
154
155 public void print(char value) throws IOException
156 {
157 printWriter.print(value);
158 }
159
160 public void print(char[] value) throws IOException
161 {
162 printWriter.print(value);
163 }
164
165 public void print(double value) throws IOException
166 {
167 printWriter.print(value);
168 }
169
170 public void print(float value) throws IOException
171 {
172 printWriter.print(value);
173 }
174
175 public void print(int value) throws IOException
176 {
177 printWriter.print(value);
178 }
179
180 public void print(long value) throws IOException
181 {
182 printWriter.print(value);
183 }
184
185 public void print(Object value) throws IOException
186 {
187 printWriter.print(value);
188 }
189
190 public void print(String value) throws IOException
191 {
192 printWriter.print(value);
193 }
194
195 public void println() throws IOException
196 {
197 printWriter.println();
198 }
199
200 public void println(boolean value) throws IOException
201 {
202 printWriter.println(value);
203 }
204
205 public void println(char value) throws IOException
206 {
207 printWriter.println(value);
208 }
209
210 public void println(char[] value) throws IOException
211 {
212 printWriter.println(value);
213 }
214
215 public void println(double value) throws IOException
216 {
217 printWriter.println(value);
218 }
219
220 public void println(float value) throws IOException
221 {
222 printWriter.println(value);
223 }
224
225 public void println(int value) throws IOException
226 {
227 printWriter.println(value);
228 }
229
230 public void println(long value) throws IOException
231 {
232 printWriter.println(value);
233 }
234
235 public void println(Object value) throws IOException
236 {
237 printWriter.println(value);
238 }
239
240 public void println(String value) throws IOException
241 {
242 printWriter.println(value);
243 }
244
245 public void write(char[] cbuf, int off, int len) throws IOException
246 {
247 printWriter.write(cbuf, off, len);
248 }
249 }