diff --git a/spring-web/src/test/java/org/springframework/web/method/ControllerAdviceBeanTests.java b/spring-web/src/test/java/org/springframework/web/method/ControllerAdviceBeanTests.java index c2f4751310..efe329f826 100644 --- a/spring-web/src/test/java/org/springframework/web/method/ControllerAdviceBeanTests.java +++ b/spring-web/src/test/java/org/springframework/web/method/ControllerAdviceBeanTests.java @@ -18,19 +18,93 @@ package org.springframework.web.method; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; +import javax.annotation.Priority; import org.junit.Test; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.core.Ordered; +import org.springframework.core.annotation.Order; import org.springframework.web.bind.annotation.ControllerAdvice; import org.springframework.web.bind.annotation.RestController; import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; +import static org.mockito.BDDMockito.given; +import static org.mockito.Mockito.mock; /** + * Unit tests for {@link ControllerAdviceBean}. + * * @author Brian Clozel + * @author Sam Brannen */ public class ControllerAdviceBeanTests { + @Test + public void constructorPreconditions() { + assertThatIllegalArgumentException() + .isThrownBy(() -> new ControllerAdviceBean(null)) + .withMessage("Bean must not be null"); + + assertThatIllegalArgumentException() + .isThrownBy(() -> new ControllerAdviceBean((String) null, null)) + .withMessage("Bean must not be null"); + + assertThatIllegalArgumentException() + .isThrownBy(() -> new ControllerAdviceBean("", null)) + .withMessage("Bean name must not be empty"); + + assertThatIllegalArgumentException() + .isThrownBy(() -> new ControllerAdviceBean("\t", null)) + .withMessage("Bean name must not be empty"); + + assertThatIllegalArgumentException() + .isThrownBy(() -> new ControllerAdviceBean("myBean", null)) + .withMessage("BeanFactory must not be null"); + } + + @Test + public void equalsHashCodeAndToStringForBeanName() { + String beanName = "myBean"; + BeanFactory beanFactory = mock(BeanFactory.class); + given(beanFactory.containsBean(beanName)).willReturn(true); + + ControllerAdviceBean bean1 = new ControllerAdviceBean(beanName, beanFactory); + ControllerAdviceBean bean2 = new ControllerAdviceBean(beanName, beanFactory); + assertEqualsHashCodeAndToString(bean1, bean2, beanName); + } + + @Test + public void equalsHashCodeAndToStringForBeanInstance() { + String toString = "beanInstance"; + Object beanInstance = new Object() { + @Override + public String toString() { + return toString; + } + }; + ControllerAdviceBean bean1 = new ControllerAdviceBean(beanInstance); + ControllerAdviceBean bean2 = new ControllerAdviceBean(beanInstance); + assertEqualsHashCodeAndToString(bean1, bean2, toString); + } + + @Test + public void orderedWithLowestPrecedenceByDefaultForBeanInstance() { + assertOrder(new SimpleControllerAdvice(), Ordered.LOWEST_PRECEDENCE); + } + + @Test + public void orderedViaAnnotationForBeanInstance() { + assertOrder(new OrderAnnotationControllerAdvice(), 42); + assertOrder(new PriorityAnnotationControllerAdvice(), 42); + } + + @Test + public void orderedViaOrderedInterfaceForBeanInstance() { + assertOrder(new OrderedControllerAdvice(), 42); + } + @Test public void shouldMatchAll() { ControllerAdviceBean bean = new ControllerAdviceBean(new SimpleControllerAdvice()); @@ -100,6 +174,18 @@ public class ControllerAdviceBeanTests { assertNotApplicable("should not match", bean, InheritanceController.class); } + private void assertEqualsHashCodeAndToString(ControllerAdviceBean bean1, ControllerAdviceBean bean2, String toString) { + assertThat(bean1).isEqualTo(bean2); + assertThat(bean2).isEqualTo(bean1); + assertThat(bean1.hashCode()).isEqualTo(bean2.hashCode()); + assertThat(bean1.toString()).isEqualTo(toString); + assertThat(bean2.toString()).isEqualTo(toString); + } + + private void assertOrder(Object bean, int expectedOrder) { + assertThat(new ControllerAdviceBean(bean).getOrder()).isEqualTo(expectedOrder); + } + private void assertApplicable(String message, ControllerAdviceBean controllerAdvice, Class controllerBeanType) { assertThat(controllerAdvice).isNotNull(); assertThat(controllerAdvice.isApplicableToBeanType(controllerBeanType)).as(message).isTrue(); @@ -116,6 +202,23 @@ public class ControllerAdviceBeanTests { @ControllerAdvice static class SimpleControllerAdvice {} + @ControllerAdvice + @Order(42) + static class OrderAnnotationControllerAdvice {} + + @ControllerAdvice + @Priority(42) + static class PriorityAnnotationControllerAdvice {} + + @ControllerAdvice + static class OrderedControllerAdvice implements Ordered { + + @Override + public int getOrder() { + return 42; + } + } + @ControllerAdvice(annotations = ControllerAnnotation.class) static class AnnotationSupport {}