|
16 | 16 |
|
17 | 17 | package org.springframework.boot.web.servlet;
|
18 | 18 |
|
| 19 | +import java.util.Collection; |
19 | 20 | import java.util.EnumSet;
|
| 21 | +import java.util.Map; |
20 | 22 |
|
21 | 23 | import jakarta.servlet.DispatcherType;
|
22 | 24 | import jakarta.servlet.Filter;
|
|
25 | 27 | import jakarta.servlet.ServletContext;
|
26 | 28 | import jakarta.servlet.ServletRequest;
|
27 | 29 | import jakarta.servlet.ServletResponse;
|
| 30 | +import jakarta.servlet.annotation.WebInitParam; |
28 | 31 | import jakarta.servlet.http.HttpServlet;
|
29 | 32 | import jakarta.servlet.http.HttpServletRequest;
|
30 | 33 | import jakarta.servlet.http.HttpServletResponse;
|
|
47 | 50 | *
|
48 | 51 | * @author Andy Wilkinson
|
49 | 52 | * @author Moritz Halbritter
|
| 53 | + * @author Daeho Kwon |
50 | 54 | */
|
51 | 55 | class ServletContextInitializerBeansTests {
|
52 | 56 |
|
@@ -141,6 +145,14 @@ void shouldApplyFilterRegistrationAnnotation() {
|
141 | 145 | assertThat(filterRegistrationBean.getServletNames()).containsExactly("test");
|
142 | 146 | assertThat(filterRegistrationBean.determineDispatcherTypes()).containsExactly(DispatcherType.ERROR);
|
143 | 147 | assertThat(filterRegistrationBean.getUrlPatterns()).containsExactly("/test/*");
|
| 148 | + assertThat(filterRegistrationBean.getInitParameters()) |
| 149 | + .containsExactlyInAnyOrderEntriesOf(Map.of("env", "test", "debug", "true")); |
| 150 | + Collection<ServletRegistrationBean<?>> servletRegistrationBeans = filterRegistrationBean |
| 151 | + .getServletRegistrationBeans(); |
| 152 | + assertThat(servletRegistrationBeans).hasSize(1); |
| 153 | + assertThat(servletRegistrationBeans.iterator().next().getServletName()) |
| 154 | + .isEqualTo("testServletRegistrationBean"); |
| 155 | + |
144 | 156 | });
|
145 | 157 | }
|
146 | 158 |
|
@@ -294,13 +306,34 @@ TestFilter testFilter() {
|
294 | 306 | static class FilterConfigurationWithAnnotation {
|
295 | 307 |
|
296 | 308 | @Bean
|
297 |
| - @FilterRegistration(enabled = false, name = "test", asyncSupported = false, |
298 |
| - dispatcherTypes = DispatcherType.ERROR, matchAfter = true, servletNames = "test", |
299 |
| - urlPatterns = "/test/*") |
| 309 | + @FilterRegistration( |
| 310 | + enabled = false, name = "test", asyncSupported = false, dispatcherTypes = DispatcherType.ERROR, |
| 311 | + matchAfter = true, servletNames = "test", urlPatterns = "/test/*", initParameters = { |
| 312 | + @WebInitParam(name = "env", value = "test"), @WebInitParam(name = "debug", value = "true") }, |
| 313 | + servletClasses = { TestServlet.class }) |
300 | 314 | TestFilter testFilter() {
|
301 | 315 | return new TestFilter();
|
302 | 316 | }
|
303 | 317 |
|
| 318 | + @Bean |
| 319 | + ServletRegistrationBean<TestServlet> testServletRegistrationBean() { |
| 320 | + return new ServletRegistrationBean<>(new TestServlet()); |
| 321 | + } |
| 322 | + |
| 323 | + @Bean |
| 324 | + ServletRegistrationBean<NonMatchingServlet> nonMatchingServletRegistrationBean() { |
| 325 | + return new ServletRegistrationBean<>(new NonMatchingServlet()); |
| 326 | + } |
| 327 | + |
| 328 | + static class NonMatchingServlet extends HttpServlet implements ServletContextInitializer { |
| 329 | + |
| 330 | + @Override |
| 331 | + public void onStartup(ServletContext servletContext) { |
| 332 | + |
| 333 | + } |
| 334 | + |
| 335 | + } |
| 336 | + |
304 | 337 | }
|
305 | 338 |
|
306 | 339 | @Configuration(proxyBeanMethods = false)
|
|
0 commit comments