001: /*
002: * $Id: MockHttpServletRequest.java 471754 2006-11-06 14:55:09Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts.mock;
022:
023: import javax.servlet.RequestDispatcher;
024: import javax.servlet.ServletInputStream;
025: import javax.servlet.http.Cookie;
026: import javax.servlet.http.HttpServletRequest;
027: import javax.servlet.http.HttpSession;
028:
029: import java.io.BufferedReader;
030:
031: import java.security.Principal;
032:
033: import java.util.Enumeration;
034: import java.util.HashMap;
035: import java.util.Locale;
036: import java.util.Map;
037:
038: /**
039: * <p>Mock <strong>HttpServletRequest</strong> object for low-level unit tests
040: * of Struts controller components. Coarser grained tests should be
041: * implemented in terms of the Cactus framework, instead of the mock object
042: * classes.</p>
043: *
044: * <p><strong>WARNING</strong> - Only the minimal set of methods needed to
045: * create unit tests is provided, plus additional methods to configure this
046: * object as necessary. Methods for unsupported operations will throw
047: * <code>UnsupportedOperationException</code>.</p>
048: *
049: * <p><strong>WARNING</strong> - Because unit tests operate in a single
050: * threaded environment, no synchronization is performed.</p>
051: *
052: * @version $Rev: 471754 $ $Date: 2006-11-06 08:55:09 -0600 (Mon, 06 Nov 2006) $
053: */
054: public class MockHttpServletRequest implements HttpServletRequest {
055: // ----------------------------------------------------- Instance Variables
056:
057: /**
058: * <p> The set of request attributes. </p>
059: */
060: protected HashMap attributes = new HashMap();
061:
062: /**
063: * <p> The context path for this request. </p>
064: */
065: protected String contextPath = null;
066:
067: /**
068: * <p> The preferred locale for this request. </p>
069: */
070: protected Locale locale = null;
071:
072: /**
073: * <p> The set of arrays of parameter values, keyed by parameter name.
074: * </p>
075: */
076: protected HashMap parameters = new HashMap();
077:
078: /**
079: * <p> The extra path information for this request. v * </p>
080: */
081: protected String pathInfo = null;
082:
083: /**
084: * <p> The authenticated user for this request. </p>
085: */
086: protected Principal principal = null;
087:
088: /**
089: * <p> The query string for this request. </p>
090: */
091: protected String queryString = null;
092:
093: /**
094: * <p> The servlet path for this request. </p>
095: */
096: protected String servletPath = null;
097:
098: /**
099: * <p> The HttpSession with which we are associated. </p>
100: */
101: protected HttpSession session = null;
102:
103: /**
104: * <p> The HTTP request method. </p>
105: */
106: protected String method = null;
107:
108: /**
109: * <p> The Content Type for this request. </p>
110: */
111: protected String contentType = null;
112:
113: // ----------------------------------------------------------- Constructors
114: public MockHttpServletRequest() {
115: super ();
116: }
117:
118: public MockHttpServletRequest(HttpSession session) {
119: super ();
120: setHttpSession(session);
121: }
122:
123: public MockHttpServletRequest(String contextPath,
124: String servletPath, String pathInfo, String queryString) {
125: super ();
126: setPathElements(contextPath, servletPath, pathInfo, queryString);
127: }
128:
129: public MockHttpServletRequest(String contextPath,
130: String servletPath, String pathInfo, String queryString,
131: HttpSession session) {
132: super ();
133: setPathElements(contextPath, servletPath, pathInfo, queryString);
134: setHttpSession(session);
135: }
136:
137: // --------------------------------------------------------- Public Methods
138: public void addParameter(String name, String value) {
139: String[] values = (String[]) parameters.get(name);
140:
141: if (values == null) {
142: String[] results = new String[] { value };
143:
144: parameters.put(name, results);
145:
146: return;
147: }
148:
149: String[] results = new String[values.length + 1];
150:
151: System.arraycopy(values, 0, results, 0, values.length);
152: results[values.length] = value;
153: parameters.put(name, results);
154: }
155:
156: public void setHttpSession(HttpSession session) {
157: this .session = session;
158: }
159:
160: public void setLocale(Locale locale) {
161: this .locale = locale;
162: }
163:
164: public void setMethod(String method) {
165: this .method = method;
166: }
167:
168: public void setContentType(String contentType) {
169: this .contentType = contentType;
170: }
171:
172: public void setPathElements(String contextPath, String servletPath,
173: String pathInfo, String queryString) {
174: this .contextPath = contextPath;
175: this .servletPath = servletPath;
176: this .pathInfo = pathInfo;
177: this .queryString = queryString;
178: }
179:
180: public void setUserPrincipal(Principal principal) {
181: this .principal = principal;
182: }
183:
184: // --------------------------------------------- HttpServletRequest Methods
185: public String getAuthType() {
186: throw new UnsupportedOperationException();
187: }
188:
189: public String getContextPath() {
190: return (contextPath);
191: }
192:
193: public Cookie[] getCookies() {
194: throw new UnsupportedOperationException();
195: }
196:
197: public long getDateHeader(String name) {
198: throw new UnsupportedOperationException();
199: }
200:
201: public String getHeader(String name) {
202: throw new UnsupportedOperationException();
203: }
204:
205: public Enumeration getHeaderNames() {
206: throw new UnsupportedOperationException();
207: }
208:
209: public Enumeration getHeaders(String name) {
210: throw new UnsupportedOperationException();
211: }
212:
213: public int getIntHeader(String name) {
214: throw new UnsupportedOperationException();
215: }
216:
217: public String getMethod() {
218: return (method);
219: }
220:
221: public String getPathInfo() {
222: return (pathInfo);
223: }
224:
225: public String getPathTranslated() {
226: throw new UnsupportedOperationException();
227: }
228:
229: public String getQueryString() {
230: return (queryString);
231: }
232:
233: public String getRemoteUser() {
234: if (principal != null) {
235: return (principal.getName());
236: } else {
237: return (null);
238: }
239: }
240:
241: public String getRequestedSessionId() {
242: throw new UnsupportedOperationException();
243: }
244:
245: public String getRequestURI() {
246: StringBuffer sb = new StringBuffer();
247:
248: if (contextPath != null) {
249: sb.append(contextPath);
250: }
251:
252: if (servletPath != null) {
253: sb.append(servletPath);
254: }
255:
256: if (pathInfo != null) {
257: sb.append(pathInfo);
258: }
259:
260: if (sb.length() > 0) {
261: return (sb.toString());
262: }
263:
264: throw new UnsupportedOperationException();
265: }
266:
267: public StringBuffer getRequestURL() {
268: throw new UnsupportedOperationException();
269: }
270:
271: public String getServletPath() {
272: return (servletPath);
273: }
274:
275: public HttpSession getSession() {
276: return (getSession(true));
277: }
278:
279: public HttpSession getSession(boolean create) {
280: if (create && (session == null)) {
281: session = new MockHttpSession();
282:
283: // modified to act like the real deal,
284: // call with (false) if you want null
285: // throw new UnsupportedOperationException();
286: }
287:
288: return (session);
289: }
290:
291: public Principal getUserPrincipal() {
292: return (principal);
293: }
294:
295: public boolean isRequestedSessionIdFromCookie() {
296: throw new UnsupportedOperationException();
297: }
298:
299: public boolean isRequestedSessionIdFromUrl() {
300: throw new UnsupportedOperationException();
301: }
302:
303: public boolean isRequestedSessionIdFromURL() {
304: throw new UnsupportedOperationException();
305: }
306:
307: public boolean isRequestedSessionIdValid() {
308: throw new UnsupportedOperationException();
309: }
310:
311: public boolean isUserInRole(String role) {
312: if ((principal != null) && (principal instanceof MockPrincipal)) {
313: return (((MockPrincipal) principal).isUserInRole(role));
314: } else {
315: return (false);
316: }
317: }
318:
319: // ------------------------------------------------- ServletRequest Methods
320: public Object getAttribute(String name) {
321: return (attributes.get(name));
322: }
323:
324: public Enumeration getAttributeNames() {
325: return (new MockEnumeration(attributes.keySet().iterator()));
326: }
327:
328: public String getCharacterEncoding() {
329: throw new UnsupportedOperationException();
330: }
331:
332: public int getContentLength() {
333: throw new UnsupportedOperationException();
334: }
335:
336: public String getContentType() {
337: return (contentType);
338: }
339:
340: public ServletInputStream getInputStream() {
341: throw new UnsupportedOperationException();
342: }
343:
344: public Locale getLocale() {
345: return (locale);
346: }
347:
348: public Enumeration getLocales() {
349: throw new UnsupportedOperationException();
350: }
351:
352: public String getParameter(String name) {
353: String[] values = (String[]) parameters.get(name);
354:
355: if (values != null) {
356: return (values[0]);
357: } else {
358: return (null);
359: }
360: }
361:
362: public Map getParameterMap() {
363: return (parameters);
364: }
365:
366: public Enumeration getParameterNames() {
367: return (new MockEnumeration(parameters.keySet().iterator()));
368: }
369:
370: public String[] getParameterValues(String name) {
371: return ((String[]) parameters.get(name));
372: }
373:
374: public String getProtocol() {
375: throw new UnsupportedOperationException();
376: }
377:
378: public BufferedReader getReader() {
379: throw new UnsupportedOperationException();
380: }
381:
382: public String getRealPath(String path) {
383: throw new UnsupportedOperationException();
384: }
385:
386: public String getRemoteAddr() {
387: throw new UnsupportedOperationException();
388: }
389:
390: public String getRemoteHost() {
391: throw new UnsupportedOperationException();
392: }
393:
394: public RequestDispatcher getRequestDispatcher(String path) {
395: throw new UnsupportedOperationException();
396: }
397:
398: public String getScheme() {
399: return ("http");
400: }
401:
402: public String getServerName() {
403: return ("localhost");
404: }
405:
406: public int getServerPort() {
407: return (8080);
408: }
409:
410: public boolean isSecure() {
411: return (false);
412: }
413:
414: public void removeAttribute(String name) {
415: attributes.remove(name);
416: }
417:
418: public void setAttribute(String name, Object value) {
419: if (value == null) {
420: attributes.remove(name);
421: } else {
422: attributes.put(name, value);
423: }
424: }
425:
426: public void setCharacterEncoding(String name) {
427: throw new UnsupportedOperationException();
428: }
429: }
|