Search in sources :

Example 41 with PropertyEditorSupport

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

the class WebRequestDataBinderTests method testBindingWithNestedObjectCreation.

@Test
public void testBindingWithNestedObjectCreation() throws Exception {
    TestBean tb = new TestBean();
    WebRequestDataBinder binder = new WebRequestDataBinder(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(new ServletWebRequest(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) ServletWebRequest(org.springframework.web.context.request.ServletWebRequest) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 42 with PropertyEditorSupport

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

the class ServletRequestDataBinderTests method testBindingWithNestedObjectCreationAndWrongOrder.

@Test
public void testBindingWithNestedObjectCreationAndWrongOrder() 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.name", "test");
    request.addParameter("spouse", "someValue");
    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 43 with PropertyEditorSupport

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

the class BindTagTests method bindTagWithIndexedPropertiesAndCustomEditor.

@Test
public void bindTagWithIndexedPropertiesAndCustomEditor() throws JspException {
    PageContext pc = createPageContext();
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new ServletRequestDataBinder(tb, "tb");
    binder.registerCustomEditor(TestBean.class, null, new PropertyEditorSupport() {

        @Override
        public String getAsText() {
            return "something";
        }
    });
    Errors errors = binder.getBindingResult();
    errors.rejectValue("array[0]", "code1", "message1");
    errors.rejectValue("array[0]", "code2", "message2");
    pc.getRequest().setAttribute(BindingResult.MODEL_KEY_PREFIX + "tb", errors);
    BindTag tag = new BindTag();
    tag.setPageContext(pc);
    tag.setPath("tb.array[0]");
    assertTrue("Correct doStartTag return value", tag.doStartTag() == Tag.EVAL_BODY_INCLUDE);
    BindStatus status = (BindStatus) pc.getAttribute(BindTag.STATUS_VARIABLE_NAME, PageContext.REQUEST_SCOPE);
    assertTrue("Has status variable", status != null);
    assertTrue("Correct expression", "array[0]".equals(status.getExpression()));
    // because of the custom editor getValue() should return a String
    assertTrue("Value is TestBean", status.getValue() instanceof String);
    assertTrue("Correct value", "something".equals(status.getValue()));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) Errors(org.springframework.validation.Errors) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) PageContext(javax.servlet.jsp.PageContext) ServletRequestDataBinder(org.springframework.web.bind.ServletRequestDataBinder) DataBinder(org.springframework.validation.DataBinder) BindStatus(org.springframework.web.servlet.support.BindStatus) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 44 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)

Example 45 with PropertyEditorSupport

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

the class SelectTagTests method withListAndTransformTagAndEditor.

@Test
public void withListAndTransformTagAndEditor() throws Exception {
    this.tag.setPath("realCountry");
    this.tag.setItems(Country.getCountries());
    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();
    TransformTag transformTag = new TransformTag();
    transformTag.setValue(Country.getCountries().get(0));
    transformTag.setVar("key");
    transformTag.setParent(this.tag);
    transformTag.setPageContext(getPageContext());
    transformTag.doStartTag();
    assertEquals("Austria", getPageContext().findAttribute("key"));
}
Also used : BeanPropertyBindingResult(org.springframework.validation.BeanPropertyBindingResult) TransformTag(org.springframework.web.servlet.tags.TransformTag) 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