Browse Source

Polishing

pull/31091/head
Sam Brannen 2 years ago
parent
commit
d8523cb033
  1. 10
      spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java
  2. 104
      spring-context/src/test/java/org/springframework/context/annotation/AnnotationBeanNameGeneratorTests.java

10
spring-context/src/main/java/org/springframework/context/annotation/AnnotationBeanNameGenerator.java

@ -107,12 +107,12 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator { @@ -107,12 +107,12 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
});
if (isStereotypeWithNameValue(type, metaTypes, attributes)) {
Object value = attributes.get("value");
if (value instanceof String strVal && !strVal.isEmpty()) {
if (beanName != null && !strVal.equals(beanName)) {
if (value instanceof String currentName && !currentName.isBlank()) {
if (beanName != null && !currentName.equals(beanName)) {
throw new IllegalStateException("Stereotype annotations suggest inconsistent " +
"component names: '" + beanName + "' versus '" + strVal + "'");
"component names: '" + beanName + "' versus '" + currentName + "'");
}
beanName = strVal;
beanName = currentName;
}
}
}
@ -122,7 +122,7 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator { @@ -122,7 +122,7 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
/**
* Check whether the given annotation is a stereotype that is allowed
* to suggest a component name through its annotation {@code value()}.
* to suggest a component name through its {@code value()} attribute.
* @param annotationType the name of the annotation class to check
* @param metaAnnotationTypes the names of meta-annotations on the given annotation
* @param attributes the map of attributes for the given annotation

104
spring-context/src/test/java/org/springframework/context/annotation/AnnotationBeanNameGeneratorTests.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2023 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.
@ -26,12 +26,12 @@ import org.junit.jupiter.api.Test; @@ -26,12 +26,12 @@ import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import static org.assertj.core.api.Assertions.assertThat;
@ -46,95 +46,81 @@ import static org.assertj.core.api.Assertions.assertThat; @@ -46,95 +46,81 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
class AnnotationBeanNameGeneratorTests {
private AnnotationBeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
private final BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
private final AnnotationBeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
@Test
void buildDefaultBeanName() {
BeanDefinition bd = annotatedBeanDef(ComponentFromNonStringMeta.class);
assertThat(this.beanNameGenerator.buildDefaultBeanName(bd, this.registry))
.isEqualTo("annotationBeanNameGeneratorTests.ComponentFromNonStringMeta");
}
@Test
void generateBeanNameWithNamedComponent() {
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentWithName.class);
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
assertThat(beanName).as("The generated beanName must *never* be null.").isNotNull();
assertThat(StringUtils.hasText(beanName)).as("The generated beanName must *never* be blank.").isTrue();
assertThat(beanName).isEqualTo("walden");
assertGeneratedName(ComponentWithName.class, "walden");
}
@Test
void generateBeanNameWithDefaultNamedComponent() {
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(DefaultNamedComponent.class);
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
assertThat(beanName).as("The generated beanName must *never* be null.").isNotNull();
assertThat(StringUtils.hasText(beanName)).as("The generated beanName must *never* be blank.").isTrue();
assertThat(beanName).isEqualTo("thoreau");
void generateBeanNameWithCustomStereotypeComponent() {
assertGeneratedName(DefaultNamedComponent.class, "thoreau");
}
@Test
void generateBeanNameWithNamedComponentWhereTheNameIsBlank() {
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentWithBlankName.class);
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
assertThat(beanName).as("The generated beanName must *never* be null.").isNotNull();
assertThat(StringUtils.hasText(beanName)).as("The generated beanName must *never* be blank.").isTrue();
String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd);
assertThat(beanName).isEqualTo(expectedGeneratedBeanName);
assertGeneratedNameIsDefault(ComponentWithBlankName.class);
}
@Test
void generateBeanNameWithAnonymousComponentYieldsGeneratedBeanName() {
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(AnonymousComponent.class);
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
assertThat(beanName).as("The generated beanName must *never* be null.").isNotNull();
assertThat(StringUtils.hasText(beanName)).as("The generated beanName must *never* be blank.").isTrue();
String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd);
assertThat(beanName).isEqualTo(expectedGeneratedBeanName);
assertGeneratedNameIsDefault(AnonymousComponent.class);
}
@Test
void generateBeanNameFromMetaComponentWithStringValue() {
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromStringMeta.class);
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
assertThat(beanName).isEqualTo("henry");
assertGeneratedName(ComponentFromStringMeta.class, "henry");
}
@Test
void generateBeanNameFromMetaComponentWithNonStringValue() {
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromNonStringMeta.class);
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
assertThat(beanName).isEqualTo("annotationBeanNameGeneratorTests.ComponentFromNonStringMeta");
assertGeneratedNameIsDefault(ComponentFromNonStringMeta.class);
}
@Test
@Test // SPR-11360
void generateBeanNameFromComposedControllerAnnotationWithoutName() {
// SPR-11360
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComposedControllerAnnotationWithoutName.class);
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd);
assertThat(beanName).isEqualTo(expectedGeneratedBeanName);
assertGeneratedNameIsDefault(ComposedControllerAnnotationWithoutName.class);
}
@Test
@Test // SPR-11360
void generateBeanNameFromComposedControllerAnnotationWithBlankName() {
// SPR-11360
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComposedControllerAnnotationWithBlankName.class);
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
String expectedGeneratedBeanName = this.beanNameGenerator.buildDefaultBeanName(bd);
assertThat(beanName).isEqualTo(expectedGeneratedBeanName);
assertGeneratedNameIsDefault(ComposedControllerAnnotationWithBlankName.class);
}
@Test
@Test // SPR-11360
void generateBeanNameFromComposedControllerAnnotationWithStringValue() {
// SPR-11360
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(
ComposedControllerAnnotationWithStringValue.class);
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
assertThat(beanName).isEqualTo("restController");
assertGeneratedName(ComposedControllerAnnotationWithStringValue.class, "restController");
}
private void assertGeneratedName(Class<?> clazz, String expectedName) {
BeanDefinition bd = annotatedBeanDef(clazz);
assertThat(generateBeanName(bd)).isNotBlank().isEqualTo(expectedName);
}
private void assertGeneratedNameIsDefault(Class<?> clazz) {
BeanDefinition bd = annotatedBeanDef(clazz);
String expectedName = this.beanNameGenerator.buildDefaultBeanName(bd);
assertThat(generateBeanName(bd)).isNotBlank().isEqualTo(expectedName);
}
private AnnotatedBeanDefinition annotatedBeanDef(Class<?> clazz) {
return new AnnotatedGenericBeanDefinition(clazz);
}
private String generateBeanName(BeanDefinition bd) {
return this.beanNameGenerator.generateBeanName(bd, registry);
}

Loading…
Cancel
Save