KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > barracuda > testbed > servlet > MockHttpServletRequest


1 /*
2  * Copyright (C) 2003 Christian Cryder [christianc@granitepeaks.com]
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  *
18  * $Id: MockHttpServletRequest.java,v 1.4 2004/02/01 05:16:32 christianc Exp $
19  */

20 package org.enhydra.barracuda.testbed.servlet;
21
22 import java.io.*;
23 import java.util.*;
24 import java.security.*;
25 import javax.servlet.*;
26 import javax.servlet.http.*;
27
28 import org.enhydra.barracuda.plankton.data.*;
29
30 /**
31  * <p>This class provides a mockup implementation for the HttpServletRequest.
32  * Note that not all the methods are implemented...you may need to extend/override
33  * to get the behaviour you expect. If the behaviour is common to all
34  * servlet requests, add it back in here. Note also that all properties of
35  * this class are public, allowing you to access them directly (makes assertions
36  * easier)
37  */

38 public class MockHttpServletRequest implements HttpServletRequest {
39
40     public String JavaDoc paramStr = null;
41     public List paramList = new ArrayList();
42     public Map hdrMap = new HashMap();
43
44     /**
45      * Noargs constructor
46      */

47     public MockHttpServletRequest() {
48         this(null);
49     }
50     
51     /**
52      * Create a request with a given param String
53      */

54     public MockHttpServletRequest(String JavaDoc paramStr) {
55         if (paramStr!=null) setParamStr(paramStr);
56     
57     }
58
59     //-------------------- MockHttpServletRequest ----------------
60
public void setParamStr(String JavaDoc paramStr) {
61         //clear the paramList
62
paramList.clear();
63         if (paramStr==null) return;
64
65         //convert the incoming parameters into param vals and store them in the list
66
String JavaDoc delimiter = "&";
67         StringTokenizer st = new StringTokenizer(paramStr, delimiter);
68         while (st.hasMoreTokens()) {
69             String JavaDoc segment = st.nextToken();
70             StringTokenizer stsub = new StringTokenizer(segment, "=");
71             String JavaDoc key = (String JavaDoc) stsub.nextElement();
72             String JavaDoc value = (String JavaDoc) stsub.nextElement();
73             paramList.add(new Param(key, value));
74         }
75     }
76     
77     public void setHeader(String JavaDoc hdrName, String JavaDoc hdrValue) {
78         List hdrs = new ArrayList(1);
79         hdrs.add(hdrValue);
80         hdrMap.put(hdrName, hdrs);
81     }
82
83     public void addHeader(String JavaDoc hdrName, String JavaDoc hdrValue) {
84         List hdrs = (List) hdrMap.get(hdrName);
85         if (hdrs==null) {
86             setHeader(hdrName, hdrValue);
87         } else {
88             hdrs.add(hdrValue);
89         }
90     }
91
92
93
94     //-------------------- HttpServletRequest --------------------
95
public String JavaDoc getAuthType() {return null;}
96     public String JavaDoc getContextPath() {return null;}
97     public Cookie[] getCookies() {return null;}
98     public long getDateHeader(String JavaDoc name) {return -1;}
99     
100     /**
101      * Get a header
102      */

103     public String JavaDoc getHeader(String JavaDoc hdrName) {
104         List hdrs = (List) hdrMap.get(hdrName);
105         if (hdrs==null) {
106             return null;
107         } else {
108             return (String JavaDoc) hdrs.get(0);
109         }
110     }
111     
112     /**
113      * Get a list of header names
114      */

115     public Enumeration getHeaderNames() {
116         Iterator it = hdrMap.keySet().iterator();
117         List keys = new ArrayList();
118         while (it.hasNext()) {
119             keys.add(it.next());
120         }
121         return new LocalEnumerator(keys);
122     }
123     
124     /**
125      * get a list of header values for a given name
126      */

127     public Enumeration getHeaders(String JavaDoc hdrName) {
128         return new LocalEnumerator((List) hdrMap.get(hdrName));
129     }
130
131     public int getIntHeader(String JavaDoc name) {return -1;}
132     public String JavaDoc getMethod() {return null;}
133     public String JavaDoc getPathInfo() {return null;}
134     public String JavaDoc getPathTranslated() {return null;}
135     public String JavaDoc getQueryString() {return null;}
136     public String JavaDoc getRemoteUser() {return null;}
137     public String JavaDoc getRequestedSessionId() {return null;}
138     public String JavaDoc getRequestURI() {return null;}
139     public String JavaDoc getServletPath() {return null;}
140     public HttpSession getSession() {return null;}
141     public HttpSession getSession(boolean create) {return null;}
142     public Principal getUserPrincipal() {return null;}
143     public boolean isRequestedSessionIdFromCookie() {return false;}
144     public boolean isRequestedSessionIdFromUrl() {return false;}
145     public boolean isRequestedSessionIdFromURL() {return false;}
146     public boolean isRequestedSessionIdValid() {return false;}
147     public boolean isUserInRole(String JavaDoc role) {return false;}
148
149     //-------------------- ServletRequest ------------------------
150
public Object JavaDoc getAttribute(String JavaDoc name) {return null;}
151     public Enumeration getAttributeNames() {return null;}
152     public void setCharacterEncoding(String JavaDoc s) {}
153     public String JavaDoc getCharacterEncoding() {return null;}
154     public int getContentLength() {return -1;}
155     public String JavaDoc getContentType() {return null;}
156     public ServletInputStream getInputStream() throws IOException {return null;}
157     public Locale getLocale() {return null;}
158     public Enumeration getLocales() {return null;}
159     public Map getParameterMap() {return null;} //probably need to integrate this with getParameter structure below
160
public StringBuffer JavaDoc getRequestURL() {return null;}
161
162     /**
163      * get a parameter
164      */

165     public String JavaDoc getParameter(String JavaDoc name) {
166         Iterator it = paramList.iterator();
167         while (it.hasNext()) {
168             Param param = (Param) it.next();
169             if (param.getKey().equals(name)) {
170                 return param.getValue();
171             }
172         }
173         return null;
174     }
175
176     /**
177      * get parameter names
178      */

179     public Enumeration getParameterNames() {
180         return new LocalEnumerator(paramList);
181     }
182
183     /**
184      * get parameter values
185      */

186     public String JavaDoc[] getParameterValues(String JavaDoc name) {
187         List valueList = new ArrayList(paramList.size());
188         Iterator it = paramList.iterator();
189         while (it.hasNext()) {
190             Param param = (Param) it.next();
191             if (param.getKey().equals(name)) valueList.add(param.getValue());
192         }
193         int idx = -1;
194         String JavaDoc[] valueArr = new String JavaDoc[valueList.size()];
195         it = valueList.iterator();
196         while (it.hasNext()) {
197             valueArr[++idx] = (String JavaDoc) it.next();
198         }
199         if (valueArr.length==0) return null;
200         else return valueArr;
201     }
202     public String JavaDoc getProtocol() {return null;}
203     public BufferedReader getReader() throws IOException {return null;}
204     public String JavaDoc getRealPath(String JavaDoc path) {return null;}
205     public String JavaDoc getRemoteAddr() {return null;}
206     public String JavaDoc getRemoteHost() {return null;}
207     public RequestDispatcher getRequestDispatcher(String JavaDoc path) {return null;}
208     public String JavaDoc getScheme() {return null;}
209     public String JavaDoc getServerName() {return null;}
210     public int getServerPort() {return -1;}
211     public boolean isSecure() {return false;}
212     public void removeAttribute(String JavaDoc name) {}
213     public void setAttribute(String JavaDoc name, Object JavaDoc o) {}
214
215
216
217     //-------------------- Utilities -----------------------------
218
/**
219      * This inner class implements Enumaration. It will effectively
220      * enumerate over all of the parameter key names.
221      */

222     class LocalEnumerator implements Enumeration {
223         List keyList = null;
224         Iterator it = null;
225
226         public LocalEnumerator(List iparamList) {
227             if (iparamList!=null) {
228                 keyList = new ArrayList(iparamList.size());
229                 it = iparamList.iterator();
230                 while (it.hasNext()) {
231                     Object JavaDoc o = it.next();
232                     if (o instanceof Param) {
233                         Param param = (Param) o;
234                         if (!keyList.contains(param.getKey())) keyList.add(param.getKey());
235                     } else {
236                         keyList.add(o);
237                     }
238                 }
239                 it = keyList.iterator();
240             }
241         }
242
243         public boolean hasMoreElements() {
244             return (it!=null && it.hasNext());
245         }
246
247         public Object JavaDoc nextElement() {
248             return it.next();
249         }
250         
251         public String JavaDoc toString() {
252             return super.toString()+" {"+keyList.size()+" items}";
253         }
254     }
255
256
257 }
258
Popular Tags