DATACMNS-1366 - Introduce PersistentEntity.requiresPropertyPopulation().

The newly introduced method indicates whether any properties have to be populated to create instances of the entity. This is useful for objects that are completely initialized through their constructors as converters then can avoid iterating over all properties just to find out none of them have to be populated.
This commit is contained in:
Oliver Gierke
2018-08-08 13:11:34 +02:00
parent 57d0b322e6
commit 14fbc11f00
3 changed files with 66 additions and 0 deletions

View File

@@ -19,6 +19,8 @@ import static org.assertj.core.api.Assertions.*;
import static org.junit.Assume.*;
import static org.mockito.Mockito.*;
import lombok.RequiredArgsConstructor;
import java.lang.annotation.Annotation;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@@ -27,6 +29,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.stream.Stream;
import org.hamcrest.CoreMatchers;
import org.junit.Rule;
@@ -44,6 +47,7 @@ import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.Immutable;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.Persistent;
import org.springframework.data.annotation.Transient;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.domain.Persistable;
import org.springframework.data.mapping.Alias;
@@ -355,6 +359,19 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
assertThat(createEntity(Entity.class).isImmutable()).isFalse();
}
@Test // DATACMNS-1366
public void exposesPropertyPopulationRequired() {
assertThat(createPopulatedPersistentEntity(PropertyPopulationRequired.class).requiresPropertyPopulation()).isTrue();
}
@Test // DATACMNS-1366
public void exposesPropertyPopulationNotRequired() {
Stream.of(PropertyPopulationNotRequired.class, PropertyPopulationNotRequiredWithTransient.class) //
.forEach(it -> assertThat(createPopulatedPersistentEntity(it).requiresPropertyPopulation()).isFalse());
}
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
return createEntity(type, null);
}
@@ -363,6 +380,12 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
return new BasicPersistentEntity<>(ClassTypeInformation.from(type), comparator);
}
private static PersistentEntity<Object, ?> createPopulatedPersistentEntity(Class<?> type) {
SampleMappingContext context = new SampleMappingContext();
return context.getRequiredPersistentEntity(type);
}
@TypeAlias("foo")
static class AliasedEntity {
@@ -427,4 +450,26 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
@Immutable
static class SomeValue {}
// DATACMNS-1366
@RequiredArgsConstructor
static class PropertyPopulationRequired {
private final String firstname, lastname;
private String email;
}
@RequiredArgsConstructor
static class PropertyPopulationNotRequired {
private final String firstname, lastname;
}
@RequiredArgsConstructor
static class PropertyPopulationNotRequiredWithTransient {
private final String firstname, lastname;
private @Transient String email;
}
}