Register AutoConfigurations using fully qualified class name
Update `AbstractApplicationContextRunner` and `Configurations` to allow registration of beans with a specific generated bean name. By default, no name is generated, however, `AutoConfigurations` has been updated to use bean names using the fully qualified class name. The update brings `ApplicationContextRunners` closer the behavior of a standard Spring Boot application where user `@Configuration` classes are usually registered with a simple name and auto-configurations are imported (via an `ImportSelector`) using a fully qualified name. Fixes gh-17963 Co-authored-by: Stéphane Nicoll <stephane.nicoll@broadcom.com> Co-authored-by: Andy Wilkinson <andy.wilkinson@broadcom.com> Co-authored-by: Dmytro Nosan <dimanosan@gmail.com>
This commit is contained in:
@@ -19,6 +19,7 @@ package org.springframework.boot.test.context.runner;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.function.BiConsumer;
|
||||
import java.util.function.Consumer;
|
||||
import java.util.function.Function;
|
||||
import java.util.function.Supplier;
|
||||
@@ -27,6 +28,7 @@ import org.springframework.beans.factory.config.BeanDefinition;
|
||||
import org.springframework.beans.factory.config.BeanDefinitionCustomizer;
|
||||
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
||||
import org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
||||
import org.springframework.beans.factory.support.BeanNameGenerator;
|
||||
import org.springframework.beans.factory.support.DefaultListableBeanFactory;
|
||||
import org.springframework.boot.context.annotation.Configurations;
|
||||
@@ -38,12 +40,14 @@ import org.springframework.boot.test.util.TestPropertyValues;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ConfigurableApplicationContext;
|
||||
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
||||
import org.springframework.context.annotation.AnnotationConfigRegistry;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.ResolvableType;
|
||||
import org.springframework.core.env.Environment;
|
||||
import org.springframework.core.io.DefaultResourceLoader;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.util.CollectionUtils;
|
||||
|
||||
/**
|
||||
* Utility design to run an {@link ApplicationContext} and provide AssertJ style
|
||||
@@ -439,15 +443,27 @@ public abstract class AbstractApplicationContextRunner<SELF extends AbstractAppl
|
||||
this.runnerConfiguration.environmentProperties.applyTo(context);
|
||||
this.runnerConfiguration.beanRegistrations.forEach((registration) -> registration.apply(context));
|
||||
this.runnerConfiguration.initializers.forEach((initializer) -> initializer.initialize(context));
|
||||
Class<?>[] classes = Configurations.getClasses(this.runnerConfiguration.configurations);
|
||||
if (classes.length > 0) {
|
||||
((AnnotationConfigRegistry) context).register(classes);
|
||||
if (!CollectionUtils.isEmpty(this.runnerConfiguration.configurations)) {
|
||||
BiConsumer<Class<?>, String> registrar = getRegistrar(context);
|
||||
for (Configurations configurations : Configurations.collate(this.runnerConfiguration.configurations)) {
|
||||
for (Class<?> beanClass : Configurations.getClasses(configurations)) {
|
||||
String beanName = configurations.getBeanName(beanClass);
|
||||
registrar.accept(beanClass, beanName);
|
||||
}
|
||||
}
|
||||
}
|
||||
if (refresh) {
|
||||
context.refresh();
|
||||
}
|
||||
}
|
||||
|
||||
private BiConsumer<Class<?>, String> getRegistrar(C context) {
|
||||
if (context instanceof BeanDefinitionRegistry registry) {
|
||||
return new AnnotatedBeanDefinitionReader(registry, context.getEnvironment())::registerBean;
|
||||
}
|
||||
return (beanClass, beanName) -> ((AnnotationConfigRegistry) context).register(beanClass);
|
||||
}
|
||||
|
||||
private void accept(ContextConsumer<? super A> consumer, A context) {
|
||||
try {
|
||||
consumer.accept(context);
|
||||
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.context.example.duplicate.first;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Example configuration to showcase handing of duplicate class names.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@Configuration
|
||||
public class EmptyConfig {
|
||||
|
||||
}
|
||||
@@ -0,0 +1,29 @@
|
||||
/*
|
||||
* Copyright 2012-2024 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
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.boot.test.context.example.duplicate.second;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
|
||||
/**
|
||||
* Example configuration to showcase handing of duplicate class names.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
@Configuration
|
||||
public class EmptyConfig {
|
||||
|
||||
}
|
||||
@@ -17,6 +17,9 @@
|
||||
package org.springframework.boot.test.context.runner;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
import java.util.UUID;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
|
||||
@@ -27,6 +30,8 @@ import org.springframework.beans.factory.BeanCurrentlyInCreationException;
|
||||
import org.springframework.beans.factory.BeanDefinitionStoreException;
|
||||
import org.springframework.beans.factory.ObjectProvider;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.beans.factory.support.BeanDefinitionOverrideException;
|
||||
import org.springframework.boot.context.annotation.Configurations;
|
||||
import org.springframework.boot.context.annotation.UserConfigurations;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
@@ -139,6 +144,38 @@ abstract class AbstractApplicationContextRunnerTests<T extends AbstractApplicati
|
||||
get().withUserConfiguration(FooConfig.class).run((context) -> assertThat(context).hasBean("foo"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runWithUserConfigurationsRegistersDefaultBeanName() {
|
||||
get().withUserConfiguration(FooConfig.class)
|
||||
.run((context) -> assertThat(context).hasBean("abstractApplicationContextRunnerTests.FooConfig"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runWithUserConfigurationsWhenHasSameShortClassNamedRegistersWithoutBeanName() {
|
||||
get()
|
||||
.withUserConfiguration(org.springframework.boot.test.context.example.duplicate.first.EmptyConfig.class,
|
||||
org.springframework.boot.test.context.example.duplicate.second.EmptyConfig.class)
|
||||
.run((context) -> assertThat(context.getStartupFailure())
|
||||
.isInstanceOf(BeanDefinitionOverrideException.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runFullyQualifiedNameConfigurationsRegistersFullyQualifiedBeanName() {
|
||||
get().withConfiguration(FullyQualifiedNameConfigurations.of(FooConfig.class))
|
||||
.run((context) -> assertThat(context).hasBean(FooConfig.class.getName()));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runWithFullyQualifiedNameConfigurationsWhenHasSameShortClassNamedRegistersWithFullyQualifiedBeanName() {
|
||||
get()
|
||||
.withConfiguration(FullyQualifiedNameConfigurations.of(
|
||||
org.springframework.boot.test.context.example.duplicate.first.EmptyConfig.class,
|
||||
org.springframework.boot.test.context.example.duplicate.second.EmptyConfig.class))
|
||||
.run((context) -> assertThat(context)
|
||||
.hasSingleBean(org.springframework.boot.test.context.example.duplicate.first.EmptyConfig.class)
|
||||
.hasSingleBean(org.springframework.boot.test.context.example.duplicate.second.EmptyConfig.class));
|
||||
}
|
||||
|
||||
@Test
|
||||
void runWithUserNamedBeanShouldRegisterBean() {
|
||||
get().withBean("foo", String.class, () -> "foo").run((context) -> assertThat(context).hasBean("foo"));
|
||||
@@ -384,4 +421,21 @@ abstract class AbstractApplicationContextRunnerTests<T extends AbstractApplicati
|
||||
|
||||
}
|
||||
|
||||
static class FullyQualifiedNameConfigurations extends Configurations {
|
||||
|
||||
protected FullyQualifiedNameConfigurations(Collection<Class<?>> classes) {
|
||||
super(null, classes, Class::getName);
|
||||
}
|
||||
|
||||
@Override
|
||||
protected Configurations merge(Set<Class<?>> mergedClasses) {
|
||||
return new FullyQualifiedNameConfigurations(mergedClasses);
|
||||
}
|
||||
|
||||
static FullyQualifiedNameConfigurations of(Class<?>... classes) {
|
||||
return new FullyQualifiedNameConfigurations(List.of(classes));
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user