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 MockServletRequest implements HttpServletRequest {
32  
33      Map parameters = new HashMap();
34      
35      /**
36       * @param map the parameters
37       */
38      public void setParameters(Map 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 headers = new Hashtable();
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 getHeaders(String arg0) {
67          return headers.elements();
68      }
69  
70      public Enumeration 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 ""; // root context
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 arg0) {
147       return (HttpSession)session;
148     }
149 
150     public HttpSession getSession() {
151       return (HttpSession)session;
152     }
153 
154     public boolean isRequestedSessionIdValid() {
155         return false;
156     }
157 
158     public boolean isRequestedSessionIdFromCookie() {
159         return false;
160     }
161 
162     public boolean isRequestedSessionIdFromURL() {
163         return false;
164     }
165 
166     public boolean isRequestedSessionIdFromUrl() {
167         return false;
168     }
169 
170     public Object getAttribute(String arg0) {
171         return null;
172     }
173 
174     public Enumeration getAttributeNames() {
175         return null;
176     }
177 
178     String charEncoding = "ISO-8859-1";
179     public String getCharacterEncoding() {
180       return charEncoding;
181     }
182 
183     public void setCharacterEncoding(String ce) throws UnsupportedEncodingException {
184       if (ce != null && ce.equals("UnsupportedEncoding"))
185         throw new UnsupportedEncodingException();
186       charEncoding = ce;
187     }
188 
189     public int getContentLength() {
190         return 0;
191     }
192 
193     public String getContentType() {
194         return null;
195     }
196 
197     public ServletInputStream getInputStream() throws IOException {
198         return null;
199     }
200 
201     /**
202      * Set a parameter.
203      */
204     public void setParameter(String name, String value) { 
205       parameters.put(name, value);
206     }
207     public String getParameter(String arg0) {
208       if (parameters.get(arg0) == null)
209         return null;
210       return (String)parameters.get(arg0);
211     }
212 
213     public Enumeration getParameterNames() {
214         return Collections.enumeration(parameters.keySet());
215     }
216 
217     public String[] getParameterValues(String key) {
218       return new String[] {(String)parameters.get(key)} ;
219     }
220 
221     public Map getParameterMap() {
222         return parameters;
223     }
224 
225     public String getProtocol() {
226         return null;
227     }
228 
229     String scheme = "http";
230     /**
231      * @param s the scheme to set
232      */
233     public void setScheme(String s) {
234       scheme = s;
235     }
236     public String getScheme() {
237         return scheme;
238     }
239 
240     public String getServerName() {
241         return "localhost";
242     }
243 
244     public int getServerPort() {
245         return 80;
246     }
247 
248     public BufferedReader getReader() throws IOException {
249         return null;
250     }
251 
252     public String getRemoteAddr() {
253         return null;
254     }
255 
256     public String getRemoteHost() {
257         return null;
258     }
259 
260     public void setAttribute(String arg0, Object arg1) {
261     }
262 
263     public void removeAttribute(String arg0) {
264     }
265 
266     public Locale getLocale() {
267         return null;
268     }
269 
270     public Enumeration getLocales() {
271         return null;
272     }
273 
274     public boolean isSecure() {
275         return false;
276     }
277 
278     public RequestDispatcher getRequestDispatcher(String arg0) {
279         return new RequestDispatcher() {
280         
281             public void include(ServletRequest arg0, ServletResponse arg1)
282                     throws ServletException, IOException {
283             }
284         
285             public void forward(ServletRequest arg0, ServletResponse arg1)
286                     throws ServletException, IOException {
287             }
288         };
289     }
290 
291     public String getRealPath(String arg0) {
292         return "test";
293     }
294 
295     public String getLocalAddr() {
296       throw new RuntimeException("TODO No one else has ever called this method." +
297                                  " Do you really want to start now?");
298       
299     }
300 
301     public String getLocalName() {
302       throw new RuntimeException("TODO No one else has ever called this method." +
303                                  " Do you really want to start now?");
304       
305     }
306 
307     public int getLocalPort() {
308       throw new RuntimeException("TODO No one else has ever called this method." +
309                                  " Do you really want to start now?");
310       
311     }
312 
313     public int getRemotePort() {
314       throw new RuntimeException("TODO No one else has ever called this method." +
315                                  " Do you really want to start now?");
316       
317     }
318     
319 }