Polish "Add support for CustomConversions with Cassandra"

Closes gh-8534
pull/9556/head
Stephane Nicoll 8 years ago
parent 744a1ed497
commit 49797b1153

@ -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.convert.MappingCassandraConverter;
import org.springframework.data.cassandra.core.mapping.CassandraMappingContext; import org.springframework.data.cassandra.core.mapping.CassandraMappingContext;
import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver; import org.springframework.data.cassandra.core.mapping.SimpleUserTypeResolver;
import org.springframework.data.convert.CustomConversions;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
@ -82,7 +81,8 @@ public class CassandraDataAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public CassandraMappingContext cassandraMapping(CustomConversions conversions) throws ClassNotFoundException { public CassandraMappingContext cassandraMapping(
CassandraCustomConversions conversions) throws ClassNotFoundException {
CassandraMappingContext context = new CassandraMappingContext(); CassandraMappingContext context = new CassandraMappingContext();
List<String> packages = EntityScanPackages.get(this.beanFactory) List<String> packages = EntityScanPackages.get(this.beanFactory)
.getPackageNames(); .getPackageNames();
@ -103,7 +103,7 @@ public class CassandraDataAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public CassandraConverter cassandraConverter(CassandraMappingContext mapping, public CassandraConverter cassandraConverter(CassandraMappingContext mapping,
CustomConversions conversions) { CassandraCustomConversions conversions) {
MappingCassandraConverter converter = new MappingCassandraConverter(mapping); MappingCassandraConverter converter = new MappingCassandraConverter(mapping);
converter.setCustomConversions(conversions); converter.setCustomConversions(conversions);
return converter; return converter;
@ -132,7 +132,7 @@ public class CassandraDataAutoConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public CustomConversions cassandraCustomConversions() { public CassandraCustomConversions cassandraCustomConversions() {
return new CassandraCustomConversions(Collections.emptyList()); return new CassandraCustomConversions(Collections.emptyList());
} }

@ -16,7 +16,7 @@
package org.springframework.boot.autoconfigure.data.cassandra; package org.springframework.boot.autoconfigure.data.cassandra;
import java.util.Arrays; import java.util.Collections;
import java.util.Set; import java.util.Set;
import com.datastax.driver.core.Session; 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.context.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.data.cassandra.city.City; import org.springframework.boot.autoconfigure.data.cassandra.city.City;
import org.springframework.boot.autoconfigure.domain.EntityScan; import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.util.EnvironmentTestUtils;
import org.springframework.boot.test.util.TestPropertyValues; import org.springframework.boot.test.util.TestPropertyValues;
import org.springframework.context.annotation.AnnotationConfigApplicationContext; import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
@ -105,11 +104,25 @@ public class CassandraDataAutoConfigurationTests {
.isInstanceOf(SimpleUserTypeResolver.class); .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 @Test
public void customConversions() { public void customConversions() {
this.context = new AnnotationConfigApplicationContext(); this.context = new AnnotationConfigApplicationContext();
EnvironmentTestUtils.addEnvironment(this.context, TestPropertyValues.of("spring.data.cassandra.keyspaceName:boot_test")
"spring.data.cassandra.keyspaceName:boot_test"); .applyTo(this.context);
this.context.register(CustomConversionConfig.class, this.context.register(CustomConversionConfig.class,
TestConfiguration.class, TestConfiguration.class,
PropertyPlaceholderAutoConfiguration.class, PropertyPlaceholderAutoConfiguration.class,
@ -148,8 +161,9 @@ public class CassandraDataAutoConfigurationTests {
static class CustomConversionConfig { static class CustomConversionConfig {
@Bean @Bean
public CassandraCustomConversions cassandraCustomConversions() { public CassandraCustomConversions myCassandraCustomConversions() {
return new CassandraCustomConversions(Arrays.asList(new MyConverter())); return new CassandraCustomConversions(Collections.singletonList(
new MyConverter()));
} }
} }

Loading…
Cancel
Save