From 49797b11539b998fc7f8f857e05c15297e9111f6 Mon Sep 17 00:00:00 2001 From: Stephane Nicoll Date: Mon, 19 Jun 2017 08:28:08 +0200 Subject: [PATCH] Polish "Add support for CustomConversions with Cassandra" Closes gh-8534 --- .../CassandraDataAutoConfiguration.java | 8 +++--- .../CassandraDataAutoConfigurationTests.java | 26 ++++++++++++++----- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java index db8ff1626f..22e8ef3333 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfiguration.java @@ -46,7 +46,6 @@ import org.springframework.data.cassandra.core.convert.CassandraCustomConversion import org.springframework.data.cassandra.core.convert.MappingCassandraConverter; import org.springframework.data.cassandra.core.mapping.CassandraMappingContext; import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver; -import org.springframework.data.convert.CustomConversions; import org.springframework.util.StringUtils; /** @@ -82,7 +81,8 @@ public class CassandraDataAutoConfiguration { @Bean @ConditionalOnMissingBean - public CassandraMappingContext cassandraMapping(CustomConversions conversions) throws ClassNotFoundException { + public CassandraMappingContext cassandraMapping( + CassandraCustomConversions conversions) throws ClassNotFoundException { CassandraMappingContext context = new CassandraMappingContext(); List packages = EntityScanPackages.get(this.beanFactory) .getPackageNames(); @@ -103,7 +103,7 @@ public class CassandraDataAutoConfiguration { @Bean @ConditionalOnMissingBean public CassandraConverter cassandraConverter(CassandraMappingContext mapping, - CustomConversions conversions) { + CassandraCustomConversions conversions) { MappingCassandraConverter converter = new MappingCassandraConverter(mapping); converter.setCustomConversions(conversions); return converter; @@ -132,7 +132,7 @@ public class CassandraDataAutoConfiguration { @Bean @ConditionalOnMissingBean - public CustomConversions cassandraCustomConversions() { + public CassandraCustomConversions cassandraCustomConversions() { return new CassandraCustomConversions(Collections.emptyList()); } diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java index 8d950fdf98..3801172dd0 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/data/cassandra/CassandraDataAutoConfigurationTests.java @@ -16,7 +16,7 @@ package org.springframework.boot.autoconfigure.data.cassandra; -import java.util.Arrays; +import java.util.Collections; import java.util.Set; import com.datastax.driver.core.Session; @@ -27,7 +27,6 @@ import org.springframework.boot.autoconfigure.cassandra.CassandraAutoConfigurati import org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration; import org.springframework.boot.autoconfigure.data.cassandra.city.City; import org.springframework.boot.autoconfigure.domain.EntityScan; -import org.springframework.boot.test.util.EnvironmentTestUtils; import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.Bean; @@ -105,11 +104,25 @@ public class CassandraDataAutoConfigurationTests { .isInstanceOf(SimpleUserTypeResolver.class); } + @Test + public void defaultConversions() { + this.context = new AnnotationConfigApplicationContext(); + TestPropertyValues.of("spring.data.cassandra.keyspaceName:boot_test") + .applyTo(this.context); + this.context.register(TestConfiguration.class, + PropertyPlaceholderAutoConfiguration.class, + CassandraAutoConfiguration.class, CassandraDataAutoConfiguration.class); + this.context.refresh(); + CassandraTemplate template = this.context.getBean(CassandraTemplate.class); + assertThat(template.getConverter().getConversionService().canConvert(Person.class, + String.class)).isFalse(); + } + @Test public void customConversions() { this.context = new AnnotationConfigApplicationContext(); - EnvironmentTestUtils.addEnvironment(this.context, - "spring.data.cassandra.keyspaceName:boot_test"); + TestPropertyValues.of("spring.data.cassandra.keyspaceName:boot_test") + .applyTo(this.context); this.context.register(CustomConversionConfig.class, TestConfiguration.class, PropertyPlaceholderAutoConfiguration.class, @@ -148,8 +161,9 @@ public class CassandraDataAutoConfigurationTests { static class CustomConversionConfig { @Bean - public CassandraCustomConversions cassandraCustomConversions() { - return new CassandraCustomConversions(Arrays.asList(new MyConverter())); + public CassandraCustomConversions myCassandraCustomConversions() { + return new CassandraCustomConversions(Collections.singletonList( + new MyConverter())); } }