Search in sources :

Example 41 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setMapPropertyNonMatchingType.

@Test
@SuppressWarnings("unchecked")
void setMapPropertyNonMatchingType() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    Map<String, String> map = new TreeMap<>();
    map.put("key", "value");
    accessor.setPropertyValue("map", map);
    Map<String, String> sortedMap = new TreeMap<>();
    sortedMap.put("sortedKey", "sortedValue");
    accessor.setPropertyValue("sortedMap", sortedMap);
    assertThat(target.getMap()).hasSize(1);
    assertThat(target.getMap().get("key")).isEqualTo("value");
    assertThat(target.getSortedMap().size()).isEqualTo(1);
    assertThat(target.getSortedMap().get("sortedKey")).isEqualTo("sortedValue");
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) TreeMap(java.util.TreeMap) Test(org.junit.jupiter.api.Test)

Example 42 with IndexedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testAnnotationOrderedResourceInjection.

@Test
public void testAnnotationOrderedResourceInjection() {
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class));
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    IndexedTestBean itb = new IndexedTestBean();
    bf.registerSingleton("indexedTestBean", itb);
    FixedOrder2NestedTestBean ntb1 = new FixedOrder2NestedTestBean();
    bf.registerSingleton("nestedTestBean1", ntb1);
    FixedOrder1NestedTestBean ntb2 = new FixedOrder1NestedTestBean();
    bf.registerSingleton("nestedTestBean2", ntb2);
    OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb);
    assertThat(bean.getTestBean3()).isSameAs(tb);
    assertThat(bean.getTestBean4()).isSameAs(tb);
    assertThat(bean.getIndexedTestBean()).isSameAs(itb);
    assertThat(bean.getNestedTestBeans().length).isEqualTo(2);
    assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb2);
    assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansField.length).isEqualTo(2);
    assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb2);
    assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb1);
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 43 with IndexedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testOptionalResourceInjection.

@Test
public void testOptionalResourceInjection() {
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class));
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    IndexedTestBean itb = new IndexedTestBean();
    bf.registerSingleton("indexedTestBean", itb);
    NestedTestBean ntb1 = new NestedTestBean();
    bf.registerSingleton("nestedTestBean1", ntb1);
    NestedTestBean ntb2 = new NestedTestBean();
    bf.registerSingleton("nestedTestBean2", ntb2);
    OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb);
    assertThat(bean.getTestBean3()).isSameAs(tb);
    assertThat(bean.getTestBean4()).isSameAs(tb);
    assertThat(bean.getIndexedTestBean()).isSameAs(itb);
    assertThat(bean.getNestedTestBeans().length).isEqualTo(2);
    assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb1);
    assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb2);
    assertThat(bean.nestedTestBeansField.length).isEqualTo(2);
    assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb2);
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 44 with IndexedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testOrderedResourceInjection.

@Test
public void testOrderedResourceInjection() {
    bf.registerBeanDefinition("annotatedBean", new RootBeanDefinition(OptionalResourceInjectionBean.class));
    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);
    OptionalResourceInjectionBean bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.getTestBean()).isSameAs(tb);
    assertThat(bean.getTestBean2()).isSameAs(tb);
    assertThat(bean.getTestBean3()).isSameAs(tb);
    assertThat(bean.getTestBean4()).isSameAs(tb);
    assertThat(bean.getIndexedTestBean()).isSameAs(itb);
    assertThat(bean.getNestedTestBeans().length).isEqualTo(2);
    assertThat(bean.getNestedTestBeans()[0]).isSameAs(ntb2);
    assertThat(bean.getNestedTestBeans()[1]).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansField.length).isEqualTo(2);
    assertThat(bean.nestedTestBeansField[0]).isSameAs(ntb2);
    assertThat(bean.nestedTestBeansField[1]).isSameAs(ntb1);
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 45 with IndexedTestBean

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

the class CustomEditorTests method testIndexedPropertiesWithDirectAccessAndSpecificPropertyEditors.

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

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

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

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

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

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

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

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

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

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

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

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            setValue(new TestBean("mapkey2" + 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("array0a");
    assertThat(bean.getArray()[1].getName()).isEqualTo("array1b");
    assertThat(((TestBean) bean.getList().get(0)).getName()).isEqualTo("list0c");
    assertThat(((TestBean) bean.getList().get(1)).getName()).isEqualTo("list1d");
    assertThat(((TestBean) bean.getMap().get("key1")).getName()).isEqualTo("mapkey1e");
    assertThat(((TestBean) bean.getMap().get("key2")).getName()).isEqualTo("mapkey2f");
}
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