|
26 | 26 |
|
27 | 27 | import org.springframework.beans.factory.BeanCurrentlyInCreationException;
|
28 | 28 | import org.springframework.beans.factory.annotation.Autowired;
|
| 29 | +import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory; |
29 | 30 | import org.springframework.boot.diagnostics.FailureAnalysis;
|
30 |
| -import org.springframework.boot.diagnostics.FailureAnalyzer; |
31 | 31 | import org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzerTests.CycleWithAutowiredFields.BeanThreeConfiguration;
|
32 | 32 | import org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzerTests.CycleWithAutowiredFields.BeanTwoConfiguration;
|
33 | 33 | import org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzerTests.CyclicBeanMethodsConfiguration.InnerConfiguration;
|
34 | 34 | import org.springframework.boot.diagnostics.analyzer.BeanCurrentlyInCreationFailureAnalyzerTests.CyclicBeanMethodsConfiguration.InnerConfiguration.InnerInnerConfiguration;
|
35 |
| -import org.springframework.context.ConfigurableApplicationContext; |
36 | 35 | import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
37 | 36 | import org.springframework.context.annotation.Bean;
|
38 | 37 | import org.springframework.context.annotation.Configuration;
|
|
47 | 46 | */
|
48 | 47 | class BeanCurrentlyInCreationFailureAnalyzerTests {
|
49 | 48 |
|
50 |
| - private final FailureAnalyzer analyzer = new BeanCurrentlyInCreationFailureAnalyzer(); |
| 49 | + private final BeanCurrentlyInCreationFailureAnalyzer analyzer = new BeanCurrentlyInCreationFailureAnalyzer(); |
51 | 50 |
|
52 | 51 | @Test
|
53 | 52 | void cyclicBeanMethods() throws IOException {
|
@@ -131,20 +130,42 @@ void cycleWithAnUnknownStartIsNotAnalyzed() {
|
131 | 130 | assertThat(this.analyzer.analyze(new BeanCurrentlyInCreationException("test"))).isNull();
|
132 | 131 | }
|
133 | 132 |
|
| 133 | + @Test |
| 134 | + void cycleWithCircularReferencesAllowed() throws IOException { |
| 135 | + FailureAnalysis analysis = performAnalysis(CyclicBeanMethodsConfiguration.class, true); |
| 136 | + assertThat(analysis.getAction()).contains("Despite circular references being allowed"); |
| 137 | + } |
| 138 | + |
| 139 | + @Test |
| 140 | + void cycleWithCircularReferencesProhibited() throws IOException { |
| 141 | + FailureAnalysis analysis = performAnalysis(CyclicBeanMethodsConfiguration.class, false); |
| 142 | + assertThat(analysis.getAction()).contains("As a last resort"); |
| 143 | + } |
| 144 | + |
134 | 145 | private List<String> readDescriptionLines(FailureAnalysis analysis) throws IOException {
|
135 | 146 | try (BufferedReader reader = new BufferedReader(new StringReader(analysis.getDescription()))) {
|
136 | 147 | return reader.lines().collect(Collectors.toList());
|
137 | 148 | }
|
138 | 149 | }
|
139 | 150 |
|
140 | 151 | private FailureAnalysis performAnalysis(Class<?> configuration) {
|
141 |
| - FailureAnalysis analysis = this.analyzer.analyze(createFailure(configuration)); |
| 152 | + return performAnalysis(configuration, true); |
| 153 | + } |
| 154 | + |
| 155 | + private FailureAnalysis performAnalysis(Class<?> configuration, boolean allowCircularReferences) { |
| 156 | + FailureAnalysis analysis = this.analyzer.analyze(createFailure(configuration, allowCircularReferences)); |
142 | 157 | assertThat(analysis).isNotNull();
|
143 | 158 | return analysis;
|
144 | 159 | }
|
145 | 160 |
|
146 |
| - private Exception createFailure(Class<?> configuration) { |
147 |
| - try (ConfigurableApplicationContext context = new AnnotationConfigApplicationContext(configuration)) { |
| 161 | + private Exception createFailure(Class<?> configuration, boolean allowCircularReferences) { |
| 162 | + try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) { |
| 163 | + context.register(configuration); |
| 164 | + AbstractAutowireCapableBeanFactory beanFactory = (AbstractAutowireCapableBeanFactory) context |
| 165 | + .getBeanFactory(); |
| 166 | + this.analyzer.setBeanFactory(beanFactory); |
| 167 | + beanFactory.setAllowCircularReferences(allowCircularReferences); |
| 168 | + context.refresh(); |
148 | 169 | fail("Expected failure did not occur");
|
149 | 170 | return null;
|
150 | 171 | }
|
|
0 commit comments