KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > tapestry > test > mock > MockRequest


1 // Copyright 2004, 2005 The Apache Software Foundation
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
// http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14

15 package org.apache.tapestry.test.mock;
16
17 import java.io.BufferedReader JavaDoc;
18 import java.io.IOException JavaDoc;
19 import java.io.UnsupportedEncodingException JavaDoc;
20 import java.security.Principal JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Enumeration JavaDoc;
25 import java.util.HashMap JavaDoc;
26 import java.util.List 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  * Mock implementation of {@link javax.servlet.http.HttpServletRequest}.
38  *
39  *
40  * @author Howard Lewis Ship
41  * @since 4.0
42  */

43
44 public class MockRequest extends AttributeHolder implements HttpServletRequest JavaDoc
45 {
46     /**
47      * HTTP content type header name.
48      */

49     private static final String JavaDoc CONTENT_TYPE_HEADER_KEY = "Content-type";
50     /**
51      * Map of String[].
52      *
53      */

54
55     private Map JavaDoc _parameters = new HashMap JavaDoc();
56
57     /**
58      * Map of String[]
59      *
60      */

61
62     private Map JavaDoc _headers = new HashMap JavaDoc();
63
64     private String JavaDoc _method = "GET";
65
66     private String JavaDoc _contextPath;
67
68     private MockContext _servletContext;
69     private MockSession _session;
70     private String JavaDoc _servletPath;
71     private List JavaDoc _cookies = new ArrayList JavaDoc();
72     private String JavaDoc _contentPath;
73
74     /**
75      * This can be stored within the header, but doing it this way emulates a browser that
76      * does not put the encoding in the request, which appears to be the general case.
77      */

78     private String JavaDoc _encoding = null;
79
80     public MockRequest(MockContext servletContext, String JavaDoc servletPath)
81     {
82         _servletContext = servletContext;
83
84         _contextPath = "/" + servletContext.getServletContextName();
85         _servletPath = servletPath;
86
87         _session = _servletContext.getSession();
88     }
89
90     public String JavaDoc getAuthType()
91     {
92         return null;
93     }
94
95     public Cookie JavaDoc[] getCookies()
96     {
97         return (Cookie JavaDoc[]) _cookies.toArray(new Cookie JavaDoc[_cookies.size()]);
98     }
99
100     public long getDateHeader(String JavaDoc arg0)
101     {
102         return 0;
103     }
104
105     public String JavaDoc getHeader(String JavaDoc key)
106     {
107         String JavaDoc getHeader = null;
108
109         if (key != null)
110         {
111             getHeader = (String JavaDoc) _headers.get(key.toLowerCase());
112         }
113         return getHeader;
114     }
115
116     public Enumeration JavaDoc getHeaders(String JavaDoc name)
117     {
118         String JavaDoc[] headers = (String JavaDoc[]) _headers.get(name);
119
120         if (headers == null)
121             return Collections.enumeration(Collections.EMPTY_LIST);
122
123         return Collections.enumeration(Arrays.asList(headers));
124     }
125
126     public Enumeration JavaDoc getHeaderNames()
127     {
128         return getEnumeration(_headers);
129     }
130
131     public int getIntHeader(String JavaDoc arg0)
132     {
133         return 0;
134     }
135
136     public String JavaDoc getMethod()
137     {
138         return _method;
139     }
140
141     public String JavaDoc getPathInfo()
142     {
143         return null;
144     }
145
146     public String JavaDoc getPathTranslated()
147     {
148         return null;
149     }
150
151     public String JavaDoc getContextPath()
152     {
153         return _contextPath;
154     }
155
156     public String JavaDoc getQueryString()
157     {
158         return null;
159     }
160
161     public String JavaDoc getRemoteUser()
162     {
163         return null;
164     }
165
166     public boolean isUserInRole(String JavaDoc arg0)
167     {
168         return false;
169     }
170
171     public Principal JavaDoc getUserPrincipal()
172     {
173         return null;
174     }
175
176     public String JavaDoc getRequestedSessionId()
177     {
178         return null;
179     }
180
181     public String JavaDoc getRequestURI()
182     {
183         return null;
184     }
185
186     public StringBuffer JavaDoc getRequestURL()
187     {
188         return null;
189     }
190
191     public String JavaDoc getServletPath()
192     {
193         return _servletPath;
194     }
195
196     public HttpSession JavaDoc getSession(boolean create)
197     {
198         if (create && _session == null)
199             _session = _servletContext.createSession();
200
201         return _session;
202     }
203
204     public HttpSession JavaDoc getSession()
205     {
206         return _session;
207     }
208
209     public boolean isRequestedSessionIdValid()
210     {
211         return false;
212     }
213
214     public boolean isRequestedSessionIdFromCookie()
215     {
216         return false;
217     }
218
219     public boolean isRequestedSessionIdFromURL()
220     {
221         return false;
222     }
223
224     public boolean isRequestedSessionIdFromUrl()
225     {
226         return false;
227     }
228
229     public String JavaDoc getCharacterEncoding()
230     {
231         return _encoding;
232     }
233
234     public void setCharacterEncoding(String JavaDoc arg0) throws UnsupportedEncodingException JavaDoc
235     {
236         _encoding = arg0;
237     }
238
239     public int getContentLength()
240     {
241         return 0;
242     }
243
244     public String JavaDoc getContentType()
245     {
246         return getHeader(CONTENT_TYPE_HEADER_KEY);
247     }
248
249     public void setContentType(String JavaDoc contentType)
250     {
251         setHeader(CONTENT_TYPE_HEADER_KEY, contentType);
252     }
253
254     public ServletInputStream JavaDoc getInputStream() throws IOException JavaDoc
255     {
256         if (_contentPath == null)
257             return null;
258
259         return new MockServletInputStream(_contentPath);
260     }
261
262     public String JavaDoc getParameter(String JavaDoc name)
263     {
264         String JavaDoc[] values = getParameterValues(name);
265
266         if (values == null || values.length == 0)
267             return null;
268
269         return values[0];
270     }
271
272     public Enumeration JavaDoc getParameterNames()
273     {
274         return Collections.enumeration(_parameters.keySet());
275     }
276
277     public String JavaDoc[] getParameterValues(String JavaDoc name)
278     {
279         return (String JavaDoc[]) _parameters.get(name);
280     }
281
282     /**
283      * Not part of 2.1 API, not used by Tapestry.
284      *
285      */

286
287     public Map JavaDoc getParameterMap()
288     {
289         return null;
290     }
291
292     public String JavaDoc getProtocol()
293     {
294         return null;
295     }
296
297     public String JavaDoc getScheme()
298     {
299         return "http";
300     }
301
302     public String JavaDoc getServerName()
303     {
304         return "junit-test";
305     }
306
307     public int getServerPort()
308     {
309         return 80;
310     }
311
312     public BufferedReader JavaDoc getReader() throws IOException JavaDoc
313     {
314         return null;
315     }
316
317     public String JavaDoc getRemoteAddr()
318     {
319         return null;
320     }
321
322     public String JavaDoc getRemoteHost()
323     {
324         return null;
325     }
326
327     private Locale JavaDoc _locale = Locale.ENGLISH;
328
329     public Locale JavaDoc getLocale()
330     {
331         return _locale;
332     }
333
334     public void setLocale(Locale JavaDoc locale)
335     {
336         _locale = locale;
337     }
338
339     public Enumeration JavaDoc getLocales()
340     {
341         return Collections.enumeration(Collections.singleton(_locale));
342     }
343
344     public boolean isSecure()
345     {
346         return false;
347     }
348
349     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc path)
350     {
351         return _servletContext.getRequestDispatcher(path);
352     }
353
354     public String JavaDoc getRealPath(String JavaDoc arg0)
355     {
356         return null;
357     }
358
359     public void setContextPath(String JavaDoc contextPath)
360     {
361         _contextPath = contextPath;
362     }
363
364     public void setMethod(String JavaDoc method)
365     {
366         _method = method;
367     }
368
369     public void setParameter(String JavaDoc name, String JavaDoc[] values)
370     {
371         _parameters.put(name, values);
372     }
373
374     public void setParameter(String JavaDoc name, String JavaDoc value)
375     {
376         setParameter(name, new String JavaDoc[] { value });
377     }
378
379     public void addCookie(Cookie JavaDoc cookie)
380     {
381         _cookies.add(cookie);
382     }
383
384     public void addCookies(Cookie JavaDoc[] cookies)
385     {
386         if (cookies == null)
387             return;
388
389         for (int i = 0; i < cookies.length; i++)
390             addCookie(cookies[i]);
391     }
392
393     private void setHeader(String JavaDoc key, String JavaDoc value)
394     {
395         if (key != null)
396         {
397             _headers.put(key.toLowerCase(), value);
398         }
399     }
400
401     /**
402      * Delegates this to the {@link org.apache.tapestry.junit.mock.MockSession}, if
403      * it exists.
404      *
405      */

406
407     public void simulateFailover()
408     {
409         if (_session != null)
410             _session.simulateFailover();
411     }
412
413     public String JavaDoc getContentPath()
414     {
415         return _contentPath;
416     }
417
418     public void setContentPath(String JavaDoc contentPath)
419     {
420         _contentPath = contentPath;
421     }
422
423     public int getRemotePort()
424     {
425         return 0;
426     }
427
428     public String JavaDoc getLocalName()
429     {
430         return null;
431     }
432
433     public String JavaDoc getLocalAddr()
434     {
435         return null;
436     }
437
438     public int getLocalPort()
439     {
440         return 0;
441     }
442
443 }
444
Popular Tags