From 558aec294cd57071b61b0e4acf94b63d8e4c17fa Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 29 Oct 2013 11:39:59 -0400 Subject: [PATCH] Polish MvcUriComponentsBuilder --- .../annotation/MvcUriComponentsBuilder.java | 59 ++++++++++--------- .../MvcUriComponentsContributorTests.java | 33 +++++------ 2 files changed, 48 insertions(+), 44 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java index d7e7cbfd5c..1126c44c9f 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java @@ -37,16 +37,13 @@ import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.request.RequestAttributes; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; -import org.springframework.web.context.support.WebApplicationContextUtils; import org.springframework.web.method.annotation.RequestParamMethodArgumentResolver; import org.springframework.web.method.support.CompositeUriComponentsContributor; import org.springframework.web.servlet.DispatcherServlet; -import org.springframework.web.servlet.support.RequestContextUtils; import org.springframework.web.servlet.support.ServletUriComponentsBuilder; import org.springframework.web.util.UriComponents; import org.springframework.web.util.UriComponentsBuilder; -import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import java.lang.reflect.Method; import java.util.*; @@ -196,7 +193,7 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { * * // Inline style with static import of MvcUriComponentsBuilder.mock * - * MvcUriComponentsBuilder.fromLastCall( + * MvcUriComponentsBuilder.fromMethodCall( * mock(CustomerController.class).showAddresses("US")).buildAndExpand(1); * * // Longer style for preparing multiple URIs and for void controller methods @@ -204,7 +201,7 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { * CustomerController controller = MvcUriComponentsBuilder.mock(CustomController.class); * controller.addAddress(null); * - * MvcUriComponentsBuilder.fromLastCall(controller); + * MvcUriComponentsBuilder.fromMethodCall(controller); * * * @@ -219,7 +216,7 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { * * @return a UriComponents instance */ - public static UriComponentsBuilder fromLastCall(Object methodInvocationInfo) { + public static UriComponentsBuilder fromMethodCall(Object methodInvocationInfo) { Assert.isInstanceOf(MethodInvocationInfo.class, methodInvocationInfo); MethodInvocationInfo info = (MethodInvocationInfo) methodInvocationInfo; @@ -319,39 +316,47 @@ public class MvcUriComponentsBuilder extends UriComponentsBuilder { } /** - * Return a "mock" controller instance. When a method on the mock is invoked, the - * supplied argument values are remembered and the result can then be used to - * prepare a UriComponents through the factory method {@link #fromLastCall(Object)}. + * Return a "mock" controller instance. When an {@code @RequestMapping} method + * on the controller is invoked, the supplied argument values are remembered + * and the result can then be used to prepare a URL to the method via + * {@link #fromMethodCall(Object)}. *

- * The controller can be invoked more than once. However, only the last invocation - * is remembered. This means the same mock controller can be used to create - * multiple links in succession, for example: + * This is a shorthand version of {@link #controller(Class)} intended for + * inline use as follows: * *

-	 * CustomerController controller = MvcUriComponentsBuilder.mock(CustomController.class);
-	 *
-	 * MvcUriComponentsBuilder.fromLastCall(controller.getFoo(1)).build();
-	 * MvcUriComponentsBuilder.fromLastCall(controller.getFoo(2)).build();
-	 *
-	 * MvcUriComponentsBuilder.fromLastCall(controller.getBar(1)).build();
-	 * MvcUriComponentsBuilder.fromLastCall(controller.getBar(2)).build();
+	 * UriComponentsBuilder builder = MvcUriComponentsBuilder.fromMethodCall(
+	 * 		on(FooController.class).getFoo(1)).build();
 	 * 
* - * If a controller method returns void, use the following style: + * @param controllerType the target controller + */ + public static T on(Class controllerType) { + return controller(controllerType); + } + + /** + * Return a "mock" controller instance. When an {@code @RequestMapping} method + * on the controller is invoked, the supplied argument values are remembered + * and the result can then be used to prepare a URL to the method via + * {@link #fromMethodCall(Object)}. + *

+ * This is a longer version of {@link #on(Class)} for use with void controller + * methods as well as for creating multiple links in succession. * *

-	 * CustomerController controller = MvcUriComponentsBuilder.mock(CustomController.class);
+	 * FooController fooController = controller(FooController.class);
 	 *
-	 * controller.handleFoo(1);
-	 * MvcUriComponentsBuilder.fromLastCall(controller).build();
+	 * fooController.saveFoo(1, null);
+	 * builder = MvcUriComponentsBuilder.fromMethodCall(fooController);
 	 *
-	 * controller.handleFoo(2)
-	 * MvcUriComponentsBuilder.fromLastCall(controller).build();
+	 * fooController.saveFoo(2, null);
+	 * builder = MvcUriComponentsBuilder.fromMethodCall(fooController);
 	 * 
* - * @param controllerType the type of controller to mock + * @param controllerType the target controller */ - public static T mock(Class controllerType) { + public static T controller(Class controllerType) { Assert.notNull(controllerType, "'controllerType' must not be null"); return initProxy(controllerType, new ControllerMethodInvocationInterceptor()); } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsContributorTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsContributorTests.java index 6a953a8eff..b81e300451 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsContributorTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsContributorTests.java @@ -33,7 +33,6 @@ import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.context.request.RequestContextHolder; import org.springframework.web.context.request.ServletRequestAttributes; -import org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder; import org.springframework.web.util.UriComponents; import java.util.Arrays; @@ -41,7 +40,7 @@ import java.util.List; import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertThat; -import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.mock; +import static org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder.on; /** * Unit tests for {@link org.springframework.web.servlet.mvc.method.annotation.MvcUriComponentsBuilder}. @@ -146,36 +145,36 @@ public class MvcUriComponentsContributorTests { } @Test - public void fromLastCall() { - UriComponents uriComponents = this.builder.fromLastCall( - mock(ControllerWithMethods.class).myMethod(null)).build(); + public void fromMethodCall() { + UriComponents uriComponents = this.builder.fromMethodCall( + on(ControllerWithMethods.class).myMethod(null)).build(); assertThat(uriComponents.toUriString(), startsWith("http://localhost")); assertThat(uriComponents.toUriString(), endsWith("/something/else")); } @Test - public void fromLastCallWithTypeLevelUriVars() { - UriComponents uriComponents = this.builder.fromLastCall( - mock(PersonsAddressesController.class).getAddressesForCountry("DE")).buildAndExpand(15); + public void fromMethodCallWithTypeLevelUriVars() { + UriComponents uriComponents = this.builder.fromMethodCall( + on(PersonsAddressesController.class).getAddressesForCountry("DE")).buildAndExpand(15); assertThat(uriComponents.toUriString(), endsWith("/people/15/addresses/DE")); } @Test - public void fromLastCallWithPathVar() { - UriComponents uriComponents = this.builder.fromLastCall( - mock(ControllerWithMethods.class).methodWithPathVariable("1")).build(); + public void fromMethodCallWithPathVar() { + UriComponents uriComponents = this.builder.fromMethodCall( + on(ControllerWithMethods.class).methodWithPathVariable("1")).build(); assertThat(uriComponents.toUriString(), startsWith("http://localhost")); assertThat(uriComponents.toUriString(), endsWith("/something/1/foo")); } @Test - public void fromLastCallWithPathVarAndRequestParams() { - UriComponents uriComponents = this.builder.fromLastCall( - mock(ControllerWithMethods.class).methodForNextPage("1", 10, 5)).build(); + public void fromMethodCallWithPathVarAndRequestParams() { + UriComponents uriComponents = this.builder.fromMethodCall( + on(ControllerWithMethods.class).methodForNextPage("1", 10, 5)).build(); assertThat(uriComponents.getPath(), is("/something/1/foo")); @@ -185,9 +184,9 @@ public class MvcUriComponentsContributorTests { } @Test - public void fromLastCallWithPathVarAndMultiValueRequestParams() { - UriComponents uriComponents = this.builder.fromLastCall( - mock(ControllerWithMethods.class).methodWithMultiValueRequestParams( + public void fromMethodCallWithPathVarAndMultiValueRequestParams() { + UriComponents uriComponents = this.builder.fromMethodCall( + on(ControllerWithMethods.class).methodWithMultiValueRequestParams( "1", Arrays.asList(3, 7), 5)).build(); assertThat(uriComponents.getPath(), is("/something/1/foo"));