KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cactus > TestWebRequest


1 /*
2  * ========================================================================
3  *
4  * Copyright 2001-2004 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  * ========================================================================
19  */

20 package org.apache.cactus;
21
22 import java.util.Enumeration JavaDoc;
23
24 import junit.framework.TestCase;
25
26 import org.apache.cactus.internal.WebRequestImpl;
27 import org.apache.cactus.internal.configuration.DefaultServletConfiguration;
28 import org.apache.cactus.util.ChainedRuntimeException;
29
30 /**
31  * Unit tests of the <code>WebRequest</code> class.
32  *
33  * @version $Id: TestWebRequest.java,v 1.1 2004/05/22 11:34:46 vmassol Exp $
34  */

35 public class TestWebRequest extends TestCase
36 {
37     /**
38      * Object to unit test.
39      */

40     private WebRequest request;
41
42     /**
43      * @see TestCase#setUp()
44      */

45     protected void setUp()
46     {
47         request = new WebRequestImpl(new DefaultServletConfiguration());
48     }
49     
50     /**
51      * Verify that an exception is thrown when an invalid HTTP METHOD is used
52      * when adding an HTTP parameter.
53      */

54     public void testAddParameterInvalidMethod()
55     {
56         try
57         {
58             request.addParameter("param1", "value1", "INVALIDMETHOD");
59             fail("Should have thrown an exception");
60         }
61         catch (ChainedRuntimeException e)
62         {
63             assertEquals("The method need to be either \"POST\" or \"GET\"",
64                 e.getMessage());
65         }
66     }
67
68     /**
69      * Verify that <code>getParameterGet</code> returns the first parameter
70      * that was added to the request.
71      */

72     public void testGetParametersGetOk()
73     {
74         request.addParameter("param1", "value1", WebRequest.GET_METHOD);
75         request.addParameter("param1", "value2", WebRequest.GET_METHOD);
76
77         String JavaDoc result = request.getParameterGet("param1");
78
79         assertEquals("value1", result);
80     }
81
82     /**
83      * Verify that <code>getParameterGet</code> returns null if no parameter
84      * of a given name was added to the request.
85      */

86     public void testGetParameterGetNull()
87     {
88         request.addParameter("param1", "value1", WebRequest.POST_METHOD);
89
90         String JavaDoc result = request.getParameterGet("param1");
91
92         assertNull(result);
93     }
94
95     /**
96      * Verify that <code>getParameterPost</code> returns the first parameter
97      * that was added to the request.
98      */

99     public void testGetParametersPostOk()
100     {
101         request.addParameter("param1", "value1", WebRequest.POST_METHOD);
102         request.addParameter("param1", "value2", WebRequest.POST_METHOD);
103
104         String JavaDoc result = request.getParameterPost("param1");
105
106         assertEquals("value1", result);
107     }
108
109     /**
110      * Verify that <code>getParameterPost</code> returns null if no parameter
111      * of a given name was added to the request.
112      */

113     public void testGetParameterPostNull()
114     {
115         request.addParameter("param1", "value1", WebRequest.GET_METHOD);
116
117         String JavaDoc result = request.getParameterPost("param1");
118
119         assertNull(result);
120     }
121
122     /**
123      * Verify that <code>getHeader</code> returns the first header
124      * that was added to the request.
125      */

126     public void testGetHeaderOk()
127     {
128         request.addHeader("header1", "value1");
129         request.addHeader("header2", "value2");
130
131         String JavaDoc result = request.getHeader("header1");
132
133         assertEquals("value1", result);
134     }
135
136     /**
137      * Verify that <code>getHeader</code> returns null if no header
138      * of a given name was added to the request.
139      */

140     public void testGetHeaderNull()
141     {
142         String JavaDoc result = request.getHeader("header1");
143
144         assertNull(result);
145     }
146
147     /**
148      * Verify that <Code>toString()</code> returns a nice string representation
149      * of the <code>WebRequest</code>.
150      */

151     public void testToString()
152     {
153         request.addHeader("header1", "value1");
154         request.addHeader("header1", "value2");
155         request.addParameter("param1", "value1", WebRequest.GET_METHOD);
156         request.addParameter("param1", "value1", WebRequest.POST_METHOD);
157         request.addCookie("cookie1", "value1");
158         request.setAutomaticSession(false);
159         request.setURL("jakarta.apache.org:80", "/catalog", "/garden",
160             "/implements/", "param1=value1&param2=&param3=value3");
161
162         String JavaDoc result = request.toString();
163
164         assertEquals("simulation URL = [protocol = [http], host name = "
165             + "[jakarta.apache.org], port = [80], context path = [/catalog], "
166             + "servlet path = [/garden], path info = [/implements/], query "
167             + "string = [param1=value1&param2=&param3=value3]], automatic "
168             + "session = [false], cookies = [[name = [cookie1], value = "
169             + "[value1], domain = [localhost], path = [null], isSecure = "
170             + "[false], comment = [null], expiryDate = [null]]], headers = "
171             + "[[[header1] = [[value1], [value2]]]], GET parameters = "
172             + "[[[param3] = [[value3]]][[param2] = [[]]][[param1] = [[value1], "
173             + "[value1]]]], POST parameters = [[[param1] = [[value1]]]]",
174             result);
175     }
176
177     /**
178      * Verify that an error in the query string of <code>setURL()</code>
179      * raises an exception.
180      */

181     public void testSetURLBadQueryString()
182     {
183         try
184         {
185             request.setURL("jakarta.apache.org:80", "/catalog", "/garden",
186                 "/implements/", "badquerystring");
187             fail("Failed to recognize invalid query string");
188         }
189         catch (RuntimeException JavaDoc e)
190         {
191             assertEquals("Bad QueryString [badquerystring] NameValue pair: "
192                 + "[badquerystring]", e.getMessage());
193         }
194     }
195
196     /**
197      * Verify that we can retrieve several POST parameters.
198      */

199     public void testGetPostParametersSeveral()
200     {
201         request.addParameter("param1", "value1", WebRequest.POST_METHOD);
202         request.addParameter("param2", "value2", WebRequest.POST_METHOD);
203         request.addParameter("param3", "value3", WebRequest.POST_METHOD);
204
205         Enumeration JavaDoc keys = request.getParameterNamesPost();
206         while (keys.hasMoreElements())
207         {
208             String JavaDoc key = (String JavaDoc) keys.nextElement();
209             String JavaDoc[] values = request.getParameterValuesPost(key);
210
211             assertEquals(1, values.length);
212             
213             if (!values[0].equals("value1") && !values[0].equals("value2")
214                 && !values[0].equals("value3"))
215             {
216                 fail("Return value was [" + values[0] + "] but should have "
217                     + "been one of [value1], [value2] or [value3]");
218             }
219         }
220     }
221 }
222
Popular Tags