Search in sources :

Example 41 with IndexedTestBean

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

the class DataBinderTests method testBindingStringArrayToIntegerSet.

@Test
public void testBindingStringArrayToIntegerSet() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    binder.registerCustomEditor(Set.class, new CustomCollectionEditor(TreeSet.class) {

        @Override
        protected Object convertElement(Object element) {
            return new Integer(element.toString());
        }
    });
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("set", new String[] { "10", "20", "30" });
    binder.bind(pvs);
    assertEquals(tb.getSet(), binder.getBindingResult().getFieldValue("set"));
    assertTrue(tb.getSet() instanceof TreeSet);
    assertEquals(3, tb.getSet().size());
    assertTrue(tb.getSet().contains(new Integer(10)));
    assertTrue(tb.getSet().contains(new Integer(20)));
    assertTrue(tb.getSet().contains(new Integer(30)));
    pvs = new MutablePropertyValues();
    pvs.add("set", null);
    binder.bind(pvs);
    assertNull(tb.getSet());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) CustomCollectionEditor(org.springframework.beans.propertyeditors.CustomCollectionEditor) TreeSet(java.util.TreeSet) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Test(org.junit.Test)

Example 42 with IndexedTestBean

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

the class DataBinderTests method testBindingWithResortedList.

@Test
public void testBindingWithResortedList() {
    IndexedTestBean tb = new IndexedTestBean();
    DataBinder binder = new DataBinder(tb, "tb");
    MutablePropertyValues pvs = new MutablePropertyValues();
    TestBean tb1 = new TestBean("tb1", 99);
    TestBean tb2 = new TestBean("tb2", 99);
    pvs.add("list[0]", tb1);
    pvs.add("list[1]", tb2);
    binder.bind(pvs);
    assertEquals(tb1.getName(), binder.getBindingResult().getFieldValue("list[0].name"));
    assertEquals(tb2.getName(), binder.getBindingResult().getFieldValue("list[1].name"));
    tb.getList().set(0, tb2);
    tb.getList().set(1, tb1);
    assertEquals(tb2.getName(), binder.getBindingResult().getFieldValue("list[0].name"));
    assertEquals(tb1.getName(), binder.getBindingResult().getFieldValue("list[1].name"));
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) DerivedTestBean(org.springframework.tests.sample.beans.DerivedTestBean) TestBean(org.springframework.tests.sample.beans.TestBean) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Test(org.junit.Test)

Example 43 with IndexedTestBean

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

the class PropertyResourceConfigurerTests method testPropertyOverrideConfigurerWithPropertiesXmlFile.

@Test
public void testPropertyOverrideConfigurerWithPropertiesXmlFile() {
    BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
    factory.registerBeanDefinition("tb", def);
    PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
    poc.setLocation(TEST_PROPS_XML);
    poc.postProcessBeanFactory(factory);
    IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
    assertEquals(99, tb.getArray()[0].getAge());
    assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) Test(org.junit.Test)

Example 44 with IndexedTestBean

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

the class PropertyResourceConfigurerTests method testPropertyOverrideConfigurerWithPropertiesFile.

@Test
public void testPropertyOverrideConfigurerWithPropertiesFile() {
    BeanDefinition def = BeanDefinitionBuilder.genericBeanDefinition(IndexedTestBean.class).getBeanDefinition();
    factory.registerBeanDefinition("tb", def);
    PropertyOverrideConfigurer poc = new PropertyOverrideConfigurer();
    poc.setLocation(TEST_PROPS);
    poc.postProcessBeanFactory(factory);
    IndexedTestBean tb = (IndexedTestBean) factory.getBean("tb");
    assertEquals(99, tb.getArray()[0].getAge());
    assertEquals("test", ((TestBean) tb.getList().get(1)).getName());
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) ChildBeanDefinition(org.springframework.beans.factory.support.ChildBeanDefinition) Test(org.junit.Test)

Example 45 with IndexedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testOrderedCollectionResourceInjection.

@Test
public void testOrderedCollectionResourceInjection() {
    DefaultListableBeanFactory bf = new DefaultListableBeanFactory();
    bf.setDependencyComparator(AnnotationAwareOrderComparator.INSTANCE);
    AutowiredAnnotationBeanPostProcessor bpp = new AutowiredAnnotationBeanPostProcessor();
    bpp.setBeanFactory(bf);
    bf.addBeanPostProcessor(bpp);
    RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class);
    rbd.setScope(RootBeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", rbd);
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    IndexedTestBean itb = new IndexedTestBean();
    bf.registerSingleton("indexedTestBean", itb);
    OrderedNestedTestBean ntb1 = new OrderedNestedTestBean();
    ntb1.setOrder(2);
    bf.registerSingleton("nestedTestBean1", ntb1);
    OrderedNestedTestBean ntb2 = new OrderedNestedTestBean();
    ntb2.setOrder(1);
    bf.registerSingleton("nestedTestBean2", ntb2);
    // Two calls to verify that caching doesn't break re-creation.
    OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean");
    bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean");
    assertSame(tb, bean.getTestBean());
    assertSame(tb, bean.getTestBean2());
    assertSame(tb, bean.getTestBean3());
    assertSame(tb, bean.getTestBean4());
    assertSame(itb, bean.getIndexedTestBean());
    assertEquals(2, bean.getNestedTestBeans().size());
    assertSame(ntb2, bean.getNestedTestBeans().get(0));
    assertSame(ntb1, bean.getNestedTestBeans().get(1));
    assertEquals(2, bean.nestedTestBeansSetter.size());
    assertSame(ntb2, bean.nestedTestBeansSetter.get(0));
    assertSame(ntb1, bean.nestedTestBeansSetter.get(1));
    assertEquals(2, bean.nestedTestBeansField.size());
    assertSame(ntb2, bean.nestedTestBeansField.get(0));
    assertSame(ntb1, bean.nestedTestBeansField.get(1));
    bf.destroySingletons();
}
Also used : IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) IndexedTestBean(org.springframework.tests.sample.beans.IndexedTestBean) ITestBean(org.springframework.tests.sample.beans.ITestBean) TestBean(org.springframework.tests.sample.beans.TestBean) NestedTestBean(org.springframework.tests.sample.beans.NestedTestBean) DefaultListableBeanFactory(org.springframework.beans.factory.support.DefaultListableBeanFactory) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) 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