Rename BeanOverrideStrategy enum constants

Closes gh-33701
This commit is contained in:
Sam Brannen
2024-10-15 18:19:37 +02:00
parent 7ea43bb252
commit 67cb3c77ec
22 changed files with 95 additions and 96 deletions

View File

@@ -106,14 +106,14 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
beanName, field.getDeclaringClass().getSimpleName(), field.getName()));
switch (overrideMetadata.getStrategy()) {
case REPLACE_DEFINITION -> replaceDefinition(beanFactory, overrideMetadata, true);
case REPLACE_OR_CREATE_DEFINITION -> replaceDefinition(beanFactory, overrideMetadata, false);
case WRAP_BEAN -> wrapBean(beanFactory, overrideMetadata);
case REPLACE -> replaceBean(beanFactory, overrideMetadata, true);
case REPLACE_OR_CREATE -> replaceBean(beanFactory, overrideMetadata, false);
case WRAP -> wrapBean(beanFactory, overrideMetadata);
}
}
private void replaceDefinition(ConfigurableListableBeanFactory beanFactory, OverrideMetadata overrideMetadata,
boolean requireExistingDefinition) {
private void replaceBean(ConfigurableListableBeanFactory beanFactory, OverrideMetadata overrideMetadata,
boolean requireExistingBean) {
// NOTE: This method supports 3 distinct scenarios which must be accounted for.
//
@@ -124,7 +124,7 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
String beanName = overrideMetadata.getBeanName();
BeanDefinition existingBeanDefinition = null;
if (beanName == null) {
beanName = getBeanNameForType(beanFactory, overrideMetadata, requireExistingDefinition);
beanName = getBeanNameForType(beanFactory, overrideMetadata, requireExistingBean);
if (beanName != null) {
// We are overriding an existing bean by-type.
beanName = BeanFactoryUtils.transformedBeanName(beanName);
@@ -146,9 +146,9 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
// We are overriding an existing bean by-name.
existingBeanDefinition = beanFactory.getBeanDefinition(beanName);
}
else if (requireExistingDefinition) {
else if (requireExistingBean) {
throw new IllegalStateException("""
Unable to override bean: there is no bean definition to replace \
Unable to override bean: there is no bean to replace \
with name [%s] and type [%s]."""
.formatted(beanName, overrideMetadata.getBeanType()));
}
@@ -237,7 +237,7 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
Set<String> candidates = getExistingBeanNamesByType(beanFactory, overrideMetadata, false);
if (!candidates.contains(beanName)) {
throw new IllegalStateException("""
Unable to override bean by wrapping: there is no existing bean definition \
Unable to override bean by wrapping: there is no existing bean \
with name [%s] and type [%s]."""
.formatted(beanName, overrideMetadata.getBeanType()));
}
@@ -249,7 +249,7 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
@Nullable
private String getBeanNameForType(ConfigurableListableBeanFactory beanFactory, OverrideMetadata overrideMetadata,
boolean requireExistingDefinition) {
boolean requireExistingBean) {
Set<String> candidateNames = getExistingBeanNamesByType(beanFactory, overrideMetadata, true);
int candidateCount = candidateNames.size();
@@ -257,10 +257,10 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
return candidateNames.iterator().next();
}
else if (candidateCount == 0) {
if (requireExistingDefinition) {
if (requireExistingBean) {
Field field = overrideMetadata.getField();
throw new IllegalStateException(
"Unable to override bean: no bean definitions of type %s (as required by annotated field '%s.%s')"
"Unable to override bean: no beans of type %s (as required by annotated field '%s.%s')"
.formatted(overrideMetadata.getBeanType(), field.getDeclaringClass().getSimpleName(), field.getName()));
}
return null;
@@ -268,7 +268,7 @@ class BeanOverrideBeanFactoryPostProcessor implements BeanFactoryPostProcessor,
Field field = overrideMetadata.getField();
throw new IllegalStateException("""
Unable to select a bean definition to override: found %s bean definitions of type %s \
Unable to select a bean to override: found %s beans of type %s \
(as required by annotated field '%s.%s'): %s"""
.formatted(candidateCount, overrideMetadata.getBeanType(), field.getDeclaringClass().getSimpleName(),
field.getName(), candidateNames));

View File

@@ -57,7 +57,7 @@ class BeanOverrideRegistrar {
*/
Object wrapIfNecessary(Object bean, String beanName) throws BeansException {
OverrideMetadata metadata = this.earlyOverrideMetadata.get(beanName);
if (metadata != null && metadata.getStrategy() == BeanOverrideStrategy.WRAP_BEAN) {
if (metadata != null && metadata.getStrategy() == BeanOverrideStrategy.WRAP) {
bean = metadata.createOverride(beanName, null, bean);
metadata.track(bean, this.beanFactory);
}

View File

@@ -27,27 +27,24 @@ package org.springframework.test.context.bean.override;
public enum BeanOverrideStrategy {
/**
* Replace a given bean definition, immediately preparing a singleton instance.
* <p>Fails if the original bean definition does not exist. To create a new bean
* definition in such a case, use {@link #REPLACE_OR_CREATE_DEFINITION} instead.
* Replace a given bean, immediately preparing a singleton instance.
* <p>Fails if the original bean does not exist. To create a new bean
* in such a case, use {@link #REPLACE_OR_CREATE} instead.
*/
REPLACE_DEFINITION,
REPLACE,
/**
* Replace or create a given bean definition, immediately preparing a
* singleton instance.
* <p>Contrary to {@link #REPLACE_DEFINITION}, this creates a new bean
* definition if the target bean definition does not exist rather than
* failing.
* Replace or create a given bean, immediately preparing a singleton instance.
* <p>Contrary to {@link #REPLACE}, this strategy creates a new bean if the
* target bean does not exist rather than failing.
*/
REPLACE_OR_CREATE_DEFINITION,
REPLACE_OR_CREATE,
/**
* Intercept and process an early bean reference rather than a bean
* definition, allowing variants of bean overriding to wrap the instance
* &mdash; for example, to delegate to actual methods in the context of a
* mocking "spy".
* Intercept and process an early bean reference, allowing variants of bean
* overriding to wrap the original bean instance &mdash; for example, to
* delegate to actual methods in the context of a mocking "spy".
*/
WRAP_BEAN
WRAP
}

View File

@@ -28,8 +28,8 @@ import org.springframework.util.StringUtils;
/**
* {@link SmartInstantiationAwareBeanPostProcessor} implementation that wraps
* beans in order to support the {@link BeanOverrideStrategy#WRAP_BEAN WRAP_BEAN}
* bean override strategy.
* beans in order to support the {@link BeanOverrideStrategy#WRAP WRAP} bean
* override strategy.
*
* @author Simon Baslé
* @author Stephane Nicoll

View File

@@ -154,8 +154,8 @@ public @interface TestBean {
* be created if a corresponding bean definition does not exist.
* <p>Set to {@code true} to cause an exception to be thrown if a corresponding
* bean definition does not exist.
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_OR_CREATE_DEFINITION
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_DEFINITION
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_OR_CREATE
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE
*/
boolean enforceOverride() default false;

View File

@@ -37,8 +37,8 @@ import org.springframework.util.ClassUtils;
import org.springframework.util.ReflectionUtils;
import org.springframework.util.ReflectionUtils.MethodFilter;
import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_DEFINITION;
import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION;
import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE;
import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE;
/**
* {@link BeanOverrideProcessor} implementation for {@link TestBean @TestBean}
@@ -62,7 +62,7 @@ class TestBeanOverrideProcessor implements BeanOverrideProcessor {
String beanName = (!testBean.name().isBlank() ? testBean.name() : null);
String methodName = testBean.methodName();
BeanOverrideStrategy strategy = (testBean.enforceOverride() ? REPLACE_DEFINITION : REPLACE_OR_CREATE_DEFINITION);
BeanOverrideStrategy strategy = (testBean.enforceOverride() ? REPLACE : REPLACE_OR_CREATE);
Method overrideMethod;
if (!methodName.isBlank()) {

View File

@@ -124,8 +124,8 @@ public @interface MockitoBean {
* be created if a corresponding bean definition does not exist.
* <p>Set to {@code true} to cause an exception to be thrown if a corresponding
* bean definition does not exist.
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_OR_CREATE_DEFINITION
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_DEFINITION
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE_OR_CREATE
* @see org.springframework.test.context.bean.override.BeanOverrideStrategy#REPLACE
*/
boolean enforceOverride() default false;

View File

@@ -37,8 +37,8 @@ import org.springframework.util.Assert;
import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils;
import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_DEFINITION;
import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE_DEFINITION;
import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE;
import static org.springframework.test.context.bean.override.BeanOverrideStrategy.REPLACE_OR_CREATE;
/**
* {@link OverrideMetadata} implementation for Mockito {@code mock} support.
@@ -59,7 +59,7 @@ class MockitoBeanOverrideMetadata extends AbstractMockitoOverrideMetadata {
MockitoBeanOverrideMetadata(Field field, ResolvableType typeToMock, MockitoBean mockitoBean) {
this(field, typeToMock, (!mockitoBean.name().isBlank() ? mockitoBean.name() : null),
(mockitoBean.enforceOverride() ? REPLACE_DEFINITION : REPLACE_OR_CREATE_DEFINITION),
(mockitoBean.enforceOverride() ? REPLACE : REPLACE_OR_CREATE),
mockitoBean.reset(), mockitoBean.extraInterfaces(), mockitoBean.answers(), mockitoBean.serializable());
}

View File

@@ -52,7 +52,7 @@ class MockitoSpyBeanOverrideMetadata extends AbstractMockitoOverrideMetadata {
MockitoSpyBeanOverrideMetadata(Field field, ResolvableType typeToSpy, @Nullable String beanName,
MockReset reset, boolean proxyTargetAware) {
super(field, typeToSpy, beanName, BeanOverrideStrategy.WRAP_BEAN, reset, proxyTargetAware);
super(field, typeToSpy, beanName, BeanOverrideStrategy.WRAP, reset, proxyTargetAware);
Assert.notNull(typeToSpy, "typeToSpy must not be null");
}