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());
}
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"));
}
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());
}
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());
}
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();
}
Aggregations