KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.beans.PropertyEditorSupport JavaDoc;
20 import java.util.Arrays JavaDoc;
21
22 import org.springframework.beans.AbstractPropertyValuesTests;
23 import org.springframework.beans.ITestBean;
24 import org.springframework.beans.TestBean;
25 import org.springframework.mock.web.MockHttpServletRequest;
26
27 /**
28  * @author Rod Johnson
29  */

30 public class ServletRequestDataBinderTests extends AbstractPropertyValuesTests {
31
32     public void testBindingWithNestedObjectCreation() throws Exception JavaDoc {
33         TestBean tb = new TestBean();
34
35         ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "person");
36         binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport JavaDoc() {
37             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
38                 setValue(new TestBean());
39             }
40         });
41
42         MockHttpServletRequest request = new MockHttpServletRequest();
43         request.addParameter("spouse", "someValue");
44         request.addParameter("spouse.name", "test");
45         binder.bind(request);
46
47         assertNotNull(tb.getSpouse());
48         assertEquals("test", tb.getSpouse().getName());
49     }
50
51     public void testBindingWithNestedObjectCreationAndWrongOrder() throws Exception JavaDoc {
52         TestBean tb = new TestBean();
53
54         ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "person");
55         binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport JavaDoc() {
56             public void setAsText(String JavaDoc text) throws IllegalArgumentException JavaDoc {
57                 setValue(new TestBean());
58             }
59         });
60
61         MockHttpServletRequest request = new MockHttpServletRequest();
62         request.addParameter("spouse.name", "test");
63         request.addParameter("spouse", "someValue");
64         binder.bind(request);
65
66         assertNotNull(tb.getSpouse());
67         assertEquals("test", tb.getSpouse().getName());
68     }
69
70     public void testNoPrefix() throws Exception JavaDoc {
71         MockHttpServletRequest request = new MockHttpServletRequest();
72         request.addParameter("forname", "Tony");
73         request.addParameter("surname", "Blair");
74         request.addParameter("age", "" + 50);
75
76         ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request);
77         doTestTony(pvs);
78     }
79
80     public void testPrefix() throws Exception JavaDoc {
81         MockHttpServletRequest request = new MockHttpServletRequest();
82         request.addParameter("test_forname", "Tony");
83         request.addParameter("test_surname", "Blair");
84         request.addParameter("test_age", "" + 50);
85
86         ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request);
87         assertTrue("Didn't fidn normal when given prefix", !pvs.contains("forname"));
88         assertTrue("Did treat prefix as normal when not given prefix", pvs.contains("test_forname"));
89
90         pvs = new ServletRequestParameterPropertyValues(request, "test");
91         doTestTony(pvs);
92     }
93
94     public void testNoParameters() throws Exception JavaDoc {
95         MockHttpServletRequest request = new MockHttpServletRequest();
96         ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request);
97         assertTrue("Found no parameters", pvs.getPropertyValues().length == 0);
98     }
99
100     public void testMultipleValuesForParameter() throws Exception JavaDoc {
101         MockHttpServletRequest request = new MockHttpServletRequest();
102         String JavaDoc[] original = new String JavaDoc[] {"Tony", "Rod"};
103         request.addParameter("forname", original);
104
105         ServletRequestParameterPropertyValues pvs = new ServletRequestParameterPropertyValues(request);
106         assertTrue("Found 1 parameter", pvs.getPropertyValues().length == 1);
107         assertTrue("Found array value", pvs.getPropertyValue("forname").getValue() instanceof String JavaDoc[]);
108         String JavaDoc[] values = (String JavaDoc[]) pvs.getPropertyValue("forname").getValue();
109         assertEquals("Correct values", Arrays.asList(values), Arrays.asList(original));
110     }
111
112 }
113
Popular Tags