View Javadoc

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