KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > servlet > MockHttpServletRequest


1 package com.mockobjects.servlet;
2
3 import com.mockobjects.*;
4
5 import javax.servlet.ServletInputStream JavaDoc;
6 import javax.servlet.RequestDispatcher JavaDoc;
7 import javax.servlet.http.HttpServletRequest JavaDoc;
8 import javax.servlet.http.HttpSession JavaDoc;
9 import javax.servlet.http.Cookie JavaDoc;
10 import java.util.*;
11 import java.io.BufferedReader JavaDoc;
12 import java.io.IOException JavaDoc;
13 import java.security.Principal JavaDoc;
14
15 import com.mockobjects.ReturnValue;
16
17 /**
18  * @version $Revision: 1.16 $
19  */

20 public class MockHttpServletRequest extends MockObject
21     implements HttpServletRequest JavaDoc {
22
23     private final ReturnObjectBag myParameters = new ReturnObjectBag("parameters");
24     private final ReturnObjectBag myHeaders = new ReturnObjectBag("headers");
25     private final ReturnValue myHttpSession = new ReturnValue("session");
26     private final ReturnValue myContentTypeToReturn = new ReturnValue("content type");
27     private final ReturnValue myContextPath = new ReturnValue("context path");
28     private final ReturnValue myPathInfo = new ReturnValue("path info");
29     private final ReturnValue myRemoteAddress = new ReturnValue("remote address");
30     private final ReturnValue myRequestURI = new ReturnValue("request uri");
31     private final ReturnValue method = new ReturnValue("method");
32     private final ReturnValue protocol = new ReturnValue("protocol");
33     private final ReturnValue inputStream = new ReturnValue("input stream");
34     private final ReturnValue myUserPrincipal = new ReturnValue("user principal");
35     private final ReturnValue myAttributesNames = new ReturnValue("attribute names");
36     private final ReturnValue queryString = new ReturnValue("query string");
37     private final ReturnValue scheme = new ReturnValue("scheme");
38     private final ReturnValue serverName = new ReturnValue("server name");
39     private final ReturnValue reader = new ReturnValue("reader");
40     private final ExpectationSet mySetAttributes = new ExpectationSet("HttpServletRequest.setAttribute");
41     private final ExpectationSet myRemoveAttributes = new ExpectationSet("HttpServletRequest.removeAttribute");
42     private final ReturnObjectList myAttributesToReturn = new ReturnObjectList("attributes");
43     private final ExpectationValue myContentType = new ExpectationValue("content type");
44     private final ExpectationList myGetAttributeNames = new ExpectationList("get attribute names");
45     private final ReturnValue servletPath = new ReturnValue("servlet path");
46     private final ReturnValue parameterMap = new ReturnValue("parameter map");
47     private final ReturnValue myParameterNames = new ReturnValue("parameter names");
48     private final ReturnValue requestDispatcher = new ReturnValue("request dispatcher");
49     private final ExpectationValue requestDispatcherURI = new ExpectationValue("request dispatcher uri");
50     private final ExpectationValue createSession = new ExpectationValue("create session");
51
52     public void setupGetAttribute(Object JavaDoc anAttributeToReturn) {
53         myAttributesToReturn.addObjectToReturn(anAttributeToReturn);
54     }
55
56     public void addExpectedGetAttributeName(String JavaDoc anAttributeName) {
57         myGetAttributeNames.addExpected(anAttributeName);
58     }
59
60     public Object JavaDoc getAttribute(String JavaDoc anAttributeName) {
61         myGetAttributeNames.addActual(anAttributeName);
62         return myAttributesToReturn.nextReturnObject();
63     }
64
65     public void setupGetAttrubuteNames(Enumeration attributeNames) {
66         myAttributesNames.setValue(attributeNames);
67     }
68
69     public Enumeration getAttributeNames() {
70         return (Enumeration) myAttributesNames.getValue();
71     }
72
73     public String JavaDoc getAuthType() {
74         notImplemented();
75         return null;
76     }
77
78     public String JavaDoc getCharacterEncoding() {
79         notImplemented();
80         return null;
81     }
82
83     public int getContentLength() {
84         notImplemented();
85         return 0;
86     }
87
88     public String JavaDoc getContentType() {
89         return (String JavaDoc) myContentTypeToReturn.getValue();
90     }
91
92     public void setupGetContentType(String JavaDoc aContentType) {
93         myContentTypeToReturn.setValue(aContentType);
94     }
95
96     public void setExpectedContentType(String JavaDoc aContentType) {
97         this.myContentType.setExpected(aContentType);
98     }
99
100     public void setContentType(String JavaDoc contentType) {
101         this.myContentType.setActual(contentType);
102     }
103
104     public String JavaDoc getContextPath() {
105         return (String JavaDoc) myContextPath.getValue();
106     }
107
108     public void setupGetContextPath(String JavaDoc contextPath) {
109         myContextPath.setValue(contextPath);
110     }
111
112     public Cookie JavaDoc[] getCookies() {
113         notImplemented();
114         return null;
115     }
116
117     public long getDateHeader(String JavaDoc arg1) {
118         notImplemented();
119         return 0;
120     }
121
122     public String JavaDoc getHeader(String JavaDoc key) {
123         return (String JavaDoc) myHeaders.getNextReturnObject(key);
124     }
125
126     public Enumeration getHeaderNames() {
127         notImplemented();
128         return null;
129     }
130
131     public Enumeration getHeaders(String JavaDoc arg1) {
132         notImplemented();
133         return null;
134     }
135
136     public void setupGetInputStream(ServletInputStream JavaDoc inputStream) {
137         this.inputStream.setValue(inputStream);
138     }
139
140     public ServletInputStream JavaDoc getInputStream()
141         throws IOException JavaDoc {
142         return (ServletInputStream JavaDoc) inputStream.getValue();
143     }
144
145     public int getIntHeader(String JavaDoc arg1) {
146         notImplemented();
147         return 0;
148     }
149
150     public Locale getLocale() {
151         notImplemented();
152         return null;
153     }
154
155     public Enumeration getLocales() {
156         notImplemented();
157         return null;
158     }
159
160     public void setupGetMethod(String JavaDoc aMethod) {
161         this.method.setValue(aMethod);
162     }
163
164     public String JavaDoc getMethod() {
165         return (String JavaDoc) method.getValue();
166     }
167
168     public String JavaDoc getParameter(String JavaDoc paramName) {
169         String JavaDoc[] values = getParameterValues(paramName);
170         if (values == null)
171             return null;
172
173         return values[0];
174     }
175
176     public void setupGetParameterNames(Enumeration names) {
177         this.myParameterNames.setValue(names);
178     }
179
180     public Enumeration getParameterNames() {
181         return (Enumeration) myParameterNames.getValue();
182     }
183
184     public String JavaDoc[] getParameterValues(String JavaDoc key) {
185         return (String JavaDoc[]) myParameters.getNextReturnObject(key);
186     }
187
188     public String JavaDoc getPathInfo() {
189         return (String JavaDoc) myPathInfo.getValue();
190     }
191
192     public String JavaDoc getPathTranslated() {
193         notImplemented();
194         return null;
195     }
196
197     public String JavaDoc getProtocol() {
198         return (String JavaDoc) protocol.getValue();
199     }
200
201     public void setupGetProtocol(String JavaDoc protocol) {
202         this.protocol.setValue(protocol);
203     }
204
205     public String JavaDoc getQueryString() {
206         return (String JavaDoc) queryString.getValue();
207     }
208
209     public BufferedReader JavaDoc getReader() {
210         return (BufferedReader JavaDoc) reader.getValue();
211     }
212
213     public void setupGetReader(BufferedReader JavaDoc reader) throws IOException JavaDoc {
214         this.reader.setValue(reader);
215     }
216
217     public String JavaDoc getRealPath(String JavaDoc arg1) {
218         notImplemented();
219         return null;
220     }
221
222     public void setupGetRemoteAddr(String JavaDoc remoteAddress) {
223         this.myRemoteAddress.setValue(remoteAddress);
224     }
225
226     public String JavaDoc getRemoteAddr() {
227         return (String JavaDoc) myRemoteAddress.getValue();
228     }
229
230     public String JavaDoc getRemoteHost() {
231         notImplemented();
232         return null;
233     }
234
235     public String JavaDoc getRemoteUser() {
236         notImplemented();
237         return null;
238     }
239
240     public void setupGetRequestDispatcher(RequestDispatcher JavaDoc requestDispatcher) {
241         this.requestDispatcher.setValue(requestDispatcher);
242     }
243
244     public RequestDispatcher JavaDoc getRequestDispatcher(String JavaDoc uri) {
245         this.requestDispatcherURI.setActual(uri);
246         return (RequestDispatcher JavaDoc) requestDispatcher.getValue();
247     }
248
249     public void setExpectedRequestDispatcherURI(String JavaDoc uri) {
250         this.requestDispatcherURI.setExpected(uri);
251     }
252
253     public String JavaDoc getRequestedSessionId() {
254         notImplemented();
255         return null;
256     }
257
258     public void setupGetRequestURI(String JavaDoc aRequestURI) {
259         myRequestURI.setValue(aRequestURI);
260     }
261
262     public String JavaDoc getRequestURI() {
263         return (String JavaDoc) myRequestURI.getValue();
264     }
265
266     public String JavaDoc getScheme() {
267         return (String JavaDoc) scheme.getValue();
268     }
269
270     public String JavaDoc getServerName() {
271         return (String JavaDoc) serverName.getValue();
272     }
273
274     public int getServerPort() {
275         notImplemented();
276         return 0;
277     }
278
279     public void setupGetServletPath(String JavaDoc path) {
280         this.servletPath.setValue(path);
281     }
282
283     public String JavaDoc getServletPath() {
284         return (String JavaDoc) servletPath.getValue();
285     }
286
287     public HttpSession JavaDoc getSession() {
288         return (HttpSession JavaDoc) myHttpSession.getValue();
289     }
290
291     public void setSession(HttpSession JavaDoc httpSession) {
292         this.myHttpSession.setValue(httpSession);
293     }
294
295     public void setExpectedCreateSession(boolean createSession) {
296         this.createSession.setExpected(createSession);
297     }
298
299     public HttpSession JavaDoc getSession(boolean createSession) {
300         this.createSession.setActual(createSession);
301         return getSession();
302     }
303
304     public void setupGetUserPrincipal(Principal JavaDoc userPrincipal) {
305         this.myUserPrincipal.setValue(userPrincipal);
306     }
307
308     public Principal JavaDoc getUserPrincipal() {
309         return (Principal JavaDoc) myUserPrincipal.getValue();
310     }
311
312     public boolean isRequestedSessionIdFromCookie() {
313         notImplemented();
314         return false;
315     }
316
317     public boolean isRequestedSessionIdFromUrl() {
318         notImplemented();
319         return false;
320     }
321
322     public boolean isRequestedSessionIdFromURL() {
323         notImplemented();
324         return false;
325     }
326
327     public boolean isRequestedSessionIdValid() {
328         notImplemented();
329         return false;
330     }
331
332     public boolean isSecure() {
333         notImplemented();
334         return false;
335     }
336
337     public boolean isUserInRole(java.lang.String JavaDoc arg1) {
338         notImplemented();
339         return false;
340     }
341
342     public void setupRemoveAttribute(String JavaDoc anAttributeToRemove) {
343         myRemoveAttributes.addExpected(anAttributeToRemove);
344     }
345
346     public void removeAttribute(String JavaDoc anAttributeToRemove) {
347         myRemoveAttributes.addActual(anAttributeToRemove);
348     }
349
350     public void addExpectedSetAttribute(String JavaDoc attributeName,
351         Object JavaDoc attributeValue) {
352         mySetAttributes.addExpected(
353             new MapEntry(attributeName, attributeValue));
354     }
355
356     public void setAttribute(String JavaDoc attributeName, Object JavaDoc attributeValue) {
357         mySetAttributes.addActual(
358             new MapEntry(attributeName, attributeValue));
359     }
360
361     public void setupAddParameter(String JavaDoc paramName, String JavaDoc[] values) {
362         myParameters.putObjectToReturn(paramName, values);
363     }
364
365     public void setupAddParameter(String JavaDoc paramName, String JavaDoc value) {
366         setupAddParameter(paramName, new String JavaDoc[]{value});
367     }
368
369     public void setupAddHeader(String JavaDoc headerName, String JavaDoc value) {
370         myHeaders.putObjectToReturn(headerName, value);
371     }
372
373     public void setupPathInfo(String JavaDoc pathInfo) {
374         myPathInfo.setValue(pathInfo);
375     }
376
377     public void setupQueryString(String JavaDoc aQueryString) {
378         queryString.setValue(aQueryString);
379     }
380
381     public void setupScheme(String JavaDoc aScheme) {
382         scheme.setValue(aScheme);
383     }
384
385     public void setupServerName(String JavaDoc aServerName) {
386         serverName.setValue(aServerName);
387     }
388
389     public StringBuffer JavaDoc getRequestURL() {
390         notImplemented();
391         return null;
392     }
393
394     public void setCharacterEncoding(String JavaDoc s) {
395         notImplemented();
396     }
397
398     public void setupGetParameterMap(Map map) {
399         this.parameterMap.setValue(map);
400     }
401
402     public Map getParameterMap() {
403         return (Map) parameterMap.getValue();
404     }
405 }
406
Popular Tags