1 |
| |
2 |
| |
3 |
| |
4 |
| |
5 |
| |
6 |
| |
7 |
| |
8 |
| |
9 |
| |
10 |
| |
11 |
| |
12 |
| |
13 |
| |
14 |
| |
15 |
| package org.apache.tapestry.test.mock; |
16 |
| |
17 |
| import java.io.BufferedWriter; |
18 |
| import java.io.ByteArrayOutputStream; |
19 |
| import java.io.IOException; |
20 |
| import java.io.OutputStreamWriter; |
21 |
| import java.io.PrintWriter; |
22 |
| import java.io.UnsupportedEncodingException; |
23 |
| import java.util.*; |
24 |
| |
25 |
| import javax.servlet.ServletOutputStream; |
26 |
| import javax.servlet.http.Cookie; |
27 |
| import javax.servlet.http.HttpServletResponse; |
28 |
| |
29 |
| import org.apache.tapestry.util.ContentType; |
30 |
| |
31 |
| |
32 |
| |
33 |
| |
34 |
| |
35 |
| |
36 |
| |
37 |
| |
38 |
| public class MockResponse implements HttpServletResponse |
39 |
| { |
40 |
| private MockRequest _request; |
41 |
| |
42 |
| private boolean _commited = false; |
43 |
| |
44 |
| private ByteArrayOutputStream _outputByteStream; |
45 |
| |
46 |
| private ServletOutputStream _outputStream; |
47 |
| |
48 |
| private String _outputString; |
49 |
| |
50 |
| private List _cookies = new ArrayList(); |
51 |
| |
52 |
| private String _redirectLocation; |
53 |
| |
54 |
| private String _contentType = "text/html;charset=utf-8"; |
55 |
| |
56 |
| private class ServletOutputStreamImpl extends ServletOutputStream |
57 |
| { |
58 |
153
| private ServletOutputStreamImpl()
|
59 |
| { |
60 |
| } |
61 |
| |
62 |
274
| public void close() throws IOException
|
63 |
| { |
64 |
274
| super.close();
|
65 |
| |
66 |
274
| if (_outputByteStream != null)
|
67 |
274
| _outputByteStream.close();
|
68 |
| } |
69 |
| |
70 |
183
| public void write(byte[] b, int off, int len) throws IOException
|
71 |
| { |
72 |
183
| commit();
|
73 |
| |
74 |
183
| _outputByteStream.write(b, off, len);
|
75 |
| } |
76 |
| |
77 |
0
| public void write(byte[] b) throws IOException
|
78 |
| { |
79 |
0
| commit();
|
80 |
| |
81 |
0
| _outputByteStream.write(b);
|
82 |
| } |
83 |
| |
84 |
0
| public void write(int b) throws IOException
|
85 |
| { |
86 |
0
| commit();
|
87 |
| |
88 |
0
| _outputByteStream.write(b);
|
89 |
| } |
90 |
| |
91 |
183
| private void commit()
|
92 |
| { |
93 |
183
| if (!_commited)
|
94 |
| { |
95 |
143
| _commited = true;
|
96 |
143
| _outputByteStream = new ByteArrayOutputStream();
|
97 |
| } |
98 |
| } |
99 |
| } |
100 |
| |
101 |
143
| public MockResponse(MockRequest request)
|
102 |
| { |
103 |
143
| _request = request;
|
104 |
| } |
105 |
| |
106 |
12
| public void addCookie(Cookie cookie)
|
107 |
| { |
108 |
12
| _cookies.add(cookie);
|
109 |
| } |
110 |
| |
111 |
0
| public boolean containsHeader(String arg0)
|
112 |
| { |
113 |
0
| return false;
|
114 |
| } |
115 |
| |
116 |
136
| public String encodeURL(String path)
|
117 |
| { |
118 |
136
| return path;
|
119 |
| } |
120 |
| |
121 |
0
| public String encodeRedirectURL(String path)
|
122 |
| { |
123 |
0
| return path;
|
124 |
| } |
125 |
| |
126 |
0
| public String encodeUrl(String path)
|
127 |
| { |
128 |
0
| return encodeURL(path);
|
129 |
| } |
130 |
| |
131 |
0
| public String encodeRedirectUrl(String path)
|
132 |
| { |
133 |
0
| return encodeRedirectURL(path);
|
134 |
| } |
135 |
| |
136 |
0
| public void sendError(int code, String message) throws IOException
|
137 |
| { |
138 |
0
| if (_commited)
|
139 |
0
| throw new IllegalStateException("sendError() when committed.");
|
140 |
| } |
141 |
| |
142 |
0
| public void sendError(int code) throws IOException
|
143 |
| { |
144 |
0
| sendError(code, null);
|
145 |
| } |
146 |
| |
147 |
0
| public void sendRedirect(String location) throws IOException
|
148 |
| { |
149 |
0
| if (_commited)
|
150 |
0
| throw new IllegalStateException("sendRedirect() when committed.");
|
151 |
| |
152 |
0
| if (location.endsWith("/FAIL_IO"))
|
153 |
0
| throw new IOException("Forced IOException in MockResponse.sendRedirect().");
|
154 |
| |
155 |
0
| _redirectLocation = location;
|
156 |
| |
157 |
0
| _commited = true;
|
158 |
| |
159 |
| } |
160 |
| |
161 |
0
| public String getRedirectLocation()
|
162 |
| { |
163 |
0
| return _redirectLocation;
|
164 |
| } |
165 |
| |
166 |
2
| public void setDateHeader(String name, long value)
|
167 |
| { |
168 |
| } |
169 |
| |
170 |
0
| public void addDateHeader(String name, long value)
|
171 |
| { |
172 |
| } |
173 |
| |
174 |
0
| public void setHeader(String name, String value)
|
175 |
| { |
176 |
| } |
177 |
| |
178 |
0
| public void addHeader(String name, String value)
|
179 |
| { |
180 |
| } |
181 |
| |
182 |
0
| public void setIntHeader(String name, int value)
|
183 |
| { |
184 |
| } |
185 |
| |
186 |
0
| public void addIntHeader(String name, int value)
|
187 |
| { |
188 |
| } |
189 |
| |
190 |
0
| public void setStatus(int name)
|
191 |
| { |
192 |
| } |
193 |
| |
194 |
0
| public void setStatus(int name, String arg1)
|
195 |
| { |
196 |
| } |
197 |
| |
198 |
0
| public String getCharacterEncoding()
|
199 |
| { |
200 |
0
| return null;
|
201 |
| } |
202 |
| |
203 |
153
| public ServletOutputStream getOutputStream() throws IOException
|
204 |
| { |
205 |
153
| if (_outputStream != null)
|
206 |
0
| throw new IllegalStateException("getOutputStream() invoked more than once.");
|
207 |
| |
208 |
153
| _outputStream = new ServletOutputStreamImpl();
|
209 |
| |
210 |
153
| return _outputStream;
|
211 |
| } |
212 |
| |
213 |
152
| public PrintWriter getWriter() throws IOException
|
214 |
| { |
215 |
152
| ContentType ct = new ContentType(_contentType);
|
216 |
| |
217 |
152
| String encoding = ct.getParameter("charset");
|
218 |
| |
219 |
152
| return new PrintWriter(new BufferedWriter(new OutputStreamWriter(getOutputStream(),
|
220 |
| encoding))); |
221 |
| } |
222 |
| |
223 |
1
| public void setContentLength(int arg0)
|
224 |
| { |
225 |
| } |
226 |
| |
227 |
142
| public void setContentType(String contentType)
|
228 |
| { |
229 |
142
| _contentType = contentType;
|
230 |
| } |
231 |
| |
232 |
0
| public void setBufferSize(int arg0)
|
233 |
| { |
234 |
| } |
235 |
| |
236 |
0
| public int getBufferSize()
|
237 |
| { |
238 |
0
| return 0;
|
239 |
| } |
240 |
| |
241 |
0
| public void flushBuffer() throws IOException
|
242 |
| { |
243 |
| } |
244 |
| |
245 |
0
| public void resetBuffer()
|
246 |
| { |
247 |
| } |
248 |
| |
249 |
0
| public boolean isCommitted()
|
250 |
| { |
251 |
0
| return _commited;
|
252 |
| } |
253 |
| |
254 |
10
| public void reset()
|
255 |
| { |
256 |
10
| _outputStream = null;
|
257 |
| } |
258 |
| |
259 |
0
| public void setLocale(Locale arg0)
|
260 |
| { |
261 |
| } |
262 |
| |
263 |
0
| public Locale getLocale()
|
264 |
| { |
265 |
0
| return null;
|
266 |
| } |
267 |
| |
268 |
| |
269 |
| |
270 |
| |
271 |
| |
272 |
| |
273 |
132
| public void end() throws IOException
|
274 |
| { |
275 |
| |
276 |
| |
277 |
132
| if (_outputStream != null)
|
278 |
132
| _outputStream.close();
|
279 |
| } |
280 |
| |
281 |
| |
282 |
| |
283 |
| |
284 |
| |
285 |
309
| public String getOutputString()
|
286 |
| { |
287 |
309
| if (_outputString != null)
|
288 |
166
| return _outputString;
|
289 |
| |
290 |
143
| if (_outputByteStream == null)
|
291 |
0
| return null;
|
292 |
| |
293 |
143
| try
|
294 |
| { |
295 |
143
| String encoding = _request.getCharacterEncoding();
|
296 |
| |
297 |
143
| if (encoding != null)
|
298 |
132
| _outputString = new String(_outputByteStream.toByteArray(), encoding);
|
299 |
| } |
300 |
| catch (UnsupportedEncodingException e) |
301 |
| { |
302 |
| } |
303 |
| |
304 |
143
| if (_outputString == null)
|
305 |
11
| _outputString = _outputByteStream.toString();
|
306 |
| |
307 |
143
| return _outputString;
|
308 |
| } |
309 |
| |
310 |
2
| public byte[] getResponseBytes()
|
311 |
| { |
312 |
2
| return _outputByteStream.toByteArray();
|
313 |
| } |
314 |
| |
315 |
91
| public Cookie[] getCookies()
|
316 |
| { |
317 |
91
| return (Cookie[]) _cookies.toArray(new Cookie[_cookies.size()]);
|
318 |
| } |
319 |
| |
320 |
2
| public String getContentType()
|
321 |
| { |
322 |
2
| return _contentType;
|
323 |
| } |
324 |
| |
325 |
0
| public void setCharacterEncoding(String enc)
|
326 |
| { |
327 |
| } |
328 |
| |
329 |
| } |