Polish customize Cassandra's Cluster

Closes gh-7320
pull/7512/head
Stephane Nicoll 8 years ago
parent cb3d14a305
commit 64e0899456

@ -49,12 +49,12 @@ public class CassandraAutoConfiguration {
private final CassandraProperties properties;
private final List<ClusterCustomizer> customizers;
private final List<ClusterCustomizer> clusterCustomizers;
public CassandraAutoConfiguration(CassandraProperties properties,
ObjectProvider<List<ClusterCustomizer>> customizers) {
ObjectProvider<List<ClusterCustomizer>> clusterCustomizersProvider) {
this.properties = properties;
this.customizers = customizers.getIfAvailable();
this.clusterCustomizers = clusterCustomizersProvider.getIfAvailable();
}
@Bean
@ -96,8 +96,8 @@ public class CassandraAutoConfiguration {
}
private void customize(Cluster cluster) {
if (this.customizers != null) {
for (ClusterCustomizer customizer : this.customizers) {
if (this.clusterCustomizers != null) {
for (ClusterCustomizer customizer : this.clusterCustomizers) {
customizer.customize(cluster);
}
}

@ -19,16 +19,17 @@ package org.springframework.boot.autoconfigure.cassandra;
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
* @since 1.5.0
* @see CassandraAutoConfiguration
*/
public interface ClusterCustomizer {
/**
* Customize the cluster.
* Customize the {@link Cluster}.
* @param cluster the cluster to customize
*/
void customize(Cluster cluster);

@ -33,6 +33,7 @@ import static org.mockito.Mockito.mock;
* Tests for {@link CassandraAutoConfiguration}
*
* @author Eddú Meléndez
* @author Stephane Nicoll
*/
public class CassandraAutoConfigurationTests {
@ -47,8 +48,7 @@ public class CassandraAutoConfigurationTests {
@Test
public void createClusterWithDefault() {
this.context = doLoad();
this.context.refresh();
load();
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
Cluster cluster = this.context.getBean(Cluster.class);
assertThat(cluster.getClusterName()).startsWith("cluster");
@ -56,8 +56,7 @@ public class CassandraAutoConfigurationTests {
@Test
public void createClusterWithOverrides() {
this.context = doLoad("spring.data.cassandra.cluster-name=testcluster");
this.context.refresh();
load("spring.data.cassandra.cluster-name=testcluster");
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
Cluster cluster = this.context.getBean(Cluster.class);
assertThat(cluster.getClusterName()).isEqualTo("testcluster");
@ -65,30 +64,27 @@ public class CassandraAutoConfigurationTests {
@Test
public void createCustomizeCluster() {
this.context = doLoad(ClusterConfig.class);
this.context.refresh();
load(ClusterConfig.class);
assertThat(this.context.getBeanNamesForType(Cluster.class).length).isEqualTo(1);
assertThat(this.context.getBeanNamesForType(ClusterCustomizer.class).length).isEqualTo(1);
}
private AnnotationConfigApplicationContext doLoad() {
AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
applicationContext.register(PropertyPlaceholderAutoConfiguration.class,
CassandraAutoConfiguration.class);
return applicationContext;
private void load(String... environment) {
load(null, environment);
}
private AnnotationConfigApplicationContext doLoad(Class<?> clazz) {
AnnotationConfigApplicationContext applicationContext = doLoad();
applicationContext.register(clazz);
return applicationContext;
private void load(Class<?> config, String... environment) {
AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext();
if (config != null) {
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
static class ClusterConfig {

Loading…
Cancel
Save