1 16 17 package org.springframework.web.bind; 18 19 import java.beans.PropertyEditorSupport ; 20 import java.util.Arrays ; 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 30 public class ServletRequestDataBinderTests extends AbstractPropertyValuesTests { 31 32 public void testBindingWithNestedObjectCreation() throws Exception { 33 TestBean tb = new TestBean(); 34 35 ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "person"); 36 binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport () { 37 public void setAsText(String text) throws IllegalArgumentException { 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 { 52 TestBean tb = new TestBean(); 53 54 ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "person"); 55 binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport () { 56 public void setAsText(String text) throws IllegalArgumentException { 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 { 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 { 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 { 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 { 101 MockHttpServletRequest request = new MockHttpServletRequest(); 102 String [] original = new String [] {"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 []); 108 String [] values = (String []) pvs.getPropertyValue("forname").getValue(); 109 assertEquals("Correct values", Arrays.asList(values), Arrays.asList(original)); 110 } 111 112 } 113 | Popular Tags |