Search in sources :

Example 46 with MutablePropertyValues

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

the class DefaultListableBeanFactoryTests method testCustomTypeConverterWithBeanReference.

@Test
public void testCustomTypeConverterWithBeanReference() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    NumberFormat nf = NumberFormat.getInstance(Locale.GERMAN);
    lbf.setTypeConverter(new CustomTypeConverter(nf));
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("myFloat", new RuntimeBeanReference("myFloat"));
    ConstructorArgumentValues cav = new ConstructorArgumentValues();
    cav.addIndexedArgumentValue(0, "myName");
    cav.addIndexedArgumentValue(1, "myAge");
    lbf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class, cav, pvs));
    lbf.registerSingleton("myFloat", "1,1");
    TestBean testBean = (TestBean) lbf.getBean("testBean");
    assertEquals("myName", testBean.getName());
    assertEquals(5, testBean.getAge());
    assertTrue(testBean.getMyFloat().floatValue() == 1.1f);
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) NumberFormat(java.text.NumberFormat) ConstructorArgumentValues(org.springframework.beans.factory.config.ConstructorArgumentValues) Test(org.junit.Test)

Example 47 with MutablePropertyValues

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

the class DefaultListableBeanFactoryTests method testExtensiveCircularReference.

@Test
public void testExtensiveCircularReference() {
    DefaultListableBeanFactory lbf = new DefaultListableBeanFactory();
    for (int i = 0; i < 1000; i++) {
        MutablePropertyValues pvs = new MutablePropertyValues();
        pvs.addPropertyValue(new PropertyValue("spouse", new RuntimeBeanReference("bean" + (i < 99 ? i + 1 : 0))));
        RootBeanDefinition bd = new RootBeanDefinition(TestBean.class);
        bd.setPropertyValues(pvs);
        lbf.registerBeanDefinition("bean" + i, bd);
    }
    lbf.preInstantiateSingletons();
    for (int i = 0; i < 1000; i++) {
        TestBean bean = (TestBean) lbf.getBean("bean" + i);
        TestBean otherBean = (TestBean) lbf.getBean("bean" + (i < 99 ? i + 1 : 0));
        assertTrue(bean.getSpouse() == otherBean);
    }
}
Also used : ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) PropertyValue(org.springframework.beans.PropertyValue) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) RuntimeBeanReference(org.springframework.beans.factory.config.RuntimeBeanReference) Test(org.junit.Test)

Example 48 with MutablePropertyValues

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

the class CustomEditorTests method testIndexedPropertiesWithDirectAccessAndPropertyEditors.

@Test
public 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);
    assertEquals("arraya", bean.getArray()[0].getName());
    assertEquals("arrayb", bean.getArray()[1].getName());
    assertEquals("listc", ((TestBean) bean.getList().get(0)).getName());
    assertEquals("listd", ((TestBean) bean.getList().get(1)).getName());
    assertEquals("mape", ((TestBean) bean.getMap().get("key1")).getName());
    assertEquals("mapf", ((TestBean) bean.getMap().get("key2")).getName());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 49 with MutablePropertyValues

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

the class CustomEditorTests method testIndexedPropertiesWithCustomEditorForProperty.

