Search in sources :

Example 46 with IndexedTestBean

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

the class XmlBeanFactoryTests method autowireByConstructorWithSimpleValues.

@Test
void autowireByConstructorWithSimpleValues() {
    DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
    new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
    ConstructorDependenciesBean rod5 = (ConstructorDependenciesBean) xbf.getBean("rod5");
    TestBean kerry1 = (TestBean) xbf.getBean("kerry1");
    TestBean kerry2 = (TestBean) xbf.getBean("kerry2");
    IndexedTestBean other = (IndexedTestBean) xbf.getBean("other");
    // should have been autowired
    assertThat(rod5.getSpouse1()).isEqualTo(kerry2);
    assertThat(rod5.getSpouse2()).isEqualTo(kerry1);
    assertThat(rod5.getOther()).isEqualTo(other);
    assertThat(rod5.getAge()).isEqualTo(99);
    assertThat(rod5.getName()).isEqualTo("myname");
    DerivedConstructorDependenciesBean rod6 = (DerivedConstructorDependenciesBean) xbf.getBean("rod6");
    // should have been autowired
    assertThat(rod6.initialized).isTrue();
    assertThat(!rod6.destroyed).isTrue();
    assertThat(rod6.getSpouse1()).isEqualTo(kerry2);
    assertThat(rod6.getSpouse2()).isEqualTo(kerry1);
    assertThat(rod6.getOther()).isEqualTo(other);
    assertThat(rod6.getAge()).isEqualTo(0);
    assertThat(rod6.getName()).isNull();
    xbf.destroySingletons();
    assertThat(rod6.destroyed).isTrue();
}
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)

Example 47 with IndexedTestBean

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

the class XmlBeanFactoryTests method doTestAutowire.

private void doTestAutowire(DefaultListableBeanFactory xbf) {
    DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");
    TestBean kerry = (TestBean) xbf.getBean("spouse");
    // should have been autowired
    assertThat(rod1.getSpouse()).isEqualTo(kerry);
    DependenciesBean rod1a = (DependenciesBean) xbf.getBean("rod1a");
    // should have been autowired
    assertThat(rod1a.getSpouse()).isEqualTo(kerry);
    DependenciesBean rod2 = (DependenciesBean) xbf.getBean("rod2");
    // should have been autowired
    assertThat(rod2.getSpouse()).isEqualTo(kerry);
    DependenciesBean rod2a = (DependenciesBean) xbf.getBean("rod2a");
    // should have been set explicitly
    assertThat(rod2a.getSpouse()).isEqualTo(kerry);
    ConstructorDependenciesBean rod3 = (ConstructorDependenciesBean) xbf.getBean("rod3");
    IndexedTestBean other = (IndexedTestBean) xbf.getBean("other");
    // should have been autowired
    assertThat(rod3.getSpouse1()).isEqualTo(kerry);
    assertThat(rod3.getSpouse2()).isEqualTo(kerry);
    assertThat(rod3.getOther()).isEqualTo(other);
    ConstructorDependenciesBean rod3a = (ConstructorDependenciesBean) xbf.getBean("rod3a");
    // should have been autowired
    assertThat(rod3a.getSpouse1()).isEqualTo(kerry);
    assertThat(rod3a.getSpouse2()).isEqualTo(kerry);
    assertThat(rod3a.getOther()).isEqualTo(other);
    assertThatExceptionOfType(FatalBeanException.class).isThrownBy(() -> xbf.getBean("rod4", ConstructorDependenciesBean.class));
    DependenciesBean rod5 = (DependenciesBean) xbf.getBean("rod5");
    // Should not have been autowired
    assertThat((Object) rod5.getSpouse()).isNull();
    BeanFactory appCtx = (BeanFactory) xbf.getBean("childAppCtx");
    assertThat(appCtx.containsBean("rod1")).isTrue();
    assertThat(appCtx.containsBean("jenny")).isTrue();
}
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) FatalBeanException(org.springframework.beans.FatalBeanException) BeanFactory(org.springframework.beans.factory.BeanFactory) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) DependenciesBean(org.springframework.beans.testfixture.beans.DependenciesBean)

Example 48 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setMapPropertyNonMatchingType.

@Test
@SuppressWarnings("unchecked")
void setMapPropertyNonMatchingType() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    Map<String, String> map = new TreeMap<>();
    map.put("key", "value");
    accessor.setPropertyValue("map", map);
    Map<String, String> sortedMap = new TreeMap<>();
    sortedMap.put("sortedKey", "sortedValue");
    accessor.setPropertyValue("sortedMap", sortedMap);
    assertThat(target.getMap()).hasSize(1);
    assertThat(target.getMap().get("key")).isEqualTo("value");
    assertThat(target.getSortedMap().size()).isEqualTo(1);
    assertThat(target.getSortedMap().get("sortedKey")).isEqualTo("sortedValue");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TreeMap(java.util.TreeMap) Test(org.junit.jupiter.api.Test)

