DATACMNS-351 - Add setFallbackSort(…) to SortHandlerMethodArgumentResolver.

SortHandlerMethodArgumentResolver now has a setFallbackSort(…) to be able to configure the Sort instance to be used if nothing can be resolved from the request and sort default is configured on the controller method.

Renamed name of getDefaults(…) method in SortHandlerMethodArgumentResolver to getDefaultFromAnnotationOrFallback(…) and refactored implementation to be more understandable.

Original pull request: #36
This commit is contained in:
Thomas Darimont
2013-08-07 12:10:43 +02:00
committed by Oliver Gierke
parent 409b1f6008
commit a63a2bbd75
2 changed files with 63 additions and 17 deletions

View File

@@ -37,6 +37,7 @@ import org.springframework.web.util.UriComponentsBuilder;
*
* @since 1.6
* @author Oliver Gierke
* @author Thomas Darimont
*/
public class SortHandlerArgumentResolverUnitTests extends SortDefaultUnitTests {
@@ -45,6 +46,34 @@ public class SortHandlerArgumentResolverUnitTests extends SortDefaultUnitTests {
static final String[] SORT_2 = new String[] { "username,ASC", "lastname,firstname,DESC" };
static final String SORT_3 = "firstname,lastname";
/**
* @see DATACMNS-351
*/
@Test
public void fallbackToGivenDefaultSort() throws Exception {
MethodParameter parameter = TestUtils.getParameterOfMethod(getControllerClass(), "unsupportedMethod", String.class);
SortHandlerMethodArgumentResolver resolver = new SortHandlerMethodArgumentResolver();
Sort fallbackSort = new Sort(Direction.ASC, "ID");
resolver.setFallbackSort(fallbackSort);
Sort sort = resolver.resolveArgument(parameter, null, new ServletWebRequest(new MockHttpServletRequest()), null);
assertThat(sort, is(fallbackSort));
}
/**
* @see DATACMNS-351
*/
@Test
public void fallbackToDefaultDefaultSort() throws Exception {
MethodParameter parameter = TestUtils.getParameterOfMethod(getControllerClass(), "unsupportedMethod", String.class);
SortHandlerMethodArgumentResolver resolver = new SortHandlerMethodArgumentResolver();
Sort sort = resolver.resolveArgument(parameter, null, new ServletWebRequest(new MockHttpServletRequest()), null);
assertThat(sort, is(nullValue()));
}
@Test
public void discoversSimpleSortFromRequest() {