View Javadoc

1   /*
2    * $Id: StrutsMockHttpServletRequest.java 439747 2006-09-03 09:22:46Z mrdon $
3    *
4    * Copyright 2006 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  package org.apache.struts2.views.jsp;
19  
20  import java.util.Collections;
21  import java.util.Enumeration;
22  import java.util.HashMap;
23  import java.util.Locale;
24  import java.util.Map;
25  import java.util.Vector;
26  
27  import javax.servlet.RequestDispatcher;
28  import javax.servlet.http.HttpSession;
29  
30  import junit.framework.AssertionFailedError;
31  
32  import com.mockobjects.servlet.MockHttpServletRequest;
33  
34  
35  /***
36   * StrutsMockHttpServletRequest
37   *
38   */
39  public class StrutsMockHttpServletRequest extends MockHttpServletRequest {
40  
41      Locale locale = Locale.US;
42      private Map attributes = new HashMap();
43      private Map parameterMap = new HashMap();
44      private String context = "";
45      private String pathInfo = "";
46      private String queryString;
47      private String requestURI;
48      private String scheme;
49      private String serverName;
50      private int serverPort;
51      private String encoding;
52      private String requestDispatherString;
53  
54  
55      public void setAttribute(String s, Object o) {
56          attributes.put(s, o);
57      }
58  
59      public Object getAttribute(String s) {
60          return attributes.get(s);
61      }
62  
63      public Enumeration getAttributeNames() {
64          Vector v = new Vector();
65          v.addAll(attributes.keySet());
66  
67          return v.elements();
68      }
69  
70      public String getContextPath() {
71          return this.context;
72      }
73  
74      public void setLocale(Locale locale) {
75          this.locale = locale;
76      }
77  
78      public Locale getLocale() {
79          return locale;
80      }
81  
82      public void setCharacterEncoding(String s) {
83          this.encoding = s;
84      }
85  
86      public String getCharacterEncoding() {
87          return encoding;
88      }
89  
90      public void setParameterMap(Map parameterMap) {
91          this.parameterMap = parameterMap;
92      }
93  
94      public Map getParameterMap() {
95          return parameterMap;
96      }
97  
98      public String getParameter(String string) {
99          return (String) parameterMap.get(string);
100     }
101 
102     public Enumeration getParameterNames() {
103         return Collections.enumeration(parameterMap.keySet());
104     }
105 
106     public String[] getParameterValues(String string) {
107         return (String[]) parameterMap.get(string);
108     }
109 
110     public String getPathInfo() {
111         return pathInfo;
112     }
113 
114     public void setQueryString(String queryString) {
115         this.queryString = queryString;
116     }
117 
118     public String getQueryString() {
119         return queryString;
120     }
121 
122     public RequestDispatcher getRequestDispatcher(String string) {
123         this.requestDispatherString = string;
124         return super.getRequestDispatcher(string);
125     }
126 
127     /***
128      * Get's the source string that was used in the last getRequestDispatcher method call.
129      */
130     public String getRequestDispatherString() {
131         return requestDispatherString;
132     }
133 
134     public void setRequestURI(String requestURI) {
135         this.requestURI = requestURI;
136     }
137 
138     public String getRequestURI() {
139         return requestURI;
140     }
141 
142     public void setScheme(String scheme) {
143         this.scheme = scheme;
144     }
145 
146     public String getScheme() {
147         return scheme;
148     }
149 
150     public void setServerName(String serverName) {
151         this.serverName = serverName;
152     }
153 
154     public String getServerName() {
155         return serverName;
156     }
157 
158     public void setServerPort(int serverPort) {
159         this.serverPort = serverPort;
160     }
161 
162     public int getServerPort() {
163         return serverPort;
164     }
165 
166     public HttpSession getSession() {
167         HttpSession session = null;
168 
169         try {
170             session = super.getSession();
171         } catch (AssertionFailedError e) {
172             //ignore
173         }
174 
175         if (session == null) {
176             session = new StrutsMockHttpSession();
177             setSession(session);
178         }
179 
180         return session;
181     }
182 
183     public void setupGetContext(String context) {
184         this.context = context;
185     }
186 
187     public void setupGetPathInfo(String pathInfo) {
188         this.pathInfo = pathInfo;
189     }
190 
191     public int getRemotePort() {
192         return 0;
193     }
194 
195     public String getLocalName() {
196         return null;
197     }
198 
199     public String getLocalAddr() {
200         return null;
201     }
202 
203     public int getLocalPort() {
204         return 0;
205     }
206 }