KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > internal > http > MockHttpServletRequest


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit.internal.http;
8
9
10 import java.io.BufferedReader JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.security.Principal JavaDoc;
13 import java.util.Enumeration JavaDoc;
14 import java.util.HashMap JavaDoc;
15 import java.util.Iterator JavaDoc;
16 import java.util.Locale JavaDoc;
17 import java.util.Map JavaDoc;
18 import java.util.Set JavaDoc;
19 import javax.servlet.RequestDispatcher JavaDoc;
20 import javax.servlet.ServletInputStream JavaDoc;
21 import javax.servlet.http.Cookie JavaDoc;
22 import javax.servlet.http.HttpServletRequest JavaDoc;
23 import javax.servlet.http.HttpSession JavaDoc;
24
25 import com.inversoft.junit.Request;
26 import com.inversoft.junit.URL;
27
28
29 /**
30  * This class is a mock request that holds the same
31  * functionality as a request inside a container.
32  *
33  * @author Brian Pontarelli
34  * @since 2.0
35  * @version 2.0
36  */

37 public class MockHttpServletRequest implements HttpServletRequest JavaDoc {
38
39
40     private Request JavaDoc request;
41     private Map JavaDoc attributes;
42     private String JavaDoc encoding;
43     private MockHttpSession session;
44     private MockRequestDispatcher dispatcher;
45
46
47     /**
48      * Constructs a new mock request
49      */

50     public MockHttpServletRequest(Request JavaDoc request) {
51         this.request = request;
52         this.attributes = new HashMap JavaDoc();
53         this.session = new MockHttpSession();
54     }
55
56
57     //-------------------------------------------------------------------------
58
// javax.servlet.ServletRequest methods
59
//-------------------------------------------------------------------------
60

61     /**
62      */

63     public Object JavaDoc getAttribute(String JavaDoc name) {
64         return attributes.get(name);
65     }
66
67     /**
68      */

69     public Enumeration JavaDoc getAttributeNames() {
70         Set JavaDoc keys = attributes.keySet();
71         final Iterator JavaDoc iter = keys.iterator();
72
73         return new Enumeration JavaDoc() {
74             public boolean hasMoreElements() {
75                 return iter.hasNext();
76             }
77             public Object JavaDoc nextElement() {
78                 return iter.next();
79             }
80         };
81     }
82
83     /**
84      * Returns the encoding which defaults to null unless it is set
85      */

86     public String JavaDoc getCharacterEncoding() {
87         return encoding;
88     }
89
90     /**
91      * This should set a new character encoding
92      */

93     public void setCharacterEncoding(String JavaDoc encoding) {
94         this.encoding = encoding;
95     }
96
97     /**
98      */

99     public int getContentLength() {
100         return -1;
101     }
102
103     /**
104      */

105     public String JavaDoc getContentType() {
106         throw new UnsupportedOperationException JavaDoc();
107     }
108
109     /**
110      */

111     public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc {
112         throw new UnsupportedOperationException JavaDoc();
113     }
114
115     /**
116      */

117     public Locale JavaDoc getLocale() {
118         throw new UnsupportedOperationException JavaDoc();
119     }
120
121     /**
122      */

123     public Enumeration JavaDoc getLocales() {
124         throw new UnsupportedOperationException JavaDoc();
125     }
126
127     /**
128      */

129     public String JavaDoc getParameter(String JavaDoc name) {
130         return request.getParameter(name);
131     }
132
133     /** Forwards to the wrapped request */
134     public Map JavaDoc getParameterMap() {
135         return request.getParameterMap();
136     }
137
138     /**
139      */

140     public Enumeration JavaDoc getParameterNames() {
141         return request.getParameterNames();
142     }
143
144     /**
145      */

146     public String JavaDoc[] getParameterValues(String JavaDoc name) {
147         return request.getParameterValues(name);
148     }
149
150     /**
151      */

152     public String JavaDoc getProtocol() {
153         return request.getURL().getProtocol().toUpperCase() + "/1.0";
154     }
155
156     /**
157      */

158     public BufferedReader JavaDoc getReader() throws IOException JavaDoc {
159         throw new UnsupportedOperationException JavaDoc();
160     }
161
162     /**
163      * @deprecated
164      */

165     public String JavaDoc getRealPath(String JavaDoc url) {
166         throw new UnsupportedOperationException JavaDoc();
167     }
168
169     /**
170      */

171     public String JavaDoc getRemoteAddr() {
172         throw new UnsupportedOperationException JavaDoc();
173     }
174
175     /**
176      */

177     public String JavaDoc getRemoteHost() {
178         throw new UnsupportedOperationException JavaDoc();
179     }
180
181     /**
182      */

183     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc thePath)
184     {
185
186         if (thePath == null) {
187             return null;
188         }
189
190         String JavaDoc fullPath;
191
192         // The spec says that the path can be relative, in which case it will
193
// be relative to the request. So for relative paths, we need to take
194
// into account the simulated URL (ServletURL).
195
if (thePath.startsWith("/")) {
196
197             fullPath = thePath;
198
199         } else {
200
201             String JavaDoc pI = getPathInfo();
202             if (pI == null) {
203                 fullPath = catPath(getServletPath(), thePath);
204             } else {
205                 fullPath = catPath(getServletPath() + pI, thePath);
206             }
207
208             if (fullPath == null) {
209                 return null;
210             }
211         }
212
213         dispatcher = new MockRequestDispatcher(fullPath);
214         return dispatcher;
215     }
216
217     /**
218      */

