Search in sources :

Example 11 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setMapPropertyWithCustomUnmodifiableMap.

@Test
void setMapPropertyWithCustomUnmodifiableMap() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });
    Map<Object, Object> inputMap = new HashMap<>();
    inputMap.put(1, "rod");
    inputMap.put(2, "rob");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map", new ReadOnlyMap<>(inputMap));
    accessor.setPropertyValues(pvs);
    assertThat(((TestBean) target.getMap().get(1)).getName()).isEqualTo("rod");
    assertThat(((TestBean) target.getMap().get(2)).getName()).isEqualTo("rob");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) HashMap(java.util.HashMap) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 12 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method getAndSetIndexedProperties.

@Test
void getAndSetIndexedProperties() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    TestBean tb0 = target.getArray()[0];
    TestBean tb1 = target.getArray()[1];
    TestBean tb2 = ((TestBean) target.getList().get(0));
    TestBean tb3 = ((TestBean) target.getList().get(1));
    TestBean tb6 = ((TestBean) target.getSet().toArray()[0]);
    TestBean tb7 = ((TestBean) target.getSet().toArray()[1]);
    TestBean tb4 = ((TestBean) target.getMap().get("key1"));
    TestBean tb5 = ((TestBean) target.getMap().get("key.3"));
    TestBean tb8 = ((TestBean) target.getMap().get("key5[foo]"));
    assertThat(tb0.getName()).isEqualTo("name0");
    assertThat(tb1.getName()).isEqualTo("name1");
    assertThat(tb2.getName()).isEqualTo("name2");
    assertThat(tb3.getName()).isEqualTo("name3");
    assertThat(tb6.getName()).isEqualTo("name6");
    assertThat(tb7.getName()).isEqualTo("name7");
    assertThat(tb4.getName()).isEqualTo("name4");
    assertThat(tb5.getName()).isEqualTo("name5");
    assertThat(tb8.getName()).isEqualTo("name8");
    assertThat(accessor.getPropertyValue("array[0].name")).isEqualTo("name0");
    assertThat(accessor.getPropertyValue("array[1].name")).isEqualTo("name1");
    assertThat(accessor.getPropertyValue("list[0].name")).isEqualTo("name2");
    assertThat(accessor.getPropertyValue("list[1].name")).isEqualTo("name3");
    assertThat(accessor.getPropertyValue("set[0].name")).isEqualTo("name6");
    assertThat(accessor.getPropertyValue("set[1].name")).isEqualTo("name7");
    assertThat(accessor.getPropertyValue("map[key1].name")).isEqualTo("name4");
    assertThat(accessor.getPropertyValue("map[key.3].name")).isEqualTo("name5");
    assertThat(accessor.getPropertyValue("map['key1'].name")).isEqualTo("name4");
    assertThat(accessor.getPropertyValue("map[\"key.3\"].name")).isEqualTo("name5");
    assertThat(accessor.getPropertyValue("map[key4][0].name")).isEqualTo("nameX");
    assertThat(accessor.getPropertyValue("map[key4][1].name")).isEqualTo("nameY");
    assertThat(accessor.getPropertyValue("map[key5[foo]].name")).isEqualTo("name8");
    assertThat(accessor.getPropertyValue("map['key5[foo]'].name")).isEqualTo("name8");
    assertThat(accessor.getPropertyValue("map[\"key5[foo]\"].name")).isEqualTo("name8");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0].name", "name5");
    pvs.add("array[1].name", "name4");
    pvs.add("list[0].name", "name3");
    pvs.add("list[1].name", "name2");
    pvs.add("set[0].name", "name8");
    pvs.add("set[1].name", "name9");
    pvs.add("map[key1].name", "name1");
    pvs.add("map['key.3'].name", "name0");
    pvs.add("map[key4][0].name", "nameA");
    pvs.add("map[key4][1].name", "nameB");
    pvs.add("map[key5[foo]].name", "name10");
    accessor.setPropertyValues(pvs);
    assertThat(tb0.getName()).isEqualTo("name5");
    assertThat(tb1.getName()).isEqualTo("name4");
    assertThat(tb2.getName()).isEqualTo("name3");
    assertThat(tb3.getName()).isEqualTo("name2");
    assertThat(tb4.getName()).isEqualTo("name1");
    assertThat(tb5.getName()).isEqualTo("name0");
    assertThat(accessor.getPropertyValue("array[0].name")).isEqualTo("name5");
    assertThat(accessor.getPropertyValue("array[1].name")).isEqualTo("name4");
    assertThat(accessor.getPropertyValue("list[0].name")).isEqualTo("name3");
    assertThat(accessor.getPropertyValue("list[1].name")).isEqualTo("name2");
    assertThat(accessor.getPropertyValue("set[0].name")).isEqualTo("name8");
    assertThat(accessor.getPropertyValue("set[1].name")).isEqualTo("name9");
    assertThat(accessor.getPropertyValue("map[\"key1\"].name")).isEqualTo("name1");
    assertThat(accessor.getPropertyValue("map['key.3'].name")).isEqualTo("name0");
    assertThat(accessor.getPropertyValue("map[key4][0].name")).isEqualTo("nameA");
    assertThat(accessor.getPropertyValue("map[key4][1].name")).isEqualTo("nameB");
    assertThat(accessor.getPropertyValue("map[key5[foo]].name")).isEqualTo("name10");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) Test(org.junit.jupiter.api.Test)

Example 13 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setMapPropertyWithUnmodifiableMap.

