Use ApplicationContextRunner in SpringDataWebAutoConfigurationTests
See gh-14413
This commit is contained in:
parent
cc8464027c
commit
3f7a01fd27
|
|
@ -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;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue