View Javadoc
1   /**
2    * 
3    */
4   package org.melati.servlet.test;
5   
6   import java.io.BufferedReader;
7   import java.io.IOException;
8   import java.io.UnsupportedEncodingException;
9   import java.security.Principal;
10  import java.util.Collections;
11  import java.util.Enumeration;
12  import java.util.HashMap;
13  import java.util.Hashtable;
14  import java.util.Locale;
15  import java.util.Map;
16  
17  import javax.servlet.RequestDispatcher;
18  import javax.servlet.ServletException;
19  import javax.servlet.ServletInputStream;
20  import javax.servlet.ServletRequest;
21  import javax.servlet.ServletResponse;
22  import javax.servlet.http.Cookie;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpSession;
25  
26  /**
27   * @author timp
28   * @since 2006/12/05
29   *
30   */
31  public class MockHttpServletRequest implements HttpServletRequest {
32  
33      Map<String,String> parameters = new HashMap<String,String>();
34      
35      /**
36       * @param map the parameters
37       */
38      public void setParameters(Map<String,String> map) {
39          parameters = map;
40      }
41      
42      public String getAuthType() {
43          return null;
44      }
45  
46      public Cookie[] getCookies() {
47          return null;
48      }
49  
50      public long getDateHeader(String arg0) {
51          return 0;
52      }
53  
54      // Note this is not correct, should be a MultiMap
55      Hashtable<String,String> headers = new Hashtable<String,String>();
56      public String getHeader(String arg0) {
57          return (String)headers.get(arg0);
58      }
59      /**
60       * @param key the header key
61       * @param value the value to set it to 
62       */
63      public void setHeader(String key, String value) {
64        headers.put(key, value);
65      }
66      public Enumeration<String> getHeaders(String arg0) {
67          return headers.elements();
68      }
69  
70      public Enumeration<String> getHeaderNames() {
71          return headers.keys();
72      }
73  
74      public int getIntHeader(String arg0) {
75          return -1;
76      }
77  
78      public String getMethod() {
79          return null;
80      }
81  
82      String pathInfo;
83      public String getPathInfo() {
84          return pathInfo;
85      }
86      /**
87       * @param info the info to set
88       */
89      public void setPathInfo(String info) {
90        pathInfo = info;
91      }
92  
93      public String getPathTranslated() {
94          return null;
95      }
96  
97      public String getContextPath() {
98          return "/servletContext"; 
99      }
100 
101     public String getQueryString() {
102         return null;
103     }
104 
105     public String getRemoteUser() {
106         return null;
107     }
108 
109     public boolean isUserInRole(String arg0) {
110         return false;
111     }
112 
113     public Principal getUserPrincipal() {
114         return null;
115     }
116 
117     public String getRequestedSessionId() {
118         return null;
119     }
120 
121     String requestURI = null;
122     public String getRequestURI() {
123         return requestURI;
124     }
125     /**
126      * @param uri the uri to set
127      */
128     public void setRequestURI(String uri) {
129       requestURI = uri;
130     }
131 
132     public StringBuffer getRequestURL() {
133         return null;
134     }
135 
136     public String getServletPath() {
137         return "/mockServletPath/";
138     }
139     Object session;
140     /**
141      * @param s the session to set
142      */
143     public void setSession(Object s){
144       session = s;
145     }
146     public HttpSession getSession(boolean create) {
147       if (create)
148         return new MockHttpSession();
149       else
150         return (HttpSession)session;
151     }
152 
153     public HttpSession getSession() {
154       return (HttpSession)session;
155     }
156 
157     public boolean isRequestedSessionIdValid() {
158         return false;
159     }
160 
161     public boolean isRequestedSessionIdFromCookie() {
162         return false;
163     }
164 
165     public boolean isRequestedSessionIdFromURL() {
166         return false;
167     }
168 
169     public boolean isRequestedSessionIdFromUrl() {
170         return false;
171     }
172 
173     public Object getAttribute(String arg0) {
174         return null;
175     }
176 
177     public Enumeration<String> getAttributeNames() {
178         return null;
179     }
180 
181     String charEncoding = "ISO-8859-1";
182     public String getCharacterEncoding() {
183       return charEncoding;
184     }
185 
186     public void setCharacterEncoding(String ce) throws UnsupportedEncodingException {
187       if (ce != null && ce.equals("UnsupportedEncoding"))
188         throw new UnsupportedEncodingException();
189       charEncoding = ce;
190     }
191 
192     public int getContentLength() {
193         return 0;
194     }
195 
196     public String getContentType() {
197         return null;
198     }
199 
200     public ServletInputStream getInputStream() throws IOException {
201         return null;
202     }
203 
204     /**
205      * Set a parameter.
206      */
207     public void setParameter(String name, String value) { 
208       parameters.put(name, value);
209     }
210     public String getParameter(String arg0) {
211       if (parameters.get(arg0) == null)
212         return null;
213       return (String)parameters.get(arg0);
214     }
215 
216     public Enumeration<String> getParameterNames() {
217         return Collections.enumeration(parameters.keySet());
218     }
219 
220     public String[] getParameterValues(String key) {
221       return new String[] {(String)parameters.get(key)} ;
222     }
223 
224     public Map<String,String> getParameterMap() {
225         return parameters;
226     }
227 
228     public String getProtocol() {
229         return null;
230     }
231 
232     String scheme = "http";
233     /**
234      * @param s the scheme to set
235      */
236     public void setScheme(String s) {
237       scheme = s;
238     }
239     public String getScheme() {
240         return scheme;
241     }
242 
243     public String getServerName() {
244         return "localhost";
245     }
246 
247     public int getServerPort() {
248         return 80;
249     }
250 
251     public BufferedReader getReader() throws IOException {
252         return null;
253     }
254 
255     public String getRemoteAddr() {
256         return null;
257     }
258 
259     public String getRemoteHost() {
260         return null;
261     }
262 
263     public void setAttribute(String arg0, Object arg1) {
264     }
265 
266     public void removeAttribute(String arg0) {
267     }
268 
269     Locale locale = null;
270     public Locale getLocale() {
271         return locale;
272     }
273     public void setLocale(Locale l) { 
274       locale = l;
275     }
276     
277     public Enumeration<Locale> getLocales() {
278         return null;
279     }
280 
281     public boolean isSecure() {
282         return false;
283     }
284 
285     public RequestDispatcher getRequestDispatcher(String arg0) {
286         return new RequestDispatcher() {
287         
288             public void include(ServletRequest arg00, ServletResponse arg1)
289                     throws ServletException, IOException {
290             }
291         
292             public void forward(ServletRequest arg00, ServletResponse arg1)
293                     throws ServletException, IOException {
294             }
295         };
296     }
297 
298     public String getRealPath(String arg0) {
299         return "test";
300     }
301 
302     public String getLocalAddr() {
303       throw new RuntimeException("TODO No one else has ever called this method." +
304                                  " Do you really want to start now?");
305       
306     }
307 
308     public String getLocalName() {
309       throw new RuntimeException("TODO No one else has ever called this method." +
310                                  " Do you really want to start now?");
311       
312     }
313 
314     public int getLocalPort() {
315       throw new RuntimeException("TODO No one else has ever called this method." +
316                                  " Do you really want to start now?");
317       
318     }
319 
320     public int getRemotePort() {
321       throw new RuntimeException("TODO No one else has ever called this method." +
322                                  " Do you really want to start now?");
323       
324     }
325     
326 }