Search in sources :

Example 26 with IndexedTestBean

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

the class CustomEditorTests method testNestedIndexedPropertiesWithCustomEditorForProperty.

@Test
void testNestedIndexedPropertiesWithCustomEditorForProperty() {
    IndexedTestBean bean = new IndexedTestBean();
    TestBean tb0 = bean.getArray()[0];
    TestBean tb1 = bean.getArray()[1];
    TestBean tb2 = ((TestBean) bean.getList().get(0));
    TestBean tb3 = ((TestBean) bean.getList().get(1));
    TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
    TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
    tb0.setNestedIndexedBean(new IndexedTestBean());
    tb1.setNestedIndexedBean(new IndexedTestBean());
    tb2.setNestedIndexedBean(new IndexedTestBean());
    tb3.setNestedIndexedBean(new IndexedTestBean());
    tb4.setNestedIndexedBean(new IndexedTestBean());
    tb5.setNestedIndexedBean(new IndexedTestBean());
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(String.class, "array.nestedIndexedBean.array.name", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("array" + text);
        }

        @Override
        public String getAsText() {
            return ((String) getValue()).substring(5);
        }
    });
    bw.registerCustomEditor(String.class, "list.nestedIndexedBean.list.name", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("list" + text);
        }

        @Override
        public String getAsText() {
            return ((String) getValue()).substring(4);
        }
    });
    bw.registerCustomEditor(String.class, "map.nestedIndexedBean.map.name", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("map" + text);
        }

        @Override
        public String getAsText() {
            return ((String) getValue()).substring(4);
        }
    });
    assertThat(tb0.getName()).isEqualTo("name0");
    assertThat(tb1.getName()).isEqualTo("name1");
    assertThat(tb2.getName()).isEqualTo("name2");
    assertThat(tb3.getName()).isEqualTo("name3");
    assertThat(tb4.getName()).isEqualTo("name4");
    assertThat(tb5.getName()).isEqualTo("name5");
    assertThat(bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name")).isEqualTo("name0");
    assertThat(bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name")).isEqualTo("name1");
    assertThat(bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name")).isEqualTo("name2");
    assertThat(bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name")).isEqualTo("name3");
    assertThat(bw.getPropertyValue("map[key1].nestedIndexedBean.map[key1].name")).isEqualTo("name4");
    assertThat(bw.getPropertyValue("map['key2'].nestedIndexedBean.map[\"key2\"].name")).isEqualTo("name5");
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0].nestedIndexedBean.array[0].name", "name5");
    pvs.add("array[1].nestedIndexedBean.array[1].name", "name4");
    pvs.add("list[0].nestedIndexedBean.list[0].name", "name3");
    pvs.add("list[1].nestedIndexedBean.list[1].name", "name2");
    pvs.add("map[key1].nestedIndexedBean.map[\"key1\"].name", "name1");
    pvs.add("map['key2'].nestedIndexedBean.map[key2].name", "name0");
    bw.setPropertyValues(pvs);
    assertThat(tb0.getNestedIndexedBean().getArray()[0].getName()).isEqualTo("arrayname5");
    assertThat(tb1.getNestedIndexedBean().getArray()[1].getName()).isEqualTo("arrayname4");
    assertThat(((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName()).isEqualTo("listname3");
    assertThat(((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName()).isEqualTo("listname2");
    assertThat(((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName()).isEqualTo("mapname1");
    assertThat(((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName()).isEqualTo("mapname0");
    assertThat(bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name")).isEqualTo("arrayname5");
    assertThat(bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name")).isEqualTo("arrayname4");
    assertThat(bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name")).isEqualTo("listname3");
    assertThat(bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name")).isEqualTo("listname2");
    assertThat(bw.getPropertyValue("map['key1'].nestedIndexedBean.map[key1].name")).isEqualTo("mapname1");
    assertThat(bw.getPropertyValue("map[key2].nestedIndexedBean.map[\"key2\"].name")).isEqualTo("mapname0");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) 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) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 27 with IndexedTestBean

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

the class CustomEditorTests method testArrayToArrayConversion.

@Test
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" });
    assertThat(tb.getArray().length).isEqualTo(2);
    assertThat(tb.getArray()[0].getName()).isEqualTo("a");
    assertThat(tb.getArray()[1].getName()).isEqualTo("b");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) 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) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 28 with IndexedTestBean

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

the class CustomEditorTests method testIndexedPropertiesWithCustomEditorForProperty.

@Test
void testIndexedPropertiesWithCustomEditorForProperty() {
    IndexedTestBean bean = new IndexedTestBean(false);
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(String.class, "array.name", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("array" + text);
        }
    });
    bw.registerCustomEditor(String.class, "list.name", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("list" + text);
        }
    });
    bw.registerCustomEditor(String.class, "map.name", new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("map" + text);
        }
    });
    bean.populate();
    TestBean tb0 = bean.getArray()[0];
    TestBean tb1 = bean.getArray()[1];
    TestBean tb2 = ((TestBean) bean.getList().get(0));
    TestBean tb3 = ((TestBean) bean.getList().get(1));
    TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
    TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
    assertThat(tb0.getName()).isEqualTo("name0");
    assertThat(tb1.getName()).isEqualTo("name1");
    assertThat(tb2.getName()).isEqualTo("name2");
    assertThat(tb3.getName()).isEqualTo("name3");
    assertThat(tb4.getName()).isEqualTo("name4");
    assertThat(tb5.getName()).isEqualTo("name5");
    assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("name0");
    assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("name1");
    assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("name2");
    assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("name3");
    assertThat(bw.getPropertyValue("map[key1].name")).isEqualTo("name4");
    assertThat(bw.getPropertyValue("map[key2].name")).isEqualTo("name5");
    assertThat(bw.getPropertyValue("map['key1'].name")).isEqualTo("name4");
    assertThat(bw.getPropertyValue("map[\"key2\"].name")).isEqualTo("name5");
    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("map[key1].name", "name1");
    pvs.add("map['key2'].name", "name0");
    bw.setPropertyValues(pvs);
    assertThat(tb0.getName()).isEqualTo("arrayname5");
    assertThat(tb1.getName()).isEqualTo("arrayname4");
    assertThat(tb2.getName()).isEqualTo("listname3");
    assertThat(tb3.getName()).isEqualTo("listname2");
    assertThat(tb4.getName()).isEqualTo("mapname1");
    assertThat(tb5.getName()).isEqualTo("mapname0");
    assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("arrayname5");
    assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("arrayname4");
    assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("listname3");
    assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("listname2");
    assertThat(bw.getPropertyValue("map[\"key1\"].name")).isEqualTo("mapname1");
    assertThat(bw.getPropertyValue("map['key2'].name")).isEqualTo("mapname0");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) 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) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 29 with IndexedTestBean

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

the class XmlBeanFactoryTests method nestedPropertyValue.

@Test
void nestedPropertyValue() {
    DefaultListableBeanFactory parent = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(parent).loadBeanDefinitions(PARENT_CONTEXT);
    DefaultListableBeanFactory child = new DefaultListableBeanFactory(parent);
    new XmlBeanDefinitionReader(child).loadBeanDefinitions(CHILD_CONTEXT);
    IndexedTestBean bean = (IndexedTestBean) child.getBean("indexedTestBean");
    assertThat(bean.getArray()[0].getName()).as("name applied correctly").isEqualTo("myname");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.jupiter.api.Test)

Example 30 with IndexedTestBean

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

the class XmlBeanFactoryTests method autowireByConstructor.

@Test
void autowireByConstructor() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    ConstructorDependenciesBean rod1 = (ConstructorDependenciesBean) xbf.getBean("rod1");
    TestBean kerry = (TestBean) xbf.getBean("kerry2");
    // should have been autowired
    assertThat(rod1.getSpouse1()).isEqualTo(kerry);
    assertThat(rod1.getAge()).isEqualTo(0);
    assertThat(rod1.getName()).isNull();
    ConstructorDependenciesBean rod2 = (ConstructorDependenciesBean) xbf.getBean("rod2");
    TestBean kerry1 = (TestBean) xbf.getBean("kerry1");
    TestBean kerry2 = (TestBean) xbf.getBean("kerry2");
    // should have been autowired
    assertThat(rod2.getSpouse1()).isEqualTo(kerry2);
    assertThat(rod2.getSpouse2()).isEqualTo(kerry1);
    assertThat(rod2.getAge()).isEqualTo(0);
    assertThat(rod2.getName()).isNull();
    ConstructorDependenciesBean rod = (ConstructorDependenciesBean) xbf.getBean("rod3");
    IndexedTestBean other = (IndexedTestBean) xbf.getBean("other");
    // should have been autowired
    assertThat(rod.getSpouse1()).isEqualTo(kerry);
    assertThat(rod.getSpouse2()).isEqualTo(kerry);
    assertThat(rod.getOther()).isEqualTo(other);
    assertThat(rod.getAge()).isEqualTo(0);
    assertThat(rod.getName()).isNull();
    xbf.getBean("rod4", ConstructorDependenciesBean.class);
    // should have been autowired
    assertThat(rod.getSpouse1()).isEqualTo(kerry);
    assertThat(rod.getSpouse2()).isEqualTo(kerry);
    assertThat(rod.getOther()).isEqualTo(other);
    assertThat(rod.getAge()).isEqualTo(0);
    assertThat(rod.getName()).isNull();
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) DerivedTestBean(org.springframework.beans.testfixture.beans.DerivedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) 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