Skip to content

Commit 9a52294

Browse files
committed
Backport tests for gh-23985
1 parent 268d029 commit 9a52294

File tree

1 file changed

+88
-0
lines changed

1 file changed

+88
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2002-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.web.servlet.mvc.annotation;
18+
19+
import java.util.List;
20+
21+
import org.junit.Test;
22+
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.core.Ordered;
26+
import org.springframework.core.annotation.Order;
27+
import org.springframework.mock.web.test.MockServletContext;
28+
import org.springframework.web.bind.annotation.ControllerAdvice;
29+
import org.springframework.web.context.annotation.RequestScope;
30+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
31+
import org.springframework.web.method.ControllerAdviceBean;
32+
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
33+
34+
import static org.junit.Assert.assertEquals;
35+
36+
/**
37+
* Integration tests for request-scoped {@link ControllerAdvice @ControllerAdvice} beans.
38+
*
39+
* @author Sam Brannen
40+
* @since 5.1.12
41+
*/
42+
public class RequestScopedControllerAdviceIntegrationTests {
43+
44+
@Test // gh-23985
45+
public void loadContextWithRequestScopedControllerAdvice() {
46+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
47+
context.setServletContext(new MockServletContext());
48+
context.register(Config.class);
49+
context.refresh();
50+
51+
// Until gh-24017 is fixed, we expect the RequestScopedControllerAdvice to show up twice.
52+
List<ControllerAdviceBean> adviceBeans = ControllerAdviceBean.findAnnotatedBeans(context);
53+
assertEquals(2, adviceBeans.size());
54+
55+
ControllerAdviceBean adviceBean1 = adviceBeans.get(0);
56+
assertEquals(RequestScopedControllerAdvice.class, adviceBean1.getBeanType());
57+
assertEquals(42, adviceBean1.getOrder());
58+
59+
ControllerAdviceBean adviceBean2 = adviceBeans.get(1);
60+
assertEquals(RequestScopedControllerAdvice.class, adviceBean2.getBeanType());
61+
assertEquals(42, adviceBean2.getOrder());
62+
63+
context.close();
64+
}
65+
66+
67+
@Configuration
68+
@EnableWebMvc
69+
static class Config {
70+
71+
@Bean
72+
@RequestScope
73+
RequestScopedControllerAdvice requestScopedControllerAdvice() {
74+
return new RequestScopedControllerAdvice();
75+
}
76+
}
77+
78+
@ControllerAdvice
79+
@Order(42)
80+
static class RequestScopedControllerAdvice implements Ordered {
81+
82+
@Override
83+
public int getOrder() {
84+
return 99;
85+
}
86+
}
87+
88+
}

0 commit comments

Comments
 (0)