Search in sources :

Example 6 with IndexedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testOptionalResourceInjectionWithBeanDefinitionRemoval.

@Test
public void testOptionalResourceInjectionWithBeanDefinitionRemoval() {
    RootBeanDefinition rbd = new RootBeanDefinition(OptionalResourceInjectionBean.class);
    rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", rbd);
    bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
    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(bf.getBean("testBean"));
    assertThat(bean.getTestBean2()).isSameAs(bf.getBean("testBean"));
    assertThat(bean.getTestBean3()).isSameAs(bf.getBean("testBean"));
    assertThat(bean.getTestBean4()).isSameAs(bf.getBean("testBean"));
    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);
    bf.removeBeanDefinition("testBean");
    bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.getTestBean()).isNull();
    assertThat(bean.getTestBean2()).isNull();
    assertThat(bean.getTestBean3()).isNull();
    assertThat(bean.getTestBean4()).isNull();
    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);
    bf.registerBeanDefinition("testBean", new RootBeanDefinition(TestBean.class));
    bean = (OptionalResourceInjectionBean) bf.getBean("annotatedBean");
    assertThat(bean.getTestBean()).isSameAs(bf.getBean("testBean"));
    assertThat(bean.getTestBean2()).isSameAs(bf.getBean("testBean"));
    assertThat(bean.getTestBean3()).isSameAs(bf.getBean("testBean"));
    assertThat(bean.getTestBean4()).isSameAs(bf.getBean("testBean"));
    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 7 with IndexedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testOptionalCollectionResourceInjectionWithSingleElement.

@Test
public void testOptionalCollectionResourceInjectionWithSingleElement() {
    RootBeanDefinition rbd = new RootBeanDefinition(OptionalCollectionResourceInjectionBean.class);
    rbd.setScope(BeanDefinition.SCOPE_PROTOTYPE);
    bf.registerBeanDefinition("annotatedBean", rbd);
    TestBean tb = new TestBean();
    bf.registerSingleton("testBean", tb);
    IndexedTestBean itb = new IndexedTestBean();
    bf.registerSingleton("indexedTestBean", itb);
    NestedTestBean ntb1 = new NestedTestBean();
    bf.registerSingleton("nestedTestBean1", ntb1);
    // Two calls to verify that caching doesn't break re-creation.
    OptionalCollectionResourceInjectionBean bean = (OptionalCollectionResourceInjectionBean) bf.getBean("annotatedBean");
    bean = (OptionalCollectionResourceInjectionBean) 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().size()).isEqualTo(1);
    assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansSetter.size()).isEqualTo(1);
    assertThat(bean.nestedTestBeansSetter.get(0)).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansField.size()).isEqualTo(1);
    assertThat(bean.nestedTestBeansField.get(0)).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) NestedTestBean(org.springframework.beans.testfixture.beans.NestedTestBean) RootBeanDefinition(org.springframework.beans.factory.support.RootBeanDefinition) Test(org.junit.jupiter.api.Test)

Example 8 with IndexedTestBean

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

the class CustomEditorTests method testUninitializedArrayPropertyWithCustomEditor.

@Test
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);
    assertThat(bean.getList().get(0)).isEqualTo(tb);
    assertThat(bw.findCustomEditor(int.class, "list.age")).isEqualTo(pe);
    assertThat(bw.findCustomEditor(null, "list.age")).isEqualTo(pe);
    assertThat(bw.findCustomEditor(int.class, "list[0].age")).isEqualTo(pe);
    assertThat(bw.findCustomEditor(null, "list[0].age")).isEqualTo(pe);
}
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) PropertyEditor(java.beans.PropertyEditor) Test(org.junit.jupiter.api.Test)

Example 9 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setMapPropertyWithTypeConversion.

@Test
void setMapPropertyWithTypeConversion() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    accessor.registerCustomEditor(TestBean.class, new PropertyEditorSupport() {

        @Override
        public void setAsText(String text) throws IllegalArgumentException {
            if (!StringUtils.hasLength(text)) {
                throw new IllegalArgumentException();
            }
            setValue(new TestBean(text));
        }
    });
    MutablePropertyValues goodValues = new MutablePropertyValues();
    goodValues.add("map[key1]", "rod");
    goodValues.add("map[key2]", "rob");
    accessor.setPropertyValues(goodValues);
    assertThat(((TestBean) target.getMap().get("key1")).getName()).isEqualTo("rod");
    assertThat(((TestBean) target.getMap().get("key2")).getName()).isEqualTo("rob");
    MutablePropertyValues badValues = new MutablePropertyValues();
    badValues.add("map[key1]", "rod");
    badValues.add("map[key2]", "");
    assertThatExceptionOfType(PropertyBatchUpdateException.class).isThrownBy(() -> accessor.setPropertyValues(badValues)).satisfies(ex -> assertThat(ex.getPropertyAccessException("map[key2]")).isInstanceOf(TypeMismatchException.class));
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) 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) Assertions.assertThatIllegalArgumentException(org.assertj.core.api.Assertions.assertThatIllegalArgumentException) PropertyEditorSupport(java.beans.PropertyEditorSupport) Test(org.junit.jupiter.api.Test)

Example 10 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setCollectionPropertyWithIntegerValue.

@Test
// list cannot be properly parameterized as it breaks other tests
@SuppressWarnings("unchecked")
void setCollectionPropertyWithIntegerValue() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    Collection<Integer> coll = new HashSet<>();
    coll.add(0);
    accessor.setPropertyValue("collection", 0);
    List<Integer> set = new ArrayList<>();
    set.add(1);
    accessor.setPropertyValue("set", 1);
    List<Integer> sortedSet = new ArrayList<>();
    sortedSet.add(2);
    accessor.setPropertyValue("sortedSet", 2);
    Set<Integer> list = new HashSet<>();
    list.add(3);
    accessor.setPropertyValue("list", 3);
    assertThat(target.getCollection()).hasSize(1);
    assertThat(target.getCollection().containsAll(coll)).isTrue();
    assertThat(target.getSet().size()).isEqualTo(1);
    assertThat(target.getSet().containsAll(set)).isTrue();
    assertThat(target.getSortedSet().size()).isEqualTo(1);
    assertThat(target.getSortedSet().containsAll(sortedSet)).isTrue();
    assertThat(target.getList().size()).isEqualTo(1);
    assertThat(target.getList().containsAll(list)).isTrue();
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) BigInteger(java.math.BigInteger) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) 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