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
|
170
|
private ServletOutputStreamImpl()
|
59
|
|
{
|
60
|
|
}
|
61
|
|
|
62
|
306
|
public void close() throws IOException
|
63
|
|
{
|
64
|
306
|
super.close();
|
65
|
|
|
66
|
306
|
if (_outputByteStream != null)
|
67
|
306
|
_outputByteStream.close();
|
68
|
|
}
|
69
|
|
|
70
|
201
|
public void write(byte[] b, int off, int len) throws IOException
|
71
|
|
{
|
72
|
201
|
commit();
|
73
|
|
|
74
|
201
|
_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
|
201
|
private void commit()
|
92
|
|
{
|
93
|
201
|
if (!_commited)
|
94
|
|
{
|
95
|
159
|
_commited = true;
|
96
|
159
|
_outputByteStream = new ByteArrayOutputStream();
|
97
|
|
}
|
98
|
|
}
|
99
|
|
}
|
100
|
|
|
101
|
159
|
public MockResponse(MockRequest request)
|
102
|
|
{
|
103
|
159
|
_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
|
155
|
public String encodeURL(String path)
|
117
|
|
{
|
118
|
155
|
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
|
0
|
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
|
170
|
public ServletOutputStream getOutputStream() throws IOException
|
204
|
|
{
|
205
|
170
|
if (_outputStream != null)
|
206
|
0
|
throw new IllegalStateException("getOutputStream() invoked more than once.");
|
207
|
|
|
208
|
170
|
_outputStream = new ServletOutputStreamImpl();
|
209
|
|
|
210
|
170
|
return _outputStream;
|
211
|
|
}
|
212
|
|
|
213
|
169
|
public PrintWriter getWriter() throws IOException
|
214
|
|
{
|
215
|
169
|
ContentType ct = new ContentType(_contentType);
|
216
|
|
|
217
|
169
|
String encoding = ct.getParameter("charset");
|
218
|
|
|
219
|
169
|
return new PrintWriter(new BufferedWriter(new OutputStreamWriter(getOutputStream(),
|
220
|
|
encoding)));
|
221
|
|
}
|
222
|
|
|
223
|
1
|
public void setContentLength(int arg0)
|
224
|
|
{
|
225
|
|
}
|
226
|
|
|
227
|
159
|
public void setContentType(String contentType)
|
228
|
|
{
|
229
|
159
|
_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
|
11
|
public void reset()
|
255
|
|
{
|
256
|
11
|
_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
|
148
|
public void end() throws IOException
|
274
|
|
{
|
275
|
|
|
276
|
|
|
277
|
148
|
if (_outputStream != null)
|
278
|
148
|
_outputStream.close();
|
279
|
|
}
|
280
|
|
|
281
|
|
|
282
|
|
|
283
|
|
|
284
|
|
|
285
|
351
|
public String getOutputString()
|
286
|
|
{
|
287
|
351
|
if (_outputString != null)
|
288
|
192
|
return _outputString;
|
289
|
|
|
290
|
159
|
if (_outputByteStream == null)
|
291
|
0
|
return null;
|
292
|
|
|
293
|
159
|
try
|
294
|
|
{
|
295
|
159
|
String encoding = _request.getCharacterEncoding();
|
296
|
|
|
297
|
159
|
if (encoding != null)
|
298
|
148
|
_outputString = new String(_outputByteStream.toByteArray(), encoding);
|
299
|
|
}
|
300
|
|
catch (UnsupportedEncodingException e)
|
301
|
|
{
|
302
|
|
}
|
303
|
|
|
304
|
159
|
if (_outputString == null)
|
305
|
11
|
_outputString = _outputByteStream.toString();
|
306
|
|
|
307
|
159
|
return _outputString;
|
308
|
|
}
|
309
|
|
|
310
|
2
|
public byte[] getResponseBytes()
|
311
|
|
{
|
312
|
2
|
return _outputByteStream.toByteArray();
|
313
|
|
}
|
314
|
|
|
315
|
103
|
public Cookie[] getCookies()
|
316
|
|
{
|
317
|
103
|
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
|
|
}
|