Add missing test cases in XmlBeanFactoryTests

Closes gh-24189
This commit is contained in:
lixiaolong11000 2019-12-11 23:44:42 +08:00 committed by Sam Brannen
parent 49ddf798e0
commit bee2b7cd73
2 changed files with 62 additions and 0 deletions

View File

@ -536,6 +536,46 @@ public class XmlBeanFactoryTests {
assertThat(complexEgo.getSpouse().getSpouse() == complexEgo).as("Correct circular reference").isTrue();
}
@Test
public void testCircularReferencesWithConstructor() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
reader.loadBeanDefinitions(REFTYPES_CONTEXT);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
xbf.getBean("jenny_constructor"))
.matches(ex -> ex.contains(BeanCurrentlyInCreationException.class));
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
xbf.getBean("david_constructor"))
.matches(ex -> ex.contains(BeanCurrentlyInCreationException.class));
}
@Test
public void testCircularReferencesWithPrototype() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
reader.loadBeanDefinitions(REFTYPES_CONTEXT);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
xbf.getBean("jenny_prototype"))
.matches(ex -> ex.contains(BeanCurrentlyInCreationException.class));
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
xbf.getBean("david_prototype"))
.matches(ex -> ex.contains(BeanCurrentlyInCreationException.class));
}
@Test
public void testCircularReferencesWithDependOn() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();
XmlBeanDefinitionReader reader = new XmlBeanDefinitionReader(xbf);
reader.setValidationMode(XmlBeanDefinitionReader.VALIDATION_NONE);
reader.loadBeanDefinitions(REFTYPES_CONTEXT);
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
xbf.getBean("jenny_depends_on"));
assertThatExceptionOfType(BeanCreationException.class).isThrownBy(() ->
xbf.getBean("david_depends_on"));
}
@Test
public void testCircularReferenceWithFactoryBeanFirst() {
DefaultListableBeanFactory xbf = new DefaultListableBeanFactory();

View File

@ -14,6 +14,28 @@
<property name="spouse" ref="jenny"/>
</bean>
<bean id="jenny_constructor" class="org.springframework.tests.sample.beans.TestBean">
<constructor-arg index="1"><ref bean="david_constructor"/></constructor-arg>
</bean>
<bean id="david_constructor" class="org.springframework.tests.sample.beans.TestBean">
<constructor-arg index="1"><ref bean="jenny_constructor"/></constructor-arg>
</bean>
<bean id="jenny_prototype" class="org.springframework.tests.sample.beans.TestBean" scope="prototype">
<property name="spouse"><ref bean="david_prototype"/></property>
</bean>
<bean id="david_prototype" class="org.springframework.tests.sample.beans.TestBean" scope="prototype">
<property name="spouse"><ref bean="jenny_prototype"/></property>
</bean>
<bean id="jenny_depends_on" class="org.springframework.tests.sample.beans.TestBean" depends-on="david_depends_on">
</bean>
<bean id="david_depends_on" class="org.springframework.tests.sample.beans.TestBean" depends-on="jenny_depends_on">
</bean>
<bean id="jenks" class="org.springframework.tests.sample.beans.TestBean" scope="prototype">
<property name="name"><value>Andrew</value></property>
<property name="age"><value>36</value></property>