Search in sources :

Example 56 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class InputTagTests method withCustomBinder.

@Test
public void withCustomBinder() throws Exception {
    this.tag.setPath("myFloat");
    BeanPropertyBindingResult errors = new BeanPropertyBindingResult(this.rob, COMMAND_NAME);
    errors.getPropertyAccessor().registerCustomEditor(Float.class, new SimpleFloatEditor());
    exposeBindingResult(errors);
    assertEquals(Tag.SKIP_BODY, this.tag.doStartTag());
    String output = getOutput();
    assertTagOpened(output);
    assertTagClosed(output);
    assertContainsAttribute(output, "type", getType());
    assertValueAttribute(output, "12.34f");
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Test(org.junit.Test)

Example 57 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class CheckboxesTagTests method collectionOfPetsWithEditor.

@Test
public void collectionOfPetsWithEditor() throws Exception {
    this.tag.setPath("pets");
    List allPets = new ArrayList();
    allPets.add(new ItemPet("Rudiger"));
    allPets.add(new ItemPet("Spot"));
    allPets.add(new ItemPet("Checkers"));
    allPets.add(new ItemPet("Fluffy"));
    allPets.add(new ItemPet("Mufty"));
    this.tag.setItems(allPets);
    this.tag.setItemLabel("label");
    this.tag.setId("myId");
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(this.bean, COMMAND_NAME);
    PropertyEditorSupport editor = new ItemPet.CustomEditor();
    bindingResult.getPropertyEditorRegistry().registerCustomEditor(ItemPet.class, editor);
    getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + COMMAND_NAME, bindingResult);
    int result = this.tag.doStartTag();
    assertEquals(Tag.SKIP_BODY, result);
    String output = getOutput();
    // wrap the output so it is valid XML
    output = "<doc>" + output + "</doc>";
    SAXReader reader = new SAXReader();
    Document document = reader.read(new StringReader(output));
    Element spanElement1 = (Element) document.getRootElement().elements().get(0);
    Element checkboxElement1 = (Element) spanElement1.elements().get(0);
    assertEquals("input", checkboxElement1.getName());
    assertEquals("checkbox", checkboxElement1.attribute("type").getValue());
    assertEquals("pets", checkboxElement1.attribute("name").getValue());
    assertEquals("checked", checkboxElement1.attribute("checked").getValue());
    assertEquals("Rudiger", checkboxElement1.attribute("value").getValue());
    assertEquals("RUDIGER", spanElement1.getStringValue());
    Element spanElement2 = (Element) document.getRootElement().elements().get(1);
    Element checkboxElement2 = (Element) spanElement2.elements().get(0);
    assertEquals("input", checkboxElement2.getName());
    assertEquals("checkbox", checkboxElement2.attribute("type").getValue());
    assertEquals("pets", checkboxElement2.attribute("name").getValue());
    assertEquals("checked", checkboxElement2.attribute("checked").getValue());
    assertEquals("Spot", checkboxElement2.attribute("value").getValue());
    assertEquals("SPOT", spanElement2.getStringValue());
    Element spanElement3 = (Element) document.getRootElement().elements().get(2);
    Element checkboxElement3 = (Element) spanElement3.elements().get(0);
    assertEquals("input", checkboxElement3.getName());
    assertEquals("checkbox", checkboxElement3.attribute("type").getValue());
    assertEquals("pets", checkboxElement3.attribute("name").getValue());
    assertNull("not checked", checkboxElement3.attribute("checked"));
    assertEquals("Checkers", checkboxElement3.attribute("value").getValue());
    assertEquals("CHECKERS", spanElement3.getStringValue());
    Element spanElement4 = (Element) document.getRootElement().elements().get(3);
    Element checkboxElement4 = (Element) spanElement4.elements().get(0);
    assertEquals("input", checkboxElement4.getName());
    assertEquals("checkbox", checkboxElement4.attribute("type").getValue());
    assertEquals("pets", checkboxElement4.attribute("name").getValue());
    assertEquals("checked", checkboxElement4.attribute("checked").getValue());
    assertEquals("Fluffy", checkboxElement4.attribute("value").getValue());
    assertEquals("FLUFFY", spanElement4.getStringValue());
    Element spanElement5 = (Element) document.getRootElement().elements().get(4);
    Element checkboxElement5 = (Element) spanElement5.elements().get(0);
    assertEquals("input", checkboxElement5.getName());
    assertEquals("checkbox", checkboxElement5.attribute("type").getValue());
    assertEquals("pets", checkboxElement5.attribute("name").getValue());
    assertEquals("checked", checkboxElement5.attribute("checked").getValue());
    assertEquals("Mufty", checkboxElement5.attribute("value").getValue());
    assertEquals("MUFTY", spanElement5.getStringValue());
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.dom4j.Document) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 58 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class SelectTagTests method withListAndEditorAndNullValue.

@Test
public void withListAndEditorAndNullValue() throws Exception {
    this.tag.setPath("realCountry");
    this.tag.setItems(Country.getCountries());
    this.tag.setItemValue("isoCode");
    this.tag.setItemLabel("name");
    TestBeanWithRealCountry testBean = (TestBeanWithRealCountry) getTestBean();
    testBean.setRealCountry(null);
    BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(testBean, "testBean");
    bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(Country.getCountryWithIsoCode(text));
        }

        @Override
        public String getAsText() {
            Country value = (Country) getValue();
            if (value == null) {
                return "";
            }
            return value.getName();
        }
    });
    getPageContext().getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "testBean", bindingResult);
    this.tag.doStartTag();
    String output = getOutput();
    assertTrue(output.startsWith("<select "));
    assertTrue(output.endsWith("</select>"));
    assertFalse(output.contains("selected=\"selected\""));
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 59 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class TextareaTagTests method customBind.

@Test
public void customBind() throws Exception {
    BeanPropertyBindingResult result = new BeanPropertyBindingResult(createTestBean(), "testBean");
    result.getPropertyAccessor().registerCustomEditor(Float.class, new SimpleFloatEditor());
    exposeBindingResult(result);
    this.tag.setPath("myFloat");
    assertEquals(Tag.SKIP_BODY, this.tag.doStartTag());
    String output = getOutput();
    assertContainsAttribute(output, "name", "myFloat");
    assertBlockTagContains(output, "12.34f");
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) Test(org.junit.Test)

Example 60 with BeanPropertyBindingResult

use of org.springframework.validation.BeanPropertyBindingResult in project spring-framework by spring-projects.

the class ErrorsTagTests method withExplicitNonWhitespaceBodyContent.

@Test
public void withExplicitNonWhitespaceBodyContent() throws Exception {
    String mockContent = "This is some explicit body content";
    this.tag.setBodyContent(new MockBodyContent(mockContent, getWriter()));
    // construct an errors instance of the tag
    TestBean target = new TestBean();
    target.setName("Rob Harrop");
    Errors errors = new BeanPropertyBindingResult(target, COMMAND_NAME);
    errors.rejectValue("name", "some.code", "Default Message");
    exposeBindingResult(errors);
    int result = this.tag.doStartTag();
    assertEquals(BodyTag.EVAL_BODY_BUFFERED, result);
    result = this.tag.doEndTag();
    assertEquals(Tag.EVAL_PAGE, result);
    assertEquals(mockContent, getOutput());
}
Also used : MockBodyContent(org.springframework.mock.web.test.MockBodyContent) Errors(org.springframework.validation.Errors) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TestBean(org.springframework.tests.sample.beans.TestBean) Test(org.junit.Test)

Aggregations

BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)73 Test (org.junit.Test)60 TestBean (org.springframework.tests.sample.beans.TestBean)19 Errors (org.springframework.validation.Errors)18 StringReader (java.io.StringReader)17 Document (org.dom4j.Document)17 Element (org.dom4j.Element)17 SAXReader (org.dom4j.io.SAXReader)17 BindingResult (org.springframework.validation.BindingResult)11 PropertyEditorSupport (java.beans.PropertyEditorSupport)10 List (java.util.List)10 ArrayList (java.util.ArrayList)8 CreateUserCommand (org.asqatasun.webapp.command.CreateUserCommand)6 CreateUserFormValidator (org.asqatasun.webapp.validator.CreateUserFormValidator)6 MockBodyContent (org.springframework.mock.web.test.MockBodyContent)6 LinkedList (java.util.LinkedList)5 ExtendedModelMap (org.springframework.ui.ExtendedModelMap)5 Model (org.springframework.ui.Model)5 FieldError (org.springframework.validation.FieldError)5 ObjectError (org.springframework.validation.ObjectError)5