Search in sources :

Example 6 with IndexedTestBean

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

the class CustomEditorTests method testUninitializedArrayPropertyWithCustomEditor.

@Test
public void testUninitializedArrayPropertyWithCustomEditor() {
    IndexedTestBean bean = new IndexedTestBean(false);
    BeanWrapper bw = new BeanWrapperImpl(bean);
    PropertyEditor pe = new CustomNumberEditor(Integer.class, true);
    bw.registerCustomEditor(null, "list.age", pe);
    TestBean tb = new TestBean();
    bw.setPropertyValue("list", new ArrayList<>());
    bw.setPropertyValue("list[0]", tb);
    assertEquals(tb, bean.getList().get(0));
    assertEquals(pe, bw.findCustomEditor(int.class, "list.age"));
    assertEquals(pe, bw.findCustomEditor(null, "list.age"));
    assertEquals(pe, bw.findCustomEditor(int.class, "list[0].age"));
    assertEquals(pe, bw.findCustomEditor(null, "list[0].age"));
}
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) PropertyEditor(java.beans.PropertyEditor) Test(org.junit.Test)

Example 7 with IndexedTestBean

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

the class CustomEditorTests method testArrayToArrayConversion.

@Test
public 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" });
    assertEquals(2, tb.getArray().length);
    assertEquals("a", tb.getArray()[0].getName());
    assertEquals("b", tb.getArray()[1].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) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.Test)

Example 8 with IndexedTestBean

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

the class XmlBeanFactoryTests method testNestedPropertyValue.

@Test
public void testNestedPropertyValue() {
    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");
    assertEquals("name applied correctly", "myname", bean.getArray()[0].getName());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 9 with IndexedTestBean

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

the class XmlBeanFactoryTests method testAutowireByConstructorWithSimpleValues.

@Test
public void testAutowireByConstructorWithSimpleValues() throws Exception {
    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
    assertEquals(kerry2, rod5.getSpouse1());
    assertEquals(kerry1, rod5.getSpouse2());
    assertEquals(other, rod5.getOther());
    assertEquals(99, rod5.getAge());
    assertEquals("myname", rod5.getName());
    DerivedConstructorDependenciesBean rod6 = (DerivedConstructorDependenciesBean) xbf.getBean("rod6");
    // should have been autowired
    assertTrue(rod6.initialized);
    assertTrue(!rod6.destroyed);
    assertEquals(kerry2, rod6.getSpouse1());
    assertEquals(kerry1, rod6.getSpouse2());
    assertEquals(other, rod6.getOther());
    assertEquals(0, rod6.getAge());
    assertEquals(null, rod6.getName());
    xbf.destroySingletons();
    assertTrue(rod6.destroyed);
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ResourceTestBean(org.springframework.tests.sample.beans.ResourceTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) Test(org.junit.Test)

Example 10 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setMapProperty.

@Test
public void setMapProperty() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    Map<String, String> map = new HashMap<>();
    map.put("key", "value");
    accessor.setPropertyValue("map", map);
    SortedMap<?, ?> sortedMap = new TreeMap<>();
    map.put("sortedKey", "sortedValue");
    accessor.setPropertyValue("sortedMap", sortedMap);
    assertSame(map, target.getMap());
    assertSame(sortedMap, target.getSortedMap());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) HashMap(java.util.HashMap) TreeMap(java.util.TreeMap) Test(org.junit.Test)

Aggregations

IndexedTestBean (org.springframework.tests.sample.beans.IndexedTestBean)60 Test (org.junit.Test)59 TestBean (org.springframework.tests.sample.beans.TestBean)28 ITestBean (org.springframework.tests.sample.beans.ITestBean)26 PropertyEditorSupport (java.beans.PropertyEditorSupport)22 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)17 BooleanTestBean (org.springframework.tests.sample.beans.BooleanTestBean)15 NumberTestBean (org.springframework.tests.sample.beans.NumberTestBean)15 RootBeanDefinition (org.springframework.beans.factory.support.RootBeanDefinition)14 DefaultListableBeanFactory (org.springframework.beans.factory.support.DefaultListableBeanFactory)11 BeanWrapper (org.springframework.beans.BeanWrapper)10 BeanWrapperImpl (org.springframework.beans.BeanWrapperImpl)10 NestedTestBean (org.springframework.tests.sample.beans.NestedTestBean)9 DerivedTestBean (org.springframework.tests.sample.beans.DerivedTestBean)8 ChildBeanDefinition (org.springframework.beans.factory.support.ChildBeanDefinition)7 ArrayList (java.util.ArrayList)6 HashSet (java.util.HashSet)6 LinkedList (java.util.LinkedList)6 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4