DATACMNS-1101 - Remove Optional from mapping/convert use except for caching of absence and computations that are used to populate caches.
This commit is contained in:
committed by
Oliver Gierke
parent
74fbe13f54
commit
6c65c02e78
@@ -22,10 +22,8 @@ import static org.springframework.data.util.ClassTypeInformation.from;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.IntStream;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
@@ -58,11 +56,6 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
@Mock PreferredConstructor<?, P> constructor;
|
||||
@Mock Parameter<?, P> parameter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
doReturn(Optional.empty()).when(entity).getPersistenceConstructor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instantiatesSimpleObjectCorrectly() {
|
||||
|
||||
@@ -82,7 +75,7 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
@Test
|
||||
public void instantiatesTypeWithPreferredConstructorUsingParameterValueProvider() {
|
||||
|
||||
Optional<? extends PreferredConstructor<Foo, P>> constructor = new PreferredConstructorDiscoverer<Foo, P>(Foo.class)
|
||||
PreferredConstructor<Foo, P> constructor = new PreferredConstructorDiscoverer<Foo, P>(Foo.class)
|
||||
.getConstructor();
|
||||
|
||||
doReturn(Foo.class).when(entity).getType();
|
||||
@@ -90,14 +83,14 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
|
||||
assertThat(instance.createInstance(entity, provider)).isInstanceOf(Foo.class);
|
||||
|
||||
assertThat(constructor).hasValueSatisfying(it -> verify(provider, times(1)).getParameterValue(it.getParameters().iterator().next()));
|
||||
assertThat(constructor)
|
||||
.satisfies(it -> verify(provider, times(1)).getParameterValue(it.getParameters().iterator().next()));
|
||||
}
|
||||
|
||||
@Test(expected = MappingInstantiationException.class) // DATACMNS-300, DATACMNS-578
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void throwsExceptionOnBeanInstantiationException() {
|
||||
|
||||
doReturn(Optional.empty()).when(entity).getPersistenceConstructor();
|
||||
doReturn(PersistentEntity.class).when(entity).getType();
|
||||
|
||||
this.instance.createInstance(entity, provider);
|
||||
@@ -107,7 +100,7 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
|
||||
public void createsInnerClassInstanceCorrectly() {
|
||||
|
||||
BasicPersistentEntity<Inner, P> entity = new BasicPersistentEntity<>(from(Inner.class));
|
||||
assertThat(entity.getPersistenceConstructor()).hasValueSatisfying(constructor -> {
|
||||
assertThat(entity.getPersistenceConstructor()).satisfies(constructor -> {
|
||||
|
||||
Parameter<Object, P> parameter = constructor.getParameters().iterator().next();
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2011-2012 the original author or authors.
|
||||
* Copyright 2011-2017 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -30,9 +30,10 @@ import org.springframework.data.mapping.PersistentProperty;
|
||||
import org.springframework.data.util.ClassTypeInformation;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link ConfigurableTypeMapper}.
|
||||
*
|
||||
* Unit tests for {@link ConfigurableTypeInformationMapper}.
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
* @author Mark Paluch
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ConfigurableTypeInformationMapperUnitTests<T extends PersistentProperty<T>> {
|
||||
@@ -69,7 +70,7 @@ public class ConfigurableTypeInformationMapperUnitTests<T extends PersistentProp
|
||||
@Test
|
||||
public void readsTypeForMapKey() {
|
||||
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("1"))).hasValue(ClassTypeInformation.from(String.class));
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("unmapped"))).isEmpty();
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("1"))).isEqualTo(ClassTypeInformation.from(String.class));
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("unmapped"))).isNull();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,6 @@ import static org.mockito.Mockito.*;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
@@ -33,7 +32,7 @@ import org.springframework.data.util.TypeInformation;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link DefaultTypeMapper}.
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
@@ -55,14 +54,14 @@ public class DefaultTypeMapperUnitTests {
|
||||
this.source = Collections.singletonMap("key", ALIAS.toString());
|
||||
|
||||
doReturn(ALIAS).when(accessor).readAliasFrom(source);
|
||||
doReturn(Optional.of(STRING_TYPE_INFO)).when(mapper).resolveTypeFrom(ALIAS);
|
||||
doReturn(STRING_TYPE_INFO).when(mapper).resolveTypeFrom(ALIAS);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cachesResolvedTypeInformation() {
|
||||
|
||||
Optional<TypeInformation<?>> information = typeMapper.readType(source);
|
||||
assertThat(information).hasValue(STRING_TYPE_INFO);
|
||||
TypeInformation<?> information = typeMapper.readType(source);
|
||||
assertThat(information).isEqualTo(STRING_TYPE_INFO);
|
||||
verify(mapper, times(1)).resolveTypeFrom(ALIAS);
|
||||
|
||||
typeMapper.readType(source);
|
||||
@@ -87,7 +86,7 @@ public class DefaultTypeMapperUnitTests {
|
||||
TypeInformation<?> barType = ClassTypeInformation.from(Bar.class);
|
||||
|
||||
doReturn(Alias.of(barType)).when(accessor).readAliasFrom(source);
|
||||
doReturn(Optional.of(barType)).when(mapper).resolveTypeFrom(Alias.of(barType));
|
||||
doReturn(barType).when(mapper).resolveTypeFrom(Alias.of(barType));
|
||||
|
||||
TypeInformation<?> result = typeMapper.readType(source, propertyType);
|
||||
|
||||
|
||||
@@ -16,7 +16,7 @@
|
||||
package org.springframework.data.convert;
|
||||
|
||||
import static org.assertj.core.api.Assertions.*;
|
||||
import static org.springframework.data.util.ClassTypeInformation.*;
|
||||
import static org.springframework.data.util.ClassTypeInformation.from;
|
||||
|
||||
import java.util.Collections;
|
||||
|
||||
@@ -32,7 +32,7 @@ import org.springframework.data.util.ClassTypeInformation;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link MappingContextTypeInformationMapper}.
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class MappingContextTypeInformationMapperUnitTests {
|
||||
@@ -89,12 +89,12 @@ public class MappingContextTypeInformationMapperUnitTests {
|
||||
mappingContext.initialize();
|
||||
|
||||
mapper = new MappingContextTypeInformationMapper(mappingContext);
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("foo"))).isEmpty();
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("foo"))).isNull();
|
||||
|
||||
PersistentEntity<?, SamplePersistentProperty> entity = mappingContext.getRequiredPersistentEntity(Entity.class);
|
||||
|
||||
assertThat(entity).isNotNull();
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("foo"))).hasValue(from(Entity.class));
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("foo"))).isEqualTo(from(Entity.class));
|
||||
}
|
||||
|
||||
@Test // DATACMNS-485
|
||||
|
||||
@@ -23,9 +23,7 @@ import static org.springframework.data.util.ClassTypeInformation.from;
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
@@ -56,11 +54,6 @@ public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<
|
||||
@Mock PreferredConstructor<?, P> constructor;
|
||||
@Mock Parameter<?, P> parameter;
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
doReturn(Optional.empty()).when(entity).getPersistenceConstructor();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void instantiatesSimpleObjectCorrectly() {
|
||||
|
||||
@@ -78,22 +71,21 @@ public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<
|
||||
@Test
|
||||
public void instantiatesTypeWithPreferredConstructorUsingParameterValueProvider() {
|
||||
|
||||
Optional<? extends PreferredConstructor<Foo, P>> constructor = new PreferredConstructorDiscoverer<Foo, P>(Foo.class)
|
||||
.getConstructor();
|
||||
PreferredConstructor<Foo, P> constructor = new PreferredConstructorDiscoverer<Foo, P>(Foo.class).getConstructor();
|
||||
|
||||
doReturn(constructor).when(entity).getPersistenceConstructor();
|
||||
|
||||
Object instance = INSTANCE.createInstance(entity, provider);
|
||||
|
||||
assertThat(instance).isInstanceOf(Foo.class);
|
||||
assertThat(constructor).hasValueSatisfying(it -> verify(provider, times(1)).getParameterValue(it.getParameters().iterator().next()));
|
||||
assertThat(constructor)
|
||||
.satisfies(it -> verify(provider, times(1)).getParameterValue(it.getParameters().iterator().next()));
|
||||
}
|
||||
|
||||
@Test(expected = MappingInstantiationException.class) // DATACMNS-300
|
||||
@SuppressWarnings({ "unchecked", "rawtypes" })
|
||||
public void throwsExceptionOnBeanInstantiationException() {
|
||||
|
||||
doReturn(Optional.empty()).when(entity).getPersistenceConstructor();
|
||||
doReturn(PersistentEntity.class).when(entity).getType();
|
||||
|
||||
INSTANCE.createInstance(entity, provider);
|
||||
@@ -103,7 +95,7 @@ public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<
|
||||
public void createsInnerClassInstanceCorrectly() {
|
||||
|
||||
BasicPersistentEntity<Inner, P> entity = new BasicPersistentEntity<>(from(Inner.class));
|
||||
assertThat(entity.getPersistenceConstructor()).hasValueSatisfying(it -> {
|
||||
assertThat(entity.getPersistenceConstructor()).satisfies(it -> {
|
||||
|
||||
Parameter<Object, P> parameter = it.getParameters().iterator().next();
|
||||
|
||||
|
||||
@@ -17,8 +17,6 @@ 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;
|
||||
@@ -26,7 +24,7 @@ import org.springframework.data.util.TypeInformation;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link SimpleTypeInformationMapper}.
|
||||
*
|
||||
*
|
||||
* @author Oliver Gierke
|
||||
*/
|
||||
public class SimpleTypeInformationMapperUnitTests {
|
||||
@@ -36,27 +34,27 @@ public class SimpleTypeInformationMapperUnitTests {
|
||||
@Test
|
||||
public void resolvesTypeByLoadingClass() {
|
||||
|
||||
Optional<TypeInformation<?>> type = mapper.resolveTypeFrom(Alias.of("java.lang.String"));
|
||||
TypeInformation<?> type = mapper.resolveTypeFrom(Alias.of("java.lang.String"));
|
||||
|
||||
TypeInformation<?> expected = ClassTypeInformation.from(String.class);
|
||||
|
||||
assertThat(type).hasValue(expected);
|
||||
assertThat(type).isEqualTo(expected);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsNullForNonStringKey() {
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of(new Object()))).isEmpty();
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of(new Object()))).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsNullForEmptyTypeKey() {
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of(""))).isEmpty();
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of(""))).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void returnsNullForUnloadableClass() {
|
||||
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("Foo"))).isEmpty();
|
||||
assertThat(mapper.resolveTypeFrom(Alias.of("Foo"))).isNull();
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
Reference in New Issue
Block a user