View Javadoc

1   /*
2    * $Id: MockHttpServletRequest.java 421119 2006-07-12 04:49:11Z wsmoak $
3    *
4    * Copyright 1999-2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts.mock;
19  
20  import javax.servlet.RequestDispatcher;
21  import javax.servlet.ServletInputStream;
22  import javax.servlet.http.Cookie;
23  import javax.servlet.http.HttpServletRequest;
24  import javax.servlet.http.HttpSession;
25  
26  import java.io.BufferedReader;
27  
28  import java.security.Principal;
29  
30  import java.util.Enumeration;
31  import java.util.HashMap;
32  import java.util.Locale;
33  import java.util.Map;
34  
35  /***
36   * <p>Mock <strong>HttpServletRequest</strong> object for low-level unit tests
37   * of Struts controller components.  Coarser grained tests should be
38   * implemented in terms of the Cactus framework, instead of the mock object
39   * classes.</p>
40   *
41   * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
42   * create unit tests is provided, plus additional methods to configure this
43   * object as necessary.  Methods for unsupported operations will throw
44   * <code>UnsupportedOperationException</code>.</p>
45   *
46   * <p><strong>WARNING</strong> - Because unit tests operate in a single
47   * threaded environment, no synchronization is performed.</p>
48   *
49   * @version $Rev: 421119 $ $Date: 2006-07-11 21:49:11 -0700 (Tue, 11 Jul 2006) $
50   */
51  public class MockHttpServletRequest implements HttpServletRequest {
52      // ----------------------------------------------------- Instance Variables
53  
54      /***
55       * <p> The set of request attributes. </p>
56       */
57      protected HashMap attributes = new HashMap();
58  
59      /***
60       * <p> The context path for this request. </p>
61       */
62      protected String contextPath = null;
63  
64      /***
65       * <p> The preferred locale for this request. </p>
66       */
67      protected Locale locale = null;
68  
69      /***
70       * <p> The set of arrays of parameter values, keyed by parameter name.
71       * </p>
72       */
73      protected HashMap parameters = new HashMap();
74  
75      /***
76       * <p> The extra path information for this request. v     * </p>
77       */
78      protected String pathInfo = null;
79  
80      /***
81       * <p> The authenticated user for this request. </p>
82       */
83      protected Principal principal = null;
84  
85      /***
86       * <p> The query string for this request. </p>
87       */
88      protected String queryString = null;
89  
90      /***
91       * <p> The servlet path for this request. </p>
92       */
93      protected String servletPath = null;
94  
95      /***
96       * <p> The HttpSession with which we are associated. </p>
97       */
98      protected HttpSession session = null;
99  
100     /***
101      * <p> The HTTP request method. </p>
102      */
103     protected String method = null;
104 
105     /***
106      * <p> The Content Type for this request. </p>
107      */
108     protected String contentType = null;
109 
110     // ----------------------------------------------------------- Constructors
111     public MockHttpServletRequest() {
112         super();
113     }
114 
115     public MockHttpServletRequest(HttpSession session) {
116         super();
117         setHttpSession(session);
118     }
119 
120     public MockHttpServletRequest(String contextPath, String servletPath,
121         String pathInfo, String queryString) {
122         super();
123         setPathElements(contextPath, servletPath, pathInfo, queryString);
124     }
125 
126     public MockHttpServletRequest(String contextPath, String servletPath,
127         String pathInfo, String queryString, HttpSession session) {
128         super();
129         setPathElements(contextPath, servletPath, pathInfo, queryString);
130         setHttpSession(session);
131     }
132 
133     // --------------------------------------------------------- Public Methods
134     public void addParameter(String name, String value) {
135         String[] values = (String[]) parameters.get(name);
136 
137         if (values == null) {
138             String[] results = new String[] { value };
139 
140             parameters.put(name, results);
141 
142             return;
143         }
144 
145         String[] results = new String[values.length + 1];
146 
147         System.arraycopy(values, 0, results, 0, values.length);
148         results[values.length] = value;
149         parameters.put(name, results);
150     }
151 
152     public void setHttpSession(HttpSession session) {
153         this.session = session;
154     }
155 
156     public void setLocale(Locale locale) {
157         this.locale = locale;
158     }
159 
160     public void setMethod(String method) {
161         this.method = method;
162     }
163 
164     public void setContentType(String contentType) {
165         this.contentType = contentType;
166     }
167 
168     public void setPathElements(String contextPath, String servletPath,
169         String pathInfo, String queryString) {
170         this.contextPath = contextPath;
171         this.servletPath = servletPath;
172         this.pathInfo = pathInfo;
173         this.queryString = queryString;
174     }
175 
176     public void setUserPrincipal(Principal principal) {
177         this.principal = principal;
178     }
179 
180     // --------------------------------------------- HttpServletRequest Methods
181     public String getAuthType() {
182         throw new UnsupportedOperationException();
183     }
184 
185     public String getContextPath() {
186         return (contextPath);
187     }
188 
189     public Cookie[] getCookies() {
190         throw new UnsupportedOperationException();
191     }
192 
193     public long getDateHeader(String name) {
194         throw new UnsupportedOperationException();
195     }
196 
197     public String getHeader(String name) {
198         throw new UnsupportedOperationException();
199     }
200 
201     public Enumeration getHeaderNames() {
202         throw new UnsupportedOperationException();
203     }
204 
205     public Enumeration getHeaders(String name) {
206         throw new UnsupportedOperationException();
207     }
208 
209     public int getIntHeader(String name) {
210         throw new UnsupportedOperationException();
211     }
212 
213     public String getMethod() {
214         return (method);
215     }
216 
217     public String getPathInfo() {
218         return (pathInfo);
219     }
220 
221     public String getPathTranslated() {
222         throw new UnsupportedOperationException();
223     }
224 
225     public String getQueryString() {
226         return (queryString);
227     }
228 
229     public String getRemoteUser() {
230         if (principal != null) {
231             return (principal.getName());
232         } else {
233             return (null);
234         }
235     }
236 
237     public String getRequestedSessionId() {
238         throw new UnsupportedOperationException();
239     }
240 
241     public String getRequestURI() {
242         StringBuffer sb = new StringBuffer();
243 
244         if (contextPath != null) {
245             sb.append(contextPath);
246         }
247 
248         if (servletPath != null) {
249             sb.append(servletPath);
250         }
251 
252         if (pathInfo != null) {
253             sb.append(pathInfo);
254         }
255 
256         if (sb.length() > 0) {
257             return (sb.toString());
258         }
259 
260         throw new UnsupportedOperationException();
261     }
262 
263     public StringBuffer getRequestURL() {
264         throw new UnsupportedOperationException();
265     }
266 
267     public String getServletPath() {
268         return (servletPath);
269     }
270 
271     public HttpSession getSession() {
272         return (getSession(true));
273     }
274 
275     public HttpSession getSession(boolean create) {
276         if (create && (session == null)) {
277             session = new MockHttpSession();
278 
279             // modified to act like the real deal,
280             // call with (false) if you want null
281             // throw new UnsupportedOperationException();
282         }
283 
284         return (session);
285     }
286 
287     public Principal getUserPrincipal() {
288         return (principal);
289     }
290 
291     public boolean isRequestedSessionIdFromCookie() {
292         throw new UnsupportedOperationException();
293     }
294 
295     public boolean isRequestedSessionIdFromUrl() {
296         throw new UnsupportedOperationException();
297     }
298 
299     public boolean isRequestedSessionIdFromURL() {
300         throw new UnsupportedOperationException();
301     }
302 
303     public boolean isRequestedSessionIdValid() {
304         throw new UnsupportedOperationException();
305     }
306 
307     public boolean isUserInRole(String role) {
308         if ((principal != null) && (principal instanceof MockPrincipal)) {
309             return (((MockPrincipal) principal).isUserInRole(role));
310         } else {
311             return (false);
312         }
313     }
314 
315     // ------------------------------------------------- ServletRequest Methods
316     public Object getAttribute(String name) {
317         return (attributes.get(name));
318     }
319 
320     public Enumeration getAttributeNames() {
321         return (new MockEnumeration(attributes.keySet().iterator()));
322     }
323 
324     public String getCharacterEncoding() {
325         throw new UnsupportedOperationException();
326     }
327 
328     public int getContentLength() {
329         throw new UnsupportedOperationException();
330     }
331 
332     public String getContentType() {
333         return (contentType);
334     }
335 
336     public ServletInputStream getInputStream() {
337         throw new UnsupportedOperationException();
338     }
339 
340     public Locale getLocale() {
341         return (locale);
342     }
343 
344     public Enumeration getLocales() {
345         throw new UnsupportedOperationException();
346     }
347 
348     public String getParameter(String name) {
349         String[] values = (String[]) parameters.get(name);
350 
351         if (values != null) {
352             return (values[0]);
353         } else {
354             return (null);
355         }
356     }
357 
358     public Map getParameterMap() {
359         return (parameters);
360     }
361 
362     public Enumeration getParameterNames() {
363         return (new MockEnumeration(parameters.keySet().iterator()));
364     }
365 
366     public String[] getParameterValues(String name) {
367         return ((String[]) parameters.get(name));
368     }
369 
370     public String getProtocol() {
371         throw new UnsupportedOperationException();
372     }
373 
374     public BufferedReader getReader() {
375         throw new UnsupportedOperationException();
376     }
377 
378     public String getRealPath(String path) {
379         throw new UnsupportedOperationException();
380     }
381 
382     public String getRemoteAddr() {
383         throw new UnsupportedOperationException();
384     }
385 
386     public String getRemoteHost() {
387         throw new UnsupportedOperationException();
388     }
389 
390     public RequestDispatcher getRequestDispatcher(String path) {
391         throw new UnsupportedOperationException();
392     }
393 
394     public String getScheme() {
395         return ("http");
396     }
397 
398     public String getServerName() {
399         return ("localhost");
400     }
401 
402     public int getServerPort() {
403         return (8080);
404     }
405 
406     public boolean isSecure() {
407         return (false);
408     }
409 
410     public void removeAttribute(String name) {
411         attributes.remove(name);
412     }
413 
414     public void setAttribute(String name, Object value) {
415         if (value == null) {
416             attributes.remove(name);
417         } else {
418             attributes.put(name, value);
419         }
420     }
421 
422     public void setCharacterEncoding(String name) {
423         throw new UnsupportedOperationException();
424     }
425 }