Skip to content

Add tests for ControllerAdviceBean#resolveBean() and revise existing tests #33401

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -88,48 +88,51 @@ void shouldFailWhenControllerAdviceNull() {
}

@Test
void equalsHashCodeAndToStringForBeanName() {
String beanName = SimpleControllerAdvice.class.getSimpleName();
ControllerAdviceBean bean1 = createControllerAdviceBean(SimpleControllerAdvice.class);
ControllerAdviceBean bean2 = createControllerAdviceBean(SimpleControllerAdvice.class);
void equalsHashCodeAndToString() {
String beanName = getSingletonBeanName(SimpleControllerAdvice.class);
ControllerAdviceBean bean1 = createSingletonControllerAdviceBean(SimpleControllerAdvice.class);
ControllerAdviceBean bean2 = createSingletonControllerAdviceBean(SimpleControllerAdvice.class);
assertEqualsHashCodeAndToString(bean1, bean2, beanName);
}

@Test
void orderedWithLowestPrecedenceByDefaultForBeanName() {
void orderedWithLowestPrecedenceByDefault() {
assertOrder(SimpleControllerAdvice.class, Ordered.LOWEST_PRECEDENCE);
}

@Test
void orderedWithLowestPrecedenceByDefaultForBeanInstance() {
assertOrder(SimpleControllerAdvice.class, Ordered.LOWEST_PRECEDENCE);
}

@Test
void orderedViaOrderedInterfaceForBeanName() {
void orderedViaOrderedInterface() {
assertOrder(OrderedControllerAdvice.class, 42);
}

@Test
void orderedViaOrderedInterfaceForBeanInstance() {
assertOrder(OrderedControllerAdvice.class, 42);
void orderedViaAnnotation() {
assertOrder(OrderAnnotationControllerAdvice.class, 100);
assertOrder(PriorityAnnotationControllerAdvice.class, 200);
}

@Test
void orderedViaAnnotationForBeanName() {
assertOrder(OrderAnnotationControllerAdvice.class, 100);
assertOrder(PriorityAnnotationControllerAdvice.class, 200);
void resolveBeanForSingletonBean() {
ControllerAdviceBean cab = createSingletonControllerAdviceBean(SimpleControllerAdvice.class);
Object bean = this.applicationContext.getBean(getSingletonBeanName(SimpleControllerAdvice.class));
assertThat(cab).extracting("resolvedBean").isNull();
Object resolvedBean = cab.resolveBean();
assertThat(cab).extracting("resolvedBean").isEqualTo(bean);
assertThat(resolvedBean).isEqualTo(bean);
}

@Test
void orderedViaAnnotationForBeanInstance() {
assertOrder(OrderAnnotationControllerAdvice.class, 100);
assertOrder(PriorityAnnotationControllerAdvice.class, 200);
void resolveBeanForNonSingletonBean() {
ControllerAdviceBean cab = createPrototypeControllerAdviceBean(SimpleControllerAdvice.class);
assertThat(cab).extracting("resolvedBean").isNull();
Object resolvedBean = cab.resolveBean();
assertThat(cab).extracting("resolvedBean").isNull();
assertThat(resolvedBean).isInstanceOf(SimpleControllerAdvice.class);
}

@Test
void shouldMatchAll() {
ControllerAdviceBean bean = createControllerAdviceBean(SimpleControllerAdvice.class);
ControllerAdviceBean bean = createSingletonControllerAdviceBean(SimpleControllerAdvice.class);
assertApplicable("should match all", bean, AnnotatedController.class);
assertApplicable("should match all", bean, ImplementationController.class);
assertApplicable("should match all", bean, InheritanceController.class);
Expand All @@ -138,7 +141,7 @@ void shouldMatchAll() {

@Test
void basePackageSupport() {
ControllerAdviceBean bean = createControllerAdviceBean(BasePackageSupport.class);
ControllerAdviceBean bean = createSingletonControllerAdviceBean(BasePackageSupport.class);
assertApplicable("base package support", bean, AnnotatedController.class);
assertApplicable("base package support", bean, ImplementationController.class);
assertApplicable("base package support", bean, InheritanceController.class);
Expand All @@ -147,7 +150,7 @@ void basePackageSupport() {

@Test
void basePackageValueSupport() {
ControllerAdviceBean bean = createControllerAdviceBean(BasePackageValueSupport.class);
ControllerAdviceBean bean = createSingletonControllerAdviceBean(BasePackageValueSupport.class);
assertApplicable("base package support", bean, AnnotatedController.class);
assertApplicable("base package support", bean, ImplementationController.class);
assertApplicable("base package support", bean, InheritanceController.class);
Expand All @@ -156,14 +159,14 @@ void basePackageValueSupport() {

@Test
void annotationSupport() {
ControllerAdviceBean bean = createControllerAdviceBean(AnnotationSupport.class);
ControllerAdviceBean bean = createSingletonControllerAdviceBean(AnnotationSupport.class);
assertApplicable("annotation support", bean, AnnotatedController.class);
assertNotApplicable("this bean is not annotated", bean, InheritanceController.class);
}

@Test
void markerClassSupport() {
ControllerAdviceBean bean = createControllerAdviceBean(MarkerClassSupport.class);
ControllerAdviceBean bean = createSingletonControllerAdviceBean(MarkerClassSupport.class);
assertApplicable("base package class support", bean, AnnotatedController.class);
assertApplicable("base package class support", bean, ImplementationController.class);
assertApplicable("base package class support", bean, InheritanceController.class);
Expand All @@ -172,7 +175,7 @@ void markerClassSupport() {

@Test
void shouldNotMatch() {
ControllerAdviceBean bean = createControllerAdviceBean(ShouldNotMatch.class);
ControllerAdviceBean bean = createSingletonControllerAdviceBean(ShouldNotMatch.class);
assertNotApplicable("should not match", bean, AnnotatedController.class);
assertNotApplicable("should not match", bean, ImplementationController.class);
assertNotApplicable("should not match", bean, InheritanceController.class);
Expand All @@ -181,7 +184,7 @@ void shouldNotMatch() {

@Test
void assignableTypesSupport() {
ControllerAdviceBean bean = createControllerAdviceBean(AssignableTypesSupport.class);
ControllerAdviceBean bean = createSingletonControllerAdviceBean(AssignableTypesSupport.class);
assertApplicable("controller implements assignable", bean, ImplementationController.class);
assertApplicable("controller inherits assignable", bean, InheritanceController.class);
assertNotApplicable("not assignable", bean, AnnotatedController.class);
Expand All @@ -190,7 +193,7 @@ void assignableTypesSupport() {

@Test
void multipleMatch() {
ControllerAdviceBean bean = createControllerAdviceBean(MultipleSelectorsSupport.class);
ControllerAdviceBean bean = createSingletonControllerAdviceBean(MultipleSelectorsSupport.class);
assertApplicable("controller implements assignable", bean, ImplementationController.class);
assertApplicable("controller is annotated", bean, AnnotatedController.class);
assertNotApplicable("should not match", bean, InheritanceController.class);
Expand All @@ -216,13 +219,28 @@ public void findAnnotatedBeansSortsBeans() {
assertThat(adviceBeans).extracting(ControllerAdviceBean::getBeanType).containsExactly(expectedTypes);
}

private ControllerAdviceBean createControllerAdviceBean(Class<?> beanType) {
String beanName = beanType.getSimpleName();
private ControllerAdviceBean createSingletonControllerAdviceBean(Class<?> beanType) {
String beanName = getSingletonBeanName(beanType);
this.applicationContext.registerSingleton(beanName, beanType);
ControllerAdvice controllerAdvice = AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class);
return new ControllerAdviceBean(beanName, this.applicationContext, controllerAdvice);
}

private static String getSingletonBeanName(Class<?> beanType) {
return beanType.getSimpleName() + "Singleton";
}

private ControllerAdviceBean createPrototypeControllerAdviceBean(Class<?> beanType) {
String beanName = getPrototypeBeanName(beanType);
this.applicationContext.registerPrototype(beanName, beanType);
ControllerAdvice controllerAdvice = AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class);
return new ControllerAdviceBean(beanName, this.applicationContext, controllerAdvice);
}

private static String getPrototypeBeanName(Class<?> beanType) {
return beanType.getSimpleName() + "Prototype";
}

private void assertEqualsHashCodeAndToString(ControllerAdviceBean bean1, ControllerAdviceBean bean2, String toString) {
assertThat(bean1).isEqualTo(bean2);
assertThat(bean2).isEqualTo(bean1);
Expand All @@ -232,7 +250,7 @@ private void assertEqualsHashCodeAndToString(ControllerAdviceBean bean1, Control
}

private void assertOrder(Class<?> beanType, int expectedOrder) {
assertThat(createControllerAdviceBean(beanType).getOrder()).isEqualTo(expectedOrder);
assertThat(createSingletonControllerAdviceBean(beanType).getOrder()).isEqualTo(expectedOrder);
}

private void assertApplicable(String message, ControllerAdviceBean controllerAdvice, Class<?> controllerBeanType) {
Expand Down