1 16 17 package org.springframework.web.bind; 18 19 import javax.servlet.ServletContext ; 20 import javax.servlet.ServletException ; 21 import javax.servlet.ServletRequest ; 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 36 public class BindUtilsTests extends TestCase { 37 38 public void testBind() throws ServletException { 39 ServletContext 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 { 67 ServletContext 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 clazz) { 76 return TestBean.class.isAssignableFrom(clazz); 77 } 78 public void validate(Object 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 { 95 ServletContext 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 request, ServletRequestDataBinder binder) { 105 binder.registerCustomEditor(String .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 { 117 ServletContext 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 clazz) { 127 return TestBean.class.isAssignableFrom(clazz); 128 } 129 public void validate(Object 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 request, ServletRequestDataBinder binder) { 138 binder.registerCustomEditor(String .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 { 152 ServletContext 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 request, ServletRequestDataBinder binder) { 159 binder.setRequiredFields(new String [] {"stringArray[0]"}); 160 } 161 }); 162 163 assertTrue("stringArray length", tb.getStringArray().length == 2); 164 } 165 166 } 167 | Popular Tags |