|
42 | 42 | *
|
43 | 43 | * @author Andy Wilkinson
|
44 | 44 | * @author Moritz Halbritter
|
| 45 | + * @author Dmytro Danilenkov |
45 | 46 | */
|
46 | 47 | class ServletContextInitializerBeansTests {
|
47 | 48 |
|
@@ -179,6 +180,55 @@ void shouldApplyOrderFromOrderAttribute() {
|
179 | 180 | .isEqualTo(ServletConfigurationWithAnnotationAndOrder.ORDER));
|
180 | 181 | }
|
181 | 182 |
|
| 183 | + @Test |
| 184 | + void shouldApplyExtendedServletRegistrationAnnotation() { |
| 185 | + load(ServletConfigurationWithExtendedAttributes.class); |
| 186 | + ServletContextInitializerBeans initializerBeans = new ServletContextInitializerBeans( |
| 187 | + this.context.getBeanFactory(), TestServletContextInitializer.class); |
| 188 | + |
| 189 | + ServletRegistrationBean<?> bean = findServletRegistrationBeanByName(initializerBeans, "extended"); |
| 190 | + |
| 191 | + assertThat(bean.getServletName()).isEqualTo("extended"); |
| 192 | + assertThat(bean.getUrlMappings()).containsExactly("/extended/*"); |
| 193 | + |
| 194 | + assertThat(bean.getInitParameters()).containsEntry("hello", "world") |
| 195 | + .containsEntry("flag", "true"); |
| 196 | + |
| 197 | + assertThat(bean.getMultipartConfig()).isNotNull(); |
| 198 | + assertThat(bean.getMultipartConfig().getLocation()).isEqualTo("/tmp"); |
| 199 | + assertThat(bean.getMultipartConfig().getMaxFileSize()).isEqualTo(1024); |
| 200 | + assertThat(bean.getMultipartConfig().getMaxRequestSize()).isEqualTo(4096); |
| 201 | + assertThat(bean.getMultipartConfig().getFileSizeThreshold()).isEqualTo(128); |
| 202 | + } |
| 203 | + |
| 204 | + @Test |
| 205 | + void shouldApplyServletRegistrationAnnotationWithExtraRegistrationBeans() { |
| 206 | + load(ServletConfigurationWithExtendedAttributes.class); |
| 207 | + ServletContextInitializerBeans initializerBeans = new ServletContextInitializerBeans( |
| 208 | + this.context.getBeanFactory(), TestServletContextInitializer.class); |
| 209 | + |
| 210 | + ServletRegistrationBean<?> bean = findServletRegistrationBeanByName(initializerBeans, "extendedWithExtraBeans"); |
| 211 | + assertThat(bean).as("extendedWithExtraBeans registration bean").isNotNull(); |
| 212 | + |
| 213 | + assertThat(bean.getServletName()).isEqualTo("extendedWithExtraBeans"); |
| 214 | + assertThat(bean.getUrlMappings()).containsExactly("/extra/*"); |
| 215 | + |
| 216 | + assertThat(bean.getInitParameters()).containsEntry("extra", "fromExtraBean"); |
| 217 | + } |
| 218 | + |
| 219 | + @SuppressWarnings("rawtypes") |
| 220 | + private ServletRegistrationBean findServletRegistrationBeanByName( |
| 221 | + ServletContextInitializerBeans initializerBeans, String servletName) { |
| 222 | + |
| 223 | + return initializerBeans.stream() |
| 224 | + .filter(ServletRegistrationBean.class::isInstance) |
| 225 | + .map(ServletRegistrationBean.class::cast) |
| 226 | + .filter((registrationBean) -> servletName.equals(registrationBean.getServletName())) |
| 227 | + .findFirst() |
| 228 | + .orElse(null); |
| 229 | + } |
| 230 | + |
| 231 | + |
182 | 232 | private void load(Class<?>... configuration) {
|
183 | 233 | this.context = new AnnotationConfigApplicationContext(configuration);
|
184 | 234 | }
|
@@ -385,4 +435,50 @@ public void onStartup(ServletContext servletContext) {
|
385 | 435 |
|
386 | 436 | }
|
387 | 437 |
|
| 438 | + @Configuration(proxyBeanMethods = false) |
| 439 | + static class ServletConfigurationWithExtendedAttributes { |
| 440 | + |
| 441 | + @Bean |
| 442 | + @ServletRegistration( |
| 443 | + name = "extended", |
| 444 | + urlMappings = "/extended/*", |
| 445 | + initParameters = { "hello=world", "flag=true" }, |
| 446 | + multipartConfig = @ServletRegistration.MultipartConfigValues( |
| 447 | + location = "/tmp", |
| 448 | + maxFileSize = 1024, |
| 449 | + maxRequestSize = 4096, |
| 450 | + fileSizeThreshold = 128 |
| 451 | + ) |
| 452 | + ) |
| 453 | + TestServlet testServletWithInitParametersAndMultipart() { |
| 454 | + return new TestServlet(); |
| 455 | + } |
| 456 | + |
| 457 | + @Bean |
| 458 | + MyExtraServletRegistrationBean myExtraServletRegistrationBean() { |
| 459 | + MyExtraServletRegistrationBean bean = new MyExtraServletRegistrationBean(); |
| 460 | + bean.addInitParameter("extra", "fromExtraBean"); |
| 461 | + return bean; |
| 462 | + } |
| 463 | + |
| 464 | + @Bean |
| 465 | + @ServletRegistration( |
| 466 | + name = "extendedWithExtraBeans", |
| 467 | + urlMappings = "/extra/*", |
| 468 | + servletRegistrationBeans = { MyExtraServletRegistrationBean.class } |
| 469 | + ) |
| 470 | + TestServlet testServletWithExtraBean() { |
| 471 | + return new TestServlet(); |
| 472 | + } |
| 473 | + |
| 474 | + static class MyExtraServletRegistrationBean extends ServletRegistrationBean<HttpServlet> { |
| 475 | + |
| 476 | + MyExtraServletRegistrationBean() { |
| 477 | + super(); |
| 478 | + } |
| 479 | + |
| 480 | + } |
| 481 | + } |
| 482 | + |
| 483 | + |
388 | 484 | }
|
0 commit comments