KickJava   Java API By Example, From Geeks To Geeks.

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


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.ServletContext JavaDoc;
20 import javax.servlet.ServletException JavaDoc;
21 import javax.servlet.ServletRequest JavaDoc;
22
23 import junit.framework.TestCase;
24
25 import org.springframework.beans.TestBean;
26 import org.springframework.beans.propertyeditors.StringTrimmerEditor;
27 import org.springframework.mock.web.MockHttpServletRequest;
28 import org.springframework.mock.web.MockServletContext;
29 import org.springframework.validation.Errors;
30 import org.springframework.validation.Validator;
31
32 /**
33  * @author Juergen Hoeller
34  * @since 23.05.2003
35  */

36 public class BindUtilsTests extends TestCase {
37
38     public void testBind() throws ServletException JavaDoc {
39         ServletContext JavaDoc sc = new MockServletContext();
40         MockHttpServletRequest request = new MockHttpServletRequest(sc);
41         request.addParameter("name", " myname ");
42         request.addParameter("age", "myage");
43         request.addParameter("spouse.name", " spousename ");
44         request.addParameter("spouse.age", "spouseage");
45
46         TestBean tb = new TestBean();
47         tb.setSpouse(new TestBean());
48         assertTrue("Name not set", tb.getName() == null);
49         assertTrue("Age not set", tb.getAge() == 0);
50         assertTrue("Spouse name not set", tb.getSpouse().getName() == null);
51         assertTrue("Spouse age not set", tb.getSpouse().getAge() == 0);
52         Errors errors = BindUtils.bind(request, tb, "tb");
53
54         assertTrue("Name set", " myname ".equals(tb.getName()));
55         assertTrue("No name error", !errors.hasFieldErrors("name"));
56         assertTrue("Age not set", tb.getAge() == 0);
57         assertTrue("Has age error", errors.hasFieldErrors("age"));
58         assertTrue("Correct age error", "typeMismatch".equals(errors.getFieldError("age").getCode()));
59         assertTrue("Spouse name set", " spousename ".equals(tb.getSpouse().getName()));
60         assertTrue("No spouse name error", !errors.hasFieldErrors("spouse.name"));
61         assertTrue("Spouse age not set", tb.getSpouse().getAge() == 0);
62         assertTrue("Has spouse age error", errors.hasFieldErrors("spouse.age"));
63         assertTrue("Correct spouse age error", "typeMismatch".equals(errors.getFieldError("spouse.age").getCode()));
64     }
65
66     public void testBindAndValidate() throws ServletException JavaDoc {
67         ServletContext JavaDoc sc = new MockServletContext();
68         MockHttpServletRequest request = new MockHttpServletRequest(sc);
69         request.addParameter("name", " myname ");
70         request.addParameter("age", "myage");
71
72         TestBean tb = new TestBean();
73         assertTrue("Name not set", tb.getName() == null);
74         Errors errors = BindUtils.bindAndValidate(request, tb, "tb", new Validator() {
75             public boolean supports(Class JavaDoc clazz) {
76                 return TestBean.class.isAssignableFrom(clazz);
77             }
78             public void validate(Object JavaDoc obj, Errors errors) {
79                 TestBean vtb = (TestBean) obj;
80                 if (" myname ".equals(vtb.getName())) {
81                     errors.rejectValue("name", "notMyname", null, "Name may not be myname");
82                 }
83             }
84         });
85
86         assertTrue("Name set", " myname ".equals(tb.getName()));
87         assertTrue("Has name error", errors.hasFieldErrors("name"));
88         assertTrue("Correct name error", "notMyname".equals(errors.getFieldError("name").getCode()));
89         assertTrue("Age not set", tb.getAge() == 0);
90         assertTrue("Has age error", errors.hasFieldErrors("age"));
91         assertTrue("Correct age error", "typeMismatch".equals(errors.getFieldError("age").getCode()));
92     }
93
94     public void testBindWithInitializer() throws ServletException JavaDoc {
95         ServletContext JavaDoc sc = new MockServletContext();
96         MockHttpServletRequest request = new MockHttpServletRequest(sc);
97         request.addParameter("name", " myname ");
98         request.addParameter("age", "myage");
99
100         TestBean tb = new TestBean();
101         assertTrue("Name not set", tb.getName() == null);
102         assertTrue("Age not set", tb.getAge() == 0);
103         Errors errors = BindUtils.bind(request, tb, "tb", new BindInitializer() {
104             public void initBinder(ServletRequest JavaDoc request, ServletRequestDataBinder binder) {
105                 binder.registerCustomEditor(String JavaDoc.class, "name", new StringTrimmerEditor(true));
106             }
107         });
108
109         assertTrue("Name set", "myname".equals(tb.getName()));
110         assertTrue("No name error", !errors.hasFieldErrors("name"));
111         assertTrue("Age not set", tb.getAge() == 0);
112         assertTrue("Has age error", errors.hasFieldErrors("age"));
113         assertTrue("Correct age error", "typeMismatch".equals(errors.getFieldError("age").getCode()));
114     }
115
116     public void testBindAndValidateWithInitializer() throws ServletException JavaDoc {
117         ServletContext JavaDoc sc = new MockServletContext();
118         MockHttpServletRequest request = new MockHttpServletRequest(sc);
119         request.addParameter("name", " myname ");
120         request.addParameter("age", "myage");
121
122         TestBean tb = new TestBean();
123         assertTrue("Name not set", tb.getName() == null);
124         Errors errors = BindUtils.bindAndValidate(request, tb, "tb",
125             new Validator() {
126                 public boolean supports(Class JavaDoc clazz) {
127                     return TestBean.class.isAssignableFrom(clazz);
128                 }
129                 public void validate(Object JavaDoc obj, Errors errors) {
130                     TestBean vtb = (TestBean) obj;
131                     if ("myname".equals(vtb.getName())) {
132                         errors.rejectValue("name", "notMyname", null, "Name may not be myname");
133                     }
134                 }
135             },
136             new BindInitializer() {
137                 public void initBinder(ServletRequest JavaDoc request, ServletRequestDataBinder binder) {
138                     binder.registerCustomEditor(String JavaDoc.class, "name", new StringTrimmerEditor(true));
139                 }
140             }
141         );
142
143         assertTrue("Name set", "myname".equals(tb.getName()));
144         assertTrue("Has name error", errors.hasFieldErrors("name"));
145         assertTrue("Correct name error", "notMyname".equals(errors.getFieldError("name").getCode()));
146         assertTrue("Age not set", tb.getAge() == 0);
147         assertTrue("Has age error", errors.hasFieldErrors("age"));
148         assertTrue("Correct age error", "typeMismatch".equals(errors.getFieldError("age").getCode()));
149     }
150
151     public void testBindWithRequiredArrayField() throws ServletException JavaDoc {
152         ServletContext JavaDoc sc = new MockServletContext();
153         MockHttpServletRequest request = new MockHttpServletRequest(sc);
154         request.addParameter("stringArray", "alias_1,alias_2");
155
156         TestBean tb = new TestBean();
157         BindUtils.bind(request, tb, "tb", new BindInitializer() {
158             public void initBinder(ServletRequest JavaDoc request, ServletRequestDataBinder binder) {
159                 binder.setRequiredFields(new String JavaDoc[] {"stringArray[0]"});
160             }
161         });
162
163         assertTrue("stringArray length", tb.getStringArray().length == 2);
164     }
165
166 }
167
Popular Tags