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,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);
}