1 7 package com.inversoft.junit.internal.http; 8 9 10 import java.io.IOException ; 11 import java.io.PrintWriter ; 12 import java.util.HashMap ; 13 import java.util.Locale ; 14 import java.util.Map ; 15 import javax.servlet.ServletOutputStream ; 16 import javax.servlet.http.Cookie ; 17 import javax.servlet.http.HttpServletResponse ; 18 19 20 27 public class MockHttpServletResponse implements HttpServletResponse { 28 29 33 public static final String ENCODE_STR = "#encode"; 34 35 40 public static final String ENCODE_REDIRECT_STR = "#encodeRedirect"; 41 42 private MockServletOutputStream outputStream; 43 private PrintWriter writer; 44 private Map cookies; 45 private Locale locale; 46 private int size; 47 private int status; 48 private String msg; 49 private String url; 50 private boolean encode; 51 52 53 55 public MockHttpServletResponse() { 56 initialize(); 57 } 58 59 60 62 private void initialize() { 63 this.outputStream = new MockServletOutputStream(); 64 this.writer = new PrintWriter (outputStream, true); 65 this.cookies = new HashMap (); 66 this.size = 0; 67 } 68 69 70 74 76 public void flushBuffer() throws IOException { 77 writer.flush(); 78 outputStream.flush(); 79 } 80 81 83 public int getBufferSize() { 84 return size; 85 } 86 87 89 public String getCharacterEncoding() { 90 if (locale == null) { 91 return "ISO-8859-1"; 92 } 93 94 return locale.getDisplayLanguage(); 95 } 96 97 100 public java.util.Locale getLocale() { 101 if (locale == null) { 102 return Locale.getDefault(); 103 } 104 105 return locale; 106 } 107 108 110 public ServletOutputStream getOutputStream() throws IOException { 111 return outputStream; 112 } 113 114 116 public PrintWriter getWriter() throws IOException { 117 return writer; 118 } 119 120 122 public boolean isCommitted() { 123 return false; 124 } 125 126 128 public void reset() { 129 initialize(); 130 } 131 132 134 public void resetBuffer(){ 135 outputStream = new MockServletOutputStream(); 136 writer = new PrintWriter (outputStream); 137 } 138 139 141 public void setBufferSize(int size) { 142 this.size = size; 143 } 144 145 147 public void setContentLength(int length) { 148 } 149 150 152 public void setContentType(String contentType) { 153 } 154 155 157 public void setLocale(Locale locale) { 158 this.locale = locale; 159 } 160 161 162 166 167 169 public void addCookie(Cookie cookie) { 170 cookies.put(cookie.getName(), cookie); 171 } 172 173 175 public void addDateHeader(String name, long date) { 176 throw new UnsupportedOperationException (); 177 } 178 179 181 public void addHeader(String name, String value) { 182 throw new UnsupportedOperationException (); 183 } 184 185 187 public void addIntHeader(String name, int value) { 188 throw new UnsupportedOperationException (); 189 } 190 191 193 public boolean containsHeader(String name) { 194 throw new UnsupportedOperationException (); 195 } 196 197 200 public String encodeRedirectUrl(String url) { 201 throw new UnsupportedOperationException (); 202 } 203 204 209 public String encodeRedirectURL(String url) { 210 if (encode) { 211 url = url + ENCODE_REDIRECT_STR; 212 } 213 214 return url; 215 } 216 217 220 public String encodeUrl(String url) { 221 throw new UnsupportedOperationException (); 222 } 223 224 229 public String encodeURL(String url) { 230 if (encode) { 231 url = url + ENCODE_STR; 232 } 233 234 return url; 235 } 236 237 242 public void setEncode(boolean encode) { 243 this.encode = encode; 244 } 245 246 251 public boolean isEncode() { 252 return encode; 253 } 254 255 257 public void sendError(int errorCode) throws IOException { 258 status = errorCode; 259 } 260 261 263 public void sendError(int errorCode, String msg) throws IOException { 264 sendError(errorCode); 265 this.msg = msg; 266 } 267 268 270 public void sendRedirect(String url) throws IOException { 271 this.url = url; 272 } 273 274 276 public void setDateHeader(String name, long date) { 277 throw new UnsupportedOperationException (); 278 } 279 280 282 public void setHeader(String name, String value) { 283 throw new UnsupportedOperationException (); 284 } 285 286 288 public void setIntHeader(String name, int value) { 289 throw new UnsupportedOperationException (); 290 } 291 292 294 public void setStatus(int status) { 295 this.status = status; 296 } 297 298 301 public void setStatus(int status, String msg) { 302 throw new UnsupportedOperationException (); 303 } 304 305 306 310 311 314 public int getStatus() { 315 return status; 316 } 317 318 321 public Cookie getCookie(String name) { 322 return (Cookie ) cookies.get(name); 323 } 324 325 328 public Cookie [] getCookies() { 329 return (Cookie []) cookies.values().toArray(new Cookie [0]); 330 } 331 332 335 public Map getCookiesMap() { 336 return cookies; 337 } 338 339 342 public String getText() { 343 return outputStream.getText(); 344 } 345 346 349 public String getRedirectURL() { 350 return url; 351 } 352 353 356 public String getErrorMessage() { 357 return msg; 358 } 359 } 360 | Popular Tags |