KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > struts > mock > MockHttpServletRequest


1 /*
2  * $Id: MockHttpServletRequest.java 54929 2004-10-16 16:38:42Z germuska $
3  *
4  * Copyright 1999-2004 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
19
20 package org.apache.struts.mock;
21
22
23 import java.io.BufferedReader JavaDoc;
24 import java.security.Principal JavaDoc;
25 import java.util.Enumeration JavaDoc;
26 import java.util.HashMap JavaDoc;
27 import java.util.Locale JavaDoc;
28 import java.util.Map JavaDoc;
29
30 import javax.servlet.RequestDispatcher JavaDoc;
31 import javax.servlet.ServletInputStream JavaDoc;
32 import javax.servlet.http.Cookie JavaDoc;
33 import javax.servlet.http.HttpServletRequest JavaDoc;
34 import javax.servlet.http.HttpSession JavaDoc;
35
36
37
38 /**
39  * <p>Mock <strong>HttpServletRequest</strong> object for low-level unit tests
40  * of Struts controller components. Coarser grained tests should be
41  * implemented in terms of the Cactus framework, instead of the mock
42  * object classes.</p>
43  *
44  * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
45  * create unit tests is provided, plus additional methods to configure this
46  * object as necessary. Methods for unsupported operations will throw
47  * <code>UnsupportedOperationException</code>.</p>
48  *
49  * <p><strong>WARNING</strong> - Because unit tests operate in a single
50  * threaded environment, no synchronization is performed.</p>
51  *
52  * @version $Rev: 54929 $ $Date: 2004-10-16 17:38:42 +0100 (Sat, 16 Oct 2004) $
53  */

