1
|
|
/*
|
2
|
|
* Angkor Web Framework
|
3
|
|
*
|
4
|
|
* Distributable under LGPL license.
|
5
|
|
* See terms of license at gnu.org.
|
6
|
|
*/
|
7
|
|
|
8
|
|
package com.tirsen.angkor.test.unit.mock;
|
9
|
|
|
10
|
|
import org.apache.commons.logging.Log;
|
11
|
|
import org.apache.commons.logging.LogFactory;
|
12
|
|
import org.jdom.Document;
|
13
|
|
import org.w3c.tidy.Tidy;
|
14
|
|
import org.jdom.input.DOMBuilder;
|
15
|
|
|
16
|
|
import javax.servlet.ServletOutputStream;
|
17
|
|
import javax.servlet.http.Cookie;
|
18
|
|
import javax.servlet.http.HttpServletResponse;
|
19
|
|
import java.io.ByteArrayInputStream;
|
20
|
|
import java.io.ByteArrayOutputStream;
|
21
|
|
import java.io.IOException;
|
22
|
|
import java.io.InputStream;
|
23
|
|
import java.io.OutputStreamWriter;
|
24
|
|
import java.io.PrintWriter;
|
25
|
|
import java.util.Locale;
|
26
|
|
|
27
|
|
/**
|
28
|
|
* TODO document MockServletResponse
|
29
|
|
*
|
30
|
|
* <!-- $Id: MockServletResponse.java,v 1.5 2002/10/13 19:59:23 tirsen Exp $ -->
|
31
|
|
*
|
32
|
|
* @author $Author: tirsen $
|
33
|
|
* @version $Revision: 1.5 $
|
34
|
|
*/
|
35
|
|
public class MockServletResponse implements HttpServletResponse
|
36
|
|
{
|
37
|
|
private static Log logger = LogFactory.getLog(MockServletResponse.class);
|
38
|
|
|
39
|
|
private ByteArrayOutputStream output = new ByteArrayOutputStream();
|
40
|
|
private PrintWriter printWriter = new PrintWriter(new OutputStreamWriter(output));
|
41
|
|
|
42
|
0
|
public String getCharacterEncoding()
|
43
|
|
{
|
44
|
|
// TODO implement MockServletResponse.getCharacterEncoding
|
45
|
0
|
throw new RuntimeException("MockServletResponse.getCharacterEncoding is not implemented yet.");
|
46
|
|
}
|
47
|
|
|
48
|
0
|
public ServletOutputStream getOutputStream() throws IOException
|
49
|
|
{
|
50
|
|
// TODO implement MockServletResponse.getOutputStream
|
51
|
0
|
throw new RuntimeException("MockServletResponse.getOutputStream is not implemented yet.");
|
52
|
|
}
|
53
|
|
|
54
|
20
|
public PrintWriter getWriter() throws IOException
|
55
|
|
{
|
56
|
20
|
return printWriter;
|
57
|
|
}
|
58
|
|
|
59
|
0
|
public void setContentLength(int i)
|
60
|
|
{
|
61
|
|
// TODO implement MockServletResponse.setContentLength
|
62
|
0
|
throw new RuntimeException("MockServletResponse.setContentLength is not implemented yet.");
|
63
|
|
}
|
64
|
|
|
65
|
0
|
public void setContentType(String s)
|
66
|
|
{
|
67
|
|
// TODO implement MockServletResponse.setContentType
|
68
|
0
|
throw new RuntimeException("MockServletResponse.setContentType is not implemented yet.");
|
69
|
|
}
|
70
|
|
|
71
|
0
|
public void setBufferSize(int i)
|
72
|
|
{
|
73
|
|
// TODO implement MockServletResponse.setBufferSize
|
74
|
0
|
throw new RuntimeException("MockServletResponse.setBufferSize is not implemented yet.");
|
75
|
|
}
|
76
|
|
|
77
|
0
|
public int getBufferSize()
|
78
|
|
{
|
79
|
|
// TODO implement MockServletResponse.getBufferSize
|
80
|
0
|
throw new RuntimeException("MockServletResponse.getBufferSize is not implemented yet.");
|
81
|
|
}
|
82
|
|
|
83
|
0
|
public void flushBuffer() throws IOException
|
84
|
|
{
|
85
|
|
// TODO implement MockServletResponse.flushBuffer
|
86
|
0
|
throw new RuntimeException("MockServletResponse.flushBuffer is not implemented yet.");
|
87
|
|
}
|
88
|
|
|
89
|
0
|
public void resetBuffer()
|
90
|
|
{
|
91
|
|
// TODO implement MockServletResponse.resetBuffer
|
92
|
0
|
throw new RuntimeException("MockServletResponse.resetBuffer is not implemented yet.");
|
93
|
|
}
|
94
|
|
|
95
|
1
|
public boolean isCommitted()
|
96
|
|
{
|
97
|
1
|
return false;
|
98
|
|
}
|
99
|
|
|
100
|
0
|
public void reset()
|
101
|
|
{
|
102
|
|
// TODO implement MockServletResponse.reset
|
103
|
0
|
throw new RuntimeException("MockServletResponse.reset is not implemented yet.");
|
104
|
|
}
|
105
|
|
|
106
|
0
|
public void setLocale(Locale locale)
|
107
|
|
{
|
108
|
|
// TODO implement MockServletResponse.setLocale
|
109
|
0
|
throw new RuntimeException("MockServletResponse.setLocale is not implemented yet.");
|
110
|
|
}
|
111
|
|
|
112
|
0
|
public Locale getLocale()
|
113
|
|
{
|
114
|
|
// TODO implement MockServletResponse.getLocale
|
115
|
0
|
throw new RuntimeException("MockServletResponse.getLocale is not implemented yet.");
|
116
|
|
}
|
117
|
|
|
118
|
0
|
public void setStatus(int i, String s)
|
119
|
|
{
|
120
|
|
// TODO implement MockServletResponse.setStatus
|
121
|
0
|
throw new RuntimeException("MockServletResponse.setStatus is not implemented yet.");
|
122
|
|
}
|
123
|
|
|
124
|
0
|
public void setStatus(int i)
|
125
|
|
{
|
126
|
|
// TODO implement MockServletResponse.setStatus
|
127
|
0
|
throw new RuntimeException("MockServletResponse.setStatus is not implemented yet.");
|
128
|
|
}
|
129
|
|
|
130
|
0
|
public void addIntHeader(String s, int i)
|
131
|
|
{
|
132
|
|
// TODO implement MockServletResponse.addIntHeader
|
133
|
0
|
throw new RuntimeException("MockServletResponse.addIntHeader is not implemented yet.");
|
134
|
|
}
|
135
|
|
|
136
|
0
|
public void setIntHeader(String s, int i)
|
137
|
|
{
|
138
|
|
// TODO implement MockServletResponse.setIntHeader
|
139
|
0
|
throw new RuntimeException("MockServletResponse.setIntHeader is not implemented yet.");
|
140
|
|
}
|
141
|
|
|
142
|
0
|
public void addHeader(String s, String s1)
|
143
|
|
{
|
144
|
|
// TODO implement MockServletResponse.addHeader
|
145
|
0
|
throw new RuntimeException("MockServletResponse.addHeader is not implemented yet.");
|
146
|
|
}
|
147
|
|
|
148
|
0
|
public void setHeader(String s, String s1)
|
149
|
|
{
|
150
|
|
// TODO implement MockServletResponse.setHeader
|
151
|
0
|
throw new RuntimeException("MockServletResponse.setHeader is not implemented yet.");
|
152
|
|
}
|
153
|
|
|
154
|
0
|
public void addDateHeader(String s, long l)
|
155
|
|
{
|
156
|
|
// TODO implement MockServletResponse.addDateHeader
|
157
|
0
|
throw new RuntimeException("MockServletResponse.addDateHeader is not implemented yet.");
|
158
|
|
}
|
159
|
|
|
160
|
0
|
public void setDateHeader(String s, long l)
|
161
|
|
{
|
162
|
|
// TODO implement MockServletResponse.setDateHeader
|
163
|
0
|
throw new RuntimeException("MockServletResponse.setDateHeader is not implemented yet.");
|
164
|
|
}
|
165
|
|
|
166
|
0
|
public void sendRedirect(String s) throws IOException
|
167
|
|
{
|
168
|
|
// TODO implement MockServletResponse.sendRedirect
|
169
|
0
|
throw new RuntimeException("MockServletResponse.sendRedirect is not implemented yet.");
|
170
|
|
}
|
171
|
|
|
172
|
0
|
public void sendError(int i) throws IOException
|
173
|
|
{
|
174
|
|
// TODO implement MockServletResponse.sendError
|
175
|
0
|
throw new RuntimeException("MockServletResponse.sendError is not implemented yet.");
|
176
|
|
}
|
177
|
|
|
178
|
0
|
public void sendError(int i, String s) throws IOException
|
179
|
|
{
|
180
|
|
// TODO implement MockServletResponse.sendError
|
181
|
0
|
throw new RuntimeException("MockServletResponse.sendError is not implemented yet.");
|
182
|
|
}
|
183
|
|
|
184
|
0
|
public String encodeRedirectUrl(String s)
|
185
|
|
{
|
186
|
|
// TODO implement MockServletResponse.encodeRedirectUrl
|
187
|
0
|
throw new RuntimeException("MockServletResponse.encodeRedirectUrl is not implemented yet.");
|
188
|
|
}
|
189
|
|
|
190
|
0
|
public String encodeUrl(String s)
|
191
|
|
{
|
192
|
|
// TODO implement MockServletResponse.encodeUrl
|
193
|
0
|
throw new RuntimeException("MockServletResponse.encodeUrl is not implemented yet.");
|
194
|
|
}
|
195
|
|
|
196
|
0
|
public String encodeRedirectURL(String s)
|
197
|
|
{
|
198
|
|
// TODO implement MockServletResponse.encodeRedirectURL
|
199
|
0
|
throw new RuntimeException("MockServletResponse.encodeRedirectURL is not implemented yet.");
|
200
|
|
}
|
201
|
|
|
202
|
0
|
public String encodeURL(String s)
|
203
|
|
{
|
204
|
|
// TODO implement MockServletResponse.encodeURL
|
205
|
0
|
throw new RuntimeException("MockServletResponse.encodeURL is not implemented yet.");
|
206
|
|
}
|
207
|
|
|
208
|
0
|
public boolean containsHeader(String s)
|
209
|
|
{
|
210
|
|
// TODO implement MockServletResponse.containsHeader
|
211
|
0
|
throw new RuntimeException("MockServletResponse.containsHeader is not implemented yet.");
|
212
|
|
}
|
213
|
|
|
214
|
0
|
public void addCookie(Cookie cookie)
|
215
|
|
{
|
216
|
|
// TODO implement MockServletResponse.addCookie
|
217
|
0
|
throw new RuntimeException("MockServletResponse.addCookie is not implemented yet.");
|
218
|
|
}
|
219
|
|
|
220
|
2
|
public Document getResultingDocument()
|
221
|
|
{
|
222
|
2
|
Tidy tidy = new Tidy();
|
223
|
2
|
org.w3c.dom.Document doc = tidy.parseDOM(getRenderedInputStream(), System.out);
|
224
|
2
|
DOMBuilder domBuilder = new DOMBuilder();
|
225
|
2
|
return new Document(domBuilder.build(doc.getDocumentElement()));
|
226
|
|
}
|
227
|
|
|
228
|
0
|
public void dumpResult()
|
229
|
|
{
|
230
|
0
|
try
|
231
|
|
{
|
232
|
0
|
InputStream renderedInputStream = getRenderedInputStream();
|
233
|
0
|
int read;
|
234
|
0
|
while((read = renderedInputStream.read()) != -1)
|
235
|
|
{
|
236
|
0
|
System.out.write(read);
|
237
|
|
}
|
238
|
|
}
|
239
|
|
catch(IOException ignore)
|
240
|
|
{
|
241
|
|
}
|
242
|
|
}
|
243
|
|
|
244
|
2
|
private InputStream getRenderedInputStream()
|
245
|
|
{
|
246
|
2
|
printWriter.flush();
|
247
|
2
|
byte[] bytes = output.toByteArray();
|
248
|
2
|
return new ByteArrayInputStream(bytes);
|
249
|
|
}
|
250
|
|
|
251
|
7
|
void resetStreams()
|
252
|
|
{
|
253
|
7
|
output = new ByteArrayOutputStream();
|
254
|
7
|
printWriter = new PrintWriter(output);
|
255
|
|
}
|
256
|
|
}
|
257
|
|
|