@Test
public 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"));
    assertEquals("name0", tb0.getName());
    assertEquals("name1", tb1.getName());
    assertEquals("name2", tb2.getName());
    assertEquals("name3", tb3.getName());
    assertEquals("name4", tb4.getName());
    assertEquals("name5", tb5.getName());
    assertEquals("name0", bw.getPropertyValue("array[0].name"));
    assertEquals("name1", bw.getPropertyValue("array[1].name"));
    assertEquals("name2", bw.getPropertyValue("list[0].name"));
    assertEquals("name3", bw.getPropertyValue("list[1].name"));
    assertEquals("name4", bw.getPropertyValue("map[key1].name"));
    assertEquals("name5", bw.getPropertyValue("map[key2].name"));
    assertEquals("name4", bw.getPropertyValue("map['key1'].name"));
    assertEquals("name5", bw.getPropertyValue("map[\"key2\"].name"));
    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);
    assertEquals("arrayname5", tb0.getName());
    assertEquals("arrayname4", tb1.getName());
    assertEquals("listname3", tb2.getName());
    assertEquals("listname2", tb3.getName());
    assertEquals("mapname1", tb4.getName());
    assertEquals("mapname0", tb5.getName());
    assertEquals("arrayname5", bw.getPropertyValue("array[0].name"));
    assertEquals("arrayname4", bw.getPropertyValue("array[1].name"));
    assertEquals("listname3", bw.getPropertyValue("list[0].name"));
    assertEquals("listname2", bw.getPropertyValue("list[1].name"));
    assertEquals("mapname1", bw.getPropertyValue("map[\"key1\"].name"));
    assertEquals("mapname0", bw.getPropertyValue("map['key2'].name"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 50 with MutablePropertyValues

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

the class CustomEditorTests method testNestedIndexedPropertiesWithCustomEditorForProperty.

@Test
public 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);
        }
    });
    assertEquals("name0", tb0.getName());
    assertEquals("name1", tb1.getName());
    assertEquals("name2", tb2.getName());
    assertEquals("name3", tb3.getName());
    assertEquals("name4", tb4.getName());
    assertEquals("name5", tb5.getName());
    assertEquals("name0", bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name"));
    assertEquals("name1", bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name"));
    assertEquals("name2", bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name"));
    assertEquals("name3", bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name"));
    assertEquals("name4", bw.getPropertyValue("map[key1].nestedIndexedBean.map[key1].name"));
    assertEquals("name5", bw.getPropertyValue("map['key2'].nestedIndexedBean.map[\"key2\"].name"));
    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);
    assertEquals("arrayname5", tb0.getNestedIndexedBean().getArray()[0].getName());
    assertEquals("arrayname4", tb1.getNestedIndexedBean().getArray()[1].getName());
    assertEquals("listname3", ((TestBean) tb2.getNestedIndexedBean().getList().get(0)).getName());
    assertEquals("listname2", ((TestBean) tb3.getNestedIndexedBean().getList().get(1)).getName());
    assertEquals("mapname1", ((TestBean) tb4.getNestedIndexedBean().getMap().get("key1")).getName());
    assertEquals("mapname0", ((TestBean) tb5.getNestedIndexedBean().getMap().get("key2")).getName());
    assertEquals("arrayname5", bw.getPropertyValue("array[0].nestedIndexedBean.array[0].name"));
    assertEquals("arrayname4", bw.getPropertyValue("array[1].nestedIndexedBean.array[1].name"));
    assertEquals("listname3", bw.getPropertyValue("list[0].nestedIndexedBean.list[0].name"));
    assertEquals("listname2", bw.getPropertyValue("list[1].nestedIndexedBean.list[1].name"));
    assertEquals("mapname1", bw.getPropertyValue("map['key1'].nestedIndexedBean.map[key1].name"));
    assertEquals("mapname0", bw.getPropertyValue("map[key2].nestedIndexedBean.map[\"key2\"].name"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BeanWrapper(org.springframework.beans.BeanWrapper) BeanWrapperImpl(org.springframework.beans.BeanWrapperImpl) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) BooleanTestBean(org.springframework.tests.sample.beans.BooleanTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NumberTestBean(org.springframework.tests.sample.beans.NumberTestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Aggregations

MutablePropertyValues (org.springframework.beans.MutablePropertyValues)305 Test (org.junit.Test)269 TestBean (org.springframework.tests.sample.beans.TestBean)86 ITestBean (org.springframework.tests.sample.beans.ITestBean)82 IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)73 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)68 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)35 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)24 PropertyEditorSupport (java.beans.PropertyEditorSupport)23 PropertyValue (org.springframework.beans.PropertyValue)16 BeanDefinition (org.springframework.beans.factory.config.BeanDefinition)16 RuntimeBeanReference (org.springframework.beans.factory.config.RuntimeBeanReference)16 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)15 ScannedGenericBeanDefinition (org.springframework.context.annotation.ScannedGenericBeanDefinition)14 HashMap (java.util.HashMap)13 BeanWrapper (org.springframework.beans.BeanWrapper)12 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)11 ParseException (java.text.ParseException)9 RelaxedDataBinder (org.springframework.boot.bind.RelaxedDataBinder)9 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)9