@@ -179,6 +179,73 @@ void shouldApplyOrderFromOrderAttribute() {
179
179
.isEqualTo (ServletConfigurationWithAnnotationAndOrder .ORDER ));
180
180
}
181
181
182
+ @ Test
183
+ @ SuppressWarnings ("unchecked" )
184
+ void shouldApplyExtendedServletRegistrationAnnotation () {
185
+ load (ServletConfigurationWithExtendedAttributes .class );
186
+ // Grab all initializers in the context, including beans that are adapted from @ServletRegistration
187
+ ServletContextInitializerBeans initializerBeans = new ServletContextInitializerBeans (
188
+ this .context .getBeanFactory (), TestServletContextInitializer .class );
189
+
190
+ // We expect two registrations in this config: 'testServletWithInitParametersAndMultipart'
191
+ // and 'testServletWithExtraBean'. So let's filter them individually or pick the one we want to assert.
192
+
193
+ // 1) Check the one with initParameters + multipartConfig
194
+ ServletRegistrationBean <?> bean = findServletRegistrationBeanByName (initializerBeans , "extended" );
195
+ assertThat (bean ).as ("extended servlet registration bean" ).isNotNull ();
196
+
197
+ // Verify that the standard attributes were applied
198
+ assertThat (bean .getServletName ()).isEqualTo ("extended" );
199
+ assertThat (bean .getUrlMappings ()).containsExactly ("/extended/*" );
200
+
201
+ // Verify our new initParameters
202
+ assertThat (bean .getInitParameters ()).containsEntry ("hello" , "world" )
203
+ .containsEntry ("flag" , "true" );
204
+
205
+ // Verify multi-part config
206
+ assertThat (bean .getMultipartConfig ()).isNotNull ();
207
+ assertThat (bean .getMultipartConfig ().getLocation ()).isEqualTo ("/tmp" );
208
+ assertThat (bean .getMultipartConfig ().getMaxFileSize ()).isEqualTo (1024 );
209
+ assertThat (bean .getMultipartConfig ().getMaxRequestSize ()).isEqualTo (4096 );
210
+ assertThat (bean .getMultipartConfig ().getFileSizeThreshold ()).isEqualTo (128 );
211
+ }
212
+
213
+ @ Test
214
+ @ SuppressWarnings ("unchecked" )
215
+ void shouldApplyServletRegistrationAnnotationWithExtraRegistrationBeans () {
216
+ load (ServletConfigurationWithExtendedAttributes .class );
217
+ ServletContextInitializerBeans initializerBeans = new ServletContextInitializerBeans (
218
+ this .context .getBeanFactory (), TestServletContextInitializer .class );
219
+
220
+ // 2) Check the one referencing 'servletRegistrationBeans'
221
+ ServletRegistrationBean <?> bean = findServletRegistrationBeanByName (initializerBeans , "extendedWithExtraBeans" );
222
+ assertThat (bean ).as ("extendedWithExtraBeans registration bean" ).isNotNull ();
223
+
224
+ // Confirm standard attributes
225
+ assertThat (bean .getServletName ()).isEqualTo ("extendedWithExtraBeans" );
226
+ assertThat (bean .getUrlMappings ()).containsExactly ("/extra/*" );
227
+
228
+ // Confirm that the extra init param from MyExtraServletRegistrationBean was merged
229
+ assertThat (bean .getInitParameters ()).containsEntry ("extra" , "fromExtraBean" );
230
+ }
231
+
232
+ /**
233
+ * Simple helper method to locate a specific ServletRegistrationBean by its name
234
+ * from the given ServletContextInitializerBeans collection.
235
+ */
236
+ @ SuppressWarnings ("rawtypes" )
237
+ private ServletRegistrationBean findServletRegistrationBeanByName (
238
+ ServletContextInitializerBeans initializerBeans , String servletName ) {
239
+
240
+ return initializerBeans .stream ()
241
+ .filter (ServletRegistrationBean .class ::isInstance )
242
+ .map (ServletRegistrationBean .class ::cast )
243
+ .filter ((registrationBean ) -> servletName .equals (registrationBean .getServletName ()))
244
+ .findFirst ()
245
+ .orElse (null );
246
+ }
247
+
248
+
182
249
private void load (Class <?>... configuration ) {
183
250
this .context = new AnnotationConfigApplicationContext (configuration );
184
251
}
@@ -385,4 +452,50 @@ public void onStartup(ServletContext servletContext) {
385
452
386
453
}
387
454
455
+ @ Configuration (proxyBeanMethods = false )
456
+ static class ServletConfigurationWithExtendedAttributes {
457
+
458
+ @ Bean
459
+ @ ServletRegistration (
460
+ name = "extended" ,
461
+ urlMappings = "/extended/*" ,
462
+ initParameters = { "hello=world" , "flag=true" },
463
+ multipartConfig = @ ServletRegistration .MultipartConfigValues (
464
+ location = "/tmp" ,
465
+ maxFileSize = 1024 ,
466
+ maxRequestSize = 4096 ,
467
+ fileSizeThreshold = 128
468
+ )
469
+ )
470
+ TestServlet testServletWithInitParametersAndMultipart () {
471
+ return new TestServlet ();
472
+ }
473
+
474
+ @ Bean
475
+ MyExtraServletRegistrationBean myExtraServletRegistrationBean () {
476
+ MyExtraServletRegistrationBean bean = new MyExtraServletRegistrationBean ();
477
+ bean .addInitParameter ("extra" , "fromExtraBean" );
478
+ return bean ;
479
+ }
480
+
481
+ @ Bean
482
+ @ ServletRegistration (
483
+ name = "extendedWithExtraBeans" ,
484
+ urlMappings = "/extra/*" ,
485
+ servletRegistrationBeans = { MyExtraServletRegistrationBean .class }
486
+ )
487
+ TestServlet testServletWithExtraBean () {
488
+ return new TestServlet ();
489
+ }
490
+
491
+ static class MyExtraServletRegistrationBean extends ServletRegistrationBean <HttpServlet > {
492
+
493
+ MyExtraServletRegistrationBean () {
494
+ super ();
495
+ }
496
+
497
+ }
498
+ }
499
+
500
+
388
501
}
0 commit comments