Search in sources :

Example 1 with ConverterAware

use of org.springframework.data.convert.ConverterBuilder.ConverterAware in project spring-data-commons by spring-projects.

the class ConverterBuilderUnitTests method setsUpBidirectionalConvertersFromReading.

// DATACMNS-1034
@Test
public void setsUpBidirectionalConvertersFromReading() {
    ConverterAware builder = ConverterBuilder.reading(String.class, Long.class, it -> Long.valueOf(it)).andWriting(Object::toString);
    assertConverter(builder.getReadingConverter(), "1", 1L);
    assertConverter(builder.getWritingConverter(), 1L, "1");
}
Also used : Supplier(org.mockito.internal.util.Supplier) WritingConverterBuilder(org.springframework.data.convert.ConverterBuilder.WritingConverterBuilder) TypeDescriptor(org.springframework.core.convert.TypeDescriptor) Assertions(org.assertj.core.api.Assertions) Test(org.junit.Test) GenericConverter(org.springframework.core.convert.converter.GenericConverter) ReadingConverterBuilder(org.springframework.data.convert.ConverterBuilder.ReadingConverterBuilder) ConvertiblePair(org.springframework.core.convert.converter.GenericConverter.ConvertiblePair) ConverterAware(org.springframework.data.convert.ConverterBuilder.ConverterAware) ConverterAware(org.springframework.data.convert.ConverterBuilder.ConverterAware) Test(org.junit.Test)

Example 2 with ConverterAware

use of org.springframework.data.convert.ConverterBuilder.ConverterAware in project spring-data-commons by spring-projects.

the class ConverterBuilderUnitTests method setsUpBidirectionalConvertersFromWriting.

// DATACMNS-1034
@Test
public void setsUpBidirectionalConvertersFromWriting() {
    ConverterAware builder = ConverterBuilder.writing(Long.class, String.class, Object::toString).andReading(it -> Long.valueOf(it));
    assertConverter(builder.getReadingConverter(), "1", 1L);
    assertConverter(builder.getWritingConverter(), 1L, "1");
}
Also used : ConverterAware(org.springframework.data.convert.ConverterBuilder.ConverterAware) Test(org.junit.Test)

Example 3 with ConverterAware

use of org.springframework.data.convert.ConverterBuilder.ConverterAware in project spring-data-commons by spring-projects.

the class CustomConversionsUnitTests method registersConverterFromConverterAware.

// DATACMNS-1034
@Test
public void registersConverterFromConverterAware() {
    ConverterAware converters = ConverterBuilder.reading(Left.class, Right.class, left -> new Right()).andWriting(right -> new Left());
    CustomConversions conversions = new CustomConversions(StoreConversions.NONE, Collections.singletonList(converters));
    assertThat(conversions.hasCustomWriteTarget(Right.class)).isTrue();
    assertThat(conversions.hasCustomReadTarget(Left.class, Right.class)).isTrue();
    ConfigurableConversionService conversionService = new GenericConversionService();
    conversions.registerConvertersIn(conversionService);
    assertThat(conversionService.canConvert(Left.class, Right.class)).isTrue();
    assertThat(conversionService.canConvert(Right.class, Left.class)).isTrue();
}
Also used : Converter(org.springframework.core.convert.converter.Converter) Arrays(java.util.Arrays) Format(java.text.Format) ConfigurableConversionService(org.springframework.core.convert.support.ConfigurableConversionService) Date(java.util.Date) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) DateTime(org.joda.time.DateTime) ConverterFactory(org.springframework.core.convert.converter.ConverterFactory) StoreConversions(org.springframework.data.convert.CustomConversions.StoreConversions) Matchers(org.hamcrest.Matchers) SimpleDateFormat(java.text.SimpleDateFormat) Test(org.junit.Test) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Assert.assertThat(org.junit.Assert.assertThat) Locale(java.util.Locale) ProxyFactory(org.springframework.aop.framework.ProxyFactory) LocalDateTime(org.threeten.bp.LocalDateTime) Collections(java.util.Collections) ConverterAware(org.springframework.data.convert.ConverterBuilder.ConverterAware) DateFormat(java.text.DateFormat) DefaultConversionService(org.springframework.core.convert.support.DefaultConversionService) ConverterAware(org.springframework.data.convert.ConverterBuilder.ConverterAware) ConfigurableConversionService(org.springframework.core.convert.support.ConfigurableConversionService) GenericConversionService(org.springframework.core.convert.support.GenericConversionService) Test(org.junit.Test)

Aggregations

Test (org.junit.Test)3 ConverterAware (org.springframework.data.convert.ConverterBuilder.ConverterAware)3 DateFormat (java.text.DateFormat)1 Format (java.text.Format)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Arrays (java.util.Arrays)1 Collections (java.util.Collections)1 Date (java.util.Date)1 Locale (java.util.Locale)1 Assertions (org.assertj.core.api.Assertions)1 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)1 Matchers (org.hamcrest.Matchers)1 DateTime (org.joda.time.DateTime)1 Assert.assertThat (org.junit.Assert.assertThat)1 Supplier (org.mockito.internal.util.Supplier)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 TypeDescriptor (org.springframework.core.convert.TypeDescriptor)1 Converter (org.springframework.core.convert.converter.Converter)1 ConverterFactory (org.springframework.core.convert.converter.ConverterFactory)1 GenericConverter (org.springframework.core.convert.converter.GenericConverter)1