KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > internal > http > MockHttpServletResponse


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit.internal.http;
8
9
10 import java.io.IOException JavaDoc;
11 import java.io.PrintWriter JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Locale JavaDoc;
14 import java.util.Map JavaDoc;
15 import javax.servlet.ServletOutputStream JavaDoc;
16 import javax.servlet.http.Cookie JavaDoc;
17 import javax.servlet.http.HttpServletResponse JavaDoc;
18
19
20 /**
21  * This class is a Mock Http servlet response.
22  *
23  * @author Brian Pontarelli
24  * @since 2.0
25  * @version 2.0
26  */

27 public class MockHttpServletResponse implements HttpServletResponse JavaDoc {
28
29     /**
30      * The string that is appened to the URL if the URL is encoded and the encode
31      * flag is set to true.
32      */

33     public static final String JavaDoc ENCODE_STR = "#encode";
34
35     /**
36      * The string that is appened to the URL if the URL is encoded and the encode
37      * flag is set to true and the URL is encoded using the encodeRedirectURL
38      * method.
39      */

40     public static final String JavaDoc ENCODE_REDIRECT_STR = "#encodeRedirect";
41
42     private MockServletOutputStream outputStream;
43     private PrintWriter JavaDoc writer;
44     private Map JavaDoc cookies;
45     private Locale JavaDoc locale;
46     private int size;
47     private int status;
48     private String JavaDoc msg;
49     private String JavaDoc url;
50     private boolean encode;
51
52
53     /**
54      */

55     public MockHttpServletResponse() {
56         initialize();
57     }
58
59
60     /**
61      */

62     private void initialize() {
63         this.outputStream = new MockServletOutputStream();
64         this.writer = new PrintWriter JavaDoc(outputStream, true);
65         this.cookies = new HashMap JavaDoc();
66         this.size = 0;
67     }
68
69
70     //-------------------------------------------------------------------------
71
// javax.servlet.ServletResponse methods
72
//-------------------------------------------------------------------------
73

74     /**
75      */

76     public void flushBuffer() throws IOException JavaDoc {
77         writer.flush();
78         outputStream.flush();
79     }
80
81     /**
82      */

83     public int getBufferSize() {
84         return size;
85     }
86
87     /**
88      */

89     public String JavaDoc getCharacterEncoding() {
90         if (locale == null) {
91             return "ISO-8859-1";
92         }
93
94         return locale.getDisplayLanguage();
95     }
96
97     /**
98      * getLocale method comment.
99      */

100     public java.util.Locale JavaDoc getLocale() {
101         if (locale == null) {
102             return Locale.getDefault();
103         }
104
105         return locale;
106     }
107
108     /**
109      */

110     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc {
111         return outputStream;
112     }
113
114     /**
115      */

116     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc {
117         return writer;
118     }
119
120     /**
121      */

122     public boolean isCommitted() {
123         return false;
124     }
125
126     /**
127      */

128     public void reset() {
129         initialize();
130     }
131
132     /**
133      */

134     public void resetBuffer(){
135         outputStream = new MockServletOutputStream();
136         writer = new PrintWriter JavaDoc(outputStream);
137     }
138
139     /**
140      */

141     public void setBufferSize(int size) {
142         this.size = size;
143     }
144
145     /**
146      */

147     public void setContentLength(int length) {
148     }
149
150     /**
151      */

152     public void setContentType(String JavaDoc contentType) {
153     }
154
155     /**
156      */

157     public void setLocale(Locale JavaDoc locale) {
158         this.locale = locale;
159     }
160
161
162     //-------------------------------------------------------------------------
163
// javax.servlet.http.HttpServletResponse methods
164
//-------------------------------------------------------------------------
165

166
167     /**
168      */

169     public void addCookie(Cookie JavaDoc cookie) {
170         cookies.put(cookie.getName(), cookie);
171     }
172
173     /**
174      */

175     public void addDateHeader(String JavaDoc name, long date) {
176         throw new UnsupportedOperationException JavaDoc();
177     }
178
179     /**
180      */

181     public void addHeader(String JavaDoc name, String JavaDoc value) {
182         throw new UnsupportedOperationException JavaDoc();
183     }
184
185     /**
186      */

187     public void addIntHeader(String JavaDoc name, int value) {
188         throw new UnsupportedOperationException JavaDoc();
189     }
190
191     /**
192      */

193     public boolean containsHeader(String JavaDoc name) {
194         throw new UnsupportedOperationException JavaDoc();
195     }
196
197     /**
198      * @deprecated
199      */

200     public String JavaDoc encodeRedirectUrl(String JavaDoc url) {
201         throw new UnsupportedOperationException JavaDoc();
202     }
203
204     /**
205      * This optionally adds a single String to the end of the URL if the encode
206      * flag of this class is set. The String is #encodeRedirect and it is always
207      * appended to the end of the url given.
208      */

209     public String JavaDoc encodeRedirectURL(String JavaDoc url) {
210         if (encode) {
211             url = url + ENCODE_REDIRECT_STR;
212         }
213
214         return url;
215     }
216
217     /**
218      * @deprecated
219      */

220     public String JavaDoc encodeUrl(String JavaDoc url) {
221         throw new UnsupportedOperationException JavaDoc();
222     }
223
224     /**
225      * This optionally adds a single String to the end of the URL if the encode
226      * flag of this class is set. The String is #encoded and it is always
227      * appended to the end of the url given.
228      */

229     public String JavaDoc encodeURL(String JavaDoc url) {
230         if (encode) {
231             url = url + ENCODE_STR;
232         }
233
234         return url;
235     }
236
237     /**
238      * Sets the encode flag
239      *
240      * @param encode The new encode flag
241      */

242     public void setEncode(boolean encode) {
243         this.encode = encode;
244     }
245
246     /**
247      * Gets the encode flag
248      *
249      * @return True if the encodeURL encodes, false otherwise
250      */

251     public boolean isEncode() {
252         return encode;
253     }
254
255     /**
256      */

257     public void sendError(int errorCode) throws IOException JavaDoc {
258         status = errorCode;
259     }
260
261     /**
262      */

263     public void sendError(int errorCode, String JavaDoc msg) throws IOException JavaDoc {
264         sendError(errorCode);
265         this.msg = msg;
266     }
267
268     /**
269      */

270     public void sendRedirect(String JavaDoc url) throws IOException JavaDoc {
271         this.url = url;
272     }
273
274     /**
275      */

276     public void setDateHeader(String JavaDoc name, long date) {
277         throw new UnsupportedOperationException JavaDoc();
278     }
279
280     /**
281      */

282     public void setHeader(String JavaDoc name, String JavaDoc value) {
283         throw new UnsupportedOperationException JavaDoc();
284     }
285
286     /**
287      */

288     public void setIntHeader(String JavaDoc name, int value) {
289         throw new UnsupportedOperationException JavaDoc();
290     }
291
292     /**
293      */

294     public void setStatus(int status) {
295         this.status = status;
296     }
297
298     /**
299      * @deprecated
300      */

301     public void setStatus(int status, String JavaDoc msg) {
302         throw new UnsupportedOperationException JavaDoc();
303     }
304
305
306     //-------------------------------------------------------------------------
307
// Additional methods
308
//-------------------------------------------------------------------------
309

310
311     /**
312      * Returns the status of the response
313      */

314     public int getStatus() {
315         return status;
316     }
317
318     /**
319      * Returns the cookie with the given name
320      */

321     public Cookie JavaDoc getCookie(String JavaDoc name) {
322         return (Cookie JavaDoc) cookies.get(name);
323     }
324
325     /**
326      * Returns all the cookies
327      */

328     public Cookie JavaDoc [] getCookies() {
329         return (Cookie JavaDoc []) cookies.values().toArray(new Cookie JavaDoc [0]);
330     }
331
332     /**
333      * Returns all the cookies in the map
334      */

335     public Map JavaDoc getCookiesMap() {
336         return cookies;
337     }
338
339     /**
340      * Returns the text of the response
341      */

342     public String JavaDoc getText() {
343         return outputStream.getText();
344     }
345
346     /**
347      * Returns the URL redirected to
348      */

349     public String JavaDoc getRedirectURL() {
350         return url;
351     }
352
353     /**
354      * Returns the error message sent.
355      */

356     public String JavaDoc getErrorMessage() {
357         return msg;
358     }
359 }
360
Popular Tags