DATACMNS-1615 - Allow fine grained store specific converter registration.

CustomConversions now registers all given user defined converters and selects only those converters from the StoreConversions that convert to or from a store supported simple type.
By doing so we make sure to only register supported converters which removes warning messages from the log and reduces the number of converters within the ConversionService.

Original pull request: #421.
This commit is contained in:
Christoph Strobl
2019-11-22 09:25:43 +01:00
committed by Mark Paluch
parent d5786edec7
commit dbcf9aefff
2 changed files with 357 additions and 41 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.data.convert;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.text.DateFormat;
import java.text.Format;
@@ -26,18 +27,21 @@ import java.util.Date;
import java.util.Locale;
import org.joda.time.DateTime;
import org.junit.Test;
import org.junit.jupiter.api.Test;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.core.convert.converter.Converter;
import org.springframework.core.convert.converter.ConverterFactory;
import org.springframework.core.convert.converter.ConverterRegistry;
import org.springframework.core.convert.converter.GenericConverter.ConvertiblePair;
import org.springframework.core.convert.support.ConfigurableConversionService;
import org.springframework.core.convert.support.DefaultConversionService;
import org.springframework.core.convert.support.GenericConversionService;
import org.springframework.data.convert.ConverterBuilder.ConverterAware;
import org.springframework.data.convert.CustomConversions.ConverterConfiguration;
import org.springframework.data.convert.CustomConversions.StoreConversions;
import org.springframework.data.convert.Jsr310Converters.LocalDateTimeToDateConverter;
import org.springframework.data.convert.ThreeTenBackPortConverters.LocalDateTimeToJavaTimeInstantConverter;
import org.springframework.data.mapping.model.SimpleTypeHolder;
import org.threeten.bp.LocalDateTime;
/**
@@ -50,6 +54,15 @@ import org.threeten.bp.LocalDateTime;
*/
public class CustomConversionsUnitTests {
static final SimpleTypeHolder DATE_EXCLUDING_SIMPLE_TYPE_HOLDER = new SimpleTypeHolder(
Collections.singleton(Date.class), true) {
@Override
public boolean isSimpleType(Class<?> type) {
return type.getName().startsWith("java.time") ? false : super.isSimpleType(type);
}
};
@Test // DATACMNS-1035
public void findsBasicReadAndWriteConversions() {
@@ -194,6 +207,53 @@ public class CustomConversionsUnitTests {
assertThat(conversionService.canConvert(Locale.class, CustomType.class)).isTrue();
}
@Test // DATACMNS-1615
public void skipsUnsupportedDefaultWritingConverter() {
ConverterRegistry registry = mock(ConverterRegistry.class);
new CustomConversions(StoreConversions.of(DATE_EXCLUDING_SIMPLE_TYPE_HOLDER), Collections.emptyList())
.registerConvertersIn(registry);
verify(registry, never()).addConverter(any(LocalDateTimeToJavaTimeInstantConverter.class));
}
@Test // DATACMNS-1615
public void doesNotSkipUnsupportedUserConverter() {
ConverterRegistry registry = mock(ConverterRegistry.class);
new CustomConversions(StoreConversions.of(DATE_EXCLUDING_SIMPLE_TYPE_HOLDER),
Collections.singletonList(LocalDateTimeToJavaTimeInstantConverter.INSTANCE)).registerConvertersIn(registry);
verify(registry).addConverter(any(LocalDateTimeToJavaTimeInstantConverter.class));
}
@Test // DATACMNS-1615
public void skipsConverterBasedOnConfiguration() {
ConverterRegistry registry = mock(ConverterRegistry.class);
ConverterConfiguration config = new ConverterConfiguration(StoreConversions.NONE, Collections.emptyList(),
Collections.singleton(new ConvertiblePair(java.time.LocalDateTime.class, Date.class)));
new CustomConversions(config).registerConvertersIn(registry);
verify(registry, never()).addConverter(any(LocalDateTimeToDateConverter.class));
}
@Test // DATACMNS-1615
public void doesNotSkipUserConverterConverterEvenWhenConfigurationWouldNotAllowIt() {
ConverterRegistry registry = mock(ConverterRegistry.class);
ConverterConfiguration config = new ConverterConfiguration(StoreConversions.NONE,
Collections.singletonList(LocalDateTimeToDateConverter.INSTANCE),
Collections.singleton(new ConvertiblePair(java.time.LocalDateTime.class, Date.class)));
new CustomConversions(config).registerConvertersIn(registry);
verify(registry).addConverter(any(LocalDateTimeToDateConverter.class));
}
private static Class<?> createProxyTypeFor(Class<?> type) {
ProxyFactory factory = new ProxyFactory();