219     public String JavaDoc getScheme() {
220         return request.getURL().getProtocol();
221     }
222
223     /**
224      */

225     public String JavaDoc getServerName() {
226         return request.getURL().getServerName();
227     }
228
229     /**
230      */

231     public int getServerPort() {
232         return request.getURL().getServerPort();
233     }
234
235     /**
236      */

237     public boolean isSecure() {
238         throw new UnsupportedOperationException JavaDoc();
239     }
240
241     /**
242      */

243     public void removeAttribute(String JavaDoc name) {
244         attributes.remove(name);
245     }
246
247     /**
248      */

249     public void setAttribute(String JavaDoc name, Object JavaDoc value) {
250         attributes.put(name, value);
251     }
252
253
254     //-------------------------------------------------------------------------
255
// javax.servlet.http.HttpServletRequest methods
256
//-------------------------------------------------------------------------
257

258
259     /**
260      * Local clients don't authenticate
261      */

262     public String JavaDoc getAuthType() {
263         return null;
264     }
265
266     /**
267      */

268     public String JavaDoc getContextPath() {
269         URL urlObj = request.getURL();
270         String JavaDoc url = "";
271         if (urlObj != null) {
272             url = urlObj.getContextPath();
273         }
274         
275         return url;
276     }
277
278     /**
279      * Returns a local cookie that is stored in the request
280      */

281     public Cookie JavaDoc[] getCookies() {
282         return request.getCookies();
283     }
284
285     /**
286      */

287     public long getDateHeader(String JavaDoc name) {
288         throw new UnsupportedOperationException JavaDoc();
289     }
290
291     /**
292      */

293     public String JavaDoc getHeader(String JavaDoc name) {
294         throw new UnsupportedOperationException JavaDoc();
295     }
296
297     /**
298      */

299     public Enumeration JavaDoc getHeaderNames() {
300         throw new UnsupportedOperationException JavaDoc();
301     }
302
303     /**
304      */

305     public Enumeration JavaDoc getHeaders(String JavaDoc name) {
306         throw new UnsupportedOperationException JavaDoc();
307     }
308
309     /**
310      */

311     public int getIntHeader(String JavaDoc name) {
312         throw new UnsupportedOperationException JavaDoc();
313     }
314
315     /**
316      */

317     public String JavaDoc getMethod() {
318         throw new UnsupportedOperationException JavaDoc();
319     }
320
321     /**
322      */

323     public String JavaDoc getPathInfo() {
324         if (request.getURL() == null) {
325             return null;
326         }
327
328         return request.getURL().getPathInfo();
329     }
330
331     /**
332      */

333     public String JavaDoc getPathTranslated() {
334         throw new UnsupportedOperationException JavaDoc();
335         /*
336         String pathTranslated;
337
338         if (request.getURL().getPathInfo() != null) {
339
340             String pathInfo = request.getURL().getPathInfo();
341
342             // If getRealPath returns null then getPathTranslated should also
343             // return null (see section SRV.4.5 of the Servlet 2.3 spec).
344             if (request.getRealPath("/") == null) {
345                 pathTranslated = null;
346             } else {
347
348                 // Compute the translated path using the root real path
349                 String newPathInfo = (pathInfo.startsWith("/") ?
350                     pathInfo.substring(1) : pathInfo);
351                 if (request.getRealPath("/").endsWith("/")) {
352                     pathTranslated = request.getRealPath("/") +
353                         newPathInfo.replace('/', File.separatorChar);
354                 } else {
355                     pathTranslated = request.getRealPath("/") +
356                         File.separatorChar + newPathInfo.replace('/',
357                             File.separatorChar);
358                 }
359             }
360         }
361
362         return pathTranslated;
363         */

364     }
365
366     /**
367      */

368     public String JavaDoc getQueryString() {
369         return request.getURL().getQueryString();
370     }
371
372     /**
373      */

374     public String JavaDoc getRemoteUser() {
375         throw new UnsupportedOperationException JavaDoc();
376     }
377
378     /**
379      */