54
55 public class MockHttpServletRequest implements HttpServletRequest JavaDoc {
56
57
58
59     // ----------------------------------------------------------- Constructors
60

61
62     public MockHttpServletRequest() {
63         super();
64     }
65
66
67     public MockHttpServletRequest(HttpSession JavaDoc session) {
68         super();
69         setHttpSession(session);
70     }
71
72
73     public MockHttpServletRequest(String JavaDoc contextPath, String JavaDoc servletPath,
74                                   String JavaDoc pathInfo, String JavaDoc queryString) {
75         super();
76         setPathElements(contextPath, servletPath, pathInfo, queryString);
77     }
78
79
80
81     public MockHttpServletRequest(String JavaDoc contextPath, String JavaDoc servletPath,
82                                   String JavaDoc pathInfo, String JavaDoc queryString,
83                                   HttpSession JavaDoc session) {
84         super();
85         setPathElements(contextPath, servletPath, pathInfo, queryString);
86         setHttpSession(session);
87     }
88
89
90
91     // ----------------------------------------------------- Instance Variables
92

93
94     /**
95      * The set of request attributes.
96      */

97     protected HashMap JavaDoc attributes = new HashMap JavaDoc();
98
99
100     /**
101      * The context path for this request.
102      */

103     protected String JavaDoc contextPath = null;
104
105
106     /**
107      * The preferred locale for this request.
108      */

109     protected Locale JavaDoc locale = null;
110
111
112     /**
113      * The set of arrays of parameter values, keyed by parameter name.
114      */

115     protected HashMap JavaDoc parameters = new HashMap JavaDoc();
116
117
118     /**
119      * The extra path information for this request.
120      */

121     protected String JavaDoc pathInfo = null;
122
123
124     /**
125      * The authenticated user for this request.
126      */

127     protected Principal JavaDoc principal = null;
128
129
130     /**
131      * The query string for this request.
132      */

133     protected String JavaDoc queryString = null;
134
135
136     /**
137      * The servlet path for this request.
138      */

139     protected String JavaDoc servletPath = null;
140
141
142     /**
143      * The HttpSession with which we are associated.
144      */

145     protected HttpSession JavaDoc session = null;
146
147
148     // --------------------------------------------------------- Public Methods
149

150
151     public void addParameter(String JavaDoc name, String JavaDoc value) {
152         String JavaDoc values[] = (String JavaDoc[]) parameters.get(name);
153         if (values == null) {
154             String JavaDoc results[] = new String JavaDoc[] { value };
155             parameters.put(name, results);
156             return;
157         }
158         String JavaDoc results[] = new String JavaDoc[values.length + 1];
159         System.arraycopy(values, 0, results, 0, values.length);
160         results[values.length] = value;
161         parameters.put(name, results);
162     }
163
164
165     public void setHttpSession(HttpSession JavaDoc session) {
166         this.session = session;
167     }
168
169
170     public void setLocale(Locale JavaDoc locale) {
171         this.locale = locale;
172     }
173
174
175     public void setPathElements(String JavaDoc contextPath, String JavaDoc servletPath,
176                                 String JavaDoc pathInfo, String JavaDoc queryString) {
177
178         this.contextPath = contextPath;
179         this.servletPath = servletPath;
180         this.pathInfo = pathInfo;
181         this.queryString = queryString;
182
183     }
184
185
186     public void setUserPrincipal(Principal JavaDoc principal) {
187         this.principal = principal;
188     }
189
190
191
192     // --------------------------------------------- HttpServletRequest Methods
193

194
195     public String JavaDoc getAuthType() {
196         throw new UnsupportedOperationException JavaDoc();
197     }
198
199
200     public String JavaDoc getContextPath() {
201         return (contextPath);
202     }
203
204
205     public Cookie JavaDoc[] getCookies() {
206         throw new UnsupportedOperationException JavaDoc();
207     }
208
209
210     public long getDateHeader(String JavaDoc name) {
211         throw new UnsupportedOperationException JavaDoc();
212     }
213
214
215     public String JavaDoc getHeader(String JavaDoc name) {
216         throw new UnsupportedOperationException JavaDoc();
217     }
218
219
220     public Enumeration JavaDoc getHeaderNames() {
221         throw new UnsupportedOperationException JavaDoc();
222     }
223
224
225     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
226         throw new UnsupportedOperationException JavaDoc();
227     }
228
229
230     public int getIntHeader(String JavaDoc name) {
231         throw new UnsupportedOperationException JavaDoc();
232     }
233
234
235     public String JavaDoc getMethod() {
236         throw new UnsupportedOperationException JavaDoc();
237     }
238
239
240     public String JavaDoc getPathInfo() {
241         return (pathInfo);
242     }
243
244
245     public String JavaDoc getPathTranslated() {
246         throw new UnsupportedOperationException JavaDoc();
247     }
248
249
250     public String JavaDoc getQueryString() {
251         return (queryString);
252     }
253
254
255     public String JavaDoc getRemoteUser() {
256         if (principal != null) {
257             return (principal.getName());
258         } else {
259             return (null);
260         }
261     }
262
263
264     public String JavaDoc getRequestedSessionId() {
265         throw new UnsupportedOperationException JavaDoc();
266     }
267
268
269     public String JavaDoc getRequestURI() {
270         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
271         if (contextPath != null) {
272             sb.append(contextPath);
273         }
274         if (servletPath != null) {
275             sb.append(servletPath);
276         }
277         if (pathInfo != null) {
278             sb.append(pathInfo);
279         }
280         if (sb.length() > 0) {
281             return (sb.toString());
282         }
283         throw new UnsupportedOperationException JavaDoc();
284     }
285
286
287     public StringBuffer JavaDoc getRequestURL() {
288         throw new UnsupportedOperationException JavaDoc();
289     }
290
291
292     public String JavaDoc getServletPath() {
293         return (servletPath);
294     }
295
296
297     public HttpSession JavaDoc getSession() {
298         return (getSession(true));
299     }
300
301
302     public HttpSession JavaDoc getSession(boolean create) {
303         if (create && (session == null)) {
304             throw new UnsupportedOperationException JavaDoc();
305         }
306         return (session);
307     }
308
309
310     public Principal JavaDoc getUserPrincipal() {
311         return (principal);
312     }
313
314
315     public boolean isRequestedSessionIdFromCookie() {
316         throw new UnsupportedOperationException JavaDoc();
317     }
318
319
320     public boolean isRequestedSessionIdFromUrl() {
321         throw new UnsupportedOperationException JavaDoc();
322     }
323
324
325     public boolean isRequestedSessionIdFromURL() {
326         throw new UnsupportedOperationException JavaDoc();
327     }
328
329
330     public boolean isRequestedSessionIdValid() {
331         throw new UnsupportedOperationException JavaDoc();
332     }
333
334
335     public boolean isUserInRole(String JavaDoc role) {
336         if ((principal != null) && (principal instanceof MockPrincipal)) {
337             return (((MockPrincipal) principal).isUserInRole(role));
338         } else {
339             return (false);
340         }
341     }
342
343
344     // ------------------------------------------------- ServletRequest Methods
345

