Merge pull request #27698 from thegeekyasian

* gh-27698:
  Polish "Avoid NPE when routing DataSource has target with null key"
  Avoid NPE when routing DataSource has target with null key

Closes gh-27698
pull/28375/head
Andy Wilkinson 3 years ago
commit ca655b639f

@ -19,6 +19,7 @@ package org.springframework.boot.actuate.autoconfigure.jdbc;
import java.util.Collection;
import java.util.Iterator;
import java.util.Map;
import java.util.Objects;
import java.util.function.Function;
import java.util.stream.Collectors;
@ -57,6 +58,7 @@ import org.springframework.util.Assert;
* @author Stephane Nicoll
* @author Arthur Kalimullin
* @author Julio Gomez
* @author Safeer Ansari
* @since 2.0.0
*/
@Configuration(proxyBeanMethods = false)
@ -124,10 +126,13 @@ public class DataSourceHealthContributorAutoConfiguration implements Initializin
private final CompositeHealthContributor delegate;
private static final String UNNAMED_DATASOURCE_KEY = "unnamed";
RoutingDataSourceHealthContributor(AbstractRoutingDataSource routingDataSource,
Function<DataSource, HealthContributor> contributorFunction) {
Map<String, DataSource> routedDataSources = routingDataSource.getResolvedDataSources().entrySet().stream()
.collect(Collectors.toMap((e) -> e.getKey().toString(), Map.Entry::getValue));
.collect(Collectors.toMap((e) -> Objects.toString(e.getKey(), UNNAMED_DATASOURCE_KEY),
Map.Entry::getValue));
this.delegate = CompositeHealthContributor.fromMap(routedDataSources, contributorFunction);
}

@ -49,6 +49,7 @@ import static org.mockito.Mockito.mock;
*
* @author Phillip Webb
* @author Julio Gomez
* @author Safeer Ansari
*/
class DataSourceHealthContributorAutoConfigurationTests {
@ -136,6 +137,20 @@ class DataSourceHealthContributorAutoConfigurationTests {
.doesNotHaveBean(CompositeHealthContributor.class));
}
@Test
void runWhenDataSourceHasNullRoutingKeyShouldProduceUnnamedComposedIndicator() {
this.contextRunner.withUserConfiguration(NullKeyRoutingDataSourceConfig.class).run((context) -> {
assertThat(context).hasSingleBean(RoutingDataSourceHealthContributor.class);
RoutingDataSourceHealthContributor routingHealthContributor = context
.getBean(RoutingDataSourceHealthContributor.class);
assertThat(routingHealthContributor.getContributor("unnamed"))
.isInstanceOf(DataSourceHealthIndicator.class);
assertThat(routingHealthContributor.getContributor("one")).isInstanceOf(DataSourceHealthIndicator.class);
assertThat(routingHealthContributor.iterator()).toIterable().extracting("name")
.containsExactlyInAnyOrder("unnamed", "one");
});
}
@Configuration(proxyBeanMethods = false)
@EnableConfigurationProperties
static class DataSourceConfig {
@ -164,4 +179,19 @@ class DataSourceHealthContributorAutoConfigurationTests {
}
@Configuration(proxyBeanMethods = false)
static class NullKeyRoutingDataSourceConfig {
@Bean
AbstractRoutingDataSource routingDataSource() {
Map<Object, DataSource> dataSources = new HashMap<>();
dataSources.put(null, mock(DataSource.class));
dataSources.put("one", mock(DataSource.class));
AbstractRoutingDataSource routingDataSource = mock(AbstractRoutingDataSource.class);
given(routingDataSource.getResolvedDataSources()).willReturn(dataSources);
return routingDataSource;
}
}
}

Loading…
Cancel
Save