Example 49 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method getAndSetIndexedPropertiesWithDirectAccess.

@Test
void getAndSetIndexedPropertiesWithDirectAccess() {
    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("key2"));
    assertThat(accessor.getPropertyValue("array[0]")).isEqualTo(tb0);
    assertThat(accessor.getPropertyValue("array[1]")).isEqualTo(tb1);
    assertThat(accessor.getPropertyValue("list[0]")).isEqualTo(tb2);
    assertThat(accessor.getPropertyValue("list[1]")).isEqualTo(tb3);
    assertThat(accessor.getPropertyValue("set[0]")).isEqualTo(tb6);
    assertThat(accessor.getPropertyValue("set[1]")).isEqualTo(tb7);
    assertThat(accessor.getPropertyValue("map[key1]")).isEqualTo(tb4);
    assertThat(accessor.getPropertyValue("map[key2]")).isEqualTo(tb5);
    assertThat(accessor.getPropertyValue("map['key1']")).isEqualTo(tb4);
    assertThat(accessor.getPropertyValue("map[\"key2\"]")).isEqualTo(tb5);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0]", tb5);
    pvs.add("array[1]", tb4);
    pvs.add("list[0]", tb3);
    pvs.add("list[1]", tb2);
    pvs.add("list[2]", tb0);
    pvs.add("list[4]", tb1);
    pvs.add("map[key1]", tb1);
    pvs.add("map['key2']", tb0);
    pvs.add("map[key5]", tb4);
    pvs.add("map['key9']", tb5);
    accessor.setPropertyValues(pvs);
    assertThat(target.getArray()[0]).isEqualTo(tb5);
    assertThat(target.getArray()[1]).isEqualTo(tb4);
    assertThat((target.getList().get(0))).isEqualTo(tb3);
    assertThat((target.getList().get(1))).isEqualTo(tb2);
    assertThat((target.getList().get(2))).isEqualTo(tb0);
    assertThat((target.getList().get(3))).isNull();
    assertThat((target.getList().get(4))).isEqualTo(tb1);
    assertThat((target.getMap().get("key1"))).isEqualTo(tb1);
    assertThat((target.getMap().get("key2"))).isEqualTo(tb0);
    assertThat((target.getMap().get("key5"))).isEqualTo(tb4);
    assertThat((target.getMap().get("key9"))).isEqualTo(tb5);
    assertThat(accessor.getPropertyValue("array[0]")).isEqualTo(tb5);
    assertThat(accessor.getPropertyValue("array[1]")).isEqualTo(tb4);
    assertThat(accessor.getPropertyValue("list[0]")).isEqualTo(tb3);
    assertThat(accessor.getPropertyValue("list[1]")).isEqualTo(tb2);
    assertThat(accessor.getPropertyValue("list[2]")).isEqualTo(tb0);
    assertThat(accessor.getPropertyValue("list[3]")).isNull();
    assertThat(accessor.getPropertyValue("list[4]")).isEqualTo(tb1);
    assertThat(accessor.getPropertyValue("map[\"key1\"]")).isEqualTo(tb1);
    assertThat(accessor.getPropertyValue("map['key2']")).isEqualTo(tb0);
    assertThat(accessor.getPropertyValue("map[\"key5\"]")).isEqualTo(tb4);
    assertThat(accessor.getPropertyValue("map['key9']")).isEqualTo(tb5);
}
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 50 with IndexedTestBean

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

the class CustomEditorTests method testIndexedPropertiesWithIndividualCustomEditorForProperty.

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

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

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

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

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

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

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue("mapkey2" + 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("array0name5");
    assertThat(tb1.getName()).isEqualTo("array1name4");
    assertThat(tb2.getName()).isEqualTo("list0name3");
    assertThat(tb3.getName()).isEqualTo("list1name2");
    assertThat(tb4.getName()).isEqualTo("mapkey1name1");
    assertThat(tb5.getName()).isEqualTo("mapkey2name0");
    assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("array0name5");
    assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("array1name4");
    assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("list0name3");
    assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("list1name2");
    assertThat(bw.getPropertyValue("map[\"key1\"].name")).isEqualTo("mapkey1name1");
    assertThat(bw.getPropertyValue("map['key2'].name")).isEqualTo("mapkey2name0");
}
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)

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