Skip to content

Commit

Permalink
Fix detection of WebApplicationType with context class
Browse files Browse the repository at this point in the history
  • Loading branch information
snicoll committed Oct 3, 2018
1 parent 73e6a39 commit b4c5aea
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.springframework.boot.context.properties.bind.Bindable;
import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.boot.context.properties.source.ConfigurationPropertySources;
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextInitializer;
Expand Down Expand Up @@ -1181,18 +1182,19 @@ public void setResourceLoader(ResourceLoader resourceLoader) {
public void setApplicationContextClass(
Class<? extends ConfigurableApplicationContext> applicationContextClass) {
this.applicationContextClass = applicationContextClass;
if (!isWebApplicationContext(applicationContextClass)) {
this.webApplicationType = WebApplicationType.NONE;
}
this.webApplicationType = deduceWebApplicationType(applicationContextClass);
}

private boolean isWebApplicationContext(Class<?> applicationContextClass) {
try {
return WebApplicationContext.class.isAssignableFrom(applicationContextClass);
private WebApplicationType deduceWebApplicationType(
Class<?> applicationContextClass) {
if (WebApplicationContext.class.isAssignableFrom(applicationContextClass)) {
return WebApplicationType.SERVLET;
}
catch (NoClassDefFoundError ex) {
return false;
if (ReactiveWebApplicationContext.class
.isAssignableFrom(applicationContextClass)) {
return WebApplicationType.REACTIVE;
}
return WebApplicationType.NONE;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@
import org.springframework.boot.testsupport.rule.OutputCapture;
import org.springframework.boot.web.embedded.netty.NettyReactiveWebServerFactory;
import org.springframework.boot.web.embedded.tomcat.TomcatServletWebServerFactory;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebApplicationContext;
import org.springframework.boot.web.reactive.context.AnnotationConfigReactiveWebServerApplicationContext;
import org.springframework.boot.web.reactive.context.ReactiveWebApplicationContext;
import org.springframework.boot.web.reactive.context.StandardReactiveWebEnvironment;
Expand Down Expand Up @@ -96,6 +97,7 @@
import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebEnvironment;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.context.support.StandardServletEnvironment;

import static org.assertj.core.api.Assertions.assertThat;
Expand Down Expand Up @@ -316,6 +318,32 @@ public void specificApplicationContextClass() {
assertThat(this.context).isInstanceOf(StaticApplicationContext.class);
}

@Test
public void specificWebApplicationContextClassDetectWebApplicationType() {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application
.setApplicationContextClass(AnnotationConfigWebApplicationContext.class);
assertThat(application.getWebApplicationType())
.isEqualTo(WebApplicationType.SERVLET);
}

@Test
public void specificReactiveApplicationContextClassDetectReactiveApplicationType() {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setApplicationContextClass(
AnnotationConfigReactiveWebApplicationContext.class);
assertThat(application.getWebApplicationType())
.isEqualTo(WebApplicationType.REACTIVE);
}

@Test
public void nonWebNorReactiveApplicationContextClassDetectNoneApplicationType() {
SpringApplication application = new SpringApplication(ExampleConfig.class);
application.setApplicationContextClass(StaticApplicationContext.class);
assertThat(application.getWebApplicationType())
.isEqualTo(WebApplicationType.NONE);
}

@Test
public void specificApplicationContextInitializer() {
SpringApplication application = new SpringApplication(ExampleConfig.class);
Expand Down

0 comments on commit b4c5aea

Please sign in to comment.