Polishing
This commit is contained in:
parent
5e171d3cf5
commit
d8523cb033
|
|
@ -107,12 +107,12 @@ public class AnnotationBeanNameGenerator implements BeanNameGenerator {
|
||||||
});
|
});
|
||||||
if (isStereotypeWithNameValue(type, metaTypes, attributes)) {
|
if (isStereotypeWithNameValue(type, metaTypes, attributes)) {
|
||||||
Object value = attributes.get("value");
|
Object value = attributes.get("value");
|
||||||
if (value instanceof String strVal && !strVal.isEmpty()) {
|
if (value instanceof String currentName && !currentName.isBlank()) {
|
||||||
if (beanName != null && !strVal.equals(beanName)) {
|
if (beanName != null && !currentName.equals(beanName)) {
|
||||||
throw new IllegalStateException("Stereotype annotations suggest inconsistent " +
|
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 {
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Check whether the given annotation is a stereotype that is allowed
|
* 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 annotationType the name of the annotation class to check
|
||||||
* @param metaAnnotationTypes the names of meta-annotations on the given annotation
|
* @param metaAnnotationTypes the names of meta-annotations on the given annotation
|
||||||
* @param attributes the map of attributes for the given annotation
|
* @param attributes the map of attributes for the given annotation
|
||||||
|
|
|
||||||
|
|
@ -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");
|
* 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.
|
||||||
|
|
@ -26,12 +26,12 @@ import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
import org.springframework.beans.factory.annotation.AnnotatedBeanDefinition;
|
||||||
import org.springframework.beans.factory.annotation.AnnotatedGenericBeanDefinition;
|
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.BeanDefinitionRegistry;
|
||||||
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
|
import org.springframework.beans.factory.support.SimpleBeanDefinitionRegistry;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
import org.springframework.stereotype.Controller;
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.util.StringUtils;
|
|
||||||
|
|
||||||
import static org.assertj.core.api.Assertions.assertThat;
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
|
@ -46,95 +46,81 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||||
*/
|
*/
|
||||||
class AnnotationBeanNameGeneratorTests {
|
class AnnotationBeanNameGeneratorTests {
|
||||||
|
|
||||||
private AnnotationBeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
|
private final BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
|
||||||
|
|
||||||
|
private final AnnotationBeanNameGenerator beanNameGenerator = new AnnotationBeanNameGenerator();
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void generateBeanNameWithNamedComponent() {
|
void buildDefaultBeanName() {
|
||||||
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
|
BeanDefinition bd = annotatedBeanDef(ComponentFromNonStringMeta.class);
|
||||||
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentWithName.class);
|
assertThat(this.beanNameGenerator.buildDefaultBeanName(bd, this.registry))
|
||||||
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
|
.isEqualTo("annotationBeanNameGeneratorTests.ComponentFromNonStringMeta");
|
||||||
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");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void generateBeanNameWithDefaultNamedComponent() {
|
void generateBeanNameWithNamedComponent() {
|
||||||
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
|
assertGeneratedName(ComponentWithName.class, "walden");
|
||||||
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(DefaultNamedComponent.class);
|
}
|
||||||
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
|
|
||||||
assertThat(beanName).as("The generated beanName must *never* be null.").isNotNull();
|
@Test
|
||||||
assertThat(StringUtils.hasText(beanName)).as("The generated beanName must *never* be blank.").isTrue();
|
void generateBeanNameWithCustomStereotypeComponent() {
|
||||||
assertThat(beanName).isEqualTo("thoreau");
|
assertGeneratedName(DefaultNamedComponent.class, "thoreau");
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void generateBeanNameWithNamedComponentWhereTheNameIsBlank() {
|
void generateBeanNameWithNamedComponentWhereTheNameIsBlank() {
|
||||||
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
|
assertGeneratedNameIsDefault(ComponentWithBlankName.class);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void generateBeanNameWithAnonymousComponentYieldsGeneratedBeanName() {
|
void generateBeanNameWithAnonymousComponentYieldsGeneratedBeanName() {
|
||||||
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
|
assertGeneratedNameIsDefault(AnonymousComponent.class);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void generateBeanNameFromMetaComponentWithStringValue() {
|
void generateBeanNameFromMetaComponentWithStringValue() {
|
||||||
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
|
assertGeneratedName(ComponentFromStringMeta.class, "henry");
|
||||||
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromStringMeta.class);
|
|
||||||
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
|
|
||||||
assertThat(beanName).isEqualTo("henry");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
void generateBeanNameFromMetaComponentWithNonStringValue() {
|
void generateBeanNameFromMetaComponentWithNonStringValue() {
|
||||||
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
|
assertGeneratedNameIsDefault(ComponentFromNonStringMeta.class);
|
||||||
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(ComponentFromNonStringMeta.class);
|
|
||||||
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
|
|
||||||
assertThat(beanName).isEqualTo("annotationBeanNameGeneratorTests.ComponentFromNonStringMeta");
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test // SPR-11360
|
||||||
void generateBeanNameFromComposedControllerAnnotationWithoutName() {
|
void generateBeanNameFromComposedControllerAnnotationWithoutName() {
|
||||||
// SPR-11360
|
assertGeneratedNameIsDefault(ComposedControllerAnnotationWithoutName.class);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test // SPR-11360
|
||||||
void generateBeanNameFromComposedControllerAnnotationWithBlankName() {
|
void generateBeanNameFromComposedControllerAnnotationWithBlankName() {
|
||||||
// SPR-11360
|
assertGeneratedNameIsDefault(ComposedControllerAnnotationWithBlankName.class);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test // SPR-11360
|
||||||
void generateBeanNameFromComposedControllerAnnotationWithStringValue() {
|
void generateBeanNameFromComposedControllerAnnotationWithStringValue() {
|
||||||
// SPR-11360
|
assertGeneratedName(ComposedControllerAnnotationWithStringValue.class, "restController");
|
||||||
BeanDefinitionRegistry registry = new SimpleBeanDefinitionRegistry();
|
}
|
||||||
AnnotatedBeanDefinition bd = new AnnotatedGenericBeanDefinition(
|
|
||||||
ComposedControllerAnnotationWithStringValue.class);
|
|
||||||
String beanName = this.beanNameGenerator.generateBeanName(bd, registry);
|
private void assertGeneratedName(Class<?> clazz, String expectedName) {
|
||||||
assertThat(beanName).isEqualTo("restController");
|
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…
Reference in New Issue