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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations