KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > inversoft > junit > Request


1 /*
2  * Copyright (c) 2003, Inversoft
3  *
4  * This software is distribuable under the GNU Lesser General Public License.
5  * For more information visit gnu.org.
6  */

7 package com.inversoft.junit;
8
9
10 import java.util.ArrayList JavaDoc;
11 import java.util.Enumeration JavaDoc;
12 import java.util.HashMap JavaDoc;
13 import java.util.Iterator JavaDoc;
14 import java.util.List JavaDoc;
15 import java.util.Map JavaDoc;
16 import javax.servlet.http.Cookie JavaDoc;
17
18 import com.inversoft.junit.internal.Constants;
19
20
21 /**
22  * This objects is used on the client side to setup the request
23  * for the test methods. The Framework will setup the request
24  * prior to calling the test methods using an instance of this
25  * class.
26  */

27 public class Request {
28     /**
29      * The Authenication object
30      */

31     protected Authentication authentication;
32
33     /**
34      * A list for Cookie objects
35      */

36     protected List JavaDoc cookies;
37
38     /**
39      * A Map for Headers
40      */

41     protected Map JavaDoc headers;
42
43     /**
44      * A Map for parameters
45      */

46     protected Map JavaDoc parameters;
47
48     /**
49      * The optional test location string
50      */

51     protected String JavaDoc testLocation;
52
53     /**
54      * The optional URL
55      */

56     protected URL url;
57
58
59     /**
60      * Constructs a new default http request
61      */

62     public Request() {
63         cookies = new ArrayList JavaDoc();
64         headers = new HashMap JavaDoc();
65         parameters = new HashMap JavaDoc();
66     }
67
68
69     /**
70      * Gets the authentication from the request.
71      *
72      * @return authentication The authentication from the request
73      */

74     public Authentication getAuthentication() {
75         return authentication;
76     }
77
78     /**
79      * Sets the authentication into the request.
80      *
81      * @param authentication The authentication for the request
82      */

83     public void setAuthentication(Authentication authentication) {
84         this.authentication = authentication;
85     }
86
87     /**
88      * Adds the given cookie to the request. This is normally an HTTP specific
89      * method and therefore can be implmeneted empty or to throw an exception
90      *
91      * @param cookie The cookie to add to the request
92      */

93     public void addCookie(Cookie JavaDoc cookie) {
94         cookies.add(cookie);
95     }
96
97     /**
98      * Gets all the cookies that will be sent with the request. This is normally
99      * an HTTP specific method, and therefore can be implemented empty or to throw
100      * an exception
101      *
102      * @return The cookies in the request
103      */

104     public Cookie JavaDoc[] getCookies() {
105         return (Cookie JavaDoc[]) cookies.toArray(new Cookie JavaDoc[0]);
106     }
107
108     /**
109      * Adds the given header to the request. This is normally an HTTP specific
110      * method, and therefore can be implemented empty or to throw an exception
111      *
112      * @param name The name of the header to add
113      * @param value The value of the header
114      */

115     public void addHeader(String JavaDoc name, String JavaDoc value) {
116         headers.put(name, value);
117     }
118
119     /**
120      * Gets the value of the header with the given string name
121      *
122      * @param name The name of the header to fetch
123      * @return The value of the header with the given name or null
124      */

125     public String JavaDoc getHeader(String JavaDoc name) {
126         return (String JavaDoc) headers.get(name);
127     }
128
129     /**
130      * Gets a list of all the header names that have bee added to the request
131      *
132      * @return An array of strings that contains all the header names added
133      */

134     public String JavaDoc[] getHeaderNames() {
135         return (String JavaDoc[]) headers.keySet().toArray(new String JavaDoc[0]);
136     }
137
138     /**
139      * Adds the given parameter to the request with the given key. The key and
140      * value are both objects so that this framework can facilitate different
141      * types of tests other than HTTP oriented tests.
142      *
143      * @param key The key of the parameter
144      * @param value The value of the parameter
145      */

146     public void addParameter(String JavaDoc key, String JavaDoc value) {
147
148         List JavaDoc list = (List JavaDoc) parameters.get(key);
149         if (list == null) {
150             list = new ArrayList JavaDoc();
151             parameters.put(key, list);
152         }
153
154         list.add(value);
155     }
156
157     /**
158      * Gets the first parameter from the request with the given key.
159      *
160      * @param key The key to retrieve
161      * @return The value with the given key or null
162      */

163     public String JavaDoc getParameter(String JavaDoc key) {
164         List JavaDoc list = (List JavaDoc) parameters.get(key);
165         if (list == null || list.size() == 0) {
166             return null;
167         }
168
169         return (String JavaDoc) list.get(0);
170     }
171
172     /**
173      * Removes the parameter from the request with the given key.
174      *
175      * @param key The key to retrieve
176      */

177     public void removeParameter(String JavaDoc key) {
178         parameters.remove(key);
179     }
180
181     /**
182      * Returns the list of the parameter names as an enumeration.
183      *
184      * @return An Enumeration of the parameter names
185      */

186     public Enumeration JavaDoc getParameterNames() {
187         final Iterator JavaDoc iter = parameters.keySet().iterator();
188         return new Enumeration JavaDoc() {
189             public boolean hasMoreElements() {
190                 return iter.hasNext();
191             }
192             public java.lang.Object JavaDoc nextElement() {
193                 return iter.next();
194             }
195         };
196     }
197
198     /**
199      * Returns all the parameters in a Map instance
200      *
201      * @return An Map of all the parameters
202      */

203     public Map JavaDoc getParameterMap() {
204         Iterator JavaDoc iter = parameters.keySet().iterator();
205         Object JavaDoc key;
206         Map JavaDoc newMap = new HashMap JavaDoc();
207         while(iter.hasNext()) {
208             key = iter.next();
209             newMap.put(key,
210                 ((List JavaDoc) parameters.get(key)).toArray(new String JavaDoc [0]));
211         }
212
213         return newMap;
214     }
215
216     /**
217      * Returns all the parameters in a Map instance
218      *
219      * @return An Map of all the parameters
220      */

221     public String JavaDoc [] getParameterValues(String JavaDoc name) {
222         List JavaDoc values = (List JavaDoc) parameters.get(name);
223         if (values == null) {
224             return null;
225         }
226
227         return (String JavaDoc []) values.toArray(new String JavaDoc [0]);
228     }
229
230     /**
231      * Clears all the parameters
232      */

233     public void clearParameters() {
234         parameters.clear();
235     }
236
237     /**
238      * Gets the test location, which could be local, or remote. This is a URL
239      * which could point to anything
240      *
241      * @return The location of the test that this request is for
242      */

243     public String JavaDoc getTestLocation() {
244         return testLocation;
245     }
246
247     /**
248      * Sets the test location, which could be local, or remote. This is a URL
249      * which could point to anything
250      *
251      * @param location The location of the test that this request is for
252      */

253     public void setTestLocation(String JavaDoc location) {
254         this.testLocation = location;
255     }
256
257     /**
258      * Gets the URL that this request will spoof on the server.
259      *
260      * @return The url that will be spoofed on the server (for HTTP tests) or null
261      * if there is no server
262      */

263     public URL getURL() {
264         return url;
265     }
266
267     /**
268      * Sets the URL that this request will spoof on the server. This method also
269      * puts the new spoofed URL parameters into the request so that they can be
270      * used on the server.
271      *
272      * @param url The url that will be spoofed on the server (for HTTP tests)
273      */

274     public void setURL(URL url) {
275         this.url = url;
276
277         //Saves the current URL to a <code>Request</code> object.
278
if (url.getContextPath() != null) {
279             addParameter(Constants.URL_CONTEXT_PARAM, url.getContextPath());
280         }
281
282         if (url.getPathInfo() != null) {
283             addParameter(Constants.URL_PATHINFO_PARAM, url.getPathInfo());
284         }
285
286         if (url.getProtocol() != null) {
287             addParameter(Constants.URL_PROTOCOL_PARAM, url.getProtocol());
288         }
289
290         if (url.getQueryString() != null) {
291             addParameter(Constants.URL_QUERYSTRING_PARAM, url.getQueryString());
292         }
293
294         if (url.getServerName() != null) {
295             String JavaDoc path = url.getServerName();
296
297             if (url.getServerPort() != -1) {
298                 path += ":" + url.getServerPort();
299             }
300
301             addParameter(Constants.URL_SERVERNAME_PARAM, path);
302         }
303
304         if (url.getServletPath() != null) {
305             addParameter(Constants.URL_SERVLETPATH_PARAM, url.getServletPath());
306         }
307     }
308 }
309
Popular Tags