KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > test > mock > MockResponse


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.test.mock;
16
17 import java.io.BufferedWriter JavaDoc;
18 import java.io.ByteArrayOutputStream JavaDoc;
19 import java.io.IOException JavaDoc;
20 import java.io.OutputStreamWriter JavaDoc;
21 import java.io.PrintWriter JavaDoc;
22 import java.io.UnsupportedEncodingException JavaDoc;
23 import java.util.*;
24
25 import javax.servlet.ServletOutputStream JavaDoc;
26 import javax.servlet.http.Cookie JavaDoc;
27 import javax.servlet.http.HttpServletResponse JavaDoc;
28
29 import org.apache.tapestry.util.ContentType;
30
31 /**
32  * Mock implementation of {@link javax.servlet.http.HttpServletResponse}.
33  *
34  * @author Howard Lewis Ship
35  * @since 4.0
36  */

37
38 public class MockResponse implements HttpServletResponse JavaDoc
39 {
40     private MockRequest _request;
41
42     private boolean _commited = false;
43
44     private ByteArrayOutputStream JavaDoc _outputByteStream;
45
46     private ServletOutputStream JavaDoc _outputStream;
47
48     private String JavaDoc _outputString;
49
50     private List _cookies = new ArrayList();
51
52     private String JavaDoc _redirectLocation;
53
54     private String JavaDoc _contentType = "text/html;charset=utf-8";
55
56     private class ServletOutputStreamImpl extends ServletOutputStream JavaDoc
57     {
58         private ServletOutputStreamImpl()
59         {
60         }
61
62         public void close() throws IOException JavaDoc
63         {
64             super.close();
65
66             if (_outputByteStream != null)
67                 _outputByteStream.close();
68         }
69
70         public void write(byte[] b, int off, int len) throws IOException JavaDoc
71         {
72             commit();
73
74             _outputByteStream.write(b, off, len);
75         }
76
77         public void write(byte[] b) throws IOException JavaDoc
78         {
79             commit();
80
81             _outputByteStream.write(b);
82         }
83
84         public void write(int b) throws IOException JavaDoc
85         {
86             commit();
87
88             _outputByteStream.write(b);
89         }
90
91         private void commit()
92         {
93             if (!_commited)
94             {
95                 _commited = true;
96                 _outputByteStream = new ByteArrayOutputStream JavaDoc();
97             }
98         }
99     }
100
101     public MockResponse(MockRequest request)
102     {
103         _request = request;
104     }
105
106     public void addCookie(Cookie JavaDoc cookie)
107     {
108         _cookies.add(cookie);
109     }
110
111     public boolean containsHeader(String JavaDoc arg0)
112     {
113         return false;
114     }
115
116     public String JavaDoc encodeURL(String JavaDoc path)
117     {
118         return path;
119     }
120
121     public String JavaDoc encodeRedirectURL(String JavaDoc path)
122     {
123         return path;
124     }
125
126     public String JavaDoc encodeUrl(String JavaDoc path)
127     {
128         return encodeURL(path);
129     }
130
131     public String JavaDoc encodeRedirectUrl(String JavaDoc path)
132     {
133         return encodeRedirectURL(path);
134     }
135
136     public void sendError(int code, String JavaDoc message) throws IOException JavaDoc
137     {
138         if (_commited)
139             throw new IllegalStateException JavaDoc("sendError() when committed.");
140     }
141
142     public void sendError(int code) throws IOException JavaDoc
143     {
144         sendError(code, null);
145     }
146
147     public void sendRedirect(String JavaDoc location) throws IOException JavaDoc
148     {
149         if (_commited)
150             throw new IllegalStateException JavaDoc("sendRedirect() when committed.");
151
152         if (location.endsWith("/FAIL_IO"))
153             throw new IOException JavaDoc("Forced IOException in MockResponse.sendRedirect().");
154
155         _redirectLocation = location;
156
157         _commited = true;
158
159     }
160
161     public String JavaDoc getRedirectLocation()
162     {
163         return _redirectLocation;
164     }
165
166     public void setDateHeader(String JavaDoc name, long value)
167     {
168     }
169
170     public void addDateHeader(String JavaDoc name, long value)
171     {
172     }
173
174     public void setHeader(String JavaDoc name, String JavaDoc value)
175     {
176     }
177
178     public void addHeader(String JavaDoc name, String JavaDoc value)
179     {
180     }
181
182     public void setIntHeader(String JavaDoc name, int value)
183     {
184     }
185
186     public void addIntHeader(String JavaDoc name, int value)
187     {
188     }
189
190     public void setStatus(int name)
191     {
192     }
193
194     public void setStatus(int name, String JavaDoc arg1)
195     {
196     }
197
198     public String JavaDoc getCharacterEncoding()
199     {
200         return null;
201     }
202
203     public ServletOutputStream JavaDoc getOutputStream() throws IOException JavaDoc
204     {
205         if (_outputStream != null)
206             throw new IllegalStateException JavaDoc("getOutputStream() invoked more than once.");
207
208         _outputStream = new ServletOutputStreamImpl();
209
210         return _outputStream;
211     }
212
213     public PrintWriter JavaDoc getWriter() throws IOException JavaDoc
214     {
215         ContentType ct = new ContentType(_contentType);
216
217         String JavaDoc encoding = ct.getParameter("charset");
218
219         return new PrintWriter JavaDoc(new BufferedWriter JavaDoc(new OutputStreamWriter JavaDoc(getOutputStream(),
220                 encoding)));
221     }
222
223     public void setContentLength(int arg0)
224     {
225     }
226
227     public void setContentType(String JavaDoc contentType)
228     {
229         _contentType = contentType;
230     }
231
232     public void setBufferSize(int arg0)
233     {
234     }
235
236     public int getBufferSize()
237     {
238         return 0;
239     }
240
241     public void flushBuffer() throws IOException JavaDoc
242     {
243     }
244
245     public void resetBuffer()
246     {
247     }
248
249     public boolean isCommitted()
250     {
251         return _commited;
252     }
253
254     public void reset()
255     {
256         _outputStream = null;
257     }
258
259     public void setLocale(Locale arg0)
260     {
261     }
262
263     public Locale getLocale()
264     {
265         return null;
266     }
267
268     /**
269      * Invoked by {@link org.apache.tapestry.junit.mock.MockTester}after the test is complete, to
270      * close and otherwise finish up.
271      */

272
273     public void end() throws IOException JavaDoc
274     {
275         // For redirects, we may never open an output stream.
276

277         if (_outputStream != null)
278             _outputStream.close();
279     }
280
281     /**
282      * Converts the binary output stream back into a String.
283      */

284
285     public String JavaDoc getOutputString()
286     {
287         if (_outputString != null)
288             return _outputString;
289
290         if (_outputByteStream == null)
291             return null;
292
293         try
294         {
295             String JavaDoc encoding = _request.getCharacterEncoding();
296
297             if (encoding != null)
298                 _outputString = new String JavaDoc(_outputByteStream.toByteArray(), encoding);
299         }
300         catch (UnsupportedEncodingException JavaDoc e)
301         {
302         }
303
304         if (_outputString == null)
305             _outputString = _outputByteStream.toString();
306
307         return _outputString;
308     }
309
310     public byte[] getResponseBytes()
311     {
312         return _outputByteStream.toByteArray();
313     }
314
315     public Cookie JavaDoc[] getCookies()
316     {
317         return (Cookie JavaDoc[]) _cookies.toArray(new Cookie JavaDoc[_cookies.size()]);
318     }
319
320     public String JavaDoc getContentType()
321     {
322         return _contentType;
323     }
324
325     public void setCharacterEncoding(String JavaDoc enc)
326     {
327     }
328
329 }
Popular Tags