PageRenderTime 24ms CodeModel.GetById 10ms app.highlight 10ms RepoModel.GetById 1ms app.codeStats 1ms

/sigmah/src/test/java/org/sigmah/server/mock/MockHttpServletResponse.java

http://sigma-h.googlecode.com/
Java | 207 lines | 157 code | 43 blank | 7 comment | 2 complexity | fdd3a2a047a066b36dc861584d80d6d4 MD5 | raw file
  1/*
  2 * All Sigmah code is released under the GNU General Public License v3
  3 * See COPYRIGHT.txt and LICENSE.txt.
  4 */
  5
  6package org.sigmah.server.mock;
  7
  8import javax.servlet.ServletOutputStream;
  9import javax.servlet.http.Cookie;
 10import javax.servlet.http.HttpServletResponse;
 11import java.io.IOException;
 12import java.io.PrintWriter;
 13import java.util.ArrayList;
 14import java.util.HashMap;
 15import java.util.Locale;
 16import java.util.Map;
 17
 18/**
 19 * @author Alex Bertram
 20 */
 21public class MockHttpServletResponse implements HttpServletResponse {
 22
 23    public ArrayList<Cookie> cookies = new ArrayList<Cookie>();
 24    public Map<String, String> headers = new HashMap<String, String>();
 25    public int statusCode = 200;
 26    public String errorMessage;
 27    public String redirectUrl;
 28    public String contentType;
 29    public String characterEncoding;
 30    public int contentLength;
 31    public MockServletOutputStream os = new MockServletOutputStream();
 32
 33
 34    @Override
 35    public void addCookie(Cookie cookie) {
 36        cookies.add(cookie);
 37    }
 38
 39    @Override
 40    public boolean containsHeader(String s) {
 41        return headers.keySet().contains(s);
 42    }
 43
 44    @Override
 45    public String encodeURL(String s) {
 46        throw new UnsupportedOperationException();
 47    }
 48
 49    @Override
 50    public String encodeRedirectURL(String s) {
 51        throw new UnsupportedOperationException();
 52    }
 53
 54    @Override
 55    @Deprecated
 56    public String encodeUrl(String s) {
 57        throw new UnsupportedOperationException();
 58    }
 59
 60    @Override
 61    @Deprecated
 62    public String encodeRedirectUrl(String s) {
 63        throw new UnsupportedOperationException();
 64    }
 65
 66    @Override
 67    public void sendError(int statusCode, String message) throws IOException {
 68        this.statusCode = statusCode;
 69        this.errorMessage = message;
 70    }
 71
 72    @Override
 73    public void sendError(int statusCode) throws IOException {
 74        this.statusCode = statusCode;
 75    }
 76
 77    @Override
 78    public void sendRedirect(String url) throws IOException {
 79        this.redirectUrl = url;
 80    }
 81
 82    @Override
 83    public void setDateHeader(String s, long l) {
 84        throw new UnsupportedOperationException();
 85    }
 86
 87    @Override
 88    public void addDateHeader(String s, long l) {
 89        throw new UnsupportedOperationException();
 90    }
 91
 92    @Override
 93    public void setHeader(String name, String content) {
 94        headers.put(name, content);
 95    }
 96
 97    @Override
 98    public void addHeader(String name, String content) {
 99        setHeader(name, content);
100    }
101
102    @Override
103    public void setIntHeader(String name, int value) {
104        headers.put(name, Integer.toString(value));
105    }
106
107    @Override
108    public void addIntHeader(String name, int value) {
109        setIntHeader(name, value);
110    }
111
112    @Override
113    public void setStatus(int code) {
114        this.statusCode = code;
115    }
116
117    @Override
118    @Deprecated
119    public void setStatus(int i, String s) {
120        this.statusCode = i;
121        this.errorMessage = s;
122    }
123
124    @Override
125    public String getCharacterEncoding() {
126        return characterEncoding;
127    }
128
129    @Override
130    public String getContentType() {
131        return contentType;
132    }
133
134    @Override
135    public ServletOutputStream getOutputStream() throws IOException {
136        return os;
137    }
138
139    @Override
140    public PrintWriter getWriter() throws IOException {
141        return new PrintWriter(os);
142    }
143
144    @Override
145    public void setCharacterEncoding(String s) {
146        this.characterEncoding = s;
147    }
148
149    @Override
150    public void setContentLength(int contentLength) {
151        this.contentLength = contentLength;
152    }
153
154    @Override
155    public void setContentType(String contentType) {
156        this.contentType = contentType;
157    }
158
159    @Override
160    public void setBufferSize(int i) {
161
162    }
163
164    @Override
165    public int getBufferSize() {
166        return 0;
167    }
168
169    @Override
170    public void flushBuffer() throws IOException {
171
172    }
173
174    @Override
175    public void resetBuffer() {
176
177    }
178
179    @Override
180    public boolean isCommitted() {
181        return false;
182    }
183
184    @Override
185    public void reset() {
186
187    }
188
189    @Override
190    public void setLocale(Locale locale) {
191
192    }
193
194    @Override
195    public Locale getLocale() {
196        throw new UnsupportedOperationException();
197    }
198
199    public String getCookie(String name) {
200        for (Cookie cookie : cookies) {
201            if (cookie.getName().equals(name)) {
202                return cookie.getValue();
203            }
204        }
205        return null;
206    }
207}