From bee2b7cd736a7f59e3c1e35b3fe471cbcfbe1349 Mon Sep 17 00:00:00 2001 From: lixiaolong11000 Date: Wed, 11 Dec 2019 23:44:42 +0800 Subject: [PATCH] Add missing test cases in XmlBeanFactoryTests Closes gh-24189 --- .../factory/xml/XmlBeanFactoryTests.java | 40 +++++++++++++++++++ .../xml/XmlBeanFactoryTests-reftypes.xml | 22 ++++++++++ 2 files changed, 62 insertions(+) diff --git a/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java b/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java index 44807840f5e..a406141b1a3 100644 --- a/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java +++ b/spring-context/src/test/java/org/springframework/beans/factory/xml/XmlBeanFactoryTests.java @@ -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(); diff --git a/spring-context/src/test/resources/org/springframework/beans/factory/xml/XmlBeanFactoryTests-reftypes.xml b/spring-context/src/test/resources/org/springframework/beans/factory/xml/XmlBeanFactoryTests-reftypes.xml index c9c7ca2400c..245511e2df5 100644 --- a/spring-context/src/test/resources/org/springframework/beans/factory/xml/XmlBeanFactoryTests-reftypes.xml +++ b/spring-context/src/test/resources/org/springframework/beans/factory/xml/XmlBeanFactoryTests-reftypes.xml @@ -14,6 +14,28 @@ + + + + + + + + + + + + + + + + + + + + + + Andrew 36