346
347     public Object JavaDoc getAttribute(String JavaDoc name) {
348         return (attributes.get(name));
349     }
350
351
352     public Enumeration JavaDoc getAttributeNames() {
353         return (new MockEnumeration(attributes.keySet().iterator()));
354     }
355
356
357     public String JavaDoc getCharacterEncoding() {
358         throw new UnsupportedOperationException JavaDoc();
359     }
360
361
362     public int getContentLength() {
363         throw new UnsupportedOperationException JavaDoc();
364     }
365
366
367     public String JavaDoc getContentType() {
368         throw new UnsupportedOperationException JavaDoc();
369     }
370
371
372     public ServletInputStream JavaDoc getInputStream() {
373         throw new UnsupportedOperationException JavaDoc();
374     }
375
376
377     public Locale JavaDoc getLocale() {
378         return (locale);
379     }
380
381
382     public Enumeration JavaDoc getLocales() {
383         throw new UnsupportedOperationException JavaDoc();
384     }
385
386
387     public String JavaDoc getParameter(String JavaDoc name) {
388         String JavaDoc values[] = (String JavaDoc[]) parameters.get(name);
389         if (values != null) {
390             return (values[0]);
391         } else {
392             return (null);
393         }
394     }
395
396
397     public Map JavaDoc getParameterMap() {
398         return (parameters);
399     }
400
401
402     public Enumeration JavaDoc getParameterNames() {
403         return (new MockEnumeration(parameters.keySet().iterator()));
404     }
405
406
407     public String JavaDoc[] getParameterValues(String JavaDoc name) {
408         return ((String JavaDoc[]) parameters.get(name));
409     }
410
411
412     public String JavaDoc getProtocol() {
413         throw new UnsupportedOperationException JavaDoc();
414     }
415
416
417     public BufferedReader JavaDoc getReader() {
418         throw new UnsupportedOperationException JavaDoc();
419     }
420
421
422     public String JavaDoc getRealPath(String JavaDoc path) {
423         throw new UnsupportedOperationException JavaDoc();
424     }
425
426
427     public String JavaDoc getRemoteAddr() {
428         throw new UnsupportedOperationException JavaDoc();
429     }
430
431
432     public String JavaDoc getRemoteHost() {
433         throw new UnsupportedOperationException JavaDoc();
434     }
435
436
437     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path) {
438         throw new UnsupportedOperationException JavaDoc();
439     }
440
441
442     public String JavaDoc getScheme() {
443         return ("http");
444     }
445
446
447     public String JavaDoc getServerName() {
448         return ("localhost");
449     }
450
451
452     public int getServerPort() {
453         return (8080);
454     }
455
456
457     public boolean isSecure() {
458         return (false);
459     }
460
461
462     public void removeAttribute(String JavaDoc name) {
463         attributes.remove(name);
464     }
465
466
467     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
468         if (value == null) {
469             attributes.remove(name);
470         } else {
471             attributes.put(name, value);
472         }
473     }
474
475
476     public void setCharacterEncoding(String JavaDoc name) {
477         throw new UnsupportedOperationException JavaDoc();
478     }
479
480
481 }
482
Popular Tags