Merge branch '1.5.x'

pull/6770/merge
Stephane Nicoll 8 years ago
commit fb70097c4f

@ -17,6 +17,7 @@
package org.springframework.boot.actuate.autoconfigure; package org.springframework.boot.actuate.autoconfigure;
import java.util.Collection; import java.util.Collection;
import java.util.LinkedHashMap;
import java.util.Map; import java.util.Map;
import javax.jms.ConnectionFactory; import javax.jms.ConnectionFactory;
@ -75,6 +76,7 @@ import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.data.mongodb.core.MongoTemplate; import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.redis.connection.RedisConnectionFactory; import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import org.springframework.mail.javamail.JavaMailSenderImpl; import org.springframework.mail.javamail.JavaMailSenderImpl;
/** /**
@ -170,7 +172,7 @@ public class HealthIndicatorAutoConfiguration {
} }
@Configuration @Configuration
@ConditionalOnClass(JdbcTemplate.class) @ConditionalOnClass({ JdbcTemplate.class, AbstractRoutingDataSource.class })
@ConditionalOnBean(DataSource.class) @ConditionalOnBean(DataSource.class)
@ConditionalOnEnabledHealthIndicator("db") @ConditionalOnEnabledHealthIndicator("db")
public static class DataSourcesHealthIndicatorConfiguration extends public static class DataSourcesHealthIndicatorConfiguration extends
@ -186,10 +188,21 @@ public class HealthIndicatorAutoConfiguration {
public DataSourcesHealthIndicatorConfiguration( public DataSourcesHealthIndicatorConfiguration(
ObjectProvider<Map<String, DataSource>> dataSourcesProvider, ObjectProvider<Map<String, DataSource>> dataSourcesProvider,
ObjectProvider<Collection<DataSourcePoolMetadataProvider>> metadataProvidersProvider) { ObjectProvider<Collection<DataSourcePoolMetadataProvider>> metadataProvidersProvider) {
this.dataSources = dataSourcesProvider.getIfAvailable(); this.dataSources = filterDataSources(dataSourcesProvider.getIfAvailable());
this.metadataProviders = metadataProvidersProvider.getIfAvailable(); this.metadataProviders = metadataProvidersProvider.getIfAvailable();
} }
private static Map<String, DataSource> filterDataSources(
Map<String,DataSource> candidates) {
Map<String,DataSource> dataSources = new LinkedHashMap<String, DataSource>();
for (Map.Entry<String, DataSource> entry : candidates.entrySet()) {
if (!(entry.getValue() instanceof AbstractRoutingDataSource)) {
dataSources.put(entry.getKey(), entry.getValue());
}
}
return dataSources;
}
@Override @Override
public void afterPropertiesSet() throws Exception { public void afterPropertiesSet() throws Exception {
this.poolMetadataProvider = new DataSourcePoolMetadataProviders( this.poolMetadataProvider = new DataSourcePoolMetadataProviders(

@ -24,8 +24,10 @@ import io.searchbox.client.JestClient;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.boot.actuate.health.ApplicationHealthIndicator; import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
import org.springframework.boot.actuate.health.CassandraHealthIndicator; import org.springframework.boot.actuate.health.CassandraHealthIndicator;
import org.springframework.boot.actuate.health.CompositeHealthIndicator;
import org.springframework.boot.actuate.health.CouchbaseHealthIndicator; import org.springframework.boot.actuate.health.CouchbaseHealthIndicator;
import org.springframework.boot.actuate.health.DataSourceHealthIndicator; import org.springframework.boot.actuate.health.DataSourceHealthIndicator;
import org.springframework.boot.actuate.health.DiskSpaceHealthIndicator; import org.springframework.boot.actuate.health.DiskSpaceHealthIndicator;
@ -61,6 +63,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.data.cassandra.core.CassandraOperations; import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.couchbase.core.CouchbaseOperations; import org.springframework.data.couchbase.core.CouchbaseOperations;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock; import static org.mockito.Mockito.mock;
@ -226,6 +229,39 @@ public class HealthIndicatorAutoConfigurationTests {
.isEqualTo(DataSourceHealthIndicator.class); .isEqualTo(DataSourceHealthIndicator.class);
} }
@Test
public void dataSourceHealthIndicatorWithSeveralDataSources() {
this.context.register(EmbeddedDataSourceConfiguration.class,
DataSourceConfig.class, ManagementServerProperties.class,
HealthIndicatorAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"management.health.diskspace.enabled:false");
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
HealthIndicator bean = beans.values().iterator().next();
assertThat(bean).isExactlyInstanceOf(CompositeHealthIndicator.class);
Map<String, HealthIndicator> indicators = (Map<String, HealthIndicator>)
new DirectFieldAccessor(bean).getPropertyValue("indicators");
assertThat(indicators).hasSize(2);
}
@Test
public void dataSourceHealthIndicatorWithAbstractRoutingDataSource() {
this.context.register(EmbeddedDataSourceConfiguration.class,
RoutingDatasourceConfig.class, ManagementServerProperties.class,
HealthIndicatorAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(this.context,
"management.health.diskspace.enabled:false");
this.context.refresh();
Map<String, HealthIndicator> beans = this.context
.getBeansOfType(HealthIndicator.class);
assertThat(beans).hasSize(1);
assertThat(beans.values().iterator().next().getClass())
.isEqualTo(DataSourceHealthIndicator.class);
}
@Test @Test
public void dataSourceHealthIndicatorWithCustomValidationQuery() { public void dataSourceHealthIndicatorWithCustomValidationQuery() {
this.context.register(PropertyPlaceholderAutoConfiguration.class, this.context.register(PropertyPlaceholderAutoConfiguration.class,
@ -507,7 +543,7 @@ public class HealthIndicatorAutoConfigurationTests {
@Bean @Bean
@ConfigurationProperties(prefix = "spring.datasource.test") @ConfigurationProperties(prefix = "spring.datasource.test")
public DataSource dataSource() { public DataSource testDataSource() {
return DataSourceBuilder.create() return DataSourceBuilder.create()
.driverClassName("org.hsqldb.jdbc.JDBCDriver") .driverClassName("org.hsqldb.jdbc.JDBCDriver")
.url("jdbc:hsqldb:mem:test").username("sa").build(); .url("jdbc:hsqldb:mem:test").username("sa").build();
@ -515,6 +551,16 @@ public class HealthIndicatorAutoConfigurationTests {
} }
@Configuration
protected static class RoutingDatasourceConfig {
@Bean
AbstractRoutingDataSource routingDataSource() {
return mock(AbstractRoutingDataSource.class);
}
}
@Configuration @Configuration
protected static class CustomHealthIndicator { protected static class CustomHealthIndicator {

Loading…
Cancel
Save