Search in sources :

Example 21 with PropertyEditorSupport

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

the class ServletRequestDataBinderTests method testBindingWithNestedObjectCreation.

@Test
public void testBindingWithNestedObjectCreation() throws Exception {
    TestBean tb = new TestBean();
    ServletRequestDataBinder binder = new ServletRequestDataBinder(tb, "person");
    binder.registerCustomEditor(ITestBean.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean());
        }
    });
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.addParameter("spouse", "someValue");
    request.addParameter("spouse.name", "test");
    binder.bind(request);
    assertNotNull(tb.getSpouse());
    assertEquals("test", tb.getSpouse().getName());
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MockHttpServletRequest(org.springframework.mock.web.test.MockHttpServletRequest) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 22 with PropertyEditorSupport

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

the class RadioButtonsTagTests 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 radioButtonElement1 = (Element) spanElement1.elements().get(0);
    assertEquals("input", radioButtonElement1.getName());
    assertEquals("radio", radioButtonElement1.attribute("type").getValue());
    assertEquals("pets", radioButtonElement1.attribute("name").getValue());
    assertEquals("checked", radioButtonElement1.attribute("checked").getValue());
    assertEquals("Rudiger", radioButtonElement1.attribute("value").getValue());
    assertEquals("RUDIGER", spanElement1.getStringValue());
    Element spanElement2 = (Element) document.getRootElement().elements().get(1);
    Element radioButtonElement2 = (Element) spanElement2.elements().get(0);
    assertEquals("input", radioButtonElement2.getName());
    assertEquals("radio", radioButtonElement2.attribute("type").getValue());
    assertEquals("pets", radioButtonElement2.attribute("name").getValue());
    assertEquals("checked", radioButtonElement2.attribute("checked").getValue());
    assertEquals("Spot", radioButtonElement2.attribute("value").getValue());
    assertEquals("SPOT", spanElement2.getStringValue());
    Element spanElement3 = (Element) document.getRootElement().elements().get(2);
    Element radioButtonElement3 = (Element) spanElement3.elements().get(0);
    assertEquals("input", radioButtonElement3.getName());
    assertEquals("radio", radioButtonElement3.attribute("type").getValue());
    assertEquals("pets", radioButtonElement3.attribute("name").getValue());
    assertNull("not checked", radioButtonElement3.attribute("checked"));
    assertEquals("Checkers", radioButtonElement3.attribute("value").getValue());
    assertEquals("CHECKERS", spanElement3.getStringValue());
    Element spanElement4 = (Element) document.getRootElement().elements().get(3);
    Element radioButtonElement4 = (Element) spanElement4.elements().get(0);
    assertEquals("input", radioButtonElement4.getName());
    assertEquals("radio", radioButtonElement4.attribute("type").getValue());
    assertEquals("pets", radioButtonElement4.attribute("name").getValue());
    assertEquals("checked", radioButtonElement4.attribute("checked").getValue());
    assertEquals("Fluffy", radioButtonElement4.attribute("value").getValue());
    assertEquals("FLUFFY", spanElement4.getStringValue());
    Element spanElement5 = (Element) document.getRootElement().elements().get(4);
    Element radioButtonElement5 = (Element) spanElement5.elements().get(0);
    assertEquals("input", radioButtonElement5.getName());
    assertEquals("radio", radioButtonElement5.attribute("type").getValue());
    assertEquals("pets", radioButtonElement5.attribute("name").getValue());
    assertEquals("checked", radioButtonElement5.attribute("checked").getValue());
    assertEquals("Mufty", radioButtonElement5.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 23 with PropertyEditorSupport

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

the class SelectTagTests method nestedPathWithListAndEditor.

@Test
public void nestedPathWithListAndEditor() throws Exception {
    this.tag.setPath("bean.realCountry");
    this.tag.setItems(Country.getCountries());
    this.tag.setItemValue("isoCode");
    this.tag.setItemLabel("name");
    TestBeanWrapper testBean = new TestBeanWrapper();
    testBean.setBean(getTestBean());
    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() {
            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 24 with PropertyEditorSupport

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

the class CheckboxTagTests 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("checkbox", 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 25 with PropertyEditorSupport

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

the class SelectTagTests method withMultiMapWithItemValueAndItemLabel.

/**
	 * Tests new support added as a result of <a
	 * href="http://opensource.atlassian.com/projects/spring/browse/SPR-2660"
	 * target="_blank">SPR-2660</a>.
	 * <p>
	 * Specifically, if the {@code items} attribute is supplied a
	 * {@link Map}, and {@code itemValue} and {@code itemLabel}
	 * are supplied non-null values, then:
	 * </p>
	 * <ul>
	 * <li>{@code itemValue} will be used as the property name of the
	 * map's <em>key</em>, and</li>
	 * <li>{@code itemLabel} will be used as the property name of the
	 * map's <em>value</em>.</li>
	 * </ul>
	 */
@Test
public void withMultiMapWithItemValueAndItemLabel() throws Exception {
    // Save original default locale.
    final Locale defaultLocale = Locale.getDefault();
    // Use a locale that doesn't result in the generation of HTML entities
    // (e.g., not German, where รค becomes &auml;)
    Locale.setDefault(Locale.US);
    try {
        final Country austria = Country.COUNTRY_AT;
        final Country usa = Country.COUNTRY_US;
        final Map someMap = new HashMap();
        someMap.put(austria, LOCALE_AT);
        someMap.put(usa, Locale.US);
        this.bean.setSomeMap(someMap);
        // see: TestBean
        this.tag.setPath("someMap");
        this.tag.setItems(getCountryToLocaleMap());
        // Map key: Country
        this.tag.setItemValue("isoCode");
        // Map value: Locale
        this.tag.setItemLabel("displayLanguage");
        BeanPropertyBindingResult bindingResult = new BeanPropertyBindingResult(getTestBean(), COMMAND_NAME);
        bindingResult.getPropertyAccessor().registerCustomEditor(Country.class, new PropertyEditorSupport() {

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

            @Override
            public String getAsText() {
                return ((Country) getValue()).getIsoCode();
            }
        });
        exposeBindingResult(bindingResult);
        int result = this.tag.doStartTag();
        assertEquals(Tag.SKIP_BODY, result);
        String output = getOutput();
        output = "<doc>" + output + "</doc>";
        SAXReader reader = new SAXReader();
        Document document = reader.read(new StringReader(output));
        Element rootElement = document.getRootElement();
        assertEquals(2, rootElement.elements().size());
        Element selectElement = rootElement.element("select");
        assertEquals("select", selectElement.getName());
        assertEquals("someMap", selectElement.attribute("name").getValue());
        List children = selectElement.elements();
        assertEquals("Incorrect number of children", 3, children.size());
        Element e;
        e = (Element) selectElement.selectSingleNode("option[@value = '" + austria.getIsoCode() + "']");
        assertNotNull("Option node not found with Country ISO code value [" + austria.getIsoCode() + "].", e);
        assertEquals("AT node not selected.", "selected", e.attribute("selected").getValue());
        assertEquals("AT Locale displayLanguage property not used for option label.", LOCALE_AT.getDisplayLanguage(), e.getData());
        e = (Element) selectElement.selectSingleNode("option[@value = '" + usa.getIsoCode() + "']");
        assertNotNull("Option node not found with Country ISO code value [" + usa.getIsoCode() + "].", e);
        assertEquals("US node not selected.", "selected", e.attribute("selected").getValue());
        assertEquals("US Locale displayLanguage property not used for option label.", Locale.US.getDisplayLanguage(), e.getData());
    } finally {
        // Restore original default locale.
        Locale.setDefault(defaultLocale);
    }
}
Also used : Locale(java.util.Locale) BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) HashMap(java.util.HashMap) SAXReader(org.dom4j.io.SAXReader) Element(org.dom4j.Element) Document(org.dom4j.Document) StringReader(java.io.StringReader) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) 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