From cc672f475f9c04d2f0994c7cf9880f804d9bf1e7 Mon Sep 17 00:00:00 2001 From: Jens Schauder Date: Mon, 16 Sep 2024 09:38:46 +0200 Subject: [PATCH] Polishing. Restructure test. Remove redundant code. Formatting. Original pull request #1876 See #1842 --- .../MappingRelationalConverter.java | 17 +++---- .../MappingRelationalConverterUnitTests.java | 45 +++++++++---------- 2 files changed, 27 insertions(+), 35 deletions(-) diff --git a/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java b/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java index f2ae42d5a3..13d84b800e 100644 --- a/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java +++ b/spring-data-relational/src/main/java/org/springframework/data/relational/core/conversion/MappingRelationalConverter.java @@ -44,16 +44,7 @@ import org.springframework.data.mapping.PersistentPropertyAccessor; import org.springframework.data.mapping.PersistentPropertyPathAccessor; import org.springframework.data.mapping.context.MappingContext; -import org.springframework.data.mapping.model.CachingValueExpressionEvaluatorFactory; -import org.springframework.data.mapping.model.ConvertingPropertyAccessor; -import org.springframework.data.mapping.model.EntityInstantiator; -import org.springframework.data.mapping.model.ParameterValueProvider; -import org.springframework.data.mapping.model.PersistentEntityParameterValueProvider; -import org.springframework.data.mapping.model.PropertyValueProvider; -import org.springframework.data.mapping.model.SimpleTypeHolder; -import org.springframework.data.mapping.model.SpELContext; -import org.springframework.data.mapping.model.ValueExpressionEvaluator; -import org.springframework.data.mapping.model.ValueExpressionParameterValueProvider; +import org.springframework.data.mapping.model.*; import org.springframework.data.projection.EntityProjection; import org.springframework.data.projection.EntityProjectionIntrospector; import org.springframework.data.projection.EntityProjectionIntrospector.ProjectionPredicate; @@ -87,6 +78,7 @@ * @author Chirag Tailor * @author Vincent Galloy * @author Chanhyeong Cho + * @author Lukáš Křečan * @see org.springframework.data.mapping.context.MappingContext * @see SimpleTypeHolder * @see CustomConversions @@ -702,8 +694,9 @@ public Object writeValue(@Nullable Object value, TypeInformation type) { if (getConversions().isSimpleType(value.getClass())) { Optional> customWriteTarget = getConversions().hasCustomWriteTarget(value.getClass(), type.getType()) - ? getConversions().getCustomWriteTarget(value.getClass(), type.getType()) - : getConversions().getCustomWriteTarget(type.getType()); + ? getConversions().getCustomWriteTarget(value.getClass(), type.getType()) + : getConversions().getCustomWriteTarget(type.getType()); + if (customWriteTarget.isPresent()) { return getConversionService().convert(value, customWriteTarget.get()); } diff --git a/spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/MappingRelationalConverterUnitTests.java b/spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/MappingRelationalConverterUnitTests.java index e30bdb10e1..4b53fa518a 100644 --- a/spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/MappingRelationalConverterUnitTests.java +++ b/spring-data-relational/src/test/java/org/springframework/data/relational/core/conversion/MappingRelationalConverterUnitTests.java @@ -24,10 +24,10 @@ import java.util.Map; import java.util.Objects; import java.util.Set; - import java.util.UUID; -import org.junit.jupiter.api.Test; +import org.assertj.core.api.SoftAssertions; +import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Value; import org.springframework.core.convert.TypeDescriptor; import org.springframework.core.convert.converter.Converter; @@ -52,6 +52,8 @@ * Unit tests for {@link MappingRelationalConverter}. * * @author Mark Paluch + * @author Lukáš Křečan + * @author Jens Schauder */ class MappingRelationalConverterUnitTests { @@ -92,7 +94,7 @@ void shouldEvaluateExpression() { } @Test - // GH-1689 + // GH-1689 void shouldApplySimpleTypeConverterSimpleType() { converter = new MappingRelationalConverter(converter.getMappingContext(), @@ -214,25 +216,24 @@ void projectShouldReadProjectionWithNestedEntity() { assertThat(person.getAddresses()).extracting(Address::getStreet).hasSize(1).containsOnly("hwy"); } - @SuppressWarnings("unchecked") - @Test + @Test // GH-1842 void shouldApplyGenericTypeConverter() { converter = new MappingRelationalConverter(converter.getMappingContext(), - new CustomConversions(StoreConversions.NONE, List.of(GenericTypeConverter.INSTANCE))); + new CustomConversions(StoreConversions.NONE, List.of(GenericTypeConverter.INSTANCE))); + + UUID uuid = UUID.randomUUID(); + GenericClass wrappedUuid = new GenericClass<>(uuid); + GenericClass wrappedString = new GenericClass<>("test"); - var stringResult = (GenericClass) converter.writeValue("test", TypeInformation.of(GenericClass.class)); - var uuidResult = (GenericClass) converter.writeValue(UUID.fromString("1234567-8910-1112-1314-151617181920"), TypeInformation.of(GenericClass.class)); + SoftAssertions.assertSoftly(softly -> { - var stringGeneric = new GenericClass<>("test"); - var stringGenericResult = (String) converter.writeValue(stringGeneric, TypeInformation.of(String.class)); - var uuidGeneric = new GenericClass<>(UUID.fromString("1234567-8910-1112-1314-151617181920")); - var uuidGenericResult = (UUID) converter.writeValue(uuidGeneric, TypeInformation.of(UUID.class)); + softly.assertThat(converter.writeValue(uuid, TypeInformation.of(GenericClass.class))).isEqualTo(wrappedUuid); + softly.assertThat(converter.writeValue(wrappedUuid, TypeInformation.of(UUID.class))).isEqualTo(uuid); - assertThat(stringResult.value()).isEqualTo("test"); - assertThat(uuidResult.value()).isEqualTo(UUID.fromString("1234567-8910-1112-1314-151617181920")); - assertThat(stringGenericResult).isEqualTo("test"); - assertThat(uuidGenericResult).isEqualTo(UUID.fromString("1234567-8910-1112-1314-151617181920")); + softly.assertThat(converter.writeValue("test", TypeInformation.of(GenericClass.class))).isEqualTo(wrappedString); + softly.assertThat(converter.writeValue(wrappedString, TypeInformation.of(String.class))).isEqualTo("test"); + }); } static class SimpleType { @@ -397,12 +398,9 @@ enum GenericTypeConverter implements GenericConverter { @Override public Set getConvertibleTypes() { - return Set.of( - new ConvertiblePair(String.class, GenericClass.class), - new ConvertiblePair(UUID.class, GenericClass.class), - new ConvertiblePair(GenericClass.class, String.class), - new ConvertiblePair(GenericClass.class, UUID.class) - ); + return Set.of(new ConvertiblePair(String.class, GenericClass.class), + new ConvertiblePair(UUID.class, GenericClass.class), new ConvertiblePair(GenericClass.class, String.class), + new ConvertiblePair(GenericClass.class, UUID.class)); } @Override @@ -415,6 +413,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t } - public record GenericClass(T value) { } + public record GenericClass(T value) { + } }