001: // StrutsTestCase - a JUnit extension for testing Struts actions
002: // within the context of the ActionServlet.
003: // Copyright (C) 2002 Deryl Seale
004: //
005: // This library is free software; you can redistribute it and/or
006: // modify it under the terms of the Apache Software License as
007: // published by the Apache Software Foundation; either version 1.1
008: // of the License, or (at your option) any later version.
009: //
010: // This library is distributed in the hope that it will be useful,
011: // but WITHOUT ANY WARRANTY; without even the implied warranty of
012: // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
013: // Apache Software Foundation Licens for more details.
014: //
015: // You may view the full text here: http://www.apache.org/LICENSE.txt
016:
017: package servletunit.struts;
018:
019: import org.apache.cactus.ServletURL;
020: import org.apache.cactus.server.HttpServletRequestWrapper;
021:
022: import java.util.*;
023:
024: /**
025: * A wrapper for the HttpServletRequest class. This is used in
026: * CactusStrutsTestCase so that we can add our own request parameters
027: * outside of the beginXXX and endXXX methods. This allows us to
028: * to use the ActionServlet as a black box, rather than mimic its
029: * behavior as was previously the case.
030: */
031: public class StrutsRequestWrapper extends HttpServletRequestWrapper {
032:
033: private String pathInfo;
034: private String servletPath;
035: private Map parameters;
036:
037: public StrutsRequestWrapper(HttpServletRequestWrapper request) {
038: super (request, new ServletURL(request.getServerName(), request
039: .getContextPath(), request.getServletPath(), request
040: .getPathInfo(), request.getQueryString()));
041: parameters = new HashMap();
042: }
043:
044: public void setPathInfo(String pathInfo) {
045: this .pathInfo = pathInfo;
046: }
047:
048: public String getPathInfo() {
049: if (this .pathInfo == null)
050: return super .getPathInfo();
051: else
052: return this .pathInfo;
053: }
054:
055: public void setServletPath(String servletPath) {
056: this .servletPath = servletPath;
057: }
058:
059: public String getServletPath() {
060: if (this .servletPath == null)
061: return super .getServletPath();
062: else
063: return this .servletPath;
064: }
065:
066: public String getParameter(String name) {
067: String[] result = getParameterValues(name);
068: if ((result != null) && (result.length > 0)) {
069: return result[0];
070: } else
071: return null;
072: }
073:
074: public String[] getParameterValues(String name) {
075: Object result = super .getParameterValues(name);
076: if ((result == null) && (parameters.containsKey(name))) {
077: result = parameters.get(name);
078: if (!(result instanceof String[])) {
079: String[] resultArray = { result.toString() };
080: result = resultArray;
081: }
082: }
083: return (String[]) result;
084: }
085:
086: public Enumeration getParameterNames() {
087: Enumeration super Names = super .getParameterNames();
088: List nameList = new ArrayList(parameters.keySet());
089: while (super Names.hasMoreElements()) {
090: nameList.add(super Names.nextElement());
091: }
092: return Collections.enumeration(nameList);
093: }
094:
095: public void addParameter(String name, String value) {
096: if ((super .getParameter(name) == null) && (name != null)
097: && (value != null))
098: parameters.put(name, value);
099: }
100:
101: public void addParameter(String name, String[] values) {
102: if ((super .getParameter(name) == null) && (name != null)
103: && (values != null))
104: parameters.put(name, values);
105: }
106:
107: public Map getParameterMap() {
108: Map result = new HashMap();
109: result.putAll(super .getParameterMap());
110: result.putAll(parameters);
111: return result;
112: }
113:
114: public void clearRequestParameters() {
115: this .parameters.clear();
116: // super.request.getParameterMap().clear();
117: }
118:
119: }
|