380     public String JavaDoc getRequestedSessionId() {
381         throw new UnsupportedOperationException JavaDoc();
382     }
383
384     /**
385      */

386     public String JavaDoc getRequestURI() {
387         return getContextPath() +
388             ((getServletPath() == null) ? "" : getServletPath()) +
389             ((getPathInfo() == null) ? "" : getPathInfo());
390     }
391
392     /**
393      */

394     public StringBuffer JavaDoc getRequestURL() {
395         throw new UnsupportedOperationException JavaDoc();
396     }
397
398     /**
399      */

400     public String JavaDoc getServletPath() {
401         if (request.getURL() == null) {
402             return "";
403         }
404
405         return request.getURL().getServletPath();
406     }
407
408     /**
409      */

410     public HttpSession JavaDoc getSession() {
411         return session;
412     }
413
414     /**
415      */

416     public HttpSession JavaDoc getSession(boolean create) {
417         return session;
418     }
419
420     /**
421      */

422     public Principal JavaDoc getUserPrincipal() {
423         throw new UnsupportedOperationException JavaDoc();
424     }
425
426     /**
427      */

428     public boolean isRequestedSessionIdFromCookie() {
429         throw new UnsupportedOperationException JavaDoc();
430     }
431
432     /**
433      * @deprecated
434      */

435     public boolean isRequestedSessionIdFromUrl() {
436         throw new UnsupportedOperationException JavaDoc();
437     }
438
439     /**
440      */

441     public boolean isRequestedSessionIdFromURL() {
442         throw new UnsupportedOperationException JavaDoc();
443     }
444
445     /**
446      */

447     public boolean isRequestedSessionIdValid() {
448         throw new UnsupportedOperationException JavaDoc();
449     }
450
451     /**
452      */

453     public boolean isUserInRole(String JavaDoc role) {
454         throw new UnsupportedOperationException JavaDoc();
455     }
456
457     //-------------------------------------------------------------------------
458
// Helper methods
459
//-------------------------------------------------------------------------
460

461
462     /**
463      * Will concatenate 2 paths, normalising it. For example :
464      * ( /a/b/c + d = /a/b/d, /a/b/c + ../d = /a/d ). Code borrowed from
465      * Tomcat 3.2.2 !
466      *
467      * @param theLookupPath the first part of the path
468      * @param thePath the part to add to the lookup path
469      * @return the concatenated thePath or null if an error occurs
470      */

471     String JavaDoc catPath(String JavaDoc theLookupPath, String JavaDoc thePath)
472     {
473         // Cut off the last slash and everything beyond
474
int index = theLookupPath.lastIndexOf("/");
475         if (index == -1) {
476             return thePath;
477         }
478
479         theLookupPath = theLookupPath.substring(0, index);
480
481         // Deal with .. by chopping dirs off the lookup thePath
482
while (thePath.startsWith("../")) {
483             if (theLookupPath.length() > 0) {
484                 index = theLookupPath.lastIndexOf("/");
485                 theLookupPath = theLookupPath.substring(0, index);
486             } else {
487                 // More ..'s than dirs, return null
488
return null;
489             }
490
491             index = thePath.indexOf("../") + 3;
492             thePath = thePath.substring(index);
493         }
494
495         return theLookupPath + "/" + thePath;
496     }
497
498
499     //-------------------------------------------------------------------------
500
// Modification Methods
501
//-------------------------------------------------------------------------
502

503
504     /**
505      * Sets the request parameter with the given name to the given value
506      */

507     public void setParameter(String JavaDoc name, String JavaDoc value) {
508         request.addParameter(name, value);
509     }
510
511     /**
512      * Removes all the values of the request parameter with the given name
513      */

514     public void removeParameter(String JavaDoc name) {
515         request.removeParameter(name);
516     }
517
518     /**
519      * Clears all the parameters
520      */

521     public void clearParameters() {
522         request.clearParameters();
523     }
524
525     /**
526      * Sets the request parameter with the given name to the given values
527      */

528     public void setParameter(String JavaDoc name, String JavaDoc [] values) {
529         for (int i = 0; i < values.length; i++) {
530             request.addParameter(name, values[i]);
531         }
532     }
533
534     /**
535      * Clears all the attributes
536      */

537     public void clearAttributes() {
538         attributes.clear();
539     }
540
541     /**
542      * Returns the RequestDispatcher if one was created from this Request
543      */

544     public MockRequestDispatcher getRequestDispatcher() {
545         return dispatcher;
546     }
547
548     /**
549      * Sets up a new request for this mock request.
550      *
551      * @param request The new request
552      */

553     public void setRequest(Request JavaDoc request) {
554         this.request = request;
555     }
556 }
Popular Tags