/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. package org.sigmah.server.mock;
  6. import javax.servlet.ServletOutputStream;
  7. import javax.servlet.http.Cookie;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.io.PrintWriter;
  11. import java.util.ArrayList;
  12. import java.util.HashMap;
  13. import java.util.Locale;
  14. import java.util.Map;
  15. /**
  16. * @author Alex Bertram
  17. */
  18. public class MockHttpServletResponse implements HttpServletResponse {
  19. public ArrayList<Cookie> cookies = new ArrayList<Cookie>();
  20. public Map<String, String> headers = new HashMap<String, String>();
  21. public int statusCode = 200;
  22. public String errorMessage;
  23. public String redirectUrl;
  24. public String contentType;
  25. public String characterEncoding;
  26. public int contentLength;
  27. public MockServletOutputStream os = new MockServletOutputStream();
  28. @Override
  29. public void addCookie(Cookie cookie) {
  30. cookies.add(cookie);
  31. }
  32. @Override
  33. public boolean containsHeader(String s) {
  34. return headers.keySet().contains(s);
  35. }
  36. @Override
  37. public String encodeURL(String s) {
  38. throw new UnsupportedOperationException();
  39. }
  40. @Override
  41. public String encodeRedirectURL(String s) {
  42. throw new UnsupportedOperationException();
  43. }
  44. @Override
  45. @Deprecated
  46. public String encodeUrl(String s) {
  47. throw new UnsupportedOperationException();
  48. }
  49. @Override
  50. @Deprecated
  51. public String encodeRedirectUrl(String s) {
  52. throw new UnsupportedOperationException();
  53. }
  54. @Override
  55. public void sendError(int statusCode, String message) throws IOException {
  56. this.statusCode = statusCode;
  57. this.errorMessage = message;
  58. }
  59. @Override
  60. public void sendError(int statusCode) throws IOException {
  61. this.statusCode = statusCode;
  62. }
  63. @Override
  64. public void sendRedirect(String url) throws IOException {
  65. this.redirectUrl = url;
  66. }
  67. @Override
  68. public void setDateHeader(String s, long l) {
  69. throw new UnsupportedOperationException();
  70. }
  71. @Override
  72. public void addDateHeader(String s, long l) {
  73. throw new UnsupportedOperationException();
  74. }
  75. @Override
  76. public void setHeader(String name, String content) {
  77. headers.put(name, content);
  78. }
  79. @Override
  80. public void addHeader(String name, String content) {
  81. setHeader(name, content);
  82. }
  83. @Override
  84. public void setIntHeader(String name, int value) {
  85. headers.put(name, Integer.toString(value));
  86. }
  87. @Override
  88. public void addIntHeader(String name, int value) {
  89. setIntHeader(name, value);
  90. }
  91. @Override
  92. public void setStatus(int code) {
  93. this.statusCode = code;
  94. }
  95. @Override
  96. @Deprecated
  97. public void setStatus(int i, String s) {
  98. this.statusCode = i;
  99. this.errorMessage = s;
  100. }
  101. @Override
  102. public String getCharacterEncoding() {
  103. return characterEncoding;
  104. }
  105. @Override
  106. public String getContentType() {
  107. return contentType;
  108. }
  109. @Override
  110. public ServletOutputStream getOutputStream() throws IOException {
  111. return os;
  112. }
  113. @Override
  114. public PrintWriter getWriter() throws IOException {
  115. return new PrintWriter(os);
  116. }
  117. @Override
  118. public void setCharacterEncoding(String s) {
  119. this.characterEncoding = s;
  120. }
  121. @Override
  122. public void setContentLength(int contentLength) {
  123. this.contentLength = contentLength;
  124. }
  125. @Override
  126. public void setContentType(String contentType) {
  127. this.contentType = contentType;
  128. }
  129. @Override
  130. public void setBufferSize(int i) {
  131. }
  132. @Override
  133. public int getBufferSize() {
  134. return 0;
  135. }
  136. @Override
  137. public void flushBuffer() throws IOException {
  138. }
  139. @Override
  140. public void resetBuffer() {
  141. }
  142. @Override
  143. public boolean isCommitted() {
  144. return false;
  145. }
  146. @Override
  147. public void reset() {
  148. }
  149. @Override
  150. public void setLocale(Locale locale) {
  151. }
  152. @Override
  153. public Locale getLocale() {
  154. throw new UnsupportedOperationException();
  155. }
  156. public String getCookie(String name) {
  157. for (Cookie cookie : cookies) {
  158. if (cookie.getName().equals(name)) {
  159. return cookie.getValue();
  160. }
  161. }
  162. return null;
  163. }
  164. }