Skip to content

Commit 328b721

Browse files
committed
Add ControllerAdviceBean#resolveBean() tests
1 parent e50383e commit 328b721

File tree

1 file changed

+48
-14
lines changed

1 file changed

+48
-14
lines changed

spring-web/src/test/java/org/springframework/web/method/ControllerAdviceBeanTests.java

Lines changed: 48 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -89,9 +89,9 @@ void shouldFailWhenControllerAdviceNull() {
8989

9090
@Test
9191
void equalsHashCodeAndToStringForBeanName() {
92-
String beanName = SimpleControllerAdvice.class.getSimpleName();
93-
ControllerAdviceBean bean1 = createControllerAdviceBean(SimpleControllerAdvice.class);
94-
ControllerAdviceBean bean2 = createControllerAdviceBean(SimpleControllerAdvice.class);
92+
String beanName = getSingletonBeanName(SimpleControllerAdvice.class);
93+
ControllerAdviceBean bean1 = createSingletonControllerAdviceBean(SimpleControllerAdvice.class);
94+
ControllerAdviceBean bean2 = createSingletonControllerAdviceBean(SimpleControllerAdvice.class);
9595
assertEqualsHashCodeAndToString(bean1, bean2, beanName);
9696
}
9797

@@ -127,9 +127,28 @@ void orderedViaAnnotationForBeanInstance() {
127127
assertOrder(PriorityAnnotationControllerAdvice.class, 200);
128128
}
129129

130+
@Test
131+
void resolveBeanForSingletonBean() {
132+
ControllerAdviceBean cab = createSingletonControllerAdviceBean(SimpleControllerAdvice.class);
133+
Object bean = this.applicationContext.getBean(getSingletonBeanName(SimpleControllerAdvice.class));
134+
assertThat(cab).extracting("resolvedBean").isNull();
135+
Object resolvedBean = cab.resolveBean();
136+
assertThat(cab).extracting("resolvedBean").isEqualTo(bean);
137+
assertThat(resolvedBean).isEqualTo(bean);
138+
}
139+
140+
@Test
141+
void resolveBeanForNonSingletonBean() {
142+
ControllerAdviceBean cab = createPrototypeControllerAdviceBean(SimpleControllerAdvice.class);
143+
assertThat(cab).extracting("resolvedBean").isNull();
144+
Object resolvedBean = cab.resolveBean();
145+
assertThat(cab).extracting("resolvedBean").isNull();
146+
assertThat(resolvedBean).isInstanceOf(SimpleControllerAdvice.class);
147+
}
148+
130149
@Test
131150
void shouldMatchAll() {
132-
ControllerAdviceBean bean = createControllerAdviceBean(SimpleControllerAdvice.class);
151+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(SimpleControllerAdvice.class);
133152
assertApplicable("should match all", bean, AnnotatedController.class);
134153
assertApplicable("should match all", bean, ImplementationController.class);
135154
assertApplicable("should match all", bean, InheritanceController.class);
@@ -138,7 +157,7 @@ void shouldMatchAll() {
138157

139158
@Test
140159
void basePackageSupport() {
141-
ControllerAdviceBean bean = createControllerAdviceBean(BasePackageSupport.class);
160+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(BasePackageSupport.class);
142161
assertApplicable("base package support", bean, AnnotatedController.class);
143162
assertApplicable("base package support", bean, ImplementationController.class);
144163
assertApplicable("base package support", bean, InheritanceController.class);
@@ -147,7 +166,7 @@ void basePackageSupport() {
147166

148167
@Test
149168
void basePackageValueSupport() {
150-
ControllerAdviceBean bean = createControllerAdviceBean(BasePackageValueSupport.class);
169+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(BasePackageValueSupport.class);
151170
assertApplicable("base package support", bean, AnnotatedController.class);
152171
assertApplicable("base package support", bean, ImplementationController.class);
153172
assertApplicable("base package support", bean, InheritanceController.class);
@@ -156,14 +175,14 @@ void basePackageValueSupport() {
156175

157176
@Test
158177
void annotationSupport() {
159-
ControllerAdviceBean bean = createControllerAdviceBean(AnnotationSupport.class);
178+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(AnnotationSupport.class);
160179
assertApplicable("annotation support", bean, AnnotatedController.class);
161180
assertNotApplicable("this bean is not annotated", bean, InheritanceController.class);
162181
}
163182

164183
@Test
165184
void markerClassSupport() {
166-
ControllerAdviceBean bean = createControllerAdviceBean(MarkerClassSupport.class);
185+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(MarkerClassSupport.class);
167186
assertApplicable("base package class support", bean, AnnotatedController.class);
168187
assertApplicable("base package class support", bean, ImplementationController.class);
169188
assertApplicable("base package class support", bean, InheritanceController.class);
@@ -172,7 +191,7 @@ void markerClassSupport() {
172191

173192
@Test
174193
void shouldNotMatch() {
175-
ControllerAdviceBean bean = createControllerAdviceBean(ShouldNotMatch.class);
194+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(ShouldNotMatch.class);
176195
assertNotApplicable("should not match", bean, AnnotatedController.class);
177196
assertNotApplicable("should not match", bean, ImplementationController.class);
178197
assertNotApplicable("should not match", bean, InheritanceController.class);
@@ -181,7 +200,7 @@ void shouldNotMatch() {
181200

182201
@Test
183202
void assignableTypesSupport() {
184-
ControllerAdviceBean bean = createControllerAdviceBean(AssignableTypesSupport.class);
203+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(AssignableTypesSupport.class);
185204
assertApplicable("controller implements assignable", bean, ImplementationController.class);
186205
assertApplicable("controller inherits assignable", bean, InheritanceController.class);
187206
assertNotApplicable("not assignable", bean, AnnotatedController.class);
@@ -190,7 +209,7 @@ void assignableTypesSupport() {
190209

191210
@Test
192211
void multipleMatch() {
193-
ControllerAdviceBean bean = createControllerAdviceBean(MultipleSelectorsSupport.class);
212+
ControllerAdviceBean bean = createSingletonControllerAdviceBean(MultipleSelectorsSupport.class);
194213
assertApplicable("controller implements assignable", bean, ImplementationController.class);
195214
assertApplicable("controller is annotated", bean, AnnotatedController.class);
196215
assertNotApplicable("should not match", bean, InheritanceController.class);
@@ -216,13 +235,28 @@ public void findAnnotatedBeansSortsBeans() {
216235
assertThat(adviceBeans).extracting(ControllerAdviceBean::getBeanType).containsExactly(expectedTypes);
217236
}
218237

219-
private ControllerAdviceBean createControllerAdviceBean(Class<?> beanType) {
220-
String beanName = beanType.getSimpleName();
238+
private ControllerAdviceBean createSingletonControllerAdviceBean(Class<?> beanType) {
239+
String beanName = getSingletonBeanName(beanType);
221240
this.applicationContext.registerSingleton(beanName, beanType);
222241
ControllerAdvice controllerAdvice = AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class);
223242
return new ControllerAdviceBean(beanName, this.applicationContext, controllerAdvice);
224243
}
225244

245+
private static String getSingletonBeanName(Class<?> beanType) {
246+
return beanType.getSimpleName() + "Singleton";
247+
}
248+
249+
private ControllerAdviceBean createPrototypeControllerAdviceBean(Class<?> beanType) {
250+
String beanName = getPrototypeBeanName(beanType);
251+
this.applicationContext.registerPrototype(beanName, beanType);
252+
ControllerAdvice controllerAdvice = AnnotatedElementUtils.findMergedAnnotation(beanType, ControllerAdvice.class);
253+
return new ControllerAdviceBean(beanName, this.applicationContext, controllerAdvice);
254+
}
255+
256+
private static String getPrototypeBeanName(Class<?> beanType) {
257+
return beanType.getSimpleName() + "Prototype";
258+
}
259+
226260
private void assertEqualsHashCodeAndToString(ControllerAdviceBean bean1, ControllerAdviceBean bean2, String toString) {
227261
assertThat(bean1).isEqualTo(bean2);
228262
assertThat(bean2).isEqualTo(bean1);
@@ -232,7 +266,7 @@ private void assertEqualsHashCodeAndToString(ControllerAdviceBean bean1, Control
232266
}
233267

234268
private void assertOrder(Class<?> beanType, int expectedOrder) {
235-
assertThat(createControllerAdviceBean(beanType).getOrder()).isEqualTo(expectedOrder);
269+
assertThat(createSingletonControllerAdviceBean(beanType).getOrder()).isEqualTo(expectedOrder);
236270
}
237271

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

0 commit comments

Comments
 (0)