Merge branch '2.0.x'

pull/14744/head
Andy Wilkinson 6 years ago
commit 37c60b6603

@ -16,19 +16,14 @@
package org.springframework.boot.autoconfigure.data.web;
import java.util.Map;
import org.junit.After;
import org.junit.Test;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.boot.autoconfigure.AutoConfigurations;
import org.springframework.boot.test.context.runner.ApplicationContextRunner;
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.data.web.SortHandlerMethodArgumentResolver;
import org.springframework.mock.web.MockServletContext;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
@ -41,104 +36,88 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class SpringDataWebAutoConfigurationTests {
private AnnotationConfigWebApplicationContext context;
@After
public void after() {
if (this.context != null) {
this.context.close();
}
}
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(SpringDataWebAutoConfiguration.class));
@Test
public void webSupportIsAutoConfiguredInWebApplicationContexts() {
load();
this.context.setServletContext(new MockServletContext());
Map<String, PageableHandlerMethodArgumentResolver> beans = this.context
.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
assertThat(beans).hasSize(1);
this.contextRunner.run((context) -> assertThat(context)
.hasSingleBean(PageableHandlerMethodArgumentResolver.class));
}
@Test
public void autoConfigurationBacksOffInNonWebApplicationContexts() {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
ctx.register(SpringDataWebAutoConfiguration.class);
try {
ctx.refresh();
Map<String, PageableHandlerMethodArgumentResolver> beans = ctx
.getBeansOfType(PageableHandlerMethodArgumentResolver.class);
assertThat(beans).isEmpty();
}
finally {
ctx.close();
}
new ApplicationContextRunner()
.withConfiguration(
AutoConfigurations.of(SpringDataWebAutoConfiguration.class))
.run((context) -> assertThat(context)
.doesNotHaveBean(PageableHandlerMethodArgumentResolver.class));
}
@Test
public void customizePageable() {
load("spring.data.web.pageable.page-parameter=p",
"spring.data.web.pageable.size-parameter=s",
"spring.data.web.pageable.default-page-size=10",
"spring.data.web.pageable.prefix=abc",
"spring.data.web.pageable.qualifier-delimiter=__",
"spring.data.web.pageable.max-page-size=100",
"spring.data.web.pageable.one-indexed-parameters=true");
PageableHandlerMethodArgumentResolver argumentResolver = this.context
.getBean(PageableHandlerMethodArgumentResolver.class);
assertThat(ReflectionTestUtils.getField(argumentResolver, "pageParameterName"))
.isEqualTo("p");
assertThat(ReflectionTestUtils.getField(argumentResolver, "sizeParameterName"))
.isEqualTo("s");
assertThat(ReflectionTestUtils.getField(argumentResolver, "oneIndexedParameters"))
.isEqualTo(true);
assertThat(ReflectionTestUtils.getField(argumentResolver, "prefix"))
.isEqualTo("abc");
assertThat(ReflectionTestUtils.getField(argumentResolver, "qualifierDelimiter"))
.isEqualTo("__");
assertThat(ReflectionTestUtils.getField(argumentResolver, "fallbackPageable"))
.isEqualTo(PageRequest.of(0, 10));
assertThat(ReflectionTestUtils.getField(argumentResolver, "maxPageSize"))
.isEqualTo(100);
this.contextRunner
.withPropertyValues("spring.data.web.pageable.page-parameter=p",
"spring.data.web.pageable.size-parameter=s",
"spring.data.web.pageable.default-page-size=10",
"spring.data.web.pageable.prefix=abc",
"spring.data.web.pageable.qualifier-delimiter=__",
"spring.data.web.pageable.max-page-size=100",
"spring.data.web.pageable.one-indexed-parameters=true")
.run((context) -> {
PageableHandlerMethodArgumentResolver argumentResolver = context
.getBean(PageableHandlerMethodArgumentResolver.class);
assertThat(argumentResolver)
.hasFieldOrPropertyWithValue("pageParameterName", "p");
assertThat(argumentResolver)
.hasFieldOrPropertyWithValue("sizeParameterName", "s");
assertThat(argumentResolver)
.hasFieldOrPropertyWithValue("oneIndexedParameters", true);
assertThat(argumentResolver).hasFieldOrPropertyWithValue("prefix",
"abc");
assertThat(argumentResolver)
.hasFieldOrPropertyWithValue("qualifierDelimiter", "__");
assertThat(argumentResolver).hasFieldOrPropertyWithValue(
"fallbackPageable", PageRequest.of(0, 10));
assertThat(argumentResolver)
.hasFieldOrPropertyWithValue("maxPageSize", 100);
});
}
@Test
public void defaultPageable() {
load();
PageableHandlerMethodArgumentResolver argumentResolver = this.context
.getBean(PageableHandlerMethodArgumentResolver.class);
SpringDataWebProperties.Pageable properties = new SpringDataWebProperties()
.getPageable();
assertThat(ReflectionTestUtils.getField(argumentResolver, "pageParameterName"))
.isEqualTo(properties.getPageParameter());
assertThat(ReflectionTestUtils.getField(argumentResolver, "sizeParameterName"))
.isEqualTo(properties.getSizeParameter());
assertThat(ReflectionTestUtils.getField(argumentResolver, "oneIndexedParameters"))
.isEqualTo(properties.isOneIndexedParameters());
assertThat(ReflectionTestUtils.getField(argumentResolver, "prefix"))
.isEqualTo(properties.getPrefix());
assertThat(ReflectionTestUtils.getField(argumentResolver, "qualifierDelimiter"))
.isEqualTo(properties.getQualifierDelimiter());
assertThat(ReflectionTestUtils.getField(argumentResolver, "fallbackPageable"))
.isEqualTo(PageRequest.of(0, properties.getDefaultPageSize()));
assertThat(ReflectionTestUtils.getField(argumentResolver, "maxPageSize"))
.isEqualTo(properties.getMaxPageSize());
this.contextRunner.run((context) -> {
SpringDataWebProperties.Pageable properties = new SpringDataWebProperties()
.getPageable();
PageableHandlerMethodArgumentResolver argumentResolver = context
.getBean(PageableHandlerMethodArgumentResolver.class);
assertThat(argumentResolver).hasFieldOrPropertyWithValue("pageParameterName",
properties.getPageParameter());
assertThat(argumentResolver).hasFieldOrPropertyWithValue("sizeParameterName",
properties.getSizeParameter());
assertThat(argumentResolver).hasFieldOrPropertyWithValue(
"oneIndexedParameters", properties.isOneIndexedParameters());
assertThat(argumentResolver).hasFieldOrPropertyWithValue("prefix",
properties.getPrefix());
assertThat(argumentResolver).hasFieldOrPropertyWithValue("qualifierDelimiter",
properties.getQualifierDelimiter());
assertThat(argumentResolver).hasFieldOrPropertyWithValue("fallbackPageable",
PageRequest.of(0, properties.getDefaultPageSize()));
assertThat(argumentResolver).hasFieldOrPropertyWithValue("maxPageSize",
properties.getMaxPageSize());
});
}
@Test
public void customizeSort() {
load("spring.data.web.sort.sort-parameter=s");
SortHandlerMethodArgumentResolver argumentResolver = this.context
.getBean(SortHandlerMethodArgumentResolver.class);
assertThat(ReflectionTestUtils.getField(argumentResolver, "sortParameter"))
.isEqualTo("s");
}
private void load(String... environment) {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
TestPropertyValues.of(environment).applyTo(ctx);
ctx.register(SpringDataWebAutoConfiguration.class);
ctx.refresh();
this.context = ctx;
this.contextRunner.withPropertyValues("spring.data.web.sort.sort-parameter=s")
.run((context) -> {
SortHandlerMethodArgumentResolver argumentResolver = context
.getBean(SortHandlerMethodArgumentResolver.class);
assertThat(argumentResolver)
.hasFieldOrPropertyWithValue("sortParameter", "s");
});
}
}

@ -2140,6 +2140,14 @@ the annotations and flags accordingly.
[[howto-use-customize-spring-datas-web-support]]
=== Customize Spring Data's Web Support
Spring Data provides web support that simplifies the use of Spring Data repositories in a
web application. Spring Boot provides properties in the `spring.data.web` namespace
for customizing its configuration. Note that if you are using Spring Data REST, you must
use the properties in the `spring.data.rest` namespace instead.
[[howto-use-exposing-spring-data-repositories-rest-endpoint]]
=== Expose Spring Data Repositories as REST Endpoint
Spring Data REST can expose the `Repository` implementations as REST endpoints for you,

Loading…
Cancel
Save