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

http://sigma-h.googlecode.com/ · Java · 384 lines · 289 code · 91 blank · 4 comment · 22 complexity · 7ac16269d614a6846344ac9a7f3047d0 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.RequestDispatcher;
  7. import javax.servlet.ServletInputStream;
  8. import javax.servlet.http.Cookie;
  9. import javax.servlet.http.HttpServletRequest;
  10. import javax.servlet.http.HttpSession;
  11. import java.io.BufferedReader;
  12. import java.security.Principal;
  13. import java.util.*;
  14. public class MockHttpServletRequest implements HttpServletRequest {
  15. private HashMap<String, Object> attributes = new HashMap<String, Object>();
  16. private String contextPath = null;
  17. private Locale locale = null;
  18. private HashMap<String, String[]> parameters = new HashMap<String, String[]>();
  19. private String pathInfo = null;
  20. private Principal principal = null;
  21. private String queryString = null;
  22. private String servletPath = null;
  23. private HttpSession session = null;
  24. private String method = null;
  25. private String contentType = null;
  26. private ArrayList<Cookie> cookies = new ArrayList<Cookie>(0);
  27. private String server = "localhost";
  28. private int port = 8080;
  29. private boolean secure = false;
  30. private StringBuffer requestURL;
  31. public MockHttpServletRequest() {
  32. super();
  33. }
  34. public MockHttpServletRequest(HttpSession session) {
  35. super();
  36. setHttpSession(session);
  37. }
  38. public void setParameter(String name, String value) {
  39. parameters.remove(name);
  40. addParameter(name, value);
  41. }
  42. public void addParameter(String name, String value) {
  43. String[] values = (String[]) parameters.get(name);
  44. if (values == null) {
  45. String[] results = new String[]{value};
  46. parameters.put(name, results);
  47. return;
  48. }
  49. String[] results = new String[values.length + 1];
  50. System.arraycopy(values, 0, results, 0, values.length);
  51. results[values.length] = value;
  52. parameters.put(name, results);
  53. }
  54. public void setHttpSession(HttpSession session) {
  55. this.session = session;
  56. }
  57. public void setLocale(Locale locale) {
  58. this.locale = locale;
  59. }
  60. public void setMethod(String method) {
  61. this.method = method;
  62. }
  63. public void setContentType(String contentType) {
  64. this.contentType = contentType;
  65. }
  66. public void setContextPath(String contextPath) {
  67. this.contextPath = contextPath;
  68. }
  69. public void setPathInfo(String pathInfo) {
  70. this.pathInfo = pathInfo;
  71. }
  72. public void setQueryString(String queryString) {
  73. this.queryString = queryString;
  74. }
  75. public void setServletPath(String servletPath) {
  76. this.servletPath = servletPath;
  77. }
  78. public void setServer(String server) {
  79. this.server = server;
  80. }
  81. public void setPort(int port) {
  82. this.port = port;
  83. }
  84. public void setSecure(boolean secure) {
  85. this.secure = secure;
  86. }
  87. public void setUserPrincipal(Principal principal) {
  88. this.principal = principal;
  89. }
  90. public String getAuthType() {
  91. throw new UnsupportedOperationException();
  92. }
  93. public String getContextPath() {
  94. return (contextPath);
  95. }
  96. public Cookie[] getCookies() {
  97. return cookies.toArray(new Cookie[0]);
  98. }
  99. public void addCookie(String name, String value) {
  100. cookies.add(new Cookie(name, value));
  101. }
  102. public long getDateHeader(String name) {
  103. throw new UnsupportedOperationException();
  104. }
  105. public String getHeader(String name) {
  106. throw new UnsupportedOperationException();
  107. }
  108. public Enumeration getHeaderNames() {
  109. throw new UnsupportedOperationException();
  110. }
  111. public Enumeration getHeaders(String name) {
  112. throw new UnsupportedOperationException();
  113. }
  114. public int getIntHeader(String name) {
  115. throw new UnsupportedOperationException();
  116. }
  117. public String getMethod() {
  118. return method;
  119. }
  120. public String getPathInfo() {
  121. return pathInfo;
  122. }
  123. public String getPathTranslated() {
  124. throw new UnsupportedOperationException();
  125. }
  126. public String getQueryString() {
  127. return queryString;
  128. }
  129. public String getRemoteUser() {
  130. if (principal != null) {
  131. return (principal.getName());
  132. } else {
  133. return (null);
  134. }
  135. }
  136. public String getRequestedSessionId() {
  137. throw new UnsupportedOperationException();
  138. }
  139. public String getRequestURI() {
  140. StringBuffer sb = new StringBuffer();
  141. if (contextPath != null) {
  142. sb.append(contextPath);
  143. }
  144. if (servletPath != null) {
  145. sb.append(servletPath);
  146. }
  147. if (pathInfo != null) {
  148. sb.append(pathInfo);
  149. }
  150. return (sb.toString());
  151. }
  152. public StringBuffer getRequestURL() {
  153. if (requestURL == null) {
  154. throw new IllegalStateException("RequestURL has not been set");
  155. }
  156. return requestURL;
  157. }
  158. public void setRequestURL(String url) {
  159. requestURL = new StringBuffer(url);
  160. }
  161. public String getServletPath() {
  162. return servletPath;
  163. }
  164. public HttpSession getSession() {
  165. return getSession(true);
  166. }
  167. @Override
  168. public int getRemotePort() {
  169. throw new UnsupportedOperationException();
  170. }
  171. @Override
  172. public String getLocalName() {
  173. throw new UnsupportedOperationException();
  174. }
  175. @Override
  176. public String getLocalAddr() {
  177. throw new UnsupportedOperationException();
  178. }
  179. @Override
  180. public int getLocalPort() {
  181. return 0;
  182. }
  183. public HttpSession getSession(boolean create) {
  184. if (create && (session == null)) {
  185. session = new MockHttpSession();
  186. }
  187. return session;
  188. }
  189. public Principal getUserPrincipal() {
  190. return (principal);
  191. }
  192. public boolean isRequestedSessionIdFromCookie() {
  193. throw new UnsupportedOperationException();
  194. }
  195. @Deprecated
  196. public boolean isRequestedSessionIdFromUrl() {
  197. throw new UnsupportedOperationException();
  198. }
  199. public boolean isRequestedSessionIdFromURL() {
  200. throw new UnsupportedOperationException();
  201. }
  202. public boolean isRequestedSessionIdValid() {
  203. throw new UnsupportedOperationException();
  204. }
  205. public boolean isUserInRole(String role) {
  206. return false;
  207. }
  208. public Object getAttribute(String name) {
  209. return (attributes.get(name));
  210. }
  211. public Enumeration getAttributeNames() {
  212. throw new UnsupportedOperationException();
  213. }
  214. public String getCharacterEncoding() {
  215. throw new UnsupportedOperationException();
  216. }
  217. public int getContentLength() {
  218. throw new UnsupportedOperationException();
  219. }
  220. public String getContentType() {
  221. return (contentType);
  222. }
  223. public ServletInputStream getInputStream() {
  224. throw new UnsupportedOperationException();
  225. }
  226. public Locale getLocale() {
  227. return (locale);
  228. }
  229. public Enumeration getLocales() {
  230. throw new UnsupportedOperationException();
  231. }
  232. public String getParameter(String name) {
  233. String[] values = (String[]) parameters.get(name);
  234. if (values != null) {
  235. return (values[0]);
  236. } else {
  237. return (null);
  238. }
  239. }
  240. public Map getParameterMap() {
  241. return (parameters);
  242. }
  243. public Enumeration getParameterNames() {
  244. return Collections.enumeration(parameters.keySet());
  245. }
  246. public String[] getParameterValues(String name) {
  247. return ((String[]) parameters.get(name));
  248. }
  249. public String getProtocol() {
  250. throw new UnsupportedOperationException();
  251. }
  252. public BufferedReader getReader() {
  253. throw new UnsupportedOperationException();
  254. }
  255. @Deprecated
  256. public String getRealPath(String path) {
  257. throw new UnsupportedOperationException();
  258. }
  259. public String getRemoteAddr() {
  260. throw new UnsupportedOperationException();
  261. }
  262. public String getRemoteHost() {
  263. throw new UnsupportedOperationException();
  264. }
  265. public RequestDispatcher getRequestDispatcher(String path) {
  266. throw new UnsupportedOperationException();
  267. }
  268. public String getScheme() {
  269. return "http";
  270. }
  271. public String getServerName() {
  272. return server;
  273. }
  274. public int getServerPort() {
  275. return port;
  276. }
  277. public boolean isSecure() {
  278. return secure;
  279. }
  280. public void removeAttribute(String name) {
  281. attributes.remove(name);
  282. }
  283. public void setAttribute(String name, Object value) {
  284. if (value == null) {
  285. attributes.remove(name);
  286. } else {
  287. attributes.put(name, value);
  288. }
  289. }
  290. public void setCharacterEncoding(String name) {
  291. throw new UnsupportedOperationException();
  292. }
  293. }