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 public String getCharacterEncoding()
43 {
44 // TODO implement MockServletResponse.getCharacterEncoding
45 throw new RuntimeException("MockServletResponse.getCharacterEncoding is not implemented yet.");
46 }
47
48 public ServletOutputStream getOutputStream() throws IOException
49 {
50 // TODO implement MockServletResponse.getOutputStream
51 throw new RuntimeException("MockServletResponse.getOutputStream is not implemented yet.");
52 }
53
54 public PrintWriter getWriter() throws IOException
55 {
56 return printWriter;
57 }
58
59 public void setContentLength(int i)
60 {
61 // TODO implement MockServletResponse.setContentLength
62 throw new RuntimeException("MockServletResponse.setContentLength is not implemented yet.");
63 }
64
65 public void setContentType(String s)
66 {
67 // TODO implement MockServletResponse.setContentType
68 throw new RuntimeException("MockServletResponse.setContentType is not implemented yet.");
69 }
70
71 public void setBufferSize(int i)
72 {
73 // TODO implement MockServletResponse.setBufferSize
74 throw new RuntimeException("MockServletResponse.setBufferSize is not implemented yet.");
75 }
76
77 public int getBufferSize()
78 {
79 // TODO implement MockServletResponse.getBufferSize
80 throw new RuntimeException("MockServletResponse.getBufferSize is not implemented yet.");
81 }
82
83 public void flushBuffer() throws IOException
84 {
85 // TODO implement MockServletResponse.flushBuffer
86 throw new RuntimeException("MockServletResponse.flushBuffer is not implemented yet.");
87 }
88
89 public void resetBuffer()
90 {
91 // TODO implement MockServletResponse.resetBuffer
92 throw new RuntimeException("MockServletResponse.resetBuffer is not implemented yet.");
93 }
94
95 public boolean isCommitted()
96 {
97 return false;
98 }
99
100 public void reset()
101 {
102 // TODO implement MockServletResponse.reset
103 throw new RuntimeException("MockServletResponse.reset is not implemented yet.");
104 }
105
106 public void setLocale(Locale locale)
107 {
108 // TODO implement MockServletResponse.setLocale
109 throw new RuntimeException("MockServletResponse.setLocale is not implemented yet.");
110 }
111
112 public Locale getLocale()
113 {
114 // TODO implement MockServletResponse.getLocale
115 throw new RuntimeException("MockServletResponse.getLocale is not implemented yet.");
116 }
117
118 public void setStatus(int i, String s)
119 {
120 // TODO implement MockServletResponse.setStatus
121 throw new RuntimeException("MockServletResponse.setStatus is not implemented yet.");
122 }
123
124 public void setStatus(int i)
125 {
126 // TODO implement MockServletResponse.setStatus
127 throw new RuntimeException("MockServletResponse.setStatus is not implemented yet.");
128 }
129
130 public void addIntHeader(String s, int i)
131 {
132 // TODO implement MockServletResponse.addIntHeader
133 throw new RuntimeException("MockServletResponse.addIntHeader is not implemented yet.");
134 }
135
136 public void setIntHeader(String s, int i)
137 {
138 // TODO implement MockServletResponse.setIntHeader
139 throw new RuntimeException("MockServletResponse.setIntHeader is not implemented yet.");
140 }
141
142 public void addHeader(String s, String s1)
143 {
144 // TODO implement MockServletResponse.addHeader
145 throw new RuntimeException("MockServletResponse.addHeader is not implemented yet.");
146 }
147
148 public void setHeader(String s, String s1)
149 {
150 // TODO implement MockServletResponse.setHeader
151 throw new RuntimeException("MockServletResponse.setHeader is not implemented yet.");
152 }
153
154 public void addDateHeader(String s, long l)
155 {
156 // TODO implement MockServletResponse.addDateHeader
157 throw new RuntimeException("MockServletResponse.addDateHeader is not implemented yet.");
158 }
159
160 public void setDateHeader(String s, long l)
161 {
162 // TODO implement MockServletResponse.setDateHeader
163 throw new RuntimeException("MockServletResponse.setDateHeader is not implemented yet.");
164 }
165
166 public void sendRedirect(String s) throws IOException
167 {
168 // TODO implement MockServletResponse.sendRedirect
169 throw new RuntimeException("MockServletResponse.sendRedirect is not implemented yet.");
170 }
171
172 public void sendError(int i) throws IOException
173 {
174 // TODO implement MockServletResponse.sendError
175 throw new RuntimeException("MockServletResponse.sendError is not implemented yet.");
176 }
177
178 public void sendError(int i, String s) throws IOException
179 {
180 // TODO implement MockServletResponse.sendError
181 throw new RuntimeException("MockServletResponse.sendError is not implemented yet.");
182 }
183
184 public String encodeRedirectUrl(String s)
185 {
186 // TODO implement MockServletResponse.encodeRedirectUrl
187 throw new RuntimeException("MockServletResponse.encodeRedirectUrl is not implemented yet.");
188 }
189
190 public String encodeUrl(String s)
191 {
192 // TODO implement MockServletResponse.encodeUrl
193 throw new RuntimeException("MockServletResponse.encodeUrl is not implemented yet.");
194 }
195
196 public String encodeRedirectURL(String s)
197 {
198 // TODO implement MockServletResponse.encodeRedirectURL
199 throw new RuntimeException("MockServletResponse.encodeRedirectURL is not implemented yet.");
200 }
201
202 public String encodeURL(String s)
203 {
204 // TODO implement MockServletResponse.encodeURL
205 throw new RuntimeException("MockServletResponse.encodeURL is not implemented yet.");
206 }
207
208 public boolean containsHeader(String s)
209 {
210 // TODO implement MockServletResponse.containsHeader
211 throw new RuntimeException("MockServletResponse.containsHeader is not implemented yet.");
212 }
213
214 public void addCookie(Cookie cookie)
215 {
216 // TODO implement MockServletResponse.addCookie
217 throw new RuntimeException("MockServletResponse.addCookie is not implemented yet.");
218 }
219
220 public Document getResultingDocument()
221 {
222 Tidy tidy = new Tidy();
223 org.w3c.dom.Document doc = tidy.parseDOM(getRenderedInputStream(), System.out);
224 DOMBuilder domBuilder = new DOMBuilder();
225 return new Document(domBuilder.build(doc.getDocumentElement()));
226 }
227
228 public void dumpResult()
229 {
230 try
231 {
232 InputStream renderedInputStream = getRenderedInputStream();
233 int read;
234 while((read = renderedInputStream.read()) != -1)
235 {
236 System.out.write(read);
237 }
238 }
239 catch(IOException ignore)
240 {
241 }
242 }
243
244 private InputStream getRenderedInputStream()
245 {
246 printWriter.flush();
247 byte[] bytes = output.toByteArray();
248 return new ByteArrayInputStream(bytes);
249 }
250
251 void resetStreams()
252 {
253 output = new ByteArrayOutputStream();
254 printWriter = new PrintWriter(output);
255 }
256 }
This page was automatically generated by Maven