Commit 64e08994 authored by Stephane Nicoll's avatar Stephane Nicoll

Polish customize Cassandra's Cluster

Closes gh-7320
parent cb3d14a3
...@@ -49,12 +49,12 @@ public class CassandraAutoConfiguration { ...@@ -49,12 +49,12 @@ public class CassandraAutoConfiguration {
private final CassandraProperties properties; private final CassandraProperties properties;
private final List<ClusterCustomizer> customizers; private final List<ClusterCustomizer> clusterCustomizers;
public CassandraAutoConfiguration(CassandraProperties properties, public CassandraAutoConfiguration(CassandraProperties properties,
ObjectProvider<List<ClusterCustomizer>> customizers) { ObjectProvider<List<ClusterCustomizer>> clusterCustomizersProvider) {
this.properties = properties; this.properties = properties;
this.customizers = customizers.getIfAvailable(); this.clusterCustomizers = clusterCustomizersProvider.getIfAvailable();
} }
@Bean @Bean
...@@ -96,8 +96,8 @@ public class CassandraAutoConfiguration { ...@@ -96,8 +96,8 @@ public class CassandraAutoConfiguration {
} }
private void customize(Cluster cluster) { private void customize(Cluster cluster) {
if (this.customizers != null) { if (this.clusterCustomizers != null) {
for (ClusterCustomizer customizer : this.customizers) { for (ClusterCustomizer customizer : this.clusterCustomizers) {
customizer.customize(cluster); customizer.customize(cluster);
} }
} }
......
...@@ -19,16 +19,17 @@ package org.springframework.boot.autoconfigure.cassandra; ...@@ -19,16 +19,17 @@ package org.springframework.boot.autoconfigure.cassandra;
import com.datastax.driver.core.Cluster; import com.datastax.driver.core.Cluster;
/** /**
* Callback interface that can be used to customize a {@link Cluster}. * Callback interface that can be implemented by beans wishing to customize the
* {@link Cluster} before it is fully initialized, in particular to tune its
* configuration.
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @since 1.5.0 * @since 1.5.0
* @see CassandraAutoConfiguration
*/ */
public interface ClusterCustomizer { public interface ClusterCustomizer {
/** /**
* Customize the cluster. * Customize the {@link Cluster}.
* @param cluster the cluster to customize * @param cluster the cluster to customize
*/ */
void customize(Cluster cluster); void customize(Cluster cluster);
......
...@@ -33,6 +33,7 @@ import static org.mockito.Mockito.mock; ...@@ -33,6 +33,7 @@ import static org.mockito.Mockito.mock;
* Tests for {@link CassandraAutoConfiguration} * Tests for {@link CassandraAutoConfiguration}
* *
* @author Eddú Meléndez * @author Eddú Meléndez
* @author Stephane Nicoll
*/ */
public class CassandraAutoConfigurationTests { public class CassandraAutoConfigurationTests {
...@@ -47,8 +48,7 @@ public class CassandraAutoConfigurationTests { ...@@ -47,8 +48,7 @@ public class CassandraAutoConfigurationTests {
@Test @Test
public void createClusterWithDefault() { public void createClusterWithDefault() {
this.context = doLoad(); load();
this.context.refresh();
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
Cluster cluster = this.context.getBean(Cluster.class); Cluster cluster = this.context.getBean(Cluster.class);
assertThat(cluster.getClusterName()).startsWith("cluster"); assertThat(cluster.getClusterName()).startsWith("cluster");
...@@ -56,8 +56,7 @@ public class CassandraAutoConfigurationTests { ...@@ -56,8 +56,7 @@ public class CassandraAutoConfigurationTests {
@Test @Test
public void createClusterWithOverrides() { public void createClusterWithOverrides() {
this.context = doLoad("spring.data.cassandra.cluster-name=testcluster"); load("spring.data.cassandra.cluster-name=testcluster");
this.context.refresh();
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
Cluster cluster = this.context.getBean(Cluster.class); Cluster cluster = this.context.getBean(Cluster.class);
assertThat(cluster.getClusterName()).isEqualTo("testcluster"); assertThat(cluster.getClusterName()).isEqualTo("testcluster");
...@@ -65,30 +64,27 @@ public class CassandraAutoConfigurationTests { ...@@ -65,30 +64,27 @@ public class CassandraAutoConfigurationTests {
@Test @Test
public void createCustomizeCluster() { public void createCustomizeCluster() {
this.context = doLoad(ClusterConfig.class); load(ClusterConfig.class);
this.context.refresh();
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1); assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
assertThat(this.context.getBeanNamesForType(ClusterCustomizer.class).length).isEqualTo(1); assertThat(this.context.getBeanNamesForType(ClusterCustomizer.class).length).isEqualTo(1);
} }
private AnnotationConfigApplicationContext doLoad() { private void load(String... environment) {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(); load(null, environment);
applicationContext.register(PropertyPlaceholderAutoConfiguration.class,
CassandraAutoConfiguration.class);
return applicationContext;
} }
private AnnotationConfigApplicationContext doLoad(Class<?> clazz) { private void load(Class<?> config, String... environment) {
AnnotationConfigApplicationContext applicationContext = doLoad(); AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
applicationContext.register(clazz); if (config != null) {
return applicationContext; ctx.register(config);
}
ctx.register(PropertyPlaceholderAutoConfiguration.class,
CassandraAutoConfiguration.class);
EnvironmentTestUtils.addEnvironment(ctx, environment);
ctx.refresh();
this.context = ctx;
} }
private AnnotationConfigApplicationContext doLoad(String... environment) {
AnnotationConfigApplicationContext applicationContext = doLoad();
EnvironmentTestUtils.addEnvironment(applicationContext, environment);
return applicationContext;
}
@Configuration @Configuration
static class ClusterConfig { static class ClusterConfig {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment