Skip to content

Commit 1e3012c

Browse files
committed
Allow to customize TestDispatcherServlet
This commit introduces the `DispatcherServletCustomizer` callback interface that can be used to customize the `DispatcherServlet` that a `MockMvc` is using. Previously, only the `dispatchOptions` flag can be customized. This commit allows to customize any property of `DispatcherServlet` before it is initialized. Issue: SPR-14277
1 parent 45b8cf3 commit 1e3012c

File tree

4 files changed

+118
-6
lines changed

4 files changed

+118
-6
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
/*
2+
* Copyright 2002-2016 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+
* http://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.test.web.servlet;
18+
19+
import org.springframework.web.servlet.DispatcherServlet;
20+
21+
/**
22+
* Strategy interface for customizing {@link DispatcherServlet} instances that are
23+
* managed by {@link MockMvc}.
24+
*
25+
* @author Stephane Nicoll
26+
* @since 4.3.4
27+
*/
28+
public interface DispatcherServletCustomizer {
29+
30+
/**
31+
* Customize the supplied {@link DispatcherServlet} <em>before</em> it is
32+
* initialized.
33+
* @param dispatcherServlet the dispatcher servlet to customize
34+
*/
35+
void customize(DispatcherServlet dispatcherServlet);
36+
37+
}

spring-test/src/main/java/org/springframework/test/web/servlet/MockMvcBuilderSupport.java

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2016 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.test.web.servlet;
1818

19+
import java.util.Collections;
1920
import java.util.List;
2021
import javax.servlet.Filter;
2122
import javax.servlet.ServletContext;
@@ -24,6 +25,7 @@
2425
import org.springframework.core.NestedRuntimeException;
2526
import org.springframework.mock.web.MockServletConfig;
2627
import org.springframework.web.context.WebApplicationContext;
28+
import org.springframework.web.servlet.DispatcherServlet;
2729

2830
/**
2931
* Base class for MockMvc builder implementations, providing the capability to
@@ -35,19 +37,34 @@
3537
*
3638
* @author Rossen Stoyanchev
3739
* @author Rob Winch
40+
* @author Stephane Nicoll
3841
* @since 3.2
3942
*/
4043
public abstract class MockMvcBuilderSupport {
4144

45+
@Deprecated
4246
protected final MockMvc createMockMvc(Filter[] filters, MockServletConfig servletConfig,
4347
WebApplicationContext webAppContext, RequestBuilder defaultRequestBuilder,
4448
List<ResultMatcher> globalResultMatchers, List<ResultHandler> globalResultHandlers,
4549
Boolean dispatchOptions) {
50+
return createMockMvc(filters, servletConfig, webAppContext, defaultRequestBuilder,
51+
globalResultMatchers, globalResultHandlers,
52+
Collections.<DispatcherServletCustomizer>singletonList(new DispatchOptionsDispatcherServletCustomizer(dispatchOptions)));
53+
}
54+
55+
protected final MockMvc createMockMvc(Filter[] filters, MockServletConfig servletConfig,
56+
WebApplicationContext webAppContext, RequestBuilder defaultRequestBuilder,
57+
List<ResultMatcher> globalResultMatchers, List<ResultHandler> globalResultHandlers,
58+
List<DispatcherServletCustomizer> dispatcherServletCustomizers) {
4659

4760
ServletContext servletContext = webAppContext.getServletContext();
4861

4962
TestDispatcherServlet dispatcherServlet = new TestDispatcherServlet(webAppContext);
50-
dispatcherServlet.setDispatchOptionsRequest(dispatchOptions);
63+
if (dispatcherServletCustomizers != null) {
64+
for (DispatcherServletCustomizer customizers : dispatcherServletCustomizers) {
65+
customizers.customize(dispatcherServlet);
66+
}
67+
}
5168
try {
5269
dispatcherServlet.init(servletConfig);
5370
}
@@ -72,4 +89,18 @@ public MockMvcBuildException(String msg, Throwable cause) {
7289
}
7390
}
7491

92+
private static class DispatchOptionsDispatcherServletCustomizer
93+
implements DispatcherServletCustomizer {
94+
private final Boolean dispatchOptions;
95+
96+
private DispatchOptionsDispatcherServletCustomizer(Boolean dispatchOptions) {
97+
this.dispatchOptions = dispatchOptions;
98+
}
99+
100+
@Override
101+
public void customize(DispatcherServlet dispatcherServlet) {
102+
dispatcherServlet.setDispatchOptionsRequest(this.dispatchOptions);
103+
}
104+
}
105+
75106
}

spring-test/src/main/java/org/springframework/test/web/servlet/setup/AbstractMockMvcBuilder.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import javax.servlet.ServletContext;
2323

2424
import org.springframework.mock.web.MockServletConfig;
25+
import org.springframework.test.web.servlet.DispatcherServletCustomizer;
2526
import org.springframework.test.web.servlet.MockMvc;
2627
import org.springframework.test.web.servlet.MockMvcBuilderSupport;
2728
import org.springframework.test.web.servlet.RequestBuilder;
@@ -32,6 +33,7 @@
3233
import org.springframework.test.web.servlet.request.RequestPostProcessor;
3334
import org.springframework.util.Assert;
3435
import org.springframework.web.context.WebApplicationContext;
36+
import org.springframework.web.servlet.DispatcherServlet;
3537

3638
/**
3739
* An abstract implementation of {@link org.springframework.test.web.servlet.MockMvcBuilder}
@@ -42,6 +44,7 @@
4244
* pass to the DispatcherServlet.
4345
*
4446
* @author Rossen Stoyanchev
47+
* @author Stephane Nicoll
4548
* @since 4.0
4649
*/
4750
public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>>
@@ -55,7 +58,7 @@ public abstract class AbstractMockMvcBuilder<B extends AbstractMockMvcBuilder<B>
5558

5659
private final List<ResultHandler> globalResultHandlers = new ArrayList<ResultHandler>();
5760

58-
private Boolean dispatchOptions = Boolean.TRUE;
61+
private final List<DispatcherServletCustomizer> dispatcherServletCustomizers = new ArrayList<DispatcherServletCustomizer>();
5962

6063
private final List<MockMvcConfigurer> configurers = new ArrayList<MockMvcConfigurer>(4);
6164

@@ -104,11 +107,20 @@ public final <T extends B> T alwaysDo(ResultHandler resultHandler) {
104107
}
105108

106109
@SuppressWarnings("unchecked")
107-
public final <T extends B> T dispatchOptions(boolean dispatchOptions) {
108-
this.dispatchOptions = dispatchOptions;
110+
public final <T extends B> T addDispatcherServletCustomizer(DispatcherServletCustomizer customizer) {
111+
this.dispatcherServletCustomizers.add(customizer);
109112
return (T) this;
110113
}
111114

115+
public final <T extends B> T dispatchOptions(final boolean dispatchOptions) {
116+
return addDispatcherServletCustomizer(new DispatcherServletCustomizer() {
117+
@Override
118+
public void customize(DispatcherServlet dispatcherServlet) {
119+
dispatcherServlet.setDispatchOptionsRequest(dispatchOptions);
120+
}
121+
});
122+
}
123+
112124
@SuppressWarnings("unchecked")
113125
public final <T extends B> T apply(MockMvcConfigurer configurer) {
114126
configurer.afterConfigurerAdded(this);
@@ -144,7 +156,7 @@ public final MockMvc build() {
144156
Filter[] filterArray = this.filters.toArray(new Filter[this.filters.size()]);
145157

146158
return super.createMockMvc(filterArray, mockServletConfig, wac, this.defaultRequestBuilder,
147-
this.globalResultMatchers, this.globalResultHandlers, this.dispatchOptions);
159+
this.globalResultMatchers, this.globalResultHandlers, this.dispatcherServletCustomizers);
148160
}
149161

150162
/**

spring-test/src/test/java/org/springframework/test/web/servlet/setup/DefaultMockMvcBuilderTests.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020
import org.junit.Test;
2121
import org.junit.rules.ExpectedException;
2222

23+
import org.springframework.beans.DirectFieldAccessor;
2324
import org.springframework.context.support.StaticApplicationContext;
2425
import org.springframework.mock.web.MockServletContext;
26+
import org.springframework.test.web.servlet.MockMvc;
2527
import org.springframework.web.context.WebApplicationContext;
2628
import org.springframework.web.context.support.StaticWebApplicationContext;
2729
import org.springframework.web.context.support.WebApplicationContextUtils;
30+
import org.springframework.web.servlet.DispatcherServlet;
2831

2932
import static org.hamcrest.CoreMatchers.*;
3033
import static org.junit.Assert.*;
@@ -36,6 +39,7 @@
3639
* @author Rob Winch
3740
* @author Sebastien Deleuze
3841
* @author Sam Brannen
42+
* @author Stephane Nicoll
3943
*/
4044
public class DefaultMockMvcBuilderTests {
4145

@@ -124,4 +128,32 @@ public void rootWacServletContainerAttributeNotPreviouslySetWithContextHierarchy
124128
assertSame(root, WebApplicationContextUtils.getRequiredWebApplicationContext(this.servletContext));
125129
}
126130

131+
/**
132+
* See /SPR-14277
133+
*/
134+
@Test
135+
public void dispatcherServletCustomizer() {
136+
StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext);
137+
DefaultMockMvcBuilder builder = webAppContextSetup(root);
138+
builder.addDispatcherServletCustomizer(ds -> ds.setContextId("test-id"));
139+
builder.dispatchOptions(true);
140+
MockMvc mvc = builder.build();
141+
DispatcherServlet ds = (DispatcherServlet) new DirectFieldAccessor(mvc)
142+
.getPropertyValue("servlet");
143+
assertEquals("test-id", ds.getContextId());
144+
}
145+
146+
@Test
147+
public void dispatcherServletCustomizerProcessedInOrder() {
148+
StubWebApplicationContext root = new StubWebApplicationContext(this.servletContext);
149+
DefaultMockMvcBuilder builder = webAppContextSetup(root);
150+
builder.addDispatcherServletCustomizer(ds -> ds.setContextId("test-id"));
151+
builder.addDispatcherServletCustomizer(ds -> ds.setContextId("override-id"));
152+
builder.dispatchOptions(true);
153+
MockMvc mvc = builder.build();
154+
DispatcherServlet ds = (DispatcherServlet) new DirectFieldAccessor(mvc)
155+
.getPropertyValue("servlet");
156+
assertEquals("override-id", ds.getContextId());
157+
}
158+
127159
}

0 commit comments

Comments
 (0)