use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.
the class XmlBeanFactoryTests method autowireByConstructorWithSimpleValues.
@Test
void autowireByConstructorWithSimpleValues() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
new XmlBeanDefinitionReader(xbf).loadBeanDefinitions(CONSTRUCTOR_ARG_CONTEXT);
ConstructorDependenciesBean rod5 = (ConstructorDependenciesBean) xbf.getBean("rod5");
TestBean kerry1 = (TestBean) xbf.getBean("kerry1");
TestBean kerry2 = (TestBean) xbf.getBean("kerry2");
IndexedTestBean other = (IndexedTestBean) xbf.getBean("other");
// should have been autowired
assertThat(rod5.getSpouse1()).isEqualTo(kerry2);
assertThat(rod5.getSpouse2()).isEqualTo(kerry1);
assertThat(rod5.getOther()).isEqualTo(other);
assertThat(rod5.getAge()).isEqualTo(99);
assertThat(rod5.getName()).isEqualTo("myname");
DerivedConstructorDependenciesBean rod6 = (DerivedConstructorDependenciesBean) xbf.getBean("rod6");
// should have been autowired
assertThat(rod6.initialized).isTrue();
assertThat(!rod6.destroyed).isTrue();
assertThat(rod6.getSpouse1()).isEqualTo(kerry2);
assertThat(rod6.getSpouse2()).isEqualTo(kerry1);
assertThat(rod6.getOther()).isEqualTo(other);
assertThat(rod6.getAge()).isEqualTo(0);
assertThat(rod6.getName()).isNull();
xbf.destroySingletons();
assertThat(rod6.destroyed).isTrue();
}
use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.
the class XmlBeanFactoryTests method doTestAutowire.
private void doTestAutowire(DefaultListableBeanFactory xbf) {
DependenciesBean rod1 = (DependenciesBean) xbf.getBean("rod1");
TestBean kerry = (TestBean) xbf.getBean("spouse");
// should have been autowired
assertThat(rod1.getSpouse()).isEqualTo(kerry);
DependenciesBean rod1a = (DependenciesBean) xbf.getBean("rod1a");
// should have been autowired
assertThat(rod1a.getSpouse()).isEqualTo(kerry);
DependenciesBean rod2 = (DependenciesBean) xbf.getBean("rod2");
// should have been autowired
assertThat(rod2.getSpouse()).isEqualTo(kerry);
DependenciesBean rod2a = (DependenciesBean) xbf.getBean("rod2a");
// should have been set explicitly
assertThat(rod2a.getSpouse()).isEqualTo(kerry);
ConstructorDependenciesBean rod3 = (ConstructorDependenciesBean) xbf.getBean("rod3");
IndexedTestBean other = (IndexedTestBean) xbf.getBean("other");
// should have been autowired
assertThat(rod3.getSpouse1()).isEqualTo(kerry);
assertThat(rod3.getSpouse2()).isEqualTo(kerry);
assertThat(rod3.getOther()).isEqualTo(other);
ConstructorDependenciesBean rod3a = (ConstructorDependenciesBean) xbf.getBean("rod3a");
// should have been autowired
assertThat(rod3a.getSpouse1()).isEqualTo(kerry);
assertThat(rod3a.getSpouse2()).isEqualTo(kerry);
assertThat(rod3a.getOther()).isEqualTo(other);
assertThatExceptionOfType(FatalBeanException.class).isThrownBy(() -> xbf.getBean("rod4", ConstructorDependenciesBean.class));
DependenciesBean rod5 = (DependenciesBean) xbf.getBean("rod5");
// Should not have been autowired
assertThat((Object) rod5.getSpouse()).isNull();
BeanFactory appCtx = (BeanFactory) xbf.getBean("childAppCtx");
assertThat(appCtx.containsBean("rod1")).isTrue();
assertThat(appCtx.containsBean("jenny")).isTrue();
}
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");
}
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);
}
use of org.springframework.beans.testfixture.beans.IndexedTestBean in project spring-framework by spring-projects.
the class CustomEditorTests method testIndexedPropertiesWithIndividualCustomEditorForProperty.
@Test
void testIndexedPropertiesWithIndividualCustomEditorForProperty() {
IndexedTestBean bean = new IndexedTestBean(false);
BeanWrapper bw = new BeanWrapperImpl(bean);
bw.registerCustomEditor(String.class, "array[0].name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("array0" + text);
}
});
bw.registerCustomEditor(String.class, "array[1].name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("array1" + text);
}
});
bw.registerCustomEditor(String.class, "list[0].name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("list0" + text);
}
});
bw.registerCustomEditor(String.class, "list[1].name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("list1" + text);
}
});
bw.registerCustomEditor(String.class, "map[key1].name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("mapkey1" + text);
}
});
bw.registerCustomEditor(String.class, "map[key2].name", new PropertyEditorSupport() {
@Override
public void setAsText(String text) throws IllegalArgumentException {
setValue("mapkey2" + text);
}
});
bean.populate();
TestBean tb0 = bean.getArray()[0];
TestBean tb1 = bean.getArray()[1];
TestBean tb2 = ((TestBean) bean.getList().get(0));
TestBean tb3 = ((TestBean) bean.getList().get(1));
TestBean tb4 = ((TestBean) bean.getMap().get("key1"));
TestBean tb5 = ((TestBean) bean.getMap().get("key2"));
assertThat(tb0.getName()).isEqualTo("name0");
assertThat(tb1.getName()).isEqualTo("name1");
assertThat(tb2.getName()).isEqualTo("name2");
assertThat(tb3.getName()).isEqualTo("name3");
assertThat(tb4.getName()).isEqualTo("name4");
assertThat(tb5.getName()).isEqualTo("name5");
assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("name0");
assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("name1");
assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("name2");
assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("name3");
assertThat(bw.getPropertyValue("map[key1].name")).isEqualTo("name4");
assertThat(bw.getPropertyValue("map[key2].name")).isEqualTo("name5");
assertThat(bw.getPropertyValue("map['key1'].name")).isEqualTo("name4");
assertThat(bw.getPropertyValue("map[\"key2\"].name")).isEqualTo("name5");
MutablePropertyValues pvs = new MutablePropertyValues();
pvs.add("array[0].name", "name5");
pvs.add("array[1].name", "name4");
pvs.add("list[0].name", "name3");
pvs.add("list[1].name", "name2");
pvs.add("map[key1].name", "name1");
pvs.add("map['key2'].name", "name0");
bw.setPropertyValues(pvs);
assertThat(tb0.getName()).isEqualTo("array0name5");
assertThat(tb1.getName()).isEqualTo("array1name4");
assertThat(tb2.getName()).isEqualTo("list0name3");
assertThat(tb3.getName()).isEqualTo("list1name2");
assertThat(tb4.getName()).isEqualTo("mapkey1name1");
assertThat(tb5.getName()).isEqualTo("mapkey2name0");
assertThat(bw.getPropertyValue("array[0].name")).isEqualTo("array0name5");
assertThat(bw.getPropertyValue("array[1].name")).isEqualTo("array1name4");
assertThat(bw.getPropertyValue("list[0].name")).isEqualTo("list0name3");
assertThat(bw.getPropertyValue("list[1].name")).isEqualTo("list1name2");
assertThat(bw.getPropertyValue("map[\"key1\"].name")).isEqualTo("mapkey1name1");
assertThat(bw.getPropertyValue("map['key2'].name")).isEqualTo("mapkey2name0");
}
Aggregations