KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > web > bind > RequestUtilsTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.springframework.web.bind;
18
19 import javax.servlet.ServletException JavaDoc;
20
21 import junit.framework.TestCase;
22
23 import org.springframework.mock.web.MockHttpServletRequest;
24
25 /**
26  * @author Juergen Hoeller
27  * @since 06.08.2003
28  */

29 public class RequestUtilsTests extends TestCase {
30
31     public void testRejectMethod() throws ServletRequestBindingException {
32         String JavaDoc methodGet = "GET";
33         String JavaDoc methodPost = "POST";
34
35         MockHttpServletRequest request = new MockHttpServletRequest();
36         request.setMethod(methodPost);
37
38         try {
39             RequestUtils.rejectRequestMethod(request, methodGet);
40         } catch (ServletException JavaDoc ex) {
41             fail("Shouldn't have thrown ServletException");
42         }
43         try {
44             RequestUtils.rejectRequestMethod(request, methodPost);
45             fail("Should have thrown ServletException");
46         } catch (ServletException JavaDoc ex) {
47         }
48     }
49
50     public void testIntParameter() throws ServletRequestBindingException {
51         MockHttpServletRequest request = new MockHttpServletRequest();
52         request.addParameter("param1", "5");
53         request.addParameter("param2", "e");
54         request.addParameter("paramEmpty", "");
55
56         assertEquals(RequestUtils.getIntParameter(request, "param1"), new Integer JavaDoc(5));
57         assertEquals(RequestUtils.getIntParameter(request, "param1", 6), 5);
58         assertEquals(RequestUtils.getRequiredIntParameter(request, "param1"), 5);
59
60         assertEquals(RequestUtils.getIntParameter(request, "param2", 6), 6);
61         try {
62             RequestUtils.getRequiredIntParameter(request, "param2");
63             fail("Should have thrown ServletRequestBindingException");
64         }
65         catch (ServletRequestBindingException ex) {
66             // expected
67
}
68
69         assertEquals(RequestUtils.getIntParameter(request, "param3"), null);
70         assertEquals(RequestUtils.getIntParameter(request, "param3", 6), 6);
71         try {
72             RequestUtils.getRequiredIntParameter(request, "param3");
73             fail("Should have thrown ServletRequestBindingException");
74         }
75         catch (ServletRequestBindingException ex) {
76             // expected
77
}
78
79         try {
80             RequestUtils.getRequiredIntParameter(request, "paramEmpty");
81             fail("Should have thrown ServletRequestBindingException");
82         }
83         catch (ServletRequestBindingException ex) {
84             // expected
85
}
86     }
87
88     public void testIntParameters() throws ServletRequestBindingException {
89         MockHttpServletRequest request = new MockHttpServletRequest();
90         request.addParameter("param", new String JavaDoc[] { "1", "2", "3" });
91
92         request.addParameter("param2", "1");
93         request.addParameter("param2", "2");
94         request.addParameter("param2", "bogus");
95
96         int[] array = new int[] { 1, 2, 3 };
97         int[] values = RequestUtils.getRequiredIntParameters(request, "param");
98         assertEquals(3, values.length);
99         for (int i = 0; i < array.length; i++) {
100             assertEquals(array[i], values[i]);
101         }
102
103         try {
104             RequestUtils.getRequiredIntParameters(request, "param2");
105             fail("Should have thrown ServletRequestBindingException");
106         }
107         catch (ServletRequestBindingException ex) {
108             // expected
109
}
110
111     }
112
113     public void testLongParameter() throws ServletRequestBindingException {
114         MockHttpServletRequest request = new MockHttpServletRequest();
115         request.addParameter("param1", "5");
116         request.addParameter("param2", "e");
117         request.addParameter("paramEmpty", "");
118
119         assertEquals(RequestUtils.getLongParameter(request, "param1"), new Long JavaDoc(5L));
120         assertEquals(RequestUtils.getLongParameter(request, "param1", 6L), 5L);
121         assertEquals(RequestUtils.getRequiredIntParameter(request, "param1"), 5L);
122
123         assertEquals(RequestUtils.getLongParameter(request, "param2", 6L), 6L);
124         try {
125             RequestUtils.getRequiredLongParameter(request, "param2");
126             fail("Should have thrown ServletRequestBindingException");
127         }
128         catch (ServletRequestBindingException ex) {
129             // expected
130
}
131
132         assertEquals(RequestUtils.getLongParameter(request, "param3"), null);
133         assertEquals(RequestUtils.getLongParameter(request, "param3", 6L), 6L);
134         try {
135             RequestUtils.getRequiredLongParameter(request, "param3");
136             fail("Should have thrown ServletRequestBindingException");
137         }
138         catch (ServletRequestBindingException ex) {
139             // expected
140
}
141
142         try {
143             RequestUtils.getRequiredLongParameter(request, "paramEmpty");
144             fail("Should have thrown ServletRequestBindingException");
145         }
146         catch (ServletRequestBindingException ex) {
147             // expected
148
}
149     }
150
151     public void testLongParameters() throws ServletRequestBindingException {
152         MockHttpServletRequest request = new MockHttpServletRequest();
153         request.addParameter("param", new String JavaDoc[] { "1", "2", "3" });
154
155         request.addParameter("param2", "1");
156         request.addParameter("param2", "2");
157         request.addParameter("param2", "bogus");
158
159         long[] array = new long[] { 1L, 2L, 3L };
160         long[] values = RequestUtils.getRequiredLongParameters(request, "param");
161         assertEquals(3, values.length);
162         for (int i = 0; i < array.length; i++) {
163             assertEquals(array[i], values[i]);
164         }
165
166         try {
167             RequestUtils.getRequiredLongParameters(request, "param2");
168             fail("Should have thrown ServletRequestBindingException");
169         }
170         catch (ServletRequestBindingException ex) {
171             // expected
172
}
173     }
174
175     public void testFloatParameter() throws ServletRequestBindingException {
176         MockHttpServletRequest request = new MockHttpServletRequest();
177         request.addParameter("param1", "5.5");
178         request.addParameter("param2", "e");
179         request.addParameter("paramEmpty", "");
180
181         assertTrue(RequestUtils.getFloatParameter(request, "param1").equals(new Float JavaDoc(5.5f)));
182         assertTrue(RequestUtils.getFloatParameter(request, "param1", 6.5f) == 5.5f);
183         assertTrue(RequestUtils.getRequiredFloatParameter(request, "param1") == 5.5f);
184
185         assertTrue(RequestUtils.getFloatParameter(request, "param2", 6.5f) == 6.5f);
186         try {
187             RequestUtils.getRequiredFloatParameter(request, "param2");
188             fail("Should have thrown ServletRequestBindingException");
189         }
190         catch (ServletRequestBindingException ex) {
191             // expected
192
}
193
194         assertTrue(RequestUtils.getFloatParameter(request, "param3") == null);
195         assertTrue(RequestUtils.getFloatParameter(request, "param3", 6.5f) == 6.5f);
196         try {
197             RequestUtils.getRequiredFloatParameter(request, "param3");
198             fail("Should have thrown ServletRequestBindingException");
199         }
200         catch (ServletRequestBindingException ex) {
201             // expected
202
}
203
204         try {
205             RequestUtils.getRequiredFloatParameter(request, "paramEmpty");
206             fail("Should have thrown ServletRequestBindingException");
207         }
208         catch (ServletRequestBindingException ex) {
209             // expected
210
}
211     }
212
213     public void testFloatParameters() throws ServletRequestBindingException {
214         MockHttpServletRequest request = new MockHttpServletRequest();
215         request.addParameter("param", new String JavaDoc[] { "1.5", "2.5", "3" });
216
217         request.addParameter("param2", "1.5");
218         request.addParameter("param2", "2");
219         request.addParameter("param2", "bogus");
220
221         float[] array = new float[] { 1.5F, 2.5F, 3 };
222         float[] values = RequestUtils.getRequiredFloatParameters(request, "param");
223         assertEquals(3, values.length);
224         for (int i = 0; i < array.length; i++) {
225             assertEquals(array[i], values[i], 0);
226         }
227
228         try {
229             RequestUtils.getRequiredFloatParameters(request, "param2");
230             fail("Should have thrown ServletRequestBindingException");
231         }
232         catch (ServletRequestBindingException ex) {
233             // expected
234
}
235     }
236
237     public void testDoubleParameter() throws ServletRequestBindingException {
238         MockHttpServletRequest request = new MockHttpServletRequest();
239         request.addParameter("param1", "5.5");
240         request.addParameter("param2", "e");
241         request.addParameter("paramEmpty", "");
242
243         assertTrue(RequestUtils.getDoubleParameter(request, "param1").equals(new Double JavaDoc(5.5)));
244         assertTrue(RequestUtils.getDoubleParameter(request, "param1", 6.5) == 5.5);
245         assertTrue(RequestUtils.getRequiredDoubleParameter(request, "param1") == 5.5);
246
247         assertTrue(RequestUtils.getDoubleParameter(request, "param2", 6.5) == 6.5);
248         try {
249             RequestUtils.getRequiredDoubleParameter(request, "param2");
250             fail("Should have thrown ServletRequestBindingException");
251         }
252         catch (ServletRequestBindingException ex) {
253             // expected
254
}
255
256         assertTrue(RequestUtils.getDoubleParameter(request, "param3") == null);
257         assertTrue(RequestUtils.getDoubleParameter(request, "param3", 6.5) == 6.5);
258         try {
259             RequestUtils.getRequiredDoubleParameter(request, "param3");
260             fail("Should have thrown ServletRequestBindingException");
261         }
262         catch (ServletRequestBindingException ex) {
263             // expected
264
}
265
266         try {
267             RequestUtils.getRequiredDoubleParameter(request, "paramEmpty");
268             fail("Should have thrown ServletRequestBindingException");
269         }
270         catch (ServletRequestBindingException ex) {
271             // expected
272
}
273     }
274
275     public void testDoubleParameters() throws ServletRequestBindingException {
276         MockHttpServletRequest request = new MockHttpServletRequest();
277         request.addParameter("param", new String JavaDoc[] { "1.5", "2.5", "3" });
278
279         request.addParameter("param2", "1.5");
280         request.addParameter("param2", "2");
281         request.addParameter("param2", "bogus");
282
283         double[] array = new double[] { 1.5, 2.5, 3 };
284         double[] values = RequestUtils.getRequiredDoubleParameters(request, "param");
285         assertEquals(3, values.length);
286         for (int i = 0; i < array.length; i++) {
287             assertEquals(array[i], values[i], 0);
288         }
289
290         try {
291             RequestUtils.getRequiredDoubleParameters(request, "param2");
292             fail("Should have thrown ServletRequestBindingException");
293         }
294         catch (ServletRequestBindingException ex) {
295             // expected
296
}
297     }
298
299     public void testBooleanParameter() throws ServletRequestBindingException {
300         MockHttpServletRequest request = new MockHttpServletRequest();
301         request.addParameter("param1", "true");
302         request.addParameter("param2", "e");
303         request.addParameter("param4", "yes");
304         request.addParameter("param5", "1");
305         request.addParameter("paramEmpty", "");
306
307         assertTrue(RequestUtils.getBooleanParameter(request, "param1").equals(Boolean.TRUE));
308         assertTrue(RequestUtils.getBooleanParameter(request, "param1", false));
309         assertTrue(RequestUtils.getRequiredBooleanParameter(request, "param1"));
310
311         assertFalse(RequestUtils.getBooleanParameter(request, "param2", true));
312         assertFalse(RequestUtils.getRequiredBooleanParameter(request, "param2"));
313
314         assertTrue(RequestUtils.getBooleanParameter(request, "param3") == null);
315         assertTrue(RequestUtils.getBooleanParameter(request, "param3", true));
316         try {
317             RequestUtils.getRequiredBooleanParameter(request, "param3");
318             fail("Should have thrown ServletRequestBindingException");
319         }
320         catch (ServletRequestBindingException ex) {
321             // expected
322
}
323
324         assertTrue(RequestUtils.getBooleanParameter(request, "param4", false));
325         assertTrue(RequestUtils.getRequiredBooleanParameter(request, "param4"));
326
327         assertTrue(RequestUtils.getBooleanParameter(request, "param5", false));
328         assertTrue(RequestUtils.getRequiredBooleanParameter(request, "param5"));
329         try {
330             RequestUtils.getRequiredBooleanParameter(request, "paramEmpty");
331             fail("Should have thrown ServletRequestBindingException");
332         }
333         catch (ServletRequestBindingException ex) {
334             // expected
335
}
336     }
337
338     public void testBooleanParameters() throws ServletRequestBindingException {
339         MockHttpServletRequest request = new MockHttpServletRequest();
340         request.addParameter("param", new String JavaDoc[] { "true", "yes", "off", "1", "bogus" });
341
342         request.addParameter("param2", "false");
343         request.addParameter("param2", "true");
344         request.addParameter("param2", "");
345
346         boolean[] array = new boolean[] { true, true, false, true, false };
347         boolean[] values = RequestUtils.getRequiredBooleanParameters(request, "param");
348         assertEquals(5, values.length);
349         for (int i = 0; i < array.length; i++) {
350             assertEquals(array[i], values[i]);
351         }
352
353         try {
354             RequestUtils.getRequiredBooleanParameters(request, "param2");
355             fail("Should have thrown ServletRequestBindingException");
356         }
357         catch (ServletRequestBindingException ex) {
358             // expected
359
}
360     }
361
362     public void testStringParameter() throws ServletRequestBindingException {
363         MockHttpServletRequest request = new MockHttpServletRequest();
364         request.addParameter("param1", "str");
365         request.addParameter("paramEmpty", "");
366
367         assertEquals(RequestUtils.getStringParameter(request, "param1"), "str");
368         assertEquals(RequestUtils.getStringParameter(request, "param1", "string"), "str");
369         assertEquals(RequestUtils.getRequiredStringParameter(request, "param1"), "str");
370
371         assertEquals(RequestUtils.getStringParameter(request, "param3"), null);
372         assertEquals(RequestUtils.getStringParameter(request, "param3", "string"), "string");
373         try {
374             RequestUtils.getRequiredStringParameter(request, "param3");
375             fail("Should have thrown ServletRequestBindingException");
376         }
377         catch (ServletRequestBindingException ex) {
378             // expected
379
}
380
381         try {
382             RequestUtils.getStringParameter(request, "paramEmpty");
383             fail("Should have thrown ServletRequestBindingException");
384         }
385         catch (ServletRequestBindingException ex) {
386             // expected
387
}
388         try {
389             RequestUtils.getRequiredStringParameter(request, "paramEmpty");
390             fail("Should have thrown ServletRequestBindingException");
391         }
392         catch (ServletRequestBindingException ex) {
393             // expected
394
}
395     }
396
397 }
398
Popular Tags