Search in sources :

Example 16 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.

the class CustomEditorTests method testCharacterEditorWithAllowEmpty.

@Test
public void testCharacterEditorWithAllowEmpty() {
    CharBean cb = new CharBean();
    BeanWrapper bw = new BeanWrapperImpl(cb);
    bw.registerCustomEditor(Character.class, new CharacterEditor(true));
    bw.setPropertyValue("myCharacter", new Character('c'));
    assertEquals(new Character('c'), cb.getMyCharacter());
    bw.setPropertyValue("myCharacter", "c");
    assertEquals(new Character('c'), cb.getMyCharacter());
    bw.setPropertyValue("myCharacter", "A");
    assertEquals(new Character('A'), cb.getMyCharacter());
    bw.setPropertyValue("myCharacter", " ");
    assertEquals(new Character(' '), cb.getMyCharacter());
    bw.setPropertyValue("myCharacter", "");
    assertNull(cb.getMyCharacter());
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.Test)

Example 17 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.

the class CustomEditorTests method testArrayToArrayConversion.

@Test
public void testArrayToArrayConversion() throws PropertyVetoException {
    IndexedTestBean tb = new IndexedTestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean(text, 99));
        }
    });
    bw.setPropertyValue("array", new String[] { "a", "b" });
    assertEquals(2, tb.getArray().length);
    assertEquals("a", tb.getArray()[0].getName());
    assertEquals("b", tb.getArray()[1].getName());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 18 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-framework by spring-projects.

the class CustomEditorTests method testArrayToStringConversion.

@Test
public void testArrayToStringConversion() throws PropertyVetoException {
    TestBean tb = new TestBean();
    BeanWrapper bw = new BeanWrapperImpl(tb);
    bw.registerCustomEditor(String.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("-" + text + "-");
        }
    });
    bw.setPropertyValue("name", new String[] { "a", "b" });
    assertEquals("-a,b-", tb.getName());
}
Also used : BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 19 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-boot by spring-projects.

the class BindingPreparationTests method testAutoGrowNewNestedMapOfListOfString.

@Test
public void testAutoGrowNewNestedMapOfListOfString() throws Exception {
    TargetWithNestedMapOfListOfString target = new TargetWithNestedMapOfListOfString();
    BeanWrapperImpl wrapper = new BeanWrapperImpl(target);
    wrapper.setAutoGrowNestedPaths(true);
    RelaxedDataBinder binder = new RelaxedDataBinder(target);
    binder.normalizePath(wrapper, "nested[foo][0]");
    assertThat(wrapper.getPropertyValue("nested")).isNotNull();
    assertThat(wrapper.getPropertyValue("nested[foo]")).isNotNull();
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.Test)

Example 20 with BeanWrapperImpl

use of org.springframework.beans.BeanWrapperImpl in project spring-boot by spring-projects.

the class BindingPreparationTests method testAutoGrowNewNestedMapOfBeansWithPeriod.

@Test
public void testAutoGrowNewNestedMapOfBeansWithPeriod() throws Exception {
    TargetWithNestedMapOfBean target = new TargetWithNestedMapOfBean();
    BeanWrapperImpl wrapper = new BeanWrapperImpl(target);
    wrapper.setAutoGrowNestedPaths(true);
    RelaxedDataBinder binder = new RelaxedDataBinder(target);
    String result = binder.normalizePath(wrapper, "nested.foo.foo");
    assertThat(wrapper.getPropertyValue("nested")).isNotNull();
    assertThat("nested[foo].foo").isEqualTo(result);
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.Test)

Aggregations

BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)85 BeanWrapper (org.springframework.beans.BeanWrapper)57 Test (org.junit.Test)47 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)21 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)20 ITestBean (org.springframework.tests.sample.beans.ITestBean)17 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)17 TestBean (org.springframework.tests.sample.beans.TestBean)17 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)11 PropertyDescriptor (java.beans.PropertyDescriptor)7 BeansException (org.springframework.beans.BeansException)7 ArrayList (java.util.ArrayList)5 BeanCreationException (org.springframework.beans.factory.BeanCreationException)5 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)5 GrailsDomainClassProperty (grails.core.GrailsDomainClassProperty)4 BigDecimal (java.math.BigDecimal)4 BigInteger (java.math.BigInteger)4 PrivilegedAction (java.security.PrivilegedAction)4 Map (java.util.Map)4