@Test
void setMapPropertyWithUnmodifiableMap() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });
    Map<Integer, String> inputMap = new HashMap<>();
    inputMap.put(1, "rod");
    inputMap.put(2, "rob");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map", Collections.unmodifiableMap(inputMap));
    accessor.setPropertyValues(pvs);
    assertThat(((TestBean) target.getMap().get(1)).getName()).isEqualTo("rod");
    assertThat(((TestBean) target.getMap().get(2)).getName()).isEqualTo("rob");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) BigInteger(java.math.BigInteger) BooleanTestBean(org.springframework.beans.testfixture.beans.BooleanTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) NumberTestBean(org.springframework.beans.testfixture.beans.NumberTestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) HashMap(java.util.HashMap) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 14 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setCollectionPropertyNonMatchingType.

@Test
// list cannot be properly parameterized as it breaks other tests
@SuppressWarnings("unchecked")
void setCollectionPropertyNonMatchingType() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    Collection<String> coll = new ArrayList<>();
    coll.add("coll1");
    accessor.setPropertyValue("collection", coll);
    List<String> set = new ArrayList<>();
    set.add("set1");
    accessor.setPropertyValue("set", set);
    List<String> sortedSet = new ArrayList<>();
    sortedSet.add("sortedSet1");
    accessor.setPropertyValue("sortedSet", sortedSet);
    Set<String> list = new HashSet<>();
    list.add("list1");
    accessor.setPropertyValue("list", list);
    assertThat(target.getCollection()).hasSize(1);
    assertThat(target.getCollection().containsAll(coll)).isTrue();
    assertThat(target.getSet().size()).isEqualTo(1);
    assertThat(target.getSet().containsAll(set)).isTrue();
    assertThat(target.getSortedSet().size()).isEqualTo(1);
    assertThat(target.getSortedSet().containsAll(sortedSet)).isTrue();
    assertThat(target.getList().size()).isEqualTo(1);
    assertThat(target.getList().containsAll(list)).isTrue();
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 15 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method isReadableWritableForIndexedProperties.

@Test
void isReadableWritableForIndexedProperties() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    assertThat(accessor.isReadableProperty("array")).isTrue();
    assertThat(accessor.isReadableProperty("list")).isTrue();
    assertThat(accessor.isReadableProperty("set")).isTrue();
    assertThat(accessor.isReadableProperty("map")).isTrue();
    assertThat(accessor.isReadableProperty("xxx")).isFalse();
    assertThat(accessor.isWritableProperty("array")).isTrue();
    assertThat(accessor.isWritableProperty("list")).isTrue();
    assertThat(accessor.isWritableProperty("set")).isTrue();
    assertThat(accessor.isWritableProperty("map")).isTrue();
    assertThat(accessor.isWritableProperty("xxx")).isFalse();
    assertThat(accessor.isReadableProperty("array[0]")).isTrue();
    assertThat(accessor.isReadableProperty("array[0].name")).isTrue();
    assertThat(accessor.isReadableProperty("list[0]")).isTrue();
    assertThat(accessor.isReadableProperty("list[0].name")).isTrue();
    assertThat(accessor.isReadableProperty("set[0]")).isTrue();
    assertThat(accessor.isReadableProperty("set[0].name")).isTrue();
    assertThat(accessor.isReadableProperty("map[key1]")).isTrue();
    assertThat(accessor.isReadableProperty("map[key1].name")).isTrue();
    assertThat(accessor.isReadableProperty("map[key4][0]")).isTrue();
    assertThat(accessor.isReadableProperty("map[key4][0].name")).isTrue();
    assertThat(accessor.isReadableProperty("map[key4][1]")).isTrue();
    assertThat(accessor.isReadableProperty("map[key4][1].name")).isTrue();
    assertThat(accessor.isReadableProperty("array[key1]")).isFalse();
    assertThat(accessor.isWritableProperty("array[0]")).isTrue();
    assertThat(accessor.isWritableProperty("array[0].name")).isTrue();
    assertThat(accessor.isWritableProperty("list[0]")).isTrue();
    assertThat(accessor.isWritableProperty("list[0].name")).isTrue();
    assertThat(accessor.isWritableProperty("set[0]")).isTrue();
    assertThat(accessor.isWritableProperty("set[0].name")).isTrue();
    assertThat(accessor.isWritableProperty("map[key1]")).isTrue();
    assertThat(accessor.isWritableProperty("map[key1].name")).isTrue();
    assertThat(accessor.isWritableProperty("map[key4][0]")).isTrue();
    assertThat(accessor.isWritableProperty("map[key4][0].name")).isTrue();
    assertThat(accessor.isWritableProperty("map[key4][1]")).isTrue();
    assertThat(accessor.isWritableProperty("map[key4][1].name")).isTrue();
    assertThat(accessor.isWritableProperty("array[key1]")).isFalse();
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) Test(org.junit.jupiter.api.Test)

Aggregations

IndexedTestBean (org.springframework.beans.testfixture.beans.IndexedTestBean)62 Test (org.junit.jupiter.api.Test)61 TestBean (org.springframework.beans.testfixture.beans.TestBean)39 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)31 PropertyEditorSupport (java.beans.PropertyEditorSupport)22 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)17 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)16 BooleanTestBean (org.springframework.beans.testfixture.beans.BooleanTestBean)15 NumberTestBean (org.springframework.beans.testfixture.beans.NumberTestBean)15 Assertions.assertThatIllegalArgumentException (org.assertj.core.api.Assertions.assertThatIllegalArgumentException)12 DerivedTestBean (org.springframework.beans.testfixture.beans.DerivedTestBean)11 NestedTestBean (org.springframework.beans.testfixture.beans.NestedTestBean)11 BeanWrapper (org.springframework.beans.BeanWrapper)10 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)10 ArrayList (java.util.ArrayList)7 BeanDefinitionBuilder.genericBeanDefinition (org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition)7 ChildBeanDefinition (org.springframework.beans.factory.support.ChildBeanDefinition)7 HashSet (java.util.HashSet)6 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4