Search in sources :

Example 36 with IndexedTestBean

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

the class AutowiredAnnotationBeanPostProcessorTests method testOrderedCollectionResourceInjection.

@Test
public void testOrderedCollectionResourceInjection() {
    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);
    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");
    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(2);
    assertThat(bean.getNestedTestBeans().get(0)).isSameAs(ntb2);
    assertThat(bean.getNestedTestBeans().get(1)).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansSetter.size()).isEqualTo(2);
    assertThat(bean.nestedTestBeansSetter.get(0)).isSameAs(ntb2);
    assertThat(bean.nestedTestBeansSetter.get(1)).isSameAs(ntb1);
    assertThat(bean.nestedTestBeansField.size()).isEqualTo(2);
    assertThat(bean.nestedTestBeansField.get(0)).isSameAs(ntb2);
    assertThat(bean.nestedTestBeansField.get(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 37 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setCollectionPropertyWithStringValue.

@Test
// list cannot be properly parameterized as it breaks other tests
@SuppressWarnings("unchecked")
void setCollectionPropertyWithStringValue() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    List<String> set = new ArrayList<>();
    set.add("set1");
    accessor.setPropertyValue("set", "set1");
    List<String> sortedSet = new ArrayList<>();
    sortedSet.add("sortedSet1");
    accessor.setPropertyValue("sortedSet", "sortedSet1");
    Set<String> list = new HashSet<>();
    list.add("list1");
    accessor.setPropertyValue("list", "list1");
    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) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 38 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method getAndSetIndexedPropertiesWithDirectAccess.

@Test
void getAndSetIndexedPropertiesWithDirectAccess() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    TestBean tb0 = target.getArray()[0];
    TestBean tb1 = target.getArray()[1];
    TestBean tb2 = ((TestBean) target.getList().get(0));
    TestBean tb3 = ((TestBean) target.getList().get(1));
    TestBean tb6 = ((TestBean) target.getSet().toArray()[0]);
    TestBean tb7 = ((TestBean) target.getSet().toArray()[1]);
    TestBean tb4 = ((TestBean) target.getMap().get("key1"));
    TestBean tb5 = ((TestBean) target.getMap().get("key2"));
    assertThat(accessor.getPropertyValue("array[0]")).isEqualTo(tb0);
    assertThat(accessor.getPropertyValue("array[1]")).isEqualTo(tb1);
    assertThat(accessor.getPropertyValue("list[0]")).isEqualTo(tb2);
    assertThat(accessor.getPropertyValue("list[1]")).isEqualTo(tb3);
    assertThat(accessor.getPropertyValue("set[0]")).isEqualTo(tb6);
    assertThat(accessor.getPropertyValue("set[1]")).isEqualTo(tb7);
    assertThat(accessor.getPropertyValue("map[key1]")).isEqualTo(tb4);
    assertThat(accessor.getPropertyValue("map[key2]")).isEqualTo(tb5);
    assertThat(accessor.getPropertyValue("map['key1']")).isEqualTo(tb4);
    assertThat(accessor.getPropertyValue("map[\"key2\"]")).isEqualTo(tb5);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("array[0]", tb5);
    pvs.add("array[1]", tb4);
    pvs.add("list[0]", tb3);
    pvs.add("list[1]", tb2);
    pvs.add("list[2]", tb0);
    pvs.add("list[4]", tb1);
    pvs.add("map[key1]", tb1);
    pvs.add("map['key2']", tb0);
    pvs.add("map[key5]", tb4);
    pvs.add("map['key9']", tb5);
    accessor.setPropertyValues(pvs);
    assertThat(target.getArray()[0]).isEqualTo(tb5);
    assertThat(target.getArray()[1]).isEqualTo(tb4);
    assertThat((target.getList().get(0))).isEqualTo(tb3);
    assertThat((target.getList().get(1))).isEqualTo(tb2);
    assertThat((target.getList().get(2))).isEqualTo(tb0);
    assertThat((target.getList().get(3))).isNull();
    assertThat((target.getList().get(4))).isEqualTo(tb1);
    assertThat((target.getMap().get("key1"))).isEqualTo(tb1);
    assertThat((target.getMap().get("key2"))).isEqualTo(tb0);
    assertThat((target.getMap().get("key5"))).isEqualTo(tb4);
    assertThat((target.getMap().get("key9"))).isEqualTo(tb5);
    assertThat(accessor.getPropertyValue("array[0]")).isEqualTo(tb5);
    assertThat(accessor.getPropertyValue("array[1]")).isEqualTo(tb4);
    assertThat(accessor.getPropertyValue("list[0]")).isEqualTo(tb3);
    assertThat(accessor.getPropertyValue("list[1]")).isEqualTo(tb2);
    assertThat(accessor.getPropertyValue("list[2]")).isEqualTo(tb0);
    assertThat(accessor.getPropertyValue("list[3]")).isNull();
    assertThat(accessor.getPropertyValue("list[4]")).isEqualTo(tb1);
    assertThat(accessor.getPropertyValue("map[\"key1\"]")).isEqualTo(tb1);
    assertThat(accessor.getPropertyValue("map['key2']")).isEqualTo(tb0);
    assertThat(accessor.getPropertyValue("map[\"key5\"]")).isEqualTo(tb4);
    assertThat(accessor.getPropertyValue("map['key9']")).isEqualTo(tb5);
}
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) Test(org.junit.jupiter.api.Test)

Example 39 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setRawMapPropertyWithNoEditorRegistered.

@Test
// must work with raw map in this test
@SuppressWarnings({ "unchecked", "rawtypes" })
void setRawMapPropertyWithNoEditorRegistered() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    Map inputMap = new HashMap();
    inputMap.put(1, "rod");
    inputMap.put(2, "rob");
    ReadOnlyMap readOnlyMap = new ReadOnlyMap(inputMap);
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("map", readOnlyMap);
    accessor.setPropertyValues(pvs);
    assertThat(target.getMap()).isSameAs(readOnlyMap);
    assertThat(readOnlyMap.isAccessed()).isFalse();
}
Also used : IndexedTestBean(org.springframework.beans.testfixture.beans.IndexedTestBean) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap) SortedMap(java.util.SortedMap) Test(org.junit.jupiter.api.Test)

Example 40 with IndexedTestBean

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

the class AbstractPropertyAccessorTests method setCollectionPropertyWithArrayValue.

@Test
// list cannot be properly parameterized as it breaks other tests
@SuppressWarnings("unchecked")
void setCollectionPropertyWithArrayValue() {
    IndexedTestBean target = new IndexedTestBean();
    AbstractPropertyAccessor accessor = createAccessor(target);
    Collection<String> coll = new HashSet<>();
    coll.add("coll1");
    accessor.setPropertyValue("collection", coll.toArray());
    List<String> set = new ArrayList<>();
    set.add("set1");
    accessor.setPropertyValue("set", set.toArray());
    List<String> sortedSet = new ArrayList<>();
    sortedSet.add("sortedSet1");
    accessor.setPropertyValue("sortedSet", sortedSet.toArray());
    Set<String> list = new HashSet<>();
    list.add("list1");
    accessor.setPropertyValue("list", list.toArray());
    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) 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