DATACMNS-1101 - Reduce Optional usage in convert/mapping packages.

Reducing Optional usage that lies on hot code paths for object mapping.
This commit is contained in:
Mark Paluch
2017-06-26 15:49:47 +02:00
committed by Oliver Gierke
parent 4aa083377b
commit 74fbe13f54
42 changed files with 428 additions and 427 deletions

View File

@@ -17,7 +17,7 @@ package org.springframework.data.convert;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.util.ClassTypeInformation.*;
import static org.springframework.data.util.ClassTypeInformation.from;
import java.lang.reflect.Constructor;
import java.util.Arrays;
@@ -46,6 +46,7 @@ import org.springframework.util.ReflectionUtils;
*
* @author Thomas Darimont
* @author Oliver Gierke
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProperty<P>> {
@@ -112,7 +113,7 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
Object outer = new Outer();
doReturn(Optional.of(outer)).when(provider).getParameterValue(parameter);
doReturn(outer).when(provider).getParameterValue(parameter);
Inner instance = this.instance.createInstance(entity, provider);
assertThat(instance).isNotNull();
@@ -133,7 +134,7 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
PersistentEntity<Sample, P> entity = new BasicPersistentEntity<>(from(Sample.class));
doReturn(Optional.of("FOO")).when(provider).getParameterValue(any(Parameter.class));
doReturn("FOO").when(provider).getParameterValue(any(Parameter.class));
Constructor constructor = Sample.class.getConstructor(Long.class, String.class);
List<Object> parameters = Arrays.asList("FOO", "FOO");
@@ -191,7 +192,7 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
doReturn(ObjCtor1ParamString.class).when(entity).getType();
doReturn(new PreferredConstructorDiscoverer<>(ObjCtor1ParamString.class).getConstructor())//
.when(entity).getPersistenceConstructor();
doReturn(Optional.of("FOO")).when(provider).getParameterValue(any());
doReturn("FOO").when(provider).getParameterValue(any());
IntStream.range(0, 2).forEach(i -> {
@@ -213,7 +214,7 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
IntStream.range(0, 2).forEach(i -> {
when(provider.getParameterValue(any())).thenReturn(Optional.of("FOO"), Optional.of("BAR"));
when(provider.getParameterValue(any())).thenReturn("FOO", "BAR");
Object instance = this.instance.createInstance(entity, provider);
@@ -234,7 +235,7 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
IntStream.range(0, 2).forEach(i -> {
doReturn(Optional.of(42)).when(provider).getParameterValue(any());
doReturn(42).when(provider).getParameterValue(any());
Object instance = this.instance.createInstance(entity, provider);
@@ -253,8 +254,7 @@ public class ClassGeneratingEntityInstantiatorUnitTests<P extends PersistentProp
IntStream.range(0, 2).forEach(i -> {
when(provider.getParameterValue(any(Parameter.class))).thenReturn(Optional.of("A"), Optional.of(1),
Optional.of(2), Optional.of(3), Optional.of(4), Optional.of(5), Optional.of("B"));
when(provider.getParameterValue(any(Parameter.class))).thenReturn("A", 1, 2, 3, 4, 5, "B");
Object instance = this.instance.createInstance(entity, provider);

View File

@@ -18,7 +18,7 @@ package org.springframework.data.convert;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import static org.springframework.data.convert.ReflectionEntityInstantiator.*;
import static org.springframework.data.util.ClassTypeInformation.*;
import static org.springframework.data.util.ClassTypeInformation.from;
import java.lang.reflect.Constructor;
import java.util.Arrays;
@@ -43,9 +43,10 @@ import org.springframework.util.ReflectionUtils;
/**
* Unit tests for {@link ReflectionEntityInstantiator}.
*
*
* @author Oliver Gierke
* @author Johannes Mockenhaupt
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<P>> {
@@ -81,7 +82,6 @@ public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<
.getConstructor();
doReturn(constructor).when(entity).getPersistenceConstructor();
doReturn(Optional.empty()).when(provider).getParameterValue(any());
Object instance = INSTANCE.createInstance(entity, provider);
@@ -109,7 +109,7 @@ public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<
Object outer = new Outer();
when(provider.getParameterValue(parameter)).thenReturn(Optional.of(outer));
when(provider.getParameterValue(parameter)).thenReturn(outer);
Inner instance = INSTANCE.createInstance(entity, provider);
assertThat(instance).isNotNull();
@@ -130,7 +130,7 @@ public class ReflectionEntityInstantiatorUnitTests<P extends PersistentProperty<
PersistentEntity<Sample, P> entity = new BasicPersistentEntity<>(from(Sample.class));
doReturn(Optional.of("FOO")).when(provider).getParameterValue(any(Parameter.class));
doReturn("FOO").when(provider).getParameterValue(any(Parameter.class));
Constructor constructor = Sample.class.getConstructor(Long.class, String.class);
List<Object> parameters = Arrays.asList("FOO", "FOO");

View File

@@ -17,14 +17,13 @@ package org.springframework.data.mapping;
import static org.assertj.core.api.Assertions.*;
import java.util.Optional;
import org.junit.Test;
/**
* Unit tests for {@link TargetAwareIdentifierAccessor}.
*
*
* @author Oliver Gierke
* @author Mark Paluch
* @soundtrack Anika Nilles - Greenfield (Pikalar)
*/
public class TargetAwareIdentifierAccessorUnitTests {
@@ -37,13 +36,13 @@ public class TargetAwareIdentifierAccessorUnitTests {
IdentifierAccessor accessor = new TargetAwareIdentifierAccessor(() -> sample) {
@Override
public Optional<Object> getIdentifier() {
return Optional.empty();
public Object getIdentifier() {
return null;
}
};
assertThatExceptionOfType(IllegalStateException.class)//
.isThrownBy(() -> accessor.getRequiredIdentifier())//
.isThrownBy(accessor::getRequiredIdentifier)//
.withMessageContaining(sample.toString());
}
}

View File

@@ -51,14 +51,14 @@ public class ClassGeneratingPropertyAccessorFactoryDatatypeTests {
private final Object bean;
private final String propertyName;
private final Optional<Object> value;
private final Object value;
public ClassGeneratingPropertyAccessorFactoryDatatypeTests(Object bean, String propertyName, Object value,
String displayName) {
this.bean = bean;
this.propertyName = propertyName;
this.value = Optional.of(value);
this.value = value;
}
@Parameters(name = "{3}")

View File

@@ -42,7 +42,7 @@ public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests {
Algorithm quickSort = new QuickSort();
assertThat(getEntityInformation(Algorithm.class).getId(quickSort)).hasValue(quickSort.getName());
assertThat(getEntityInformation(Algorithm.class).getId(quickSort)).isEqualTo(quickSort.getName());
}
@Test // DATACMNS-853
@@ -50,7 +50,7 @@ public class ClassGeneratingPropertyAccessorFactoryEntityTypeTests {
Person jonDoe = new Person("JonDoe");
assertThat(getEntityInformation(Person.class).getId(jonDoe)).hasValue(jonDoe.name);
assertThat(getEntityInformation(Person.class).getId(jonDoe)).isEqualTo(jonDoe.name);
}
private EntityInformation<Object, Serializable> getEntityInformation(Class<?> type) {

View File

@@ -16,8 +16,7 @@
package org.springframework.data.mapping.model;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
import static org.assertj.core.api.Assertions.*;
import java.lang.reflect.Constructor;
import java.util.ArrayList;
@@ -29,7 +28,6 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import org.springframework.data.annotation.AccessType;
import org.springframework.data.annotation.AccessType.Type;
import org.springframework.data.mapping.PersistentProperty;
@@ -103,8 +101,8 @@ public class ClassGeneratingPropertyAccessorFactoryTests {
PersistentPropertyAccessor persistentPropertyAccessor = getPersistentPropertyAccessor(bean);
persistentPropertyAccessor.setProperty(property, Optional.of("value"));
assertThat(persistentPropertyAccessor.getProperty(property)).isEqualTo(Optional.of("value"));
persistentPropertyAccessor.setProperty(property, "value");
assertThat(persistentPropertyAccessor.getProperty(property)).isEqualTo("value");
});
}

View File

@@ -29,8 +29,9 @@ import org.springframework.format.support.DefaultFormattingConversionService;
/**
* Unit tests for {@link ConvertingPropertyAccessor}.
*
*
* @author Oliver Gierke
* @author Mark Paluch
*/
public class ConvertingPropertyAccessorUnitTests {
@@ -60,7 +61,7 @@ public class ConvertingPropertyAccessorUnitTests {
entity.id = 1L;
assertThat(getIdProperty()).hasValueSatisfying(
it -> assertThat(getAccessor(entity, CONVERSION_SERVICE).getProperty(it, String.class)).hasValue("1"));
it -> assertThat(getAccessor(entity, CONVERSION_SERVICE).getProperty(it, String.class)).isEqualTo("1"));
}
@Test // DATACMNS-596
@@ -69,7 +70,7 @@ public class ConvertingPropertyAccessorUnitTests {
ConversionService conversionService = mock(ConversionService.class);
assertThat(getIdProperty()).hasValueSatisfying(it -> {
assertThat(getAccessor(new Entity(), conversionService).getProperty(it, Number.class)).isNotPresent();
assertThat(getAccessor(new Entity(), conversionService).getProperty(it, Number.class)).isNull();
verify(conversionService, times(0)).convert(1L, Number.class);
});
}
@@ -83,7 +84,7 @@ public class ConvertingPropertyAccessorUnitTests {
ConversionService conversionService = mock(ConversionService.class);
assertThat(getIdProperty()).hasValueSatisfying(it -> {
assertThat(getAccessor(entity, conversionService).getProperty(it, Number.class)).hasValue(1L);
assertThat(getAccessor(entity, conversionService).getProperty(it, Number.class)).isEqualTo(1L);
verify(conversionService, times(0)).convert(1L, Number.class);
});
}
@@ -94,7 +95,7 @@ public class ConvertingPropertyAccessorUnitTests {
Entity entity = new Entity();
assertThat(getIdProperty()).hasValueSatisfying(property -> {
getAccessor(entity, CONVERSION_SERVICE).setProperty(property, Optional.of("1"));
getAccessor(entity, CONVERSION_SERVICE).setProperty(property, "1");
assertThat(entity.id).isEqualTo(1L);
});
}
@@ -103,7 +104,7 @@ public class ConvertingPropertyAccessorUnitTests {
public void doesNotInvokeConversionIfTypeAlreadyMatchesOnSet() {
assertThat(getIdProperty()).hasValueSatisfying(it -> {
getAccessor(new Entity(), mock(ConversionService.class)).setProperty(it, Optional.of(1L));
getAccessor(new Entity(), mock(ConversionService.class)).setProperty(it, 1L);
verify(mock(ConversionService.class), times(0)).convert(1L, Long.class);
});
}

View File

@@ -24,6 +24,7 @@ import org.springframework.data.mapping.context.SampleMappingContext;
/**
* @author Oliver Gierke
* @author Mark Paluch
*/
public class IdPropertyIdentifierAccessorUnitTests {
@@ -50,7 +51,7 @@ public class IdPropertyIdentifierAccessorUnitTests {
IdentifierAccessor accessor = new IdPropertyIdentifierAccessor(
mappingContext.getRequiredPersistentEntity(SampleWithId.class), sample);
assertThat(accessor.getIdentifier()).hasValue(sample.id);
assertThat(accessor.getIdentifier()).isEqualTo(sample.id);
}
static class Sample {}

View File

@@ -16,7 +16,6 @@
package org.springframework.data.mapping.model;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.Iterator;
import java.util.Optional;
@@ -33,8 +32,9 @@ import org.springframework.data.util.ClassTypeInformation;
/**
* Unit tests for {@link PersistentEntityParameterValueProvider}.
*
*
* @author Oliver Gierke
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class PersistentEntityParameterValueProviderUnitTests<P extends PersistentProperty<P>> {
@@ -55,16 +55,14 @@ public class PersistentEntityParameterValueProviderUnitTests<P extends Persisten
}
};
doReturn(Optional.empty()).when(propertyValueProvider).getPropertyValue(any());
assertThat(entity.getPersistenceConstructor()).hasValueSatisfying(constructor -> {
Iterator<Parameter<Object, P>> iterator = constructor.getParameters().iterator();
ParameterValueProvider<P> provider = new PersistentEntityParameterValueProvider<>(entity, propertyValueProvider,
Optional.of(outer));
outer);
assertThat(provider.getParameterValue(iterator.next())).hasValue(outer);
assertThat(provider.getParameterValue(iterator.next())).isNotPresent();
assertThat(provider.getParameterValue(iterator.next())).isEqualTo(outer);
assertThat(provider.getParameterValue(iterator.next())).isNull();
assertThat(iterator.hasNext()).isFalse();
});
}
@@ -76,10 +74,11 @@ public class PersistentEntityParameterValueProviderUnitTests<P extends Persisten
ParameterValueProvider<P> provider = new PersistentEntityParameterValueProvider<>(entity, propertyValueProvider,
Optional.of(property));
assertThat(entity.getPersistenceConstructor()).hasValueSatisfying(constructor -> assertThatExceptionOfType(MappingException.class)//
.isThrownBy(() -> provider.getParameterValue(constructor.getParameters().iterator().next()))//
.withMessageContaining("bar")//
.withMessageContaining(Entity.class.getName()));
assertThat(entity.getPersistenceConstructor())
.hasValueSatisfying(constructor -> assertThatExceptionOfType(MappingException.class)//
.isThrownBy(() -> provider.getParameterValue(constructor.getParameters().iterator().next()))//
.withMessageContaining("bar")//
.withMessageContaining(Entity.class.getName()));
}
static class Outer {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-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,7 +30,10 @@ import org.springframework.data.mapping.PreferredConstructor.Parameter;
import org.springframework.data.mapping.model.AbstractPersistentPropertyUnitTests.SamplePersistentProperty;
/**
* Unit tests for {@link SpELExpressionParameterValueProvider}.
*
* @author Oliver Gierke
* @author Mark Paluch
*/
@RunWith(MockitoJUnitRunner.class)
public class SpelExpressionParameterProviderUnitTests {
@@ -49,7 +52,7 @@ public class SpelExpressionParameterProviderUnitTests {
provider = new SpELExpressionParameterValueProvider<>(evaluator, conversionService, delegate);
parameter = mock(Parameter.class);
when(parameter.getSpelExpression()).thenReturn(Optional.empty());
when(parameter.hasSpelExpression()).thenReturn(true);
when(parameter.getRawType()).thenReturn(Object.class);
}
@@ -58,6 +61,7 @@ public class SpelExpressionParameterProviderUnitTests {
public void delegatesIfParameterDoesNotHaveASpELExpression() {
Parameter<Object, SamplePersistentProperty> parameter = mock(Parameter.class);
when(parameter.hasSpelExpression()).thenReturn(false);
provider.getParameterValue(parameter);
verify(delegate, times(1)).getParameterValue(parameter);
@@ -114,7 +118,7 @@ public class SpelExpressionParameterProviderUnitTests {
doReturn(Optional.of("source")).when(parameter).getSpelExpression();
doReturn("value").when(evaluator).evaluate(any());
assertThat(provider.getParameterValue(parameter)).hasValue("FOO");
assertThat(provider.getParameterValue(parameter)).isEqualTo("FOO");
verify(delegate, times(0)).getParameterValue(parameter);
}

View File

@@ -18,7 +18,6 @@ package org.springframework.data.repository.core.support;
import static org.assertj.core.api.Assertions.*;
import java.io.Serializable;
import java.util.Optional;
import org.junit.Test;
import org.springframework.data.annotation.Id;
@@ -28,9 +27,10 @@ import org.springframework.util.ReflectionUtils;
/**
* Unit tests for {@link AbstractEntityInformation}.
*
*
* @author Oliver Gierke
* @author Nick Williams
* @author Mark Paluch
*/
public class AbstractEntityInformationUnitTests {
@@ -111,8 +111,8 @@ public class AbstractEntityInformationUnitTests {
@Override
@SuppressWarnings("unchecked")
public Optional<ID> getId(T entity) {
return Optional.ofNullable((ID) ReflectionTestUtils.getField(entity, "id"));
public ID getId(T entity) {
return (ID) ReflectionTestUtils.getField(entity, "id");
}
@Override

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012 the original author or authors.
* Copyright 2012-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.
@@ -16,18 +16,17 @@
package org.springframework.data.repository.core.support;
import java.io.Serializable;
import java.util.Optional;
/**
* Dummy implementation of {@link AbstractEntityInformation}.
*
*
* @author Oliver Gierke
*/
public class DummyEntityInformation<T> extends AbstractEntityInformation<T, Serializable> {
/**
* Creates a new {@link DummyEntityInformation} for the given domain class.
*
*
* @param domainClass
*/
public DummyEntityInformation(Class<T> domainClass) {
@@ -38,8 +37,8 @@ public class DummyEntityInformation<T> extends AbstractEntityInformation<T, Seri
* (non-Javadoc)
* @see org.springframework.data.repository.core.EntityInformation#getId(java.lang.Object)
*/
public Optional<Serializable> getId(Object entity) {
return Optional.ofNullable(entity == null ? null : entity.toString());
public Serializable getId(Object entity) {
return entity == null ? null : entity.toString();
}
/*

View File

@@ -25,8 +25,8 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.domain.Persistable;
/**
* Unit tests for {@link PersistableEntityMetadata}.
*
* Unit tests for {@link Persistable}.
*
* @author Oliver Gierke
*/
@RunWith(MockitoJUnitRunner.class)
@@ -44,9 +44,9 @@ public class PersistableEntityInformationUnitTests {
when(persistable.getId()).thenReturn(2L, 1L, 3L);
assertThat(metadata.getId(persistable)).hasValue(2L);
assertThat(metadata.getId(persistable)).hasValue(1L);
assertThat(metadata.getId(persistable)).hasValue(3L);
assertThat(metadata.getId(persistable)).isEqualTo(2L);
assertThat(metadata.getId(persistable)).isEqualTo(1L);
assertThat(metadata.getId(persistable)).isEqualTo(3L);
}
@Test

View File

@@ -28,7 +28,7 @@ import org.springframework.data.repository.core.EntityInformation;
/**
* Unit tests for {@link PersistentEntityInformation}.
*
*
* @author Oliver Gierke
*/
public class PersistentEntityInformationUnitTests {
@@ -45,7 +45,7 @@ public class PersistentEntityInformationUnitTests {
Sample sample = new Sample();
sample.id = 5L;
assertThat(information.getId(sample)).hasValue(5L);
assertThat(information.getId(sample)).isEqualTo(5L);
}
@Test // DATACMNS-596
@@ -57,7 +57,7 @@ public class PersistentEntityInformationUnitTests {
PersistentEntityInformation<Object, Serializable> information = new PersistentEntityInformation<>(
entity);
assertThat(information.getId(new EntityWithoutId())).isNotPresent();
assertThat(information.getId(new EntityWithoutId())).isNull();
}
static class Sample {