Search in sources :

Example 21 with IndexedTestBean

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

the class PropertyResourceConfigurerTests method testPropertyOverrideConfigurerWithNestedMapPropertyAndDotInMapKey.

@Test
public void testPropertyOverrideConfigurerWithNestedMapPropertyAndDotInMapKey() {
    BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
    factory.registerBeanDefinition("tb", def);
    PropertyOverrideConfigurer poc;
    poc = new PropertyOverrideConfigurer();
    Properties props = new Properties();
    props.setProperty("tb.map[key1]", "99");
    props.setProperty("tb.map[key2.ext]", "test");
    poc.setProperties(props);
    poc.postProcessBeanFactory(factory);
    IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
    assertThat(tb.getMap().get("key1")).isEqualTo("99");
    assertThat(tb.getMap().get("key2.ext")).isEqualTo("test");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinitionBuilder.genericBeanDefinition(org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 22 with IndexedTestBean

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

the class PropertyResourceConfigurerTests method testPropertyOverrideConfigurerWithNestedProperty.

@Test
public void testPropertyOverrideConfigurerWithNestedProperty() {
    BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
    factory.registerBeanDefinition("tb", def);
    PropertyOverrideConfigurer poc;
    poc = new PropertyOverrideConfigurer();
    Properties props = new Properties();
    props.setProperty("tb.array[0].age", "99");
    props.setProperty("tb.list[1].name", "test");
    poc.setProperties(props);
    poc.postProcessBeanFactory(factory);
    IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
    assertThat(tb.getArray()[0].getAge()).isEqualTo(99);
    assertThat(((TestBean) tb.getList().get(1)).getName()).isEqualTo("test");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinitionBuilder.genericBeanDefinition(org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 23 with IndexedTestBean

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

the class PropertyResourceConfigurerTests method testPropertyOverrideConfigurerWithInvalidPropertiesFile.

@Test
public void testPropertyOverrideConfigurerWithInvalidPropertiesFile() {
    BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
    factory.registerBeanDefinition("tb", def);
    PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
    poc.setLocations(TEST_PROPS, XTEST_PROPS);
    poc.setIgnoreResourceNotFound(true);
    poc.postProcessBeanFactory(factory);
    IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
    assertThat(tb.getArray()[0].getAge()).isEqualTo(99);
    assertThat(((TestBean) tb.getList().get(1)).getName()).isEqualTo("test");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinitionBuilder.genericBeanDefinition(org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 24 with IndexedTestBean

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

the class PropertyResourceConfigurerTests method testPropertyOverrideConfigurerWithConvertProperties.

@Test
public void testPropertyOverrideConfigurerWithConvertProperties() {
    BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
    factory.registerBeanDefinition("tb", def);
    ConvertingOverrideConfigurer bfpp = new ConvertingOverrideConfigurer();
    Properties props = new Properties();
    props.setProperty("tb.array[0].name", "99");
    props.setProperty("tb.list[1].name", "test");
    bfpp.setProperties(props);
    bfpp.postProcessBeanFactory(factory);
    IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
    assertThat(tb.getArray()[0].getName()).isEqualTo("X99");
    assertThat(((TestBean) tb.getList().get(1)).getName()).isEqualTo("Xtest");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) BeanDefinitionBuilder.genericBeanDefinition(org.springframework.beans.factory.support.BeanDefinitionBuilder.genericBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) Properties(java.util.Properties) Test(org.junit.jupiter.api.Test)

Example 25 with IndexedTestBean

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

the class CustomEditorTests method testIndexedPropertiesWithDirectAccessAndPropertyEditors.

@Test
void testIndexedPropertiesWithDirectAccessAndPropertyEditors() {
    IndexedTestBean bean = new IndexedTestBean();
    BeanWrapper bw = new BeanWrapperImpl(bean);
    bw.registerCustomEditor(TestBean.class, "array", new PropertyEditorSupport() {

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

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    bw.registerCustomEditor(TestBean.class, "list", new PropertyEditorSupport() {

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

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    bw.registerCustomEditor(TestBean.class, "map", new PropertyEditorSupport() {

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

        @Override
        public String getAsText() {
            return ((TestBean) getValue()).getName();
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0]", "a");
    pvs.add("array[1]", "b");
    pvs.add("list[0]", "c");
    pvs.add("list[1]", "d");
    pvs.add("map[key1]", "e");
    pvs.add("map['key2']", "f");
    bw.setPropertyValues(pvs);
    assertThat(bean.getArray()[0].getName()).isEqualTo("arraya");
    assertThat(bean.getArray()[1].getName()).isEqualTo("arrayb");
    assertThat(((TestBean) bean.getList().get(0)).getName()).isEqualTo("listc");
    assertThat(((TestBean) bean.getList().get(1)).getName()).isEqualTo("listd");
    assertThat(((TestBean) bean.getMap().get("key1")).getName()).isEqualTo("mape");
    assertThat(((TestBean) bean.getMap().get("key2")).getName()).isEqualTo("mapf");
}
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