Skip to content

Commit

Permalink
Polishing.
Browse files Browse the repository at this point in the history
Restructure test.
Remove redundant code.
Formatting.

Original pull request #1876
See #1842
  • Loading branch information
schauder committed Sep 16, 2024
1 parent 5b76259 commit 4840f7c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -694,8 +686,9 @@ public Object writeValue(@Nullable Object value, TypeInformation<?> type) {
if (getConversions().isSimpleType(value.getClass())) {

Optional<Class<?>> 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());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -52,6 +52,8 @@
* Unit tests for {@link MappingRelationalConverter}.
*
* @author Mark Paluch
* @author Lukáš Křečan
* @author Jens Schauder
*/
class MappingRelationalConverterUnitTests {

Expand Down Expand Up @@ -92,7 +94,7 @@ void shouldEvaluateExpression() {
}

@Test
// GH-1689
// GH-1689
void shouldApplySimpleTypeConverterSimpleType() {

converter = new MappingRelationalConverter(converter.getMappingContext(),
Expand Down Expand Up @@ -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<UUID> wrappedUuid = new GenericClass<>(uuid);
GenericClass<String> wrappedString = new GenericClass<>("test");

var stringResult = (GenericClass<String>) converter.writeValue("test", TypeInformation.of(GenericClass.class));
var uuidResult = (GenericClass<UUID>) 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 {
Expand Down Expand Up @@ -397,12 +398,9 @@ enum GenericTypeConverter implements GenericConverter {

@Override
public Set<ConvertiblePair> 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
Expand All @@ -415,6 +413,7 @@ public Object convert(Object source, TypeDescriptor sourceType, TypeDescriptor t

}

public record GenericClass<T>(T value) { }
public record GenericClass<T>(T value) {
}

}

0 comments on commit 4840f7c

Please sign in to comment.