Migrate ApplicationContext to common hierarchy

This commit migrates `AnnotationConfigReactiveWebApplicationContext`
parent to the `GenericApplicationContext` abstraction. Any use of
`AnnotationConfigWebApplicationContext` is also removed as it also
inherits from the `AbstractRefreshableApplicationContext` outdated
hierarchy.

A new `AnnotationConfigServletWebApplicationContext` context is
introduced instead, extending from `GenericApplicationContext` and
providing the counter part of the reactive context for the Servlet-based
web app tests.

See gh-16096
This commit is contained in:
Stephane Nicoll
2019-04-02 10:06:33 +02:00
parent 4bcc632850
commit c432288ed1
24 changed files with 303 additions and 353 deletions

View File

@@ -24,13 +24,13 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplicat
import org.springframework.boot.autoconfigure.web.reactive.MockReactiveWebServerFactory;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
import org.springframework.boot.web.reactive.server.ReactiveWebServerFactory;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.server.reactive.HttpHandler;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.entry;
@@ -54,7 +54,7 @@ public class ConditionalOnWebApplicationTests {
@Test
public void testWebApplicationWithServletContext() {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
AnnotationConfigServletWebApplicationContext ctx = new AnnotationConfigServletWebApplicationContext();
ctx.register(AnyWebApplicationConfiguration.class,
ServletWebApplicationConfiguration.class,
ReactiveWebApplicationConfiguration.class);

View File

@@ -26,11 +26,11 @@ import org.springframework.boot.autoconfigure.data.jpa.city.CityRepository;
import org.springframework.boot.autoconfigure.data.web.SpringDataWebAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.format.support.FormattingConversionService;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.assertj.core.api.Assertions.assertThat;
@@ -43,7 +43,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class JpaWebAutoConfigurationTests {
private AnnotationConfigWebApplicationContext context;
private AnnotationConfigServletWebApplicationContext context;
@After
public void close() {
@@ -52,7 +52,7 @@ public class JpaWebAutoConfigurationTests {
@Test
public void testDefaultRepositoryConfiguration() {
this.context = new AnnotationConfigWebApplicationContext();
this.context = new AnnotationConfigServletWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(TestConfiguration.class,
EmbeddedDataSourceConfiguration.class,

View File

@@ -30,6 +30,7 @@ import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
@@ -41,7 +42,6 @@ import org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguratio
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import static org.assertj.core.api.Assertions.assertThat;
@@ -55,7 +55,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class RepositoryRestMvcAutoConfigurationTests {
private AnnotationConfigWebApplicationContext context;
private AnnotationConfigServletWebApplicationContext context;
@After
public void tearDown() {
@@ -143,7 +143,7 @@ public class RepositoryRestMvcAutoConfigurationTests {
}
private void load(Class<?> config, String... environment) {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
AnnotationConfigServletWebApplicationContext applicationContext = new AnnotationConfigServletWebApplicationContext();
applicationContext.setServletContext(new MockServletContext());
applicationContext.register(config, BaseConfiguration.class);
TestPropertyValues.of(environment).applyTo(applicationContext);

View File

@@ -31,6 +31,7 @@ import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.boot.web.servlet.filter.OrderedCharacterEncodingFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@@ -38,7 +39,6 @@ import org.springframework.context.annotation.Import;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.resource.ResourceUrlEncodingFilter;
import org.springframework.web.servlet.support.RequestContext;
@@ -57,7 +57,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class FreeMarkerAutoConfigurationServletIntegrationTests {
private AnnotationConfigWebApplicationContext context;
private AnnotationConfigServletWebApplicationContext context;
@After
public void close() {
@@ -206,7 +206,7 @@ public class FreeMarkerAutoConfigurationServletIntegrationTests {
}
private void load(Class<?> config, String... env) {
this.context = new AnnotationConfigWebApplicationContext();
this.context = new AnnotationConfigServletWebApplicationContext();
this.context.setServletContext(new MockServletContext());
TestPropertyValues.of(env).applyTo(this.context);
this.context.register(config);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -32,12 +32,12 @@ import org.junit.Test;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.testsupport.BuildOutput;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.context.i18n.LocaleContextHolder;
import org.springframework.core.io.ClassPathResource;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.support.RequestContext;
@@ -56,7 +56,7 @@ public class GroovyTemplateAutoConfigurationTests {
private final BuildOutput buildOutput = new BuildOutput(getClass());
private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
private AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
@Before
public void setupContext() {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2018 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -23,8 +23,8 @@ import org.junit.Test;
import org.springframework.beans.factory.BeanCreationException;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -38,7 +38,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*/
public class H2ConsoleAutoConfigurationTests {
private AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
private AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
@Before
public void setupContext() {

View File

@@ -28,6 +28,7 @@ import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConf
import org.springframework.boot.autoconfigure.jackson.JacksonAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.hateoas.MediaTypes;
import org.springframework.hateoas.client.LinkDiscoverer;
@@ -40,7 +41,6 @@ import org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2Http
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
import static org.assertj.core.api.Assertions.assertThat;
@@ -54,7 +54,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class HypermediaAutoConfigurationTests {
private AnnotationConfigWebApplicationContext context;
private AnnotationConfigServletWebApplicationContext context;
@After
public void close() {
@@ -65,7 +65,7 @@ public class HypermediaAutoConfigurationTests {
@Test
public void linkDiscoverersCreated() {
this.context = new AnnotationConfigWebApplicationContext();
this.context = new AnnotationConfigServletWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(BaseConfig.class);
this.context.refresh();
@@ -78,7 +78,7 @@ public class HypermediaAutoConfigurationTests {
@Test
public void entityLinksCreated() {
this.context = new AnnotationConfigWebApplicationContext();
this.context = new AnnotationConfigServletWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(BaseConfig.class);
this.context.refresh();
@@ -88,7 +88,7 @@ public class HypermediaAutoConfigurationTests {
@Test
public void doesBackOffIfEnableHypermediaSupportIsDeclaredManually() {
this.context = new AnnotationConfigWebApplicationContext();
this.context = new AnnotationConfigServletWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(EnableHypermediaSupportConfig.class, BaseConfig.class);
TestPropertyValues.of("spring.jackson.serialization.INDENT_OUTPUT:true")
@@ -100,7 +100,7 @@ public class HypermediaAutoConfigurationTests {
@Test
public void supportedMediaTypesOfTypeConstrainedConvertersIsCustomized() {
this.context = new AnnotationConfigWebApplicationContext();
this.context = new AnnotationConfigServletWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(BaseConfig.class);
this.context.refresh();
@@ -116,7 +116,7 @@ public class HypermediaAutoConfigurationTests {
@Test
public void customizationOfSupportedMediaTypesCanBeDisabled() {
this.context = new AnnotationConfigWebApplicationContext();
this.context = new AnnotationConfigServletWebApplicationContext();
this.context.setServletContext(new MockServletContext());
this.context.register(BaseConfig.class);
TestPropertyValues.of("spring.hateoas.use-hal-as-default-json-media-type:false")

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2017 the original author or authors.
* Copyright 2012-2019 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,8 +24,8 @@ import org.springframework.boot.autoconfigure.http.HttpMessageConvertersAutoConf
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
/**
* Tests for {@link HypermediaAutoConfiguration} when Jackson is not on the classpath.
@@ -36,11 +36,11 @@ import org.springframework.web.context.support.AnnotationConfigWebApplicationCon
@ClassPathExclusions("jackson-*.jar")
public class HypermediaAutoConfigurationWithoutJacksonTests {
private AnnotationConfigWebApplicationContext context;
private AnnotationConfigServletWebApplicationContext context;
@Test
public void jacksonRelatedConfigurationBacksOff() {
this.context = new AnnotationConfigWebApplicationContext();
this.context = new AnnotationConfigServletWebApplicationContext();
this.context.register(BaseConfig.class);
this.context.setServletContext(new MockServletContext());
this.context.refresh();

View File

@@ -33,13 +33,13 @@ import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguratio
import org.springframework.boot.context.event.ApplicationFailedEvent;
import org.springframework.boot.logging.LogLevel;
import org.springframework.boot.testsupport.rule.OutputCapture;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
@@ -121,7 +121,7 @@ public class ConditionEvaluationReportLoggingListenerTests {
@Test
public void canBeUsedInNonGenericApplicationContext() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext();
context.setServletContext(new MockServletContext());
context.register(Config.class);
new ConditionEvaluationReportLoggingListener().initialize(context);

View File

@@ -21,11 +21,11 @@ import org.junit.Test;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.boot.web.servlet.view.MustacheViewResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
@@ -36,7 +36,7 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class MustacheAutoConfigurationTests {
private AnnotationConfigWebApplicationContext webContext;
private AnnotationConfigServletWebApplicationContext webContext;
private AnnotationConfigReactiveWebApplicationContext reactiveWebContext;
@@ -91,7 +91,7 @@ public class MustacheAutoConfigurationTests {
}
private void loadWithServlet(Class<?> config) {
this.webContext = new AnnotationConfigWebApplicationContext();
this.webContext = new AnnotationConfigServletWebApplicationContext();
TestPropertyValues.of("spring.mustache.prefix=classpath:/mustache-templates/")
.applyTo(this.webContext);
if (config != null) {

View File

@@ -29,10 +29,10 @@ import org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAut
import org.springframework.boot.autoconfigure.security.servlet.SecurityFilterAutoConfigurationEarlyInitializationTests.JacksonModuleBean;
import org.springframework.boot.autoconfigure.web.servlet.DispatcherServletAutoConfiguration;
import org.springframework.boot.autoconfigure.web.servlet.WebMvcAutoConfiguration;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
/**
* Tests for {@link SecurityFilterAutoConfiguration}.
@@ -43,7 +43,7 @@ public class SecurityFilterAutoConfigurationTests {
@Test
public void filterAutoConfigurationWorksWithoutSecurityAutoConfiguration() {
try (AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext()) {
try (AnnotationConfigServletWebApplicationContext context = new AnnotationConfigServletWebApplicationContext()) {
context.setServletContext(new MockServletContext());
context.register(Config.class);
context.refresh();

View File

@@ -31,13 +31,13 @@ import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.boot.web.server.WebServerFactoryCustomizer;
import org.springframework.boot.web.server.WebServerFactoryCustomizerBeanPostProcessor;
import org.springframework.boot.web.servlet.context.AnnotationConfigServletWebApplicationContext;
import org.springframework.boot.web.servlet.filter.OrderedFormContentFilter;
import org.springframework.boot.web.servlet.filter.OrderedHiddenHttpMethodFilter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.mock.web.MockServletContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.filter.HiddenHttpMethodFilter;
@@ -51,7 +51,7 @@ import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
*/
public class HttpEncodingAutoConfigurationTests {
private AnnotationConfigWebApplicationContext context;
private AnnotationConfigServletWebApplicationContext context;
@After
public void close() {
@@ -179,9 +179,9 @@ public class HttpEncodingAutoConfigurationTests {
this.context = doLoad(new Class<?>[] { config }, environment);
}
private AnnotationConfigWebApplicationContext doLoad(Class<?>[] configs,
private AnnotationConfigServletWebApplicationContext doLoad(Class<?>[] configs,
String... environment) {
AnnotationConfigWebApplicationContext applicationContext = new AnnotationConfigWebApplicationContext();
AnnotationConfigServletWebApplicationContext applicationContext = new AnnotationConfigServletWebApplicationContext();
TestPropertyValues.of(environment).applyTo(applicationContext);
applicationContext.register(configs);
applicationContext.register(MinimalWebAutoConfiguration.class,