Migrate FailureAnalyzers to constructor injection
All `FailureAnalyzer` implementations should use constructor injection for `BeanFactory` and `Environment` instead of implementing `BeanFactoryAware` or `EnvironmentAware` interfaces. Fixes gh-30585
This commit is contained in:
parent
612e4114d2
commit
d67dcf16cd
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2020 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -27,9 +27,7 @@ import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
import java.util.stream.Collectors;
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.beans.factory.BeanFactoryAware;
|
|
||||||
import org.springframework.beans.factory.BeanFactoryUtils;
|
import org.springframework.beans.factory.BeanFactoryUtils;
|
||||||
import org.springframework.beans.factory.InjectionPoint;
|
import org.springframework.beans.factory.InjectionPoint;
|
||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
|
@ -63,18 +61,17 @@ import org.springframework.util.ClassUtils;
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class NoSuchBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnalyzer<NoSuchBeanDefinitionException>
|
class NoSuchBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnalyzer<NoSuchBeanDefinitionException> {
|
||||||
implements BeanFactoryAware {
|
|
||||||
|
|
||||||
private ConfigurableListableBeanFactory beanFactory;
|
private final ConfigurableListableBeanFactory beanFactory;
|
||||||
|
|
||||||
private MetadataReaderFactory metadataReaderFactory;
|
private final MetadataReaderFactory metadataReaderFactory;
|
||||||
|
|
||||||
private ConditionEvaluationReport report;
|
private final ConditionEvaluationReport report;
|
||||||
|
|
||||||
@Override
|
NoSuchBeanDefinitionFailureAnalyzer(BeanFactory beanFactory) {
|
||||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
|
||||||
Assert.isInstanceOf(ConfigurableListableBeanFactory.class, beanFactory);
|
Assert.isInstanceOf(ConfigurableListableBeanFactory.class, beanFactory);
|
||||||
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
|
this.beanFactory = (ConfigurableListableBeanFactory) beanFactory;
|
||||||
this.metadataReaderFactory = new CachingMetadataReaderFactory(this.beanFactory.getBeanClassLoader());
|
this.metadataReaderFactory = new CachingMetadataReaderFactory(this.beanFactory.getBeanClassLoader());
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2020 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -49,10 +49,14 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* Tests for {@link NoSuchBeanDefinitionFailureAnalyzer}.
|
* Tests for {@link NoSuchBeanDefinitionFailureAnalyzer}.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class NoSuchBeanDefinitionFailureAnalyzerTests {
|
class NoSuchBeanDefinitionFailureAnalyzerTests {
|
||||||
|
|
||||||
private final NoSuchBeanDefinitionFailureAnalyzer analyzer = new NoSuchBeanDefinitionFailureAnalyzer();
|
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||||
|
|
||||||
|
private final NoSuchBeanDefinitionFailureAnalyzer analyzer = new NoSuchBeanDefinitionFailureAnalyzer(
|
||||||
|
this.context.getBeanFactory());
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void failureAnalysisForMultipleBeans() {
|
void failureAnalysisForMultipleBeans() {
|
||||||
|
@ -227,11 +231,10 @@ class NoSuchBeanDefinitionFailureAnalyzerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
private FatalBeanException createFailure(Class<?> config, String... environment) {
|
private FatalBeanException createFailure(Class<?> config, String... environment) {
|
||||||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
|
try {
|
||||||
this.analyzer.setBeanFactory(context.getBeanFactory());
|
TestPropertyValues.of(environment).applyTo(this.context);
|
||||||
TestPropertyValues.of(environment).applyTo(context);
|
this.context.register(config);
|
||||||
context.register(config);
|
this.context.refresh();
|
||||||
context.refresh();
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
catch (FatalBeanException ex) {
|
catch (FatalBeanException ex) {
|
||||||
|
|
|
@ -1,3 +1,19 @@
|
||||||
|
/*
|
||||||
|
* Copyright 2012-2022 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
|
* you may not use this file except in compliance with the License.
|
||||||
|
* You may obtain a copy of the License at
|
||||||
|
*
|
||||||
|
* https://www.apache.org/licenses/LICENSE-2.0
|
||||||
|
*
|
||||||
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||||
|
* See the License for the specific language governing permissions and
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
|
||||||
package org.springframework.boot.autoconfigure.diagnostics.analyzer
|
package org.springframework.boot.autoconfigure.diagnostics.analyzer
|
||||||
|
|
||||||
import org.assertj.core.api.Assertions.assertThat
|
import org.assertj.core.api.Assertions.assertThat
|
||||||
|
@ -18,11 +34,12 @@ import org.springframework.context.annotation.Configuration
|
||||||
* on the classpath.
|
* on the classpath.
|
||||||
*
|
*
|
||||||
* @author Madhura Bhave
|
* @author Madhura Bhave
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
@ClassPathExclusions("kotlin-reflect*.jar")
|
@ClassPathExclusions("kotlin-reflect*.jar")
|
||||||
class KotlinNoSuchBeanFailureAnalyzerNoKotlinReflectTests {
|
class KotlinNoSuchBeanFailureAnalyzerNoKotlinReflectTests {
|
||||||
|
|
||||||
private val analyzer = NoSuchBeanDefinitionFailureAnalyzer()
|
private val context = AnnotationConfigApplicationContext()
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
fun failureAnalysisForConfigurationPropertiesThatMaybeShouldHaveBeenConstructorBound() {
|
fun failureAnalysisForConfigurationPropertiesThatMaybeShouldHaveBeenConstructorBound() {
|
||||||
|
@ -37,7 +54,6 @@ class KotlinNoSuchBeanFailureAnalyzerNoKotlinReflectTests {
|
||||||
private fun createFailure(config: Class<*>, vararg environment: String): FatalBeanException? {
|
private fun createFailure(config: Class<*>, vararg environment: String): FatalBeanException? {
|
||||||
try {
|
try {
|
||||||
AnnotationConfigApplicationContext().use { context ->
|
AnnotationConfigApplicationContext().use { context ->
|
||||||
this.analyzer.setBeanFactory(context.beanFactory)
|
|
||||||
TestPropertyValues.of(*environment).applyTo(context)
|
TestPropertyValues.of(*environment).applyTo(context)
|
||||||
context.register(config)
|
context.register(config)
|
||||||
context.refresh()
|
context.refresh()
|
||||||
|
@ -50,7 +66,8 @@ class KotlinNoSuchBeanFailureAnalyzerNoKotlinReflectTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
private fun analyzeFailure(failure: Exception?): FailureAnalysis? {
|
private fun analyzeFailure(failure: Exception?): FailureAnalysis? {
|
||||||
val analysis = this.analyzer.analyze(failure)
|
val analyzer = NoSuchBeanDefinitionFailureAnalyzer(this.context.beanFactory)
|
||||||
|
val analysis = analyzer.analyze(failure)
|
||||||
if (analysis != null) {
|
if (analysis != null) {
|
||||||
LoggingFailureAnalysisReporter().report(analysis)
|
LoggingFailureAnalysisReporter().report(analysis)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2021 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -19,11 +19,9 @@ package org.springframework.boot.diagnostics.analyzer;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.BeanCreationException;
|
import org.springframework.beans.factory.BeanCreationException;
|
||||||
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
|
import org.springframework.beans.factory.BeanCurrentlyInCreationException;
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.beans.factory.BeanFactoryAware;
|
|
||||||
import org.springframework.beans.factory.InjectionPoint;
|
import org.springframework.beans.factory.InjectionPoint;
|
||||||
import org.springframework.beans.factory.UnsatisfiedDependencyException;
|
import org.springframework.beans.factory.UnsatisfiedDependencyException;
|
||||||
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
|
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
|
||||||
|
@ -36,17 +34,19 @@ import org.springframework.util.StringUtils;
|
||||||
* {@link BeanCurrentlyInCreationException}.
|
* {@link BeanCurrentlyInCreationException}.
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class BeanCurrentlyInCreationFailureAnalyzer extends AbstractFailureAnalyzer<BeanCurrentlyInCreationException>
|
class BeanCurrentlyInCreationFailureAnalyzer extends AbstractFailureAnalyzer<BeanCurrentlyInCreationException> {
|
||||||
implements BeanFactoryAware {
|
|
||||||
|
|
||||||
private AbstractAutowireCapableBeanFactory beanFactory;
|
private final AbstractAutowireCapableBeanFactory beanFactory;
|
||||||
|
|
||||||
@Override
|
BeanCurrentlyInCreationFailureAnalyzer(BeanFactory beanFactory) {
|
||||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
if (beanFactory != null && beanFactory instanceof AbstractAutowireCapableBeanFactory) {
|
||||||
if (beanFactory instanceof AbstractAutowireCapableBeanFactory) {
|
|
||||||
this.beanFactory = (AbstractAutowireCapableBeanFactory) beanFactory;
|
this.beanFactory = (AbstractAutowireCapableBeanFactory) beanFactory;
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
this.beanFactory = null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -27,7 +27,6 @@ import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||||
import org.springframework.boot.diagnostics.FailureAnalyzer;
|
import org.springframework.boot.diagnostics.FailureAnalyzer;
|
||||||
import org.springframework.boot.origin.Origin;
|
import org.springframework.boot.origin.Origin;
|
||||||
import org.springframework.boot.origin.OriginLookup;
|
import org.springframework.boot.origin.OriginLookup;
|
||||||
import org.springframework.context.EnvironmentAware;
|
|
||||||
import org.springframework.core.env.ConfigurableEnvironment;
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
|
@ -38,14 +37,14 @@ import org.springframework.util.StringUtils;
|
||||||
* {@link InvalidConfigurationPropertyValueException}.
|
* {@link InvalidConfigurationPropertyValueException}.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class InvalidConfigurationPropertyValueFailureAnalyzer
|
class InvalidConfigurationPropertyValueFailureAnalyzer
|
||||||
extends AbstractFailureAnalyzer<InvalidConfigurationPropertyValueException> implements EnvironmentAware {
|
extends AbstractFailureAnalyzer<InvalidConfigurationPropertyValueException> {
|
||||||
|
|
||||||
private ConfigurableEnvironment environment;
|
private final ConfigurableEnvironment environment;
|
||||||
|
|
||||||
@Override
|
InvalidConfigurationPropertyValueFailureAnalyzer(Environment environment) {
|
||||||
public void setEnvironment(Environment environment) {
|
|
||||||
this.environment = (ConfigurableEnvironment) environment;
|
this.environment = (ConfigurableEnvironment) environment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2021 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -32,7 +32,6 @@ import org.springframework.boot.diagnostics.FailureAnalysis;
|
||||||
import org.springframework.boot.diagnostics.FailureAnalyzer;
|
import org.springframework.boot.diagnostics.FailureAnalyzer;
|
||||||
import org.springframework.boot.origin.Origin;
|
import org.springframework.boot.origin.Origin;
|
||||||
import org.springframework.boot.origin.OriginLookup;
|
import org.springframework.boot.origin.OriginLookup;
|
||||||
import org.springframework.context.EnvironmentAware;
|
|
||||||
import org.springframework.core.env.ConfigurableEnvironment;
|
import org.springframework.core.env.ConfigurableEnvironment;
|
||||||
import org.springframework.core.env.Environment;
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.core.env.PropertySource;
|
import org.springframework.core.env.PropertySource;
|
||||||
|
@ -42,14 +41,14 @@ import org.springframework.core.env.PropertySource;
|
||||||
* {@link MutuallyExclusiveConfigurationPropertiesException}.
|
* {@link MutuallyExclusiveConfigurationPropertiesException}.
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class MutuallyExclusiveConfigurationPropertiesFailureAnalyzer
|
class MutuallyExclusiveConfigurationPropertiesFailureAnalyzer
|
||||||
extends AbstractFailureAnalyzer<MutuallyExclusiveConfigurationPropertiesException> implements EnvironmentAware {
|
extends AbstractFailureAnalyzer<MutuallyExclusiveConfigurationPropertiesException> {
|
||||||
|
|
||||||
private ConfigurableEnvironment environment;
|
private final ConfigurableEnvironment environment;
|
||||||
|
|
||||||
@Override
|
MutuallyExclusiveConfigurationPropertiesFailureAnalyzer(Environment environment) {
|
||||||
public void setEnvironment(Environment environment) {
|
|
||||||
this.environment = (ConfigurableEnvironment) environment;
|
this.environment = (ConfigurableEnvironment) environment;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -16,9 +16,7 @@
|
||||||
|
|
||||||
package org.springframework.boot.diagnostics.analyzer;
|
package org.springframework.boot.diagnostics.analyzer;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
|
||||||
import org.springframework.beans.factory.BeanFactory;
|
import org.springframework.beans.factory.BeanFactory;
|
||||||
import org.springframework.beans.factory.BeanFactoryAware;
|
|
||||||
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
|
||||||
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
|
import org.springframework.beans.factory.NoUniqueBeanDefinitionException;
|
||||||
import org.springframework.beans.factory.config.BeanDefinition;
|
import org.springframework.beans.factory.config.BeanDefinition;
|
||||||
|
@ -32,14 +30,13 @@ import org.springframework.util.StringUtils;
|
||||||
* by a {@link NoUniqueBeanDefinitionException}.
|
* by a {@link NoUniqueBeanDefinitionException}.
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class NoUniqueBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnalyzer<NoUniqueBeanDefinitionException>
|
class NoUniqueBeanDefinitionFailureAnalyzer extends AbstractInjectionFailureAnalyzer<NoUniqueBeanDefinitionException> {
|
||||||
implements BeanFactoryAware {
|
|
||||||
|
|
||||||
private ConfigurableBeanFactory beanFactory;
|
private final ConfigurableBeanFactory beanFactory;
|
||||||
|
|
||||||
@Override
|
NoUniqueBeanDefinitionFailureAnalyzer(BeanFactory beanFactory) {
|
||||||
public void setBeanFactory(BeanFactory beanFactory) throws BeansException {
|
|
||||||
Assert.isInstanceOf(ConfigurableBeanFactory.class, beanFactory);
|
Assert.isInstanceOf(ConfigurableBeanFactory.class, beanFactory);
|
||||||
this.beanFactory = (ConfigurableBeanFactory) beanFactory;
|
this.beanFactory = (ConfigurableBeanFactory) beanFactory;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2021 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -43,10 +43,14 @@ import static org.junit.jupiter.api.Assertions.fail;
|
||||||
* Tests for {@link BeanCurrentlyInCreationFailureAnalyzer}.
|
* Tests for {@link BeanCurrentlyInCreationFailureAnalyzer}.
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class BeanCurrentlyInCreationFailureAnalyzerTests {
|
class BeanCurrentlyInCreationFailureAnalyzerTests {
|
||||||
|
|
||||||
private final BeanCurrentlyInCreationFailureAnalyzer analyzer = new BeanCurrentlyInCreationFailureAnalyzer();
|
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||||
|
|
||||||
|
private final BeanCurrentlyInCreationFailureAnalyzer analyzer = new BeanCurrentlyInCreationFailureAnalyzer(
|
||||||
|
this.context.getBeanFactory());
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cyclicBeanMethods() throws IOException {
|
void cyclicBeanMethods() throws IOException {
|
||||||
|
@ -131,13 +135,13 @@ class BeanCurrentlyInCreationFailureAnalyzerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cycleWithCircularReferencesAllowed() throws IOException {
|
void cycleWithCircularReferencesAllowed() {
|
||||||
FailureAnalysis analysis = performAnalysis(CyclicBeanMethodsConfiguration.class, true);
|
FailureAnalysis analysis = performAnalysis(CyclicBeanMethodsConfiguration.class, true);
|
||||||
assertThat(analysis.getAction()).contains("Despite circular references being allowed");
|
assertThat(analysis.getAction()).contains("Despite circular references being allowed");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void cycleWithCircularReferencesProhibited() throws IOException {
|
void cycleWithCircularReferencesProhibited() {
|
||||||
FailureAnalysis analysis = performAnalysis(CyclicBeanMethodsConfiguration.class, false);
|
FailureAnalysis analysis = performAnalysis(CyclicBeanMethodsConfiguration.class, false);
|
||||||
assertThat(analysis.getAction()).contains("As a last resort");
|
assertThat(analysis.getAction()).contains("As a last resort");
|
||||||
}
|
}
|
||||||
|
@ -159,13 +163,12 @@ class BeanCurrentlyInCreationFailureAnalyzerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
private Exception createFailure(Class<?> configuration, boolean allowCircularReferences) {
|
private Exception createFailure(Class<?> configuration, boolean allowCircularReferences) {
|
||||||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
|
try {
|
||||||
context.register(configuration);
|
this.context.register(configuration);
|
||||||
AbstractAutowireCapableBeanFactory beanFactory = (AbstractAutowireCapableBeanFactory) context
|
AbstractAutowireCapableBeanFactory beanFactory = (AbstractAutowireCapableBeanFactory) this.context
|
||||||
.getBeanFactory();
|
.getBeanFactory();
|
||||||
this.analyzer.setBeanFactory(beanFactory);
|
|
||||||
beanFactory.setAllowCircularReferences(allowCircularReferences);
|
beanFactory.setAllowCircularReferences(allowCircularReferences);
|
||||||
context.refresh();
|
this.context.refresh();
|
||||||
fail("Expected failure did not occur");
|
fail("Expected failure did not occur");
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -34,6 +34,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* Tests for {@link InvalidConfigurationPropertyValueFailureAnalyzer}.
|
* Tests for {@link InvalidConfigurationPropertyValueFailureAnalyzer}.
|
||||||
*
|
*
|
||||||
* @author Stephane Nicoll
|
* @author Stephane Nicoll
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class InvalidConfigurationPropertyValueFailureAnalyzerTests {
|
class InvalidConfigurationPropertyValueFailureAnalyzerTests {
|
||||||
|
|
||||||
|
@ -43,7 +44,7 @@ class InvalidConfigurationPropertyValueFailureAnalyzerTests {
|
||||||
void analysisWithNullEnvironment() {
|
void analysisWithNullEnvironment() {
|
||||||
InvalidConfigurationPropertyValueException failure = new InvalidConfigurationPropertyValueException(
|
InvalidConfigurationPropertyValueException failure = new InvalidConfigurationPropertyValueException(
|
||||||
"test.property", "invalid", "This is not valid.");
|
"test.property", "invalid", "This is not valid.");
|
||||||
FailureAnalysis analysis = new InvalidConfigurationPropertyValueFailureAnalyzer().analyze(failure);
|
FailureAnalysis analysis = new InvalidConfigurationPropertyValueFailureAnalyzer(null).analyze(failure);
|
||||||
assertThat(analysis).isNull();
|
assertThat(analysis).isNull();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -106,8 +107,8 @@ class InvalidConfigurationPropertyValueFailureAnalyzerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
private FailureAnalysis performAnalysis(InvalidConfigurationPropertyValueException failure) {
|
private FailureAnalysis performAnalysis(InvalidConfigurationPropertyValueException failure) {
|
||||||
InvalidConfigurationPropertyValueFailureAnalyzer analyzer = new InvalidConfigurationPropertyValueFailureAnalyzer();
|
InvalidConfigurationPropertyValueFailureAnalyzer analyzer = new InvalidConfigurationPropertyValueFailureAnalyzer(
|
||||||
analyzer.setEnvironment(this.environment);
|
this.environment);
|
||||||
return analyzer.analyze(failure);
|
return analyzer.analyze(failure);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2021 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -39,6 +39,7 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* Tests for {@link MutuallyExclusiveConfigurationPropertiesFailureAnalyzer}.
|
* Tests for {@link MutuallyExclusiveConfigurationPropertiesFailureAnalyzer}.
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class MutuallyExclusiveConfigurationPropertiesFailureAnalyzerTests {
|
class MutuallyExclusiveConfigurationPropertiesFailureAnalyzerTests {
|
||||||
|
|
||||||
|
@ -49,7 +50,7 @@ class MutuallyExclusiveConfigurationPropertiesFailureAnalyzerTests {
|
||||||
MutuallyExclusiveConfigurationPropertiesException failure = new MutuallyExclusiveConfigurationPropertiesException(
|
MutuallyExclusiveConfigurationPropertiesException failure = new MutuallyExclusiveConfigurationPropertiesException(
|
||||||
new HashSet<>(Arrays.asList("com.example.a", "com.example.b")),
|
new HashSet<>(Arrays.asList("com.example.a", "com.example.b")),
|
||||||
new HashSet<>(Arrays.asList("com.example.a", "com.example.b")));
|
new HashSet<>(Arrays.asList("com.example.a", "com.example.b")));
|
||||||
FailureAnalysis failureAnalysis = new MutuallyExclusiveConfigurationPropertiesFailureAnalyzer()
|
FailureAnalysis failureAnalysis = new MutuallyExclusiveConfigurationPropertiesFailureAnalyzer(null)
|
||||||
.analyze(failure);
|
.analyze(failure);
|
||||||
assertThat(failureAnalysis).isNull();
|
assertThat(failureAnalysis).isNull();
|
||||||
}
|
}
|
||||||
|
@ -112,8 +113,8 @@ class MutuallyExclusiveConfigurationPropertiesFailureAnalyzerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
private FailureAnalysis performAnalysis(MutuallyExclusiveConfigurationPropertiesException failure) {
|
private FailureAnalysis performAnalysis(MutuallyExclusiveConfigurationPropertiesException failure) {
|
||||||
MutuallyExclusiveConfigurationPropertiesFailureAnalyzer analyzer = new MutuallyExclusiveConfigurationPropertiesFailureAnalyzer();
|
MutuallyExclusiveConfigurationPropertiesFailureAnalyzer analyzer = new MutuallyExclusiveConfigurationPropertiesFailureAnalyzer(
|
||||||
analyzer.setEnvironment(this.environment);
|
this.environment);
|
||||||
return analyzer.analyze(failure);
|
return analyzer.analyze(failure);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright 2012-2019 the original author or authors.
|
* Copyright 2012-2022 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
|
@ -36,10 +36,14 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
* Tests for {@link NoUniqueBeanDefinitionFailureAnalyzer}.
|
* Tests for {@link NoUniqueBeanDefinitionFailureAnalyzer}.
|
||||||
*
|
*
|
||||||
* @author Andy Wilkinson
|
* @author Andy Wilkinson
|
||||||
|
* @author Scott Frederick
|
||||||
*/
|
*/
|
||||||
class NoUniqueBeanDefinitionFailureAnalyzerTests {
|
class NoUniqueBeanDefinitionFailureAnalyzerTests {
|
||||||
|
|
||||||
private final NoUniqueBeanDefinitionFailureAnalyzer analyzer = new NoUniqueBeanDefinitionFailureAnalyzer();
|
private final AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
|
||||||
|
|
||||||
|
private final NoUniqueBeanDefinitionFailureAnalyzer analyzer = new NoUniqueBeanDefinitionFailureAnalyzer(
|
||||||
|
this.context.getBeanFactory());
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void failureAnalysisForFieldConsumer() {
|
void failureAnalysisForFieldConsumer() {
|
||||||
|
@ -90,19 +94,16 @@ class NoUniqueBeanDefinitionFailureAnalyzerTests {
|
||||||
}
|
}
|
||||||
|
|
||||||
private BeanCreationException createFailure(Class<?> consumer) {
|
private BeanCreationException createFailure(Class<?> consumer) {
|
||||||
try (AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext()) {
|
this.context.register(DuplicateBeansProducer.class, consumer);
|
||||||
context.register(DuplicateBeansProducer.class, consumer);
|
this.context.setParent(new AnnotationConfigApplicationContext(ParentProducer.class));
|
||||||
context.setParent(new AnnotationConfigApplicationContext(ParentProducer.class));
|
|
||||||
try {
|
try {
|
||||||
context.refresh();
|
this.context.refresh();
|
||||||
}
|
}
|
||||||
catch (BeanCreationException ex) {
|
catch (BeanCreationException ex) {
|
||||||
this.analyzer.setBeanFactory(context.getBeanFactory());
|
|
||||||
return ex;
|
return ex;
|
||||||
}
|
}
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
private FailureAnalysis analyzeFailure(BeanCreationException failure) {
|
private FailureAnalysis analyzeFailure(BeanCreationException failure) {
|
||||||
return this.analyzer.analyze(failure);
|
return this.analyzer.analyze(failure);
|
||||||
|
|
Loading…
Reference in New Issue