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
"spring.data.web.pageable.size-parameter=s", .withPropertyValues("spring.data.web.pageable.page-parameter=p",
"spring.data.web.pageable.default-page-size=10", "spring.data.web.pageable.size-parameter=s",
"spring.data.web.pageable.prefix=abc", "spring.data.web.pageable.default-page-size=10",
"spring.data.web.pageable.qualifier-delimiter=__", "spring.data.web.pageable.prefix=abc",
"spring.data.web.pageable.max-page-size=100", "spring.data.web.pageable.qualifier-delimiter=__",
"spring.data.web.pageable.one-indexed-parameters=true"); "spring.data.web.pageable.max-page-size=100",
PageableHandlerMethodArgumentResolver argumentResolver = this.context "spring.data.web.pageable.one-indexed-parameters=true")
.getBean(PageableHandlerMethodArgumentResolver.class); .run((context) -> {
assertThat(ReflectionTestUtils.getField(argumentResolver, "pageParameterName")) PageableHandlerMethodArgumentResolver argumentResolver = context
.isEqualTo("p"); .getBean(PageableHandlerMethodArgumentResolver.class);
assertThat(ReflectionTestUtils.getField(argumentResolver, "sizeParameterName")) assertThat(argumentResolver)
.isEqualTo("s"); .hasFieldOrPropertyWithValue("pageParameterName", "p");
assertThat(ReflectionTestUtils.getField(argumentResolver, "oneIndexedParameters")) assertThat(argumentResolver)
.isEqualTo(true); .hasFieldOrPropertyWithValue("sizeParameterName", "s");
assertThat(ReflectionTestUtils.getField(argumentResolver, "prefix")) assertThat(argumentResolver)
.isEqualTo("abc"); .hasFieldOrPropertyWithValue("oneIndexedParameters", true);
assertThat(ReflectionTestUtils.getField(argumentResolver, "qualifierDelimiter")) assertThat(argumentResolver).hasFieldOrPropertyWithValue("prefix",
.isEqualTo("__"); "abc");
assertThat(ReflectionTestUtils.getField(argumentResolver, "fallbackPageable")) assertThat(argumentResolver)
.isEqualTo(PageRequest.of(0, 10)); .hasFieldOrPropertyWithValue("qualifierDelimiter", "__");
assertThat(ReflectionTestUtils.getField(argumentResolver, "maxPageSize")) assertThat(argumentResolver).hasFieldOrPropertyWithValue(
.isEqualTo(100); "fallbackPageable", PageRequest.of(0, 10));
assertThat(argumentResolver)
.hasFieldOrPropertyWithValue("maxPageSize", 100);
});
} }
@Test @Test
public void defaultPageable() { public void defaultPageable() {
load(); this.contextRunner.run((context) -> {
PageableHandlerMethodArgumentResolver argumentResolver = this.context SpringDataWebProperties.Pageable properties = new SpringDataWebProperties()
.getBean(PageableHandlerMethodArgumentResolver.class); .getPageable();
SpringDataWebProperties.Pageable properties = new SpringDataWebProperties() PageableHandlerMethodArgumentResolver argumentResolver = context
.getPageable(); .getBean(PageableHandlerMethodArgumentResolver.class);
assertThat(ReflectionTestUtils.getField(argumentResolver, "pageParameterName")) assertThat(argumentResolver).hasFieldOrPropertyWithValue("pageParameterName",
.isEqualTo(properties.getPageParameter()); properties.getPageParameter());
assertThat(ReflectionTestUtils.getField(argumentResolver, "sizeParameterName")) assertThat(argumentResolver).hasFieldOrPropertyWithValue("sizeParameterName",
.isEqualTo(properties.getSizeParameter()); properties.getSizeParameter());
assertThat(ReflectionTestUtils.getField(argumentResolver, "oneIndexedParameters")) assertThat(argumentResolver).hasFieldOrPropertyWithValue(
.isEqualTo(properties.isOneIndexedParameters()); "oneIndexedParameters", properties.isOneIndexedParameters());
assertThat(ReflectionTestUtils.getField(argumentResolver, "prefix")) assertThat(argumentResolver).hasFieldOrPropertyWithValue("prefix",
.isEqualTo(properties.getPrefix()); properties.getPrefix());
assertThat(ReflectionTestUtils.getField(argumentResolver, "qualifierDelimiter")) assertThat(argumentResolver).hasFieldOrPropertyWithValue("qualifierDelimiter",
.isEqualTo(properties.getQualifierDelimiter()); properties.getQualifierDelimiter());
assertThat(ReflectionTestUtils.getField(argumentResolver, "fallbackPageable")) assertThat(argumentResolver).hasFieldOrPropertyWithValue("fallbackPageable",
.isEqualTo(PageRequest.of(0, properties.getDefaultPageSize())); PageRequest.of(0, properties.getDefaultPageSize()));
assertThat(ReflectionTestUtils.getField(argumentResolver, "maxPageSize")) assertThat(argumentResolver).hasFieldOrPropertyWithValue("maxPageSize",
.isEqualTo(properties.getMaxPageSize()); 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) -> {
.getBean(SortHandlerMethodArgumentResolver.class); SortHandlerMethodArgumentResolver argumentResolver = context
assertThat(ReflectionTestUtils.getField(argumentResolver, "sortParameter")) .getBean(SortHandlerMethodArgumentResolver.class);
.isEqualTo("s"); assertThat(argumentResolver)
} .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