Skip to content

Commit 558aec2

Browse files
committed
Polish MvcUriComponentsBuilder
1 parent c81754f commit 558aec2

File tree

2 files changed

+48
-44
lines changed

2 files changed

+48
-44
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java

Lines changed: 32 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,13 @@
3737
import org.springframework.web.context.request.RequestAttributes;
3838
import org.springframework.web.context.request.RequestContextHolder;
3939
import org.springframework.web.context.request.ServletRequestAttributes;
40-
import org.springframework.web.context.support.WebApplicationContextUtils;
4140
import org.springframework.web.method.annotation.RequestParamMethodArgumentResolver;
4241
import org.springframework.web.method.support.CompositeUriComponentsContributor;
4342
import org.springframework.web.servlet.DispatcherServlet;
44-
import org.springframework.web.servlet.support.RequestContextUtils;
4543
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
4644
import org.springframework.web.util.UriComponents;
4745
import org.springframework.web.util.UriComponentsBuilder;
4846

49-
import javax.servlet.ServletContext;
5047
import javax.servlet.http.HttpServletRequest;
5148
import java.lang.reflect.Method;
5249
import java.util.*;
@@ -196,15 +193,15 @@ public static UriComponentsBuilder fromMethod(Method method, Object... argumentV
196193
*
197194
* // Inline style with static import of MvcUriComponentsBuilder.mock
198195
*
199-
* MvcUriComponentsBuilder.fromLastCall(
196+
* MvcUriComponentsBuilder.fromMethodCall(
200197
* mock(CustomerController.class).showAddresses("US")).buildAndExpand(1);
201198
*
202199
* // Longer style for preparing multiple URIs and for void controller methods
203200
*
204201
* CustomerController controller = MvcUriComponentsBuilder.mock(CustomController.class);
205202
* controller.addAddress(null);
206203
*
207-
* MvcUriComponentsBuilder.fromLastCall(controller);
204+
* MvcUriComponentsBuilder.fromMethodCall(controller);
208205
*
209206
* </pre>
210207
*
@@ -219,7 +216,7 @@ public static UriComponentsBuilder fromMethod(Method method, Object... argumentV
219216
*
220217
* @return a UriComponents instance
221218
*/
222-
public static UriComponentsBuilder fromLastCall(Object methodInvocationInfo) {
219+
public static UriComponentsBuilder fromMethodCall(Object methodInvocationInfo) {
223220

224221
Assert.isInstanceOf(MethodInvocationInfo.class, methodInvocationInfo);
225222
MethodInvocationInfo info = (MethodInvocationInfo) methodInvocationInfo;
@@ -319,39 +316,47 @@ protected static CompositeUriComponentsContributor getConfiguredUriComponentsCon
319316
}
320317

321318
/**
322-
* Return a "mock" controller instance. When a method on the mock is invoked, the
323-
* supplied argument values are remembered and the result can then be used to
324-
* prepare a UriComponents through the factory method {@link #fromLastCall(Object)}.
319+
* Return a "mock" controller instance. When an {@code @RequestMapping} method
320+
* on the controller is invoked, the supplied argument values are remembered
321+
* and the result can then be used to prepare a URL to the method via
322+
* {@link #fromMethodCall(Object)}.
325323
* <p>
326-
* The controller can be invoked more than once. However, only the last invocation
327-
* is remembered. This means the same mock controller can be used to create
328-
* multiple links in succession, for example:
324+
* This is a shorthand version of {@link #controller(Class)} intended for
325+
* inline use as follows:
329326
*
330327
* <pre>
331-
* CustomerController controller = MvcUriComponentsBuilder.mock(CustomController.class);
332-
*
333-
* MvcUriComponentsBuilder.fromLastCall(controller.getFoo(1)).build();
334-
* MvcUriComponentsBuilder.fromLastCall(controller.getFoo(2)).build();
335-
*
336-
* MvcUriComponentsBuilder.fromLastCall(controller.getBar(1)).build();
337-
* MvcUriComponentsBuilder.fromLastCall(controller.getBar(2)).build();
328+
* UriComponentsBuilder builder = MvcUriComponentsBuilder.fromMethodCall(
329+
* on(FooController.class).getFoo(1)).build();
338330
* </pre>
339331
*
340-
* If a controller method returns void, use the following style:
332+
* @param controllerType the target controller
333+
*/
334+
public static <T> T on(Class<T> controllerType) {
335+
return controller(controllerType);
336+
}
337+
338+
/**
339+
* Return a "mock" controller instance. When an {@code @RequestMapping} method
340+
* on the controller is invoked, the supplied argument values are remembered
341+
* and the result can then be used to prepare a URL to the method via
342+
* {@link #fromMethodCall(Object)}.
343+
* <p>
344+
* This is a longer version of {@link #on(Class)} for use with void controller
345+
* methods as well as for creating multiple links in succession.
341346
*
342347
* <pre>
343-
* CustomerController controller = MvcUriComponentsBuilder.mock(CustomController.class);
348+
* FooController fooController = controller(FooController.class);
344349
*
345-
* controller.handleFoo(1);
346-
* MvcUriComponentsBuilder.fromLastCall(controller).build();
350+
* fooController.saveFoo(1, null);
351+
* builder = MvcUriComponentsBuilder.fromMethodCall(fooController);
347352
*
348-
* controller.handleFoo(2)
349-
* MvcUriComponentsBuilder.fromLastCall(controller).build();
353+
* fooController.saveFoo(2, null);
354+
* builder = MvcUriComponentsBuilder.fromMethodCall(fooController);
350355
* </pre>
351356
*
352-
* @param controllerType the type of controller to mock
357+
* @param controllerType the target controller
353358
*/
354-
public static <T> T mock(Class<T> controllerType) {
359+
public static <T> T controller(Class<T> controllerType) {
355360
Assert.notNull(controllerType, "'controllerType' must not be null");
356361
return initProxy(controllerType, new ControllerMethodInvocationInterceptor());
357362
}

spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsContributorTests.java

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,14 @@
3333
import org.springframework.web.bind.annotation.RequestParam;
3434
import org.springframework.web.context.request.RequestContextHolder;
3535
import org.springframework.web.context.request.ServletRequestAttributes;
36-
import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder;
3736
import org.springframework.web.util.UriComponents;
3837

3938
import java.util.Arrays;
4039
import java.util.List;
4140

4241
import static org.hamcrest.Matchers.*;
4342
import static org.junit.Assert.assertThat;
44-
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.mock;
43+
import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.on;
4544

4645
/**
4746
* Unit tests for {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}.
@@ -146,36 +145,36 @@ public void fromMethodNotMapped() throws Exception {
146145
}
147146

148147
@Test
149-
public void fromLastCall() {
150-
UriComponents uriComponents = this.builder.fromLastCall(
151-
mock(ControllerWithMethods.class).myMethod(null)).build();
148+
public void fromMethodCall() {
149+
UriComponents uriComponents = this.builder.fromMethodCall(
150+
on(ControllerWithMethods.class).myMethod(null)).build();
152151

153152
assertThat(uriComponents.toUriString(), startsWith("http://localhost"));
154153
assertThat(uriComponents.toUriString(), endsWith("/something/else"));
155154
}
156155

157156
@Test
158-
public void fromLastCallWithTypeLevelUriVars() {
159-
UriComponents uriComponents = this.builder.fromLastCall(
160-
mock(PersonsAddressesController.class).getAddressesForCountry("DE")).buildAndExpand(15);
157+
public void fromMethodCallWithTypeLevelUriVars() {
158+
UriComponents uriComponents = this.builder.fromMethodCall(
159+
on(PersonsAddressesController.class).getAddressesForCountry("DE")).buildAndExpand(15);
161160

162161
assertThat(uriComponents.toUriString(), endsWith("/people/15/addresses/DE"));
163162
}
164163

165164

166165
@Test
167-
public void fromLastCallWithPathVar() {
168-
UriComponents uriComponents = this.builder.fromLastCall(
169-
mock(ControllerWithMethods.class).methodWithPathVariable("1")).build();
166+
public void fromMethodCallWithPathVar() {
167+
UriComponents uriComponents = this.builder.fromMethodCall(
168+
on(ControllerWithMethods.class).methodWithPathVariable("1")).build();
170169

171170
assertThat(uriComponents.toUriString(), startsWith("http://localhost"));
172171
assertThat(uriComponents.toUriString(), endsWith("/something/1/foo"));
173172
}
174173

175174
@Test
176-
public void fromLastCallWithPathVarAndRequestParams() {
177-
UriComponents uriComponents = this.builder.fromLastCall(
178-
mock(ControllerWithMethods.class).methodForNextPage("1", 10, 5)).build();
175+
public void fromMethodCallWithPathVarAndRequestParams() {
176+
UriComponents uriComponents = this.builder.fromMethodCall(
177+
on(ControllerWithMethods.class).methodForNextPage("1", 10, 5)).build();
179178

180179
assertThat(uriComponents.getPath(), is("/something/1/foo"));
181180

@@ -185,9 +184,9 @@ public void fromLastCallWithPathVarAndRequestParams() {
185184
}
186185

187186
@Test
188-
public void fromLastCallWithPathVarAndMultiValueRequestParams() {
189-
UriComponents uriComponents = this.builder.fromLastCall(
190-
mock(ControllerWithMethods.class).methodWithMultiValueRequestParams(
187+
public void fromMethodCallWithPathVarAndMultiValueRequestParams() {
188+
UriComponents uriComponents = this.builder.fromMethodCall(
189+
on(ControllerWithMethods.class).methodWithMultiValueRequestParams(
191190
"1", Arrays.asList(3, 7), 5)).build();
192191

193192
assertThat(uriComponents.getPath(), is("/something/1/foo"));

0 commit comments

Comments
 (0)