DATACMNS-867 - Second draft.
This commit is contained in:
@@ -16,7 +16,7 @@
|
||||
package org.springframework.data.convert;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.data.util.ClassTypeInformation.*;
|
||||
|
||||
@@ -24,9 +24,7 @@ import java.lang.reflect.Constructor;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
import java.util.stream.IntStream;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -279,12 +277,6 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testname() {
|
||||
|
||||
List<String> result = Stream.of("1", null).map(it -> (String) null).collect(Collectors.toList());
|
||||
}
|
||||
|
||||
static class Foo {
|
||||
|
||||
Foo(String foo) {
|
||||
|
||||
@@ -25,9 +25,9 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.mapping.Alias;
|
||||
import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ConfigurableTypeMapper}.
|
||||
@@ -62,15 +62,14 @@ public class ConfigurableTypeInformationMapperUnitTests<T extends PersistentProp
|
||||
@Test
|
||||
public void writesMapKeyForType() {
|
||||
|
||||
assertThat(mapper.createAliasFor(ClassTypeInformation.from(String.class))).isEqualTo("1");
|
||||
assertThat(mapper.createAliasFor(ClassTypeInformation.from(Object.class))).isNull();
|
||||
assertThat(mapper.createAliasFor(ClassTypeInformation.from(String.class))).isEqualTo(Alias.of("1"));
|
||||
assertThat(mapper.createAliasFor(ClassTypeInformation.from(Object.class))).isEqualTo(Alias.NONE);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void readsTypeForMapKey() {
|
||||
|
||||
assertThat(mapper.resolveTypeFrom("1")).isEqualTo((TypeInformation) ClassTypeInformation.from(String.class));
|
||||
assertThat(mapper.resolveTypeFrom("unmapped")).isNull();
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("1"))).hasValue(ClassTypeInformation.from(String.class));
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("unmapped"))).isEmpty();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -21,13 +21,16 @@ import static org.mockito.Mockito.*;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.runners.MockitoJUnitRunner;
|
||||
import org.springframework.data.mapping.Alias;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.OptionalAssert;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
/**
|
||||
@@ -39,10 +42,9 @@ import org.springframework.data.util.TypeInformation;
|
||||
public class DefaultTypeMapperUnitTests {
|
||||
|
||||
static final TypeInformation<String> STRING_TYPE_INFO = ClassTypeInformation.from(String.class);
|
||||
static final String STRING = String.class.getName();
|
||||
static final Alias ALIAS = Alias.of(String.class.getName());
|
||||
|
||||
@Mock TypeAliasAccessor<Map<String, String>> accessor;
|
||||
|
||||
@Mock TypeInformationMapper mapper;
|
||||
|
||||
DefaultTypeMapper<Map<String, String>> typeMapper;
|
||||
@@ -52,29 +54,28 @@ public class DefaultTypeMapperUnitTests {
|
||||
public void setUp() {
|
||||
|
||||
this.typeMapper = new DefaultTypeMapper<Map<String, String>>(accessor, Arrays.asList(mapper));
|
||||
this.source = Collections.singletonMap("key", STRING);
|
||||
this.source = Collections.singletonMap("key", ALIAS.toString());
|
||||
|
||||
doReturn(STRING).when(accessor).readAliasFrom(source);
|
||||
doReturn(STRING_TYPE_INFO).when(mapper).resolveTypeFrom(STRING);
|
||||
doReturn(ALIAS).when(accessor).readAliasFrom(source);
|
||||
doReturn(Optional.of(STRING_TYPE_INFO)).when(mapper).resolveTypeFrom(ALIAS);
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void cachesResolvedTypeInformation() {
|
||||
|
||||
TypeInformation<?> information = typeMapper.readType(source);
|
||||
assertThat(information).isEqualTo((TypeInformation) STRING_TYPE_INFO);
|
||||
verify(mapper, times(1)).resolveTypeFrom(STRING);
|
||||
Optional<TypeInformation<?>> information = typeMapper.readType(source);
|
||||
assertThat(information).hasValue(STRING_TYPE_INFO);
|
||||
verify(mapper, times(1)).resolveTypeFrom(ALIAS);
|
||||
|
||||
typeMapper.readType(source);
|
||||
verify(mapper, times(1)).resolveTypeFrom(STRING);
|
||||
verify(mapper, times(1)).resolveTypeFrom(ALIAS);
|
||||
}
|
||||
|
||||
@Test // DATACMNS-349
|
||||
public void returnsTypeAliasForInformation() {
|
||||
|
||||
Object alias = "alias";
|
||||
when(mapper.createAliasFor(STRING_TYPE_INFO)).thenReturn(alias);
|
||||
Alias alias = Alias.of("alias");
|
||||
doReturn(alias).when(mapper).createAliasFor(STRING_TYPE_INFO);
|
||||
|
||||
assertThat(this.typeMapper.getAliasFor(STRING_TYPE_INFO)).isEqualTo(alias);
|
||||
}
|
||||
@@ -83,16 +84,22 @@ public class DefaultTypeMapperUnitTests {
|
||||
public void specializesRawSourceTypeUsingGenericContext() {
|
||||
|
||||
ClassTypeInformation<Foo> root = ClassTypeInformation.from(Foo.class);
|
||||
TypeInformation<?> propertyType = root.getProperty("abstractBar");
|
||||
TypeInformation<?> propertyType = root.getProperty("abstractBar")
|
||||
.orElseThrow(() -> new IllegalStateException("Property abstractBar not found!"));
|
||||
TypeInformation<?> barType = ClassTypeInformation.from(Bar.class);
|
||||
|
||||
doReturn(barType).when(accessor).readAliasFrom(source);
|
||||
doReturn(barType).when(mapper).resolveTypeFrom(barType);
|
||||
doReturn(Alias.of(barType)).when(accessor).readAliasFrom(source);
|
||||
doReturn(Optional.of(barType)).when(mapper).resolveTypeFrom(Alias.of(barType));
|
||||
|
||||
TypeInformation<?> result = typeMapper.readType(source, propertyType);
|
||||
|
||||
assertThat(result.getType()).isEqualTo(Bar.class);
|
||||
assertThat(result.getProperty("field").getType()).isEqualTo(Character.class);
|
||||
assertThat(result).isInstanceOf(TypeInformation.class);
|
||||
|
||||
TypeInformation<?> typeInformation = TypeInformation.class.cast(result);
|
||||
|
||||
assertThat(typeInformation.getType()).isEqualTo(Bar.class);
|
||||
OptionalAssert.assertOptional(typeInformation.getProperty("field")).value(nested -> nested.getType())
|
||||
.isEqualTo(Character.class);
|
||||
}
|
||||
|
||||
static class TypeWithAbstractGenericType<T> {
|
||||
|
||||
@@ -23,12 +23,12 @@ import java.util.Collections;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.annotation.TypeAlias;
|
||||
import org.springframework.data.mapping.Alias;
|
||||
import org.springframework.data.mapping.PersistentEntity;
|
||||
import org.springframework.data.mapping.context.SampleMappingContext;
|
||||
import org.springframework.data.mapping.context.SamplePersistentProperty;
|
||||
import org.springframework.data.util.AnnotatedTypeScanner;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MappingContextTypeInformationMapper}.
|
||||
@@ -58,7 +58,7 @@ public class MappingContextTypeInformationMapperUnitTests {
|
||||
|
||||
mapper = new MappingContextTypeInformationMapper(mappingContext);
|
||||
|
||||
assertThat(mapper.createAliasFor(ClassTypeInformation.from(Entity.class))).isEqualTo("foo");
|
||||
assertThat(mapper.createAliasFor(ClassTypeInformation.from(Entity.class)).hasValue("foo")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -69,7 +69,7 @@ public class MappingContextTypeInformationMapperUnitTests {
|
||||
|
||||
mapper = new MappingContextTypeInformationMapper(mappingContext);
|
||||
|
||||
assertThat(mapper.createAliasFor(from(Entity.class))).isEqualTo("foo");
|
||||
assertThat(mapper.createAliasFor(from(Entity.class)).hasValue("foo")).isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
@@ -79,23 +79,22 @@ public class MappingContextTypeInformationMapperUnitTests {
|
||||
mappingContext.initialize();
|
||||
|
||||
mapper = new MappingContextTypeInformationMapper(mappingContext);
|
||||
assertThat(mapper.createAliasFor(from(String.class))).isNull();
|
||||
assertThat(mapper.createAliasFor(from(String.class)).isPresent()).isFalse();
|
||||
}
|
||||
|
||||
@Test
|
||||
@SuppressWarnings("rawtypes")
|
||||
public void detectsTypeForUnknownEntity() {
|
||||
|
||||
SampleMappingContext mappingContext = new SampleMappingContext();
|
||||
mappingContext.initialize();
|
||||
|
||||
mapper = new MappingContextTypeInformationMapper(mappingContext);
|
||||
assertThat(mapper.resolveTypeFrom("foo")).isNull();
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("foo"))).isEmpty();
|
||||
|
||||
PersistentEntity<?, SamplePersistentProperty> entity = mappingContext.getPersistentEntity(Entity.class);
|
||||
PersistentEntity<?, SamplePersistentProperty> entity = mappingContext.getRequiredPersistentEntity(Entity.class);
|
||||
|
||||
assertThat(entity).isNotNull();
|
||||
assertThat(mapper.resolveTypeFrom("foo")).isEqualTo((TypeInformation) from(Entity.class));
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("foo"))).hasValue(from(Entity.class));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-485
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
package org.springframework.data.convert;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.mockito.Matchers.*;
|
||||
import static org.mockito.ArgumentMatchers.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.springframework.data.convert.ReflectionEntityInstantiator.*;
|
||||
import static org.springframework.data.util.ClassTypeInformation.*;
|
||||
@@ -81,7 +81,6 @@ public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<
|
||||
Optional<? extends PreferredConstructor<Foo, P>> constructor = new PreferredConstructorDiscoverer<Foo, P>(Foo.class)
|
||||
.getConstructor();
|
||||
|
||||
doReturn(Foo.class).when(entity).getType();
|
||||
doReturn(constructor).when(entity).getPersistenceConstructor();
|
||||
doReturn(Optional.empty()).when(provider).getParameterValue(any());
|
||||
|
||||
|
||||
@@ -17,7 +17,10 @@ package org.springframework.data.convert;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.data.mapping.Alias;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
import org.springframework.data.util.TypeInformation;
|
||||
|
||||
@@ -28,46 +31,38 @@ import org.springframework.data.util.TypeInformation;
|
||||
*/
|
||||
public class SimpleTypeInformationMapperUnitTests {
|
||||
|
||||
TypeInformationMapper mapper = new SimpleTypeInformationMapper();
|
||||
|
||||
@Test
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
public void resolvesTypeByLoadingClass() {
|
||||
|
||||
TypeInformationMapper mapper = new SimpleTypeInformationMapper();
|
||||
TypeInformation type = mapper.resolveTypeFrom("java.lang.String");
|
||||
Optional<TypeInformation<?>> type = mapper.resolveTypeFrom(Alias.of("java.lang.String"));
|
||||
|
||||
TypeInformation expected = ClassTypeInformation.from(String.class);
|
||||
TypeInformation<?> expected = ClassTypeInformation.from(String.class);
|
||||
|
||||
assertThat(type).isEqualTo(expected);
|
||||
assertThat(type).hasValue(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsNullForNonStringKey() {
|
||||
|
||||
TypeInformationMapper mapper = new SimpleTypeInformationMapper();
|
||||
assertThat(mapper.resolveTypeFrom(new Object())).isNull();
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of(new Object()))).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsNullForEmptyTypeKey() {
|
||||
|
||||
TypeInformationMapper mapper = new SimpleTypeInformationMapper();
|
||||
assertThat(mapper.resolveTypeFrom("")).isNull();
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of(""))).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsNullForUnloadableClass() {
|
||||
|
||||
TypeInformationMapper mapper = new SimpleTypeInformationMapper();
|
||||
assertThat(mapper.resolveTypeFrom("Foo")).isNull();
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("Foo"))).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void usesFullyQualifiedClassNameAsTypeKey() {
|
||||
|
||||
TypeInformationMapper mapper = new SimpleTypeInformationMapper();
|
||||
Object alias = mapper.createAliasFor(ClassTypeInformation.from(String.class));
|
||||
|
||||
assertThat(alias).isInstanceOf(String.class);
|
||||
assertThat(alias).isEqualTo(String.class.getName());
|
||||
assertThat(mapper.createAliasFor(ClassTypeInformation.from(String.class)))
|
||||
.isEqualTo(Alias.of(String.class.getName()));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user