|
24 | 24 | import org.junit.After;
|
25 | 25 | import org.junit.Test;
|
26 | 26 |
|
| 27 | +import org.springframework.beans.DirectFieldAccessor; |
27 | 28 | import org.springframework.boot.actuate.health.ApplicationHealthIndicator;
|
28 | 29 | import org.springframework.boot.actuate.health.CassandraHealthIndicator;
|
| 30 | +import org.springframework.boot.actuate.health.CompositeHealthIndicator; |
29 | 31 | import org.springframework.boot.actuate.health.CouchbaseHealthIndicator;
|
30 | 32 | import org.springframework.boot.actuate.health.DataSourceHealthIndicator;
|
31 | 33 | import org.springframework.boot.actuate.health.DiskSpaceHealthIndicator;
|
|
61 | 63 | import org.springframework.context.annotation.Configuration;
|
62 | 64 | import org.springframework.data.cassandra.core.CassandraOperations;
|
63 | 65 | import org.springframework.data.couchbase.core.CouchbaseOperations;
|
| 66 | +import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; |
64 | 67 |
|
65 | 68 | import static org.assertj.core.api.Assertions.assertThat;
|
66 | 69 | import static org.mockito.Mockito.mock;
|
@@ -226,6 +229,39 @@ public void dataSourceHealthIndicator() {
|
226 | 229 | .isEqualTo(DataSourceHealthIndicator.class);
|
227 | 230 | }
|
228 | 231 |
|
| 232 | + @Test |
| 233 | + public void dataSourceHealthIndicatorWithSeveralDataSources() { |
| 234 | + this.context.register(EmbeddedDataSourceConfiguration.class, |
| 235 | + DataSourceConfig.class, ManagementServerProperties.class, |
| 236 | + HealthIndicatorAutoConfiguration.class); |
| 237 | + EnvironmentTestUtils.addEnvironment(this.context, |
| 238 | + "management.health.diskspace.enabled:false"); |
| 239 | + this.context.refresh(); |
| 240 | + Map<String, HealthIndicator> beans = this.context |
| 241 | + .getBeansOfType(HealthIndicator.class); |
| 242 | + assertThat(beans).hasSize(1); |
| 243 | + HealthIndicator bean = beans.values().iterator().next(); |
| 244 | + assertThat(bean).isExactlyInstanceOf(CompositeHealthIndicator.class); |
| 245 | + Map<String, HealthIndicator> indicators = (Map<String, HealthIndicator>) |
| 246 | + new DirectFieldAccessor(bean).getPropertyValue("indicators"); |
| 247 | + assertThat(indicators).hasSize(2); |
| 248 | + } |
| 249 | + |
| 250 | + @Test |
| 251 | + public void dataSourceHealthIndicatorWithAbstractRoutingDataSource() { |
| 252 | + this.context.register(EmbeddedDataSourceConfiguration.class, |
| 253 | + RoutingDatasourceConfig.class, ManagementServerProperties.class, |
| 254 | + HealthIndicatorAutoConfiguration.class); |
| 255 | + EnvironmentTestUtils.addEnvironment(this.context, |
| 256 | + "management.health.diskspace.enabled:false"); |
| 257 | + this.context.refresh(); |
| 258 | + Map<String, HealthIndicator> beans = this.context |
| 259 | + .getBeansOfType(HealthIndicator.class); |
| 260 | + assertThat(beans).hasSize(1); |
| 261 | + assertThat(beans.values().iterator().next().getClass()) |
| 262 | + .isEqualTo(DataSourceHealthIndicator.class); |
| 263 | + } |
| 264 | + |
229 | 265 | @Test
|
230 | 266 | public void dataSourceHealthIndicatorWithCustomValidationQuery() {
|
231 | 267 | this.context.register(PropertyPlaceholderAutoConfiguration.class,
|
@@ -507,14 +543,24 @@ protected static class DataSourceConfig {
|
507 | 543 |
|
508 | 544 | @Bean
|
509 | 545 | @ConfigurationProperties(prefix = "spring.datasource.test")
|
510 |
| - public DataSource dataSource() { |
| 546 | + public DataSource testDataSource() { |
511 | 547 | return DataSourceBuilder.create()
|
512 | 548 | .driverClassName("org.hsqldb.jdbc.JDBCDriver")
|
513 | 549 | .url("jdbc:hsqldb:mem:test").username("sa").build();
|
514 | 550 | }
|
515 | 551 |
|
516 | 552 | }
|
517 | 553 |
|
| 554 | + @Configuration |
| 555 | + protected static class RoutingDatasourceConfig { |
| 556 | + |
| 557 | + @Bean |
| 558 | + AbstractRoutingDataSource routingDataSource() { |
| 559 | + return mock(AbstractRoutingDataSource.class); |
| 560 | + } |
| 561 | + |
| 562 | + } |
| 563 | + |
518 | 564 | @Configuration
|
519 | 565 | protected static class CustomHealthIndicator {
|
520 | 566 |
|
|
0 commit comments