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

@ -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]] [[howto-use-exposing-spring-data-repositories-rest-endpoint]]
=== Expose Spring Data Repositories as REST Endpoint === Expose Spring Data Repositories as REST Endpoint
Spring Data REST can expose the `Repository` implementations as REST endpoints for you, Spring Data REST can expose the `Repository` implementations as REST endpoints for you,

Loading…
Cancel
Save