Skip to content

Commit bb46a54

Browse files
committed
Add DispatcherServlet to Tests
Issue gh-13551
1 parent df239b6 commit bb46a54

File tree

6 files changed

+150
-68
lines changed

6 files changed

+150
-68
lines changed
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
/*
2+
* Copyright 2002-2022 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.security.config;
18+
19+
import java.util.Collection;
20+
import java.util.LinkedHashMap;
21+
import java.util.Map;
22+
import java.util.Set;
23+
24+
import javax.servlet.MultipartConfigElement;
25+
import javax.servlet.Servlet;
26+
import javax.servlet.ServletRegistration;
27+
import javax.servlet.ServletSecurityElement;
28+
29+
import org.springframework.lang.NonNull;
30+
import org.springframework.web.servlet.DispatcherServlet;
31+
32+
public class MockServletContext extends org.springframework.mock.web.MockServletContext {
33+
34+
private final Map<String, ServletRegistration> registrations = new LinkedHashMap<>();
35+
36+
public static MockServletContext mvc() {
37+
MockServletContext servletContext = new MockServletContext();
38+
servletContext.addServlet("dispatcherServlet", DispatcherServlet.class);
39+
return servletContext;
40+
}
41+
42+
@NonNull
43+
@Override
44+
public ServletRegistration.Dynamic addServlet(@NonNull String servletName, Class<? extends Servlet> clazz) {
45+
ServletRegistration.Dynamic dynamic = new MockServletRegistration(servletName, clazz);
46+
this.registrations.put(servletName, dynamic);
47+
return dynamic;
48+
}
49+
50+
@NonNull
51+
@Override
52+
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
53+
return this.registrations;
54+
}
55+
56+
private static class MockServletRegistration implements ServletRegistration.Dynamic {
57+
58+
private final String name;
59+
60+
private final Class<?> clazz;
61+
62+
MockServletRegistration(String name, Class<?> clazz) {
63+
this.name = name;
64+
this.clazz = clazz;
65+
}
66+
67+
@Override
68+
public void setLoadOnStartup(int loadOnStartup) {
69+
70+
}
71+
72+
@Override
73+
public Set<String> setServletSecurity(ServletSecurityElement constraint) {
74+
return null;
75+
}
76+
77+
@Override
78+
public void setMultipartConfig(MultipartConfigElement multipartConfig) {
79+
80+
}
81+
82+
@Override
83+
public void setRunAsRole(String roleName) {
84+
85+
}
86+
87+
@Override
88+
public void setAsyncSupported(boolean isAsyncSupported) {
89+
90+
}
91+
92+
@Override
93+
public Set<String> addMapping(String... urlPatterns) {
94+
return null;
95+
}
96+
97+
@Override
98+
public Collection<String> getMappings() {
99+
return null;
100+
}
101+
102+
@Override
103+
public String getRunAsRole() {
104+
return null;
105+
}
106+
107+
@Override
108+
public String getName() {
109+
return this.name;
110+
}
111+
112+
@Override
113+
public String getClassName() {
114+
return this.clazz.getName();
115+
}
116+
117+
@Override
118+
public boolean setInitParameter(String name, String value) {
119+
return false;
120+
}
121+
122+
@Override
123+
public String getInitParameter(String name) {
124+
return null;
125+
}
126+
127+
@Override
128+
public Set<String> setInitParameters(Map<String, String> initParameters) {
129+
return null;
130+
}
131+
132+
@Override
133+
public Map<String, String> getInitParameters() {
134+
return null;
135+
}
136+
137+
}
138+
139+
}

config/src/test/java/org/springframework/security/config/annotation/web/AbstractRequestMatcherRegistryTests.java

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,18 @@
1818

1919
import java.lang.reflect.Field;
2020
import java.lang.reflect.Modifier;
21-
import java.util.LinkedHashMap;
2221
import java.util.List;
23-
import java.util.Map;
2422

2523
import javax.servlet.DispatcherType;
2624
import javax.servlet.Servlet;
27-
import javax.servlet.ServletContext;
28-
import javax.servlet.ServletRegistration;
2925

30-
import org.jetbrains.annotations.NotNull;
3126
import org.junit.jupiter.api.BeforeEach;
3227
import org.junit.jupiter.api.Test;
3328

3429
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3530
import org.springframework.context.ApplicationContext;
3631
import org.springframework.http.HttpMethod;
32+
import org.springframework.security.config.MockServletContext;
3733
import org.springframework.security.config.annotation.ObjectPostProcessor;
3834
import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher;
3935
import org.springframework.security.web.util.matcher.AntPathRequestMatcher;
@@ -70,10 +66,8 @@ public <O> O postProcess(O object) {
7066
public void setUp() {
7167
this.matcherRegistry = new TestRequestMatcherRegistry();
7268
this.context = mock(WebApplicationContext.class);
73-
ServletContext servletContext = new MockServletContext();
74-
servletContext.addServlet("dispatcherServlet", DispatcherServlet.class);
7569
given(this.context.getBean(ObjectPostProcessor.class)).willReturn(NO_OP_OBJECT_POST_PROCESSOR);
76-
given(this.context.getServletContext()).willReturn(servletContext);
70+
given(this.context.getServletContext()).willReturn(MockServletContext.mvc());
7771
this.matcherRegistry.setApplicationContext(this.context);
7872
}
7973

@@ -256,25 +250,4 @@ protected List<RequestMatcher> chainRequestMatchers(List<RequestMatcher> request
256250

257251
}
258252

259-
private static class MockServletContext extends org.springframework.mock.web.MockServletContext {
260-
261-
private final Map<String, ServletRegistration> registrations = new LinkedHashMap<>();
262-
263-
@NotNull
264-
@Override
265-
public ServletRegistration.Dynamic addServlet(@NotNull String servletName, Class<? extends Servlet> clazz) {
266-
ServletRegistration.Dynamic dynamic = mock(ServletRegistration.Dynamic.class);
267-
given(dynamic.getClassName()).willReturn(clazz.getName());
268-
this.registrations.put(servletName, dynamic);
269-
return dynamic;
270-
}
271-
272-
@NotNull
273-
@Override
274-
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
275-
return this.registrations;
276-
}
277-
278-
}
279-
280253
}

config/src/test/java/org/springframework/security/config/annotation/web/configurers/AuthorizeRequestsTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,10 +29,10 @@
2929
import org.springframework.mock.web.MockFilterChain;
3030
import org.springframework.mock.web.MockHttpServletRequest;
3131
import org.springframework.mock.web.MockHttpServletResponse;
32-
import org.springframework.mock.web.MockServletContext;
3332
import org.springframework.security.access.hierarchicalroles.RoleHierarchy;
3433
import org.springframework.security.access.hierarchicalroles.RoleHierarchyImpl;
3534
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
35+
import org.springframework.security.config.MockServletContext;
3636
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
3737
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3838
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -75,7 +75,7 @@ public class AuthorizeRequestsTests {
7575

7676
@BeforeEach
7777
public void setup() {
78-
this.servletContext = spy(new MockServletContext());
78+
this.servletContext = spy(MockServletContext.mvc());
7979
this.request = new MockHttpServletRequest("GET", "");
8080
this.request.setMethod("GET");
8181
this.response = new MockHttpServletResponse();

config/src/test/java/org/springframework/security/config/annotation/web/configurers/HttpSecuritySecurityMatchersTests.java

Lines changed: 2 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,9 @@
1818

1919
import java.lang.reflect.Field;
2020
import java.lang.reflect.Modifier;
21-
import java.util.LinkedHashMap;
22-
import java.util.Map;
2321

24-
import javax.servlet.Servlet;
25-
import javax.servlet.ServletRegistration;
2622
import javax.servlet.http.HttpServletResponse;
2723

28-
import org.jetbrains.annotations.NotNull;
2924
import org.junit.jupiter.api.AfterEach;
3025
import org.junit.jupiter.api.BeforeEach;
3126
import org.junit.jupiter.api.Test;
@@ -39,6 +34,7 @@
3934
import org.springframework.mock.web.MockFilterChain;
4035
import org.springframework.mock.web.MockHttpServletRequest;
4136
import org.springframework.mock.web.MockHttpServletResponse;
37+
import org.springframework.security.config.MockServletContext;
4238
import org.springframework.security.config.annotation.web.AbstractRequestMatcherRegistry;
4339
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
4440
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -52,15 +48,12 @@
5248
import org.springframework.web.bind.annotation.RequestMapping;
5349
import org.springframework.web.bind.annotation.RestController;
5450
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
55-
import org.springframework.web.servlet.DispatcherServlet;
5651
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
5752
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
5853
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
5954
import org.springframework.web.servlet.handler.HandlerMappingIntrospector;
6055

6156
import static org.assertj.core.api.Assertions.assertThat;
62-
import static org.mockito.BDDMockito.given;
63-
import static org.mockito.Mockito.mock;
6457
import static org.springframework.security.config.Customizer.withDefaults;
6558

6659
/**
@@ -240,9 +233,7 @@ public void securityMatchersWhenMultiMvcMatcherThenAllPathsAreDenied() throws Ex
240233
public void loadConfig(Class<?>... configs) {
241234
this.context = new AnnotationConfigWebApplicationContext();
242235
this.context.register(configs);
243-
MockServletContext servletContext = new MockServletContext();
244-
servletContext.addServlet("dispatcherServlet", DispatcherServlet.class);
245-
this.context.setServletContext(servletContext);
236+
this.context.setServletContext(MockServletContext.mvc());
246237
this.context.refresh();
247238
this.context.getAutowireCapableBeanFactory().autowireBean(this);
248239
}
@@ -573,25 +564,4 @@ public void configurePathMatch(PathMatchConfigurer configurer) {
573564

574565
}
575566

576-
private static class MockServletContext extends org.springframework.mock.web.MockServletContext {
577-
578-
private final Map<String, ServletRegistration> registrations = new LinkedHashMap<>();
579-
580-
@NotNull
581-
@Override
582-
public ServletRegistration.Dynamic addServlet(@NotNull String servletName, Class<? extends Servlet> clazz) {
583-
ServletRegistration.Dynamic dynamic = mock(ServletRegistration.Dynamic.class);
584-
given(dynamic.getClassName()).willReturn(clazz.getName());
585-
this.registrations.put(servletName, dynamic);
586-
return dynamic;
587-
}
588-
589-
@NotNull
590-
@Override
591-
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
592-
return this.registrations;
593-
}
594-
595-
}
596-
597567
}

config/src/test/java/org/springframework/security/config/annotation/web/configurers/UrlAuthorizationConfigurerTests.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131
import org.springframework.mock.web.MockFilterChain;
3232
import org.springframework.mock.web.MockHttpServletRequest;
3333
import org.springframework.mock.web.MockHttpServletResponse;
34-
import org.springframework.mock.web.MockServletContext;
3534
import org.springframework.security.config.Customizer;
35+
import org.springframework.security.config.MockServletContext;
3636
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
3737
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
3838
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
@@ -167,7 +167,7 @@ public void multiMvcMatchersConfig() throws Exception {
167167
public void loadConfig(Class<?>... configs) {
168168
this.context = new AnnotationConfigWebApplicationContext();
169169
this.context.register(configs);
170-
this.context.setServletContext(new MockServletContext());
170+
this.context.setServletContext(MockServletContext.mvc());
171171
this.context.refresh();
172172
this.context.getAutowireCapableBeanFactory().autowireBean(this);
173173
}

config/src/test/java/org/springframework/security/config/test/SpringTestContext.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@
2828

2929
import org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor;
3030
import org.springframework.mock.web.MockServletConfig;
31-
import org.springframework.mock.web.MockServletContext;
3231
import org.springframework.security.config.BeanIds;
32+
import org.springframework.security.config.MockServletContext;
3333
import org.springframework.security.config.util.InMemoryXmlWebApplicationContext;
3434
import org.springframework.test.context.web.GenericXmlWebContextLoader;
3535
import org.springframework.test.web.servlet.MockMvc;
@@ -129,15 +129,15 @@ private SpringTestContext addFilter(Filter filter) {
129129

130130
public ConfigurableWebApplicationContext getContext() {
131131
if (!this.context.isRunning()) {
132-
this.context.setServletContext(new MockServletContext());
132+
this.context.setServletContext(MockServletContext.mvc());
133133
this.context.setServletConfig(new MockServletConfig());
134134
this.context.refresh();
135135
}
136136
return this.context;
137137
}
138138

139139
public void autowire() {
140-
this.context.setServletContext(new MockServletContext());
140+
this.context.setServletContext(MockServletContext.mvc());
141141
this.context.setServletConfig(new MockServletConfig());
142142
for (Consumer<ConfigurableWebApplicationContext> postProcessor : this.postProcessors) {
143143
postProcessor.accept(this.context);

0 commit comments

Comments
 (0)