@ -1,5 +1,5 @@
/ *
* Copyright 2012 - 201 6 the original author or authors .
* Copyright 2012 - 201 7 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 .
@ -16,59 +16,106 @@
package org.springframework.boot.autoconfigure.condition ;
import org.junit.After ;
import org.junit.Test ;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type ;
import org.springframework.boot.context.embedded.ReactiveWebApplicationContext ;
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.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 ;
/ * *
* Tests for { @link ConditionalOnWebApplication } .
*
* @author Dave Syer
* @author Stephane Nicoll
* /
public class ConditionalOnWebApplicationTests {
private final AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext ( ) ;
private ConfigurableApplicationContext context ;
@After
public void closeContext ( ) {
if ( this . context ! = null ) {
this . context . close ( ) ;
}
}
@Test
public void testWebApplication ( ) {
this . context . register ( BasicConfiguration . class ) ;
this . context . setServletContext ( new MockServletContext ( ) ) ;
this . context . refresh ( ) ;
assertThat ( this . context . containsBean ( "foo" ) ) . isTrue ( ) ;
assertThat ( this . context . getBean ( "foo" ) ) . isEqualTo ( "foo" ) ;
public void testWebApplicationWithServletContext ( ) {
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext ( ) ;
ctx . register ( AnyWebApplicationConfiguration . class ,
ServletWebApplicationConfiguration . class ,
ReactiveWebApplicationConfiguration . class ) ;
ctx . setServletContext ( new MockServletContext ( ) ) ;
ctx . refresh ( ) ;
this . context = ctx ;
assertThat ( this . context . getBeansOfType ( String . class ) ) . containsExactly (
entry ( "any" , "any" ) , entry ( "servlet" , "servlet" ) ) ;
}
@Test
public void testNotWebApplication ( ) {
this . context . register ( MissingConfiguration . class ) ;
this . context . setServletContext ( new MockServletContext ( ) ) ;
this . context . refresh ( ) ;
assertThat ( this . context . containsBean ( "foo" ) ) . isFalse ( ) ;
public void testWebApplicationWithReactiveContext ( ) {
ReactiveWebApplicationContext ctx = new ReactiveWebApplicationContext ( ) ;
ctx . register ( AnyWebApplicationConfiguration . class ,
ServletWebApplicationConfiguration . class ,
ReactiveWebApplicationConfiguration . class ) ;
ctx . refresh ( ) ;
this . context = ctx ;
assertThat ( this . context . getBeansOfType ( String . class ) ) . containsExactly (
entry ( "any" , "any" ) , entry ( "reactive" , "reactive" ) ) ;
}
@Test
public void testNonWebApplication ( ) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext ( ) ;
ctx . register ( AnyWebApplicationConfiguration . class ,
ServletWebApplicationConfiguration . class ,
ReactiveWebApplicationConfiguration . class ) ;
ctx . refresh ( ) ;
this . context = ctx ;
assertThat ( this . context . getBeansOfType ( String . class ) ) . isEmpty ( ) ;
}
@Configuration
@ConditionalOnNotWebApplication
protected static class MissingConfiguration {
@ConditionalOn WebApplication
protected static class AnyWebApplication Configuration {
@Bean
public String bar ( ) {
return "bar" ;
public String any ( ) {
return " any ";
}
}
@Configuration
@ConditionalOnWebApplication
protected static class BasicConfiguration {
@ConditionalOnWebApplication ( type = Type . SERVLET )
protected static class ServletWebApplicationConfiguration {
@Bean
public String servlet ( ) {
return "servlet" ;
}
}
@Configuration
@ConditionalOnWebApplication ( type = Type . REACTIVE )
protected static class ReactiveWebApplicationConfiguration {
@Bean
public String foo ( ) {
return "foo" ;
public String reactive ( ) {
return " reactive ";
}
}