001: /*
002: * Copyright 2004, 2005, 2006 Odysseus Software GmbH
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package de.odysseus.calyxo.base.test;
017:
018: import java.io.BufferedReader;
019: import java.io.IOException;
020: import java.security.Principal;
021: import java.util.Collections;
022: import java.util.Enumeration;
023: import java.util.HashMap;
024: import java.util.Locale;
025: import java.util.Map;
026:
027: import javax.servlet.RequestDispatcher;
028: import javax.servlet.ServletInputStream;
029: import javax.servlet.http.Cookie;
030: import javax.servlet.http.HttpServletRequest;
031: import javax.servlet.http.HttpSession;
032:
033: /**
034: * Mock request for testing purposes.
035: * Contains nothing but attribute and parameter values.
036: *
037: * @author Oliver Stuhr
038: */
039: public class TestRequest implements HttpServletRequest {
040:
041: private HttpSession session;
042: // contains entries of type string (attribute name) -> object (attribute values)
043: private Map attributes;
044: // contains entries of type string (parameter name) -> string array (parameter values)
045: private Map parameters = new HashMap();
046:
047: private String recentDispatchPath;
048: private String contextPath = "/context";
049:
050: /**
051: * Creates a new instance.
052: */
053: public TestRequest() {
054: this (new TestSession(), new HashMap());
055: }
056:
057: /**
058: * Creates a new instance.
059: *
060: * @param attributes A java.util.Map containing entries whose
061: * keys are strings and whose values are objects.
062: * It represents the mapping from attribute names
063: * to attribute values.
064: */
065: public TestRequest(Map attributes) {
066: this (new TestSession(), attributes);
067: }
068:
069: /**
070: * Creates a new instance.
071: *
072: * @param session the session we belong to
073: */
074: public TestRequest(HttpSession session) {
075: this (session, new HashMap());
076: }
077:
078: /**
079: * Creates a new instance.
080: *
081: * @param session the session we belong to
082: * @param attributes A java.util.Map containing entries whose
083: * keys are strings and whose values are objects.
084: * It represents the mapping from attribute names
085: * to attribute values.
086: */
087: public TestRequest(HttpSession session, Map attributes) {
088: super ();
089:
090: this .session = session;
091: this .attributes = attributes;
092: }
093:
094: public void reset() {
095: attributes = new HashMap();
096: parameters = new HashMap();
097: }
098:
099: /**
100: * @param name Parameter name.
101: * @param value Parameter value.
102: */
103: public void setParameter(String name, String value) {
104: setParameter(name, new String[] { value });
105: }
106:
107: /**
108: * @param name Parameter name.
109: * @param values Parameter values.
110: */
111: public void setParameter(String name, String[] values) {
112: parameters.put(name, values);
113: }
114:
115: public String toString() {
116: return getClass().getName() + " [attr " + attributes
117: + ", params " + parameters + "]";
118: }
119:
120: /* (non-Javadoc)
121: * @see javax.servlet.http.HttpServletRequest#getAuthType()
122: */
123: public String getAuthType() {
124: throw new UnsupportedOperationException();
125: }
126:
127: /* (non-Javadoc)
128: * @see javax.servlet.http.HttpServletRequest#getCookies()
129: */
130: public Cookie[] getCookies() {
131: throw new UnsupportedOperationException();
132: }
133:
134: /* (non-Javadoc)
135: * @see javax.servlet.http.HttpServletRequest#getDateHeader(java.lang.String)
136: */
137: public long getDateHeader(String name) {
138: throw new UnsupportedOperationException();
139: }
140:
141: /* (non-Javadoc)
142: * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
143: */
144: public String getHeader(String name) {
145: throw new UnsupportedOperationException();
146: }
147:
148: /* (non-Javadoc)
149: * @see javax.servlet.http.HttpServletRequest#getHeaders(java.lang.String)
150: */
151: public Enumeration getHeaders(String name) {
152: throw new UnsupportedOperationException();
153: }
154:
155: /* (non-Javadoc)
156: * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
157: */
158: public Enumeration getHeaderNames() {
159: throw new UnsupportedOperationException();
160: }
161:
162: /* (non-Javadoc)
163: * @see javax.servlet.http.HttpServletRequest#getIntHeader(java.lang.String)
164: */
165: public int getIntHeader(String name) {
166: throw new UnsupportedOperationException();
167: }
168:
169: /* (non-Javadoc)
170: * @see javax.servlet.http.HttpServletRequest#getMethod()
171: */
172: public String getMethod() {
173: throw new UnsupportedOperationException();
174: }
175:
176: /* (non-Javadoc)
177: * @see javax.servlet.http.HttpServletRequest#getPathInfo()
178: */
179: public String getPathInfo() {
180: throw new UnsupportedOperationException();
181: }
182:
183: /* (non-Javadoc)
184: * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
185: */
186: public String getPathTranslated() {
187: throw new UnsupportedOperationException();
188: }
189:
190: /* (non-Javadoc)
191: * @see javax.servlet.http.HttpServletRequest#getContextPath()
192: */
193: public String getContextPath() {
194: return contextPath;
195: }
196:
197: /**
198: * Set context path to be returned in {@link #getContextPath()}
199: */
200: public void setContextPath(String value) {
201: contextPath = value;
202: }
203:
204: /* (non-Javadoc)
205: * @see javax.servlet.http.HttpServletRequest#getQueryString()
206: */
207: public String getQueryString() {
208: throw new UnsupportedOperationException();
209: }
210:
211: /* (non-Javadoc)
212: * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
213: */
214: public String getRemoteUser() {
215: throw new UnsupportedOperationException();
216: }
217:
218: /* (non-Javadoc)
219: * @see javax.servlet.http.HttpServletRequest#isUserInRole(java.lang.String)
220: */
221: public boolean isUserInRole(String role) {
222: throw new UnsupportedOperationException();
223: }
224:
225: /* (non-Javadoc)
226: * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
227: */
228: public Principal getUserPrincipal() {
229: throw new UnsupportedOperationException();
230: }
231:
232: /* (non-Javadoc)
233: * @see javax.servlet.http.HttpServletRequest#getRequestedSessionId()
234: */
235: public String getRequestedSessionId() {
236: throw new UnsupportedOperationException();
237: }
238:
239: /* (non-Javadoc)
240: * @see javax.servlet.http.HttpServletRequest#getRequestURI()
241: */
242: public String getRequestURI() {
243: throw new UnsupportedOperationException();
244: }
245:
246: /* (non-Javadoc)
247: * @see javax.servlet.http.HttpServletRequest#getRequestURL()
248: */
249: public StringBuffer getRequestURL() {
250: throw new UnsupportedOperationException();
251: }
252:
253: /* (non-Javadoc)
254: * @see javax.servlet.http.HttpServletRequest#getServletPath()
255: */
256: public String getServletPath() {
257: throw new UnsupportedOperationException();
258: }
259:
260: /* (non-Javadoc)
261: * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
262: */
263: public HttpSession getSession(boolean create) {
264: if (session == null && create)
265: session = new TestSession();
266: return session;
267: }
268:
269: /* (non-Javadoc)
270: * @see javax.servlet.http.HttpServletRequest#getSession()
271: */
272: public HttpSession getSession() {
273: return getSession(true);
274: }
275:
276: /* (non-Javadoc)
277: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdValid()
278: */
279: public boolean isRequestedSessionIdValid() {
280: throw new UnsupportedOperationException();
281: }
282:
283: /* (non-Javadoc)
284: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromCookie()
285: */
286: public boolean isRequestedSessionIdFromCookie() {
287: throw new UnsupportedOperationException();
288: }
289:
290: /* (non-Javadoc)
291: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromURL()
292: */
293: public boolean isRequestedSessionIdFromURL() {
294: throw new UnsupportedOperationException();
295: }
296:
297: /* (non-Javadoc)
298: * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdFromUrl()
299: */
300: public boolean isRequestedSessionIdFromUrl() {
301: throw new UnsupportedOperationException();
302: }
303:
304: /* (non-Javadoc)
305: * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
306: */
307: public Object getAttribute(String name) {
308: return attributes.get(name);
309: }
310:
311: /* (non-Javadoc)
312: * @see javax.servlet.ServletRequest#getAttributeNames()
313: */
314: public Enumeration getAttributeNames() {
315: return Collections.enumeration(attributes.keySet());
316: }
317:
318: /* (non-Javadoc)
319: * @see javax.servlet.ServletRequest#getCharacterEncoding()
320: */
321: public String getCharacterEncoding() {
322: throw new UnsupportedOperationException();
323: }
324:
325: /* (non-Javadoc)
326: * @see javax.servlet.ServletRequest#setCharacterEncoding(java.lang.String)
327: */
328: public void setCharacterEncoding(String env) {
329: throw new UnsupportedOperationException();
330: }
331:
332: /* (non-Javadoc)
333: * @see javax.servlet.ServletRequest#getContentLength()
334: */
335: public int getContentLength() {
336: throw new UnsupportedOperationException();
337: }
338:
339: /* (non-Javadoc)
340: * @see javax.servlet.ServletRequest#getContentType()
341: */
342: public String getContentType() {
343: throw new UnsupportedOperationException();
344: }
345:
346: /* (non-Javadoc)
347: * @see javax.servlet.ServletRequest#getInputStream()
348: */
349: public ServletInputStream getInputStream() throws IOException {
350: throw new UnsupportedOperationException();
351: }
352:
353: /* (non-Javadoc)
354: * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
355: */
356: public String getParameter(String name) {
357: String[] values = (String[]) parameters.get(name);
358: return values == null || values.length == 0 ? null : values[0];
359: }
360:
361: /* (non-Javadoc)
362: * @see javax.servlet.ServletRequest#getParameterNames()
363: */
364: public Enumeration getParameterNames() {
365: return Collections.enumeration(parameters.keySet());
366: }
367:
368: /* (non-Javadoc)
369: * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
370: */
371: public String[] getParameterValues(String name) {
372: return (String[]) parameters.get(name);
373: }
374:
375: /* (non-Javadoc)
376: * @see javax.servlet.ServletRequest#getParameterMap()
377: */
378: public Map getParameterMap() {
379: return parameters;
380: }
381:
382: /**
383: * Set parameter map.
384: * @param parameters A java.util.Map containing entries whose keys
385: * are strings and whose values are string arrays.
386: * It represents the mapping from parameter names
387: * to parameter values.
388: */
389: public void setParameterMap(Map parameters) {
390: this .parameters = parameters;
391: }
392:
393: /* (non-Javadoc)
394: * @see javax.servlet.ServletRequest#getProtocol()
395: */
396: public String getProtocol() {
397: throw new UnsupportedOperationException();
398: }
399:
400: /* (non-Javadoc)
401: * @see javax.servlet.ServletRequest#getScheme()
402: */
403: public String getScheme() {
404: throw new UnsupportedOperationException();
405: }
406:
407: /* (non-Javadoc)
408: * @see javax.servlet.ServletRequest#getServerName()
409: */
410: public String getServerName() {
411: throw new UnsupportedOperationException();
412: }
413:
414: /* (non-Javadoc)
415: * @see javax.servlet.ServletRequest#getServerPort()
416: */
417: public int getServerPort() {
418: throw new UnsupportedOperationException();
419: }
420:
421: /* (non-Javadoc)
422: * @see javax.servlet.ServletRequest#getReader()
423: */
424: public BufferedReader getReader() throws IOException {
425: throw new UnsupportedOperationException();
426: }
427:
428: /* (non-Javadoc)
429: * @see javax.servlet.ServletRequest#getRemoteAddr()
430: */
431: public String getRemoteAddr() {
432: throw new UnsupportedOperationException();
433: }
434:
435: /* (non-Javadoc)
436: * @see javax.servlet.ServletRequest#getRemoteHost()
437: */
438: public String getRemoteHost() {
439: throw new UnsupportedOperationException();
440: }
441:
442: /* (non-Javadoc)
443: * @see javax.servlet.ServletRequest#setAttribute(java.lang.String, java.lang.Object)
444: */
445: public void setAttribute(String name, Object o) {
446: attributes.put(name, o);
447: }
448:
449: /* (non-Javadoc)
450: * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
451: */
452: public void removeAttribute(String name) {
453: attributes.remove(name);
454: }
455:
456: /* (non-Javadoc)
457: * @see javax.servlet.ServletRequest#getLocale()
458: */
459: public Locale getLocale() {
460: return Locale.getDefault();
461: }
462:
463: /* (non-Javadoc)
464: * @see javax.servlet.ServletRequest#getLocales()
465: */
466: public Enumeration getLocales() {
467: throw new UnsupportedOperationException();
468: }
469:
470: /* (non-Javadoc)
471: * @see javax.servlet.ServletRequest#isSecure()
472: */
473: public boolean isSecure() {
474: throw new UnsupportedOperationException();
475: }
476:
477: /* (non-Javadoc)
478: * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
479: */
480: public RequestDispatcher getRequestDispatcher(String path) {
481: recentDispatchPath = path;
482: return new TestRequestDispatcher(path);
483: }
484:
485: /**
486: * Get dispatch path recently passed to
487: * {@link #getRequestDispatcher(String)}.
488: */
489: public String getRecentRequestDispatchPath() {
490: return recentDispatchPath;
491: }
492:
493: /* (non-Javadoc)
494: * @see javax.servlet.ServletRequest#getRealPath(java.lang.String)
495: */
496: public String getRealPath(String path) {
497: throw new UnsupportedOperationException();
498: }
499:
500: /* (non-Javadoc)
501: * @see javax.servlet.ServletRequest#getLocalAddr()
502: */
503: public String getLocalAddr() {
504: throw new UnsupportedOperationException();
505: }
506:
507: /* (non-Javadoc)
508: * @see javax.servlet.ServletRequest#getLocalName()
509: */
510: public String getLocalName() {
511: throw new UnsupportedOperationException();
512: }
513:
514: /* (non-Javadoc)
515: * @see javax.servlet.ServletRequest#getLocalPort()
516: */
517: public int getLocalPort() {
518: throw new UnsupportedOperationException();
519: }
520:
521: /* (non-Javadoc)
522: * @see javax.servlet.ServletRequest#getRemotePort()
523: */
524: public int getRemotePort() {
525: throw new UnsupportedOperationException();
526: }
527:
528: }
|