Search in sources :

Example 46 with PropertyEditorSupport

use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.

the class SelectTagTests method nestedPathWithListAndEditorAndNullValue.

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

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (text == null || text.length() == 0) {
                setValue(null);
                return;
            }
            setValue(Country.getCountryWithIsoCode(text));
        }

        @Override
        public String getAsText() {
            Country value = (Country) getValue();
            if (value == null) {
                return null;
            }
            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\""));
    assertFalse(output.contains("multiple=\"multiple\""));
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 47 with PropertyEditorSupport

use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.

the class SelectTagTests method withListAndEditor.

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

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

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

Example 48 with PropertyEditorSupport

use of java.beans.PropertyEditorSupport in project spring-framework by spring-projects.

the class RadioButtonTagTests method collectionOfPetsWithEditor.

@Test
public void collectionOfPetsWithEditor() throws Exception {
    this.tag.setPath("pets");
    this.tag.setValue(new ItemPet("Rudiger"));
    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 checkboxElement = (Element) document.getRootElement().elements().get(0);
    assertEquals("input", checkboxElement.getName());
    assertEquals("radio", checkboxElement.attribute("type").getValue());
    assertEquals("pets", checkboxElement.attribute("name").getValue());
    assertEquals("Rudiger", checkboxElement.attribute("value").getValue());
    assertEquals("checked", checkboxElement.attribute("checked").getValue());
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) StringReader(java.io.StringReader) Document(org.dom4j.Document) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 49 with PropertyEditorSupport

use of java.beans.PropertyEditorSupport 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 50 with PropertyEditorSupport

use of java.beans.PropertyEditorSupport 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)

Aggregations

PropertyEditorSupport (java.beans.PropertyEditorSupport)56 Test (org.junit.Test)55 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)37 TestBean (org.springframework.tests.sample.beans.TestBean)31 ITestBean (org.springframework.tests.sample.beans.ITestBean)30 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)23 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)19 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)19 BeanWrapper (org.springframework.beans.BeanWrapper)14 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)14 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)12 BeanPropertyBindingResult (org.springframework.validation.BeanPropertyBindingResult)10 StringReader (java.io.StringReader)5 BigInteger (java.math.BigInteger)5 ArrayList (java.util.ArrayList)5 Document (org.dom4j.Document)5 Element (org.dom4j.Element)5 SAXReader (org.dom4j.io.SAXReader)5 HashMap (java.util.HashMap)4 List (java.util.List)4