Search in sources :

Example 61 with BeanWrapperImpl

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

the class BindingPreparationTests method testAutoGrowListOfMaps.

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

Example 62 with BeanWrapperImpl

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

the class BindingPreparationTests method testListOfBeansWithList.

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

Example 63 with BeanWrapperImpl

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

the class BindingPreparationTests method testBeanWrapperCreatesNewNestedMaps.

@Test
public void testBeanWrapperCreatesNewNestedMaps() throws Exception {
    TargetWithNestedMap target = new TargetWithNestedMap();
    BeanWrapperImpl wrapper = new BeanWrapperImpl(target);
    wrapper.setAutoGrowNestedPaths(true);
    // For a nested map, you only have to get an element of it for it to be created
    wrapper.getPropertyValue("nested[foo]");
    // To decide what type to create for nested[foo] we need to look ahead and see
    // what the user is trying to bind it to, e.g. if nested[foo][bar] then it's a map
    wrapper.setPropertyValue("nested[foo]", new LinkedHashMap<String, Object>());
    // But it might equally well be a collection, if nested[foo][0]
    wrapper.setPropertyValue("nested[foo]", new ArrayList<>());
    // Then it would have to be actually bound to get the list to auto-grow
    wrapper.setPropertyValue("nested[foo][0]", "bar");
    assertThat(wrapper.getPropertyValue("nested[foo][0]")).isNotNull();
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) TargetWithNestedObject(org.springframework.boot.bind.RelaxedDataBinderTests.TargetWithNestedObject) Test(org.junit.Test)

Example 64 with BeanWrapperImpl

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

the class BindingPreparationTests method testBeanWrapperLists.

@Test
public void testBeanWrapperLists() throws Exception {
    TargetWithNestedMapOfListOfString target = new TargetWithNestedMapOfListOfString();
    BeanWrapperImpl wrapper = new BeanWrapperImpl(target);
    wrapper.setAutoGrowNestedPaths(true);
    TypeDescriptor descriptor = wrapper.getPropertyTypeDescriptor("nested");
    assertThat(descriptor.isMap()).isTrue();
    wrapper.getPropertyValue("nested[foo]");
    assertThat(wrapper.getPropertyValue("nested")).isNotNull();
    // You also need to bind to a value here
    wrapper.setPropertyValue("nested[foo][0]", "bar");
    wrapper.getPropertyValue("nested[foo][0]");
    assertThat(wrapper.getPropertyValue("nested[foo]")).isNotNull();
}
Also used : BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Test(org.junit.Test)

Example 65 with BeanWrapperImpl

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

the class BindingPreparationTests method testBeanWrapperCreatesNewObjects.

@Test
public void testBeanWrapperCreatesNewObjects() throws Exception {
    TargetWithNestedObject target = new TargetWithNestedObject();
    BeanWrapperImpl wrapper = new BeanWrapperImpl(target);
    wrapper.setAutoGrowNestedPaths(true);
    // For a nested object, you have to set a property for it to be created
    wrapper.setPropertyValue("nested.foo", "bar");
    wrapper.getPropertyValue("nested");
    assertThat(wrapper.getPropertyValue("nested")).isNotNull();
}
Also used : TargetWithNestedObject(org.springframework.boot.bind.RelaxedDataBinderTests.TargetWithNestedObject) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) Test(org.junit.Test)

Aggregations

BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)113 BeanWrapper (org.springframework.beans.BeanWrapper)75 Test (org.junit.jupiter.api.Test)32 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)21 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)20 Test (org.junit.Test)19 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)17 IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)17 TestBean (org.springframework.beans.testfixture.beans.TestBean)17 PropertyDescriptor (java.beans.PropertyDescriptor)14 PropertyEditorSupport (java.beans.PropertyEditorSupport)14 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)14 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)12 HashSet (java.util.HashSet)11 BeansException (org.springframework.beans.BeansException)9 ArrayList (java.util.ArrayList)5 Map (java.util.Map)5 BeanCreationException (org.springframework.beans.factory.BeanCreationException)5 ConstructorArgumentValues (org.springframework.beans.factory.config.ConstructorArgumentValues)5 IOException (java.io.IOException)4