16
16
17
17
package org .springframework .security .config .annotation .web ;
18
18
19
- import java .lang .reflect .Field ;
20
- import java .lang .reflect .Modifier ;
21
19
import java .util .List ;
22
20
23
21
import jakarta .servlet .DispatcherType ;
43
41
* Tests for {@link AbstractRequestMatcherRegistry}.
44
42
*
45
43
* @author Joe Grandja
44
+ * @author Marcus Da Coregio
46
45
*/
47
46
public class AbstractRequestMatcherRegistryTests {
48
47
@@ -61,6 +60,7 @@ public void setUp() {
61
60
ApplicationContext context = mock (ApplicationContext .class );
62
61
given (context .getBean (ObjectPostProcessor .class )).willReturn (NO_OP_OBJECT_POST_PROCESSOR );
63
62
this .matcherRegistry .setApplicationContext (context );
63
+ mockMvcIntrospector (true );
64
64
}
65
65
66
66
@ Test
@@ -113,70 +113,31 @@ public void dispatcherMatchersWhenPatternParamThenReturnAntPathRequestMatcherTyp
113
113
}
114
114
115
115
@ Test
116
- public void requestMatchersWhenPatternAndMvcPresentThenReturnMvcRequestMatcherType () throws Exception {
117
- mockMvcPresentClasspath (true );
118
- mockMvcIntrospector (true );
116
+ public void requestMatchersWhenPatternAndMvcPresentThenReturnMvcRequestMatcherType () {
119
117
List <RequestMatcher > requestMatchers = this .matcherRegistry .requestMatchers ("/path" );
120
118
assertThat (requestMatchers ).isNotEmpty ();
121
119
assertThat (requestMatchers .size ()).isEqualTo (1 );
122
120
assertThat (requestMatchers .get (0 )).isExactlyInstanceOf (MvcRequestMatcher .class );
123
121
}
124
122
125
123
@ Test
126
- public void requestMatchersWhenHttpMethodAndPatternAndMvcPresentThenReturnMvcRequestMatcherType () throws Exception {
127
- mockMvcPresentClasspath (true );
128
- mockMvcIntrospector (true );
124
+ public void requestMatchersWhenHttpMethodAndPatternAndMvcPresentThenReturnMvcRequestMatcherType () {
129
125
List <RequestMatcher > requestMatchers = this .matcherRegistry .requestMatchers (HttpMethod .GET , "/path" );
130
126
assertThat (requestMatchers ).isNotEmpty ();
131
127
assertThat (requestMatchers .size ()).isEqualTo (1 );
132
128
assertThat (requestMatchers .get (0 )).isExactlyInstanceOf (MvcRequestMatcher .class );
133
129
}
134
130
135
131
@ Test
136
- public void requestMatchersWhenHttpMethodAndMvcPresentThenReturnMvcRequestMatcherType () throws Exception {
137
- mockMvcPresentClasspath (true );
138
- mockMvcIntrospector (true );
132
+ public void requestMatchersWhenHttpMethodAndMvcPresentThenReturnMvcRequestMatcherType () {
139
133
List <RequestMatcher > requestMatchers = this .matcherRegistry .requestMatchers (HttpMethod .GET );
140
134
assertThat (requestMatchers ).isNotEmpty ();
141
135
assertThat (requestMatchers .size ()).isEqualTo (1 );
142
136
assertThat (requestMatchers .get (0 )).isExactlyInstanceOf (MvcRequestMatcher .class );
143
137
}
144
138
145
139
@ Test
146
- public void requestMatchersWhenPatternAndMvcNotPresentThenReturnAntPathRequestMatcherType () throws Exception {
147
- mockMvcPresentClasspath (false );
148
- mockMvcIntrospector (false );
149
- List <RequestMatcher > requestMatchers = this .matcherRegistry .requestMatchers ("/path" );
150
- assertThat (requestMatchers ).isNotEmpty ();
151
- assertThat (requestMatchers .size ()).isEqualTo (1 );
152
- assertThat (requestMatchers .get (0 )).isExactlyInstanceOf (AntPathRequestMatcher .class );
153
- }
154
-
155
- @ Test
156
- public void requestMatchersWhenHttpMethodAndPatternAndMvcNotPresentThenReturnAntPathRequestMatcherType ()
157
- throws Exception {
158
- mockMvcPresentClasspath (false );
159
- mockMvcIntrospector (false );
160
- List <RequestMatcher > requestMatchers = this .matcherRegistry .requestMatchers (HttpMethod .GET , "/path" );
161
- assertThat (requestMatchers ).isNotEmpty ();
162
- assertThat (requestMatchers .size ()).isEqualTo (1 );
163
- assertThat (requestMatchers .get (0 )).isExactlyInstanceOf (AntPathRequestMatcher .class );
164
- }
165
-
166
- @ Test
167
- public void requestMatchersWhenHttpMethodAndMvcNotPresentThenReturnAntPathMatcherType () throws Exception {
168
- mockMvcPresentClasspath (false );
169
- mockMvcIntrospector (false );
170
- List <RequestMatcher > requestMatchers = this .matcherRegistry .requestMatchers (HttpMethod .GET );
171
- assertThat (requestMatchers ).isNotEmpty ();
172
- assertThat (requestMatchers .size ()).isEqualTo (1 );
173
- assertThat (requestMatchers .get (0 )).isExactlyInstanceOf (AntPathRequestMatcher .class );
174
- }
175
-
176
- @ Test
177
- public void requestMatchersWhenMvcPresentInClassPathAndMvcIntrospectorBeanNotAvailableThenException ()
178
- throws Exception {
179
- mockMvcPresentClasspath (true );
140
+ public void requestMatchersWhenMvcPresentInClassPathAndMvcIntrospectorBeanNotAvailableThenException () {
180
141
mockMvcIntrospector (false );
181
142
assertThatExceptionOfType (NoSuchBeanDefinitionException .class )
182
143
.isThrownBy (() -> this .matcherRegistry .requestMatchers ("/path" )).withMessageContaining (
@@ -188,15 +149,6 @@ private void mockMvcIntrospector(boolean isPresent) {
188
149
given (context .containsBean ("mvcHandlerMappingIntrospector" )).willReturn (isPresent );
189
150
}
190
151
191
- private void mockMvcPresentClasspath (Object newValue ) throws Exception {
192
- Field mvcPresentField = AbstractRequestMatcherRegistry .class .getDeclaredField ("mvcPresent" );
193
- mvcPresentField .setAccessible (true );
194
- Field modifiersField = Field .class .getDeclaredField ("modifiers" );
195
- modifiersField .setAccessible (true );
196
- modifiersField .setInt (mvcPresentField , mvcPresentField .getModifiers () & ~Modifier .FINAL );
197
- mvcPresentField .set (null , newValue );
198
- }
199
-
200
152
private static class TestRequestMatcherRegistry extends AbstractRequestMatcherRegistry <List <RequestMatcher >> {
201
153
202
154
@ Override
0 commit comments