20
20
import java .util .Arrays ;
21
21
import java .util .Collections ;
22
22
import java .util .EnumSet ;
23
+ import java .util .HashMap ;
23
24
import java .util .List ;
24
25
import java .util .Map ;
25
26
import java .util .concurrent .CountDownLatch ;
@@ -66,15 +67,18 @@ void customizeShouldAddFilters() {
66
67
DefaultMockMvcBuilder builder = MockMvcBuilders .webAppContextSetup (context );
67
68
SpringBootMockMvcBuilderCustomizer customizer = new SpringBootMockMvcBuilderCustomizer (context );
68
69
customizer .customize (builder );
69
- FilterRegistrationBean <?> registrationBean = (FilterRegistrationBean <?>) context
70
- .getBean ("filterRegistrationBean" );
71
- Filter testFilter = (Filter ) context .getBean ("testFilter" );
72
- Filter otherTestFilter = registrationBean .getFilter ();
70
+ FilterRegistrationBean <?> registrationBean = (FilterRegistrationBean <?>) context .getBean ("otherTestFilter" );
71
+ TestFilter testFilter = context .getBean ("testFilter" , TestFilter .class );
72
+ OtherTestFilter otherTestFilter = (OtherTestFilter ) registrationBean .getFilter ();
73
73
assertThat (builder ).extracting ("filters" , as (InstanceOfAssertFactories .LIST ))
74
- .extracting ("delegate" , "initParams" , "dispatcherTypes" )
75
- .containsExactlyInAnyOrder (tuple (testFilter , Collections .emptyMap (), EnumSet .of (DispatcherType .REQUEST )),
76
- tuple (otherTestFilter , Map .of ("a" , "alpha" , "b" , "bravo" ),
77
- EnumSet .of (DispatcherType .REQUEST , DispatcherType .ERROR )));
74
+ .extracting ("delegate" , "dispatcherTypes" )
75
+ .containsExactlyInAnyOrder (tuple (testFilter , EnumSet .of (DispatcherType .REQUEST )),
76
+ tuple (otherTestFilter , EnumSet .of (DispatcherType .REQUEST , DispatcherType .ERROR )));
77
+ builder .build ();
78
+ assertThat (testFilter .filterName ).isEqualTo ("testFilter" );
79
+ assertThat (testFilter .initParams ).isEmpty ();
80
+ assertThat (otherTestFilter .filterName ).isEqualTo ("otherTestFilter" );
81
+ assertThat (otherTestFilter .initParams ).isEqualTo (Map .of ("a" , "alpha" , "b" , "bravo" ));
78
82
}
79
83
80
84
@ Test
@@ -137,7 +141,7 @@ TestServlet testServlet() {
137
141
static class FilterConfiguration {
138
142
139
143
@ Bean
140
- FilterRegistrationBean <OtherTestFilter > filterRegistrationBean () {
144
+ FilterRegistrationBean <OtherTestFilter > otherTestFilter () {
141
145
FilterRegistrationBean <OtherTestFilter > filterRegistrationBean = new FilterRegistrationBean <>(
142
146
new OtherTestFilter ());
143
147
filterRegistrationBean .setInitParameters (Map .of ("a" , "alpha" , "b" , "bravo" ));
@@ -158,9 +162,15 @@ static class TestServlet extends HttpServlet {
158
162
159
163
static class TestFilter implements Filter {
160
164
165
+ private String filterName ;
166
+
167
+ private Map <String , String > initParams = new HashMap <>();
168
+
161
169
@ Override
162
170
public void init (FilterConfig filterConfig ) {
163
-
171
+ this .filterName = filterConfig .getFilterName ();
172
+ Collections .list (filterConfig .getInitParameterNames ())
173
+ .forEach ((name ) -> this .initParams .put (name , filterConfig .getInitParameter (name )));
164
174
}
165
175
166
176
@ Override
@@ -177,9 +187,15 @@ public void destroy() {
177
187
178
188
static class OtherTestFilter implements Filter {
179
189
190
+ private String filterName ;
191
+
192
+ private Map <String , String > initParams = new HashMap <>();
193
+
180
194
@ Override
181
195
public void init (FilterConfig filterConfig ) {
182
-
196
+ this .filterName = filterConfig .getFilterName ();
197
+ Collections .list (filterConfig .getInitParameterNames ())
198
+ .forEach ((name ) -> this .initParams .put (name , filterConfig .getInitParameter (name )));
183
199
}
184
200
185
201
@ Override
0 commit comments