@ -18,6 +18,8 @@ package org.springframework.boot.actuate.autoconfigure;
import java.util.Map ;
import javax.sql.DataSource ;
import org.junit.After ;
import org.junit.Before ;
import org.junit.Test ;
@ -28,14 +30,22 @@ import org.springframework.boot.actuate.health.RabbitHealthIndicator;
import org.springframework.boot.actuate.health.RedisHealthIndicator ;
import org.springframework.boot.actuate.health.SolrHealthIndicator ;
import org.springframework.boot.actuate.health.ApplicationHealthIndicator ;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration ;
import org.springframework.boot.autoconfigure.amqp.RabbitAutoConfiguration ;
import org.springframework.boot.autoconfigure.jdbc.DataSourceBuilder ;
import org.springframework.boot.autoconfigure.jdbc.DataSourceMetadataProvidersConfiguration ;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties ;
import org.springframework.boot.autoconfigure.jdbc.EmbeddedDataSourceConfiguration ;
import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration ;
import org.springframework.boot.autoconfigure.mongo.MongoDataAutoConfiguration ;
import org.springframework.boot.autoconfigure.redis.RedisAutoConfiguration ;
import org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration ;
import org.springframework.boot.context.properties.ConfigurationProperties ;
import org.springframework.boot.context.properties.EnableConfigurationProperties ;
import org.springframework.boot.test.EnvironmentTestUtils ;
import org.springframework.context.annotation.AnnotationConfigApplicationContext ;
import org.springframework.context.annotation.Bean ;
import org.springframework.context.annotation.Configuration ;
import static org.junit.Assert.assertEquals ;
@ -153,6 +163,22 @@ public class HealthIndicatorAutoConfigurationTests {
. getClass ( ) ) ;
}
@Test
public void dataSourceHealthIndicatorWithCustomValidationQuery ( ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
this . context . register ( PropertyPlaceholderAutoConfiguration . class , DataSourceProperties . class ,
DataSourceConfig . class , DataSourceMetadataProvidersConfiguration . class ,
HealthIndicatorAutoConfiguration . class ) ;
EnvironmentTestUtils . addEnvironment ( this . context , "spring.datasource.validation-query:SELECT from FOOBAR" ) ;
this . context . refresh ( ) ;
Map < String , HealthIndicator > beans = this . context . getBeansOfType ( HealthIndicator . class ) ;
assertEquals ( 1 , beans . size ( ) ) ;
HealthIndicator healthIndicator = beans . values ( ) . iterator ( ) . next ( ) ;
assertEquals ( DataSourceHealthIndicator . class , healthIndicator . getClass ( ) ) ;
DataSourceHealthIndicator dataSourceHealthIndicator = ( DataSourceHealthIndicator ) healthIndicator ;
assertEquals ( "SELECT from FOOBAR" , dataSourceHealthIndicator . getQuery ( ) ) ;
}
@Test
public void notDataSourceHealthIndicator ( ) {
this . context = new AnnotationConfigApplicationContext ( ) ;
@ -220,4 +246,19 @@ public class HealthIndicatorAutoConfigurationTests {
assertEquals ( ApplicationHealthIndicator . class , beans . values ( ) . iterator ( ) . next ( )
. getClass ( ) ) ;
}
@Configuration
@EnableConfigurationProperties
protected static class DataSourceConfig {
@Bean
@ConfigurationProperties ( prefix = DataSourceProperties . PREFIX )
public DataSource dataSource ( ) {
return DataSourceBuilder . create ( )
. driverClassName ( "org.hsqldb.jdbc.JDBCDriver" )
. url ( "jdbc:hsqldb:mem:test" )
. username ( "sa" ) . build ( ) ;
}
}
}