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:
@@ -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