diff --git a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java index f25baef86..a5b689673 100644 --- a/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java +++ b/src/main/java/org/springframework/data/mapping/context/AbstractMappingContext.java @@ -15,6 +15,7 @@ */ package org.springframework.data.mapping.context; +import lombok.AccessLevel; import lombok.NonNull; import lombok.RequiredArgsConstructor; @@ -498,12 +499,16 @@ public abstract class AbstractMappingContext descriptors; - private final Map remainingDescriptors = new HashMap<>(); + private final @NonNull Map remainingDescriptors; + + public PersistentPropertyCreator(E entity, Map descriptors) { + this(entity, descriptors, descriptors); + } /* * (non-Javadoc) diff --git a/src/main/java/org/springframework/data/repository/support/CrudRepositoryInvoker.java b/src/main/java/org/springframework/data/repository/support/CrudRepositoryInvoker.java index ab0ad4256..bd1173d11 100644 --- a/src/main/java/org/springframework/data/repository/support/CrudRepositoryInvoker.java +++ b/src/main/java/org/springframework/data/repository/support/CrudRepositoryInvoker.java @@ -115,7 +115,7 @@ class CrudRepositoryInvoker extends ReflectionRepositoryInvoker { } } - protected boolean isRedeclaredMethod(Optional method) { + private static boolean isRedeclaredMethod(Optional method) { return method.map(it -> !it.getDeclaringClass().equals(CrudRepository.class)).orElse(false); } } diff --git a/src/main/java/org/springframework/data/repository/support/PagingAndSortingRepositoryInvoker.java b/src/main/java/org/springframework/data/repository/support/PagingAndSortingRepositoryInvoker.java index ffb7c7c28..c6c4802bc 100644 --- a/src/main/java/org/springframework/data/repository/support/PagingAndSortingRepositoryInvoker.java +++ b/src/main/java/org/springframework/data/repository/support/PagingAndSortingRepositoryInvoker.java @@ -17,6 +17,7 @@ package org.springframework.data.repository.support; import java.io.Serializable; import java.lang.reflect.Method; +import java.util.Optional; import org.springframework.core.convert.ConversionService; import org.springframework.data.domain.Pageable; @@ -35,7 +36,6 @@ import org.springframework.data.repository.core.RepositoryMetadata; class PagingAndSortingRepositoryInvoker extends CrudRepositoryInvoker { private final PagingAndSortingRepository repository; - private final boolean customFindAll; /** @@ -63,7 +63,7 @@ class PagingAndSortingRepositoryInvoker extends CrudRepositoryInvoker { */ @Override public Iterable invokeFindAll(Sort sort) { - return customFindAll ? invokeSortedFindAllReflectively(sort) : repository.findAll(sort); + return customFindAll ? invokeFindAllReflectively(sort) : repository.findAll(sort); } /* @@ -72,10 +72,10 @@ class PagingAndSortingRepositoryInvoker extends CrudRepositoryInvoker { */ @Override public Iterable invokeFindAll(Pageable pageable) { - return customFindAll ? invokePagedFindAllReflectively(pageable) : repository.findAll(pageable); + return customFindAll ? invokeFindAllReflectively(pageable) : repository.findAll(pageable); } - private boolean isRedeclaredMethod(Method method) { - return !method.getDeclaringClass().equals(PagingAndSortingRepository.class); + private static boolean isRedeclaredMethod(Optional method) { + return method.map(it -> !it.getDeclaringClass().equals(PagingAndSortingRepository.class)).orElse(false); } } diff --git a/src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java b/src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java index 944f3d3f5..f04c8cca0 100644 --- a/src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java +++ b/src/main/java/org/springframework/data/repository/support/ReflectionRepositoryInvoker.java @@ -88,7 +88,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { */ @Override public Iterable invokeFindAll(Sort sort) { - return invokeSortedFindAllReflectively(sort); + return invokeFindAllReflectively(sort); } /* @@ -97,7 +97,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { */ @Override public Iterable invokeFindAll(Pageable pageable) { - return invokePagedFindAllReflectively(pageable); + return invokeFindAllReflectively(pageable); } /* @@ -113,12 +113,12 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { * @see org.springframework.data.rest.core.invoke.RepositoryInvoker#invokeSave(java.lang.Object) */ @Override - @SuppressWarnings("unchecked") public T invokeSave(T object) { - return methods.getSaveMethod()// - .map(it -> (T) invoke(it, object))// + Method method = methods.getSaveMethod()// .orElseThrow(() -> new IllegalStateException("Repository doesn't have a save-method declared!")); + + return invoke(method, object); } /* @@ -138,9 +138,10 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { @SuppressWarnings("unchecked") public T invokeFindOne(Serializable id) { - return methods.getFindOneMethod()// - .map(it -> (T) invoke(it, convertId(id)))// + Method method = methods.getFindOneMethod()// .orElseThrow(() -> new IllegalStateException("Repository doesn't have a find-one-method declared!")); + + return invoke(method, convertId(id)); } /* @@ -262,7 +263,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { return conversionService.convert(id, idType); } - protected Iterable invokePagedFindAllReflectively(Pageable pageable) { + protected Iterable invokeFindAllReflectively(Pageable pageable) { Method method = methods.getFindAllMethod() .orElseThrow(() -> new IllegalStateException("Repository doesn't have a find-all-method declared!")); @@ -279,7 +280,7 @@ class ReflectionRepositoryInvoker implements RepositoryInvoker { return invokeFindAll(pageable.getSort()); } - protected Iterable invokeSortedFindAllReflectively(Sort sort) { + protected Iterable invokeFindAllReflectively(Sort sort) { Method method = methods.getFindAllMethod() .orElseThrow(() -> new IllegalStateException("Repository doesn't have a find-all-method declared!")); diff --git a/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java b/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java index 98354ca06..cb13345ec 100755 --- a/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java +++ b/src/test/java/org/springframework/data/mapping/model/AnnotationBasedPersistentPropertyUnitTests.java @@ -110,22 +110,34 @@ public class AnnotationBasedPersistentPropertyUnitTests

{ + assertThat(it.usePropertyAccess()).isFalse(); + }); } @Test // DATACMNS-243 public void usesAccessTypeDeclaredOnTypeAsDefault() { - assertThat(getProperty(PropertyAccess.class, "firstname").usePropertyAccess()).isTrue(); + + assertThat(getProperty(PropertyAccess.class, "firstname")).hasValueSatisfying(it -> { + assertThat(it.usePropertyAccess()).isTrue(); + }); } @Test // DATACMNS-243 public void propertyAnnotationOverridesTypeConfiguration() { - assertThat(getProperty(PropertyAccess.class, "lastname").usePropertyAccess()).isFalse(); + + assertThat(getProperty(PropertyAccess.class, "lastname")).hasValueSatisfying(it -> { + assertThat(it.usePropertyAccess()).isFalse(); + }); } @Test // DATACMNS-243 public void fieldAnnotationOverridesTypeConfiguration() { - assertThat(getProperty(PropertyAccess.class, "emailAddress").usePropertyAccess()).isFalse(); + + assertThat(getProperty(PropertyAccess.class, "emailAddress")).hasValueSatisfying(it -> { + assertThat(it.usePropertyAccess()).isFalse(); + }); } @Test // DATACMNS-243 @@ -135,22 +147,31 @@ public class AnnotationBasedPersistentPropertyUnitTests

{ + assertThat(it.isWritable()).isTrue(); + }); } @Test // DATACMNS-534 public void treatsTransientAsNotExisting() { - assertThat(getProperty(ClassWithReadOnlyProperties.class, "transientProperty")).isNull(); + assertThat(getProperty(ClassWithReadOnlyProperties.class, "transientProperty")).isEmpty(); } @Test // DATACMNS-534 public void treatsReadOnlyAsNonWritable() { - assertThat(getProperty(ClassWithReadOnlyProperties.class, "readOnlyProperty").isWritable()).isFalse(); + + assertThat(getProperty(ClassWithReadOnlyProperties.class, "readOnlyProperty")).hasValueSatisfying(it -> { + assertThat(it.isWritable()).isFalse(); + }); } @Test // DATACMNS-534 public void considersPropertyWithReadOnlyMetaAnnotationReadOnly() { - assertThat(getProperty(ClassWithReadOnlyProperties.class, "customReadOnlyProperty").isWritable()).isFalse(); + + assertThat(getProperty(ClassWithReadOnlyProperties.class, "customReadOnlyProperty")).hasValueSatisfying(it -> { + assertThat(it.isWritable()).isFalse(); + }); } @Test // DATACMNS-556 @@ -162,14 +183,17 @@ public class AnnotationBasedPersistentPropertyUnitTests

property = getProperty(Sample.class, "getterWithoutField"); - assertThat(property.findAnnotation(MyAnnotation.class)).isNotPresent(); + assertThat(property).hasValueSatisfying(it -> { - Map, ?> field = (Map, ?>) ReflectionTestUtils.getField(property, "annotationCache"); + assertThat(it.findAnnotation(MyAnnotation.class)).isNotPresent(); - assertThat(field.containsKey(MyAnnotation.class)).isTrue(); - assertThat(field.get(MyAnnotation.class)).isEqualTo(Optional.empty()); + Map, ?> field = (Map, ?>) ReflectionTestUtils.getField(it, "annotationCache"); + + assertThat(field.containsKey(MyAnnotation.class)).isTrue(); + assertThat(field.get(MyAnnotation.class)).isEqualTo(Optional.empty()); + }); } @Test // DATACMNS-825 @@ -214,8 +238,8 @@ public class AnnotationBasedPersistentPropertyUnitTests

type, String name) { - return context.getRequiredPersistentEntity(type).getPersistentProperty(name).orElse(null); + private Optional getProperty(Class type, String name) { + return context.getRequiredPersistentEntity(type).getPersistentProperty(name); } static class Sample { diff --git a/src/test/java/org/springframework/data/repository/core/support/ReactiveRepositoryInformationUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/ReactiveRepositoryInformationUnitTests.java index c5dcceaca..764475e9a 100644 --- a/src/test/java/org/springframework/data/repository/core/support/ReactiveRepositoryInformationUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/ReactiveRepositoryInformationUnitTests.java @@ -22,6 +22,7 @@ import rx.Observable; import java.io.Serializable; import java.lang.reflect.Method; +import java.util.Optional; import org.junit.Test; import org.junit.runner.RunWith; @@ -48,7 +49,7 @@ public class ReactiveRepositoryInformationUnitTests { Method method = RxJava1InterfaceWithGenerics.class.getMethod("deleteAll"); RepositoryMetadata metadata = new DefaultRepositoryMetadata(RxJava1InterfaceWithGenerics.class); - DefaultRepositoryInformation information = new DefaultRepositoryInformation(metadata, REPOSITORY, null); + DefaultRepositoryInformation information = new DefaultRepositoryInformation(metadata, REPOSITORY, Optional.empty()); Method reference = information.getTargetClassMethod(method); assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass()); @@ -60,7 +61,8 @@ public class ReactiveRepositoryInformationUnitTests { Method method = RxJava1InterfaceWithGenerics.class.getMethod("save", Observable.class); RepositoryMetadata metadata = new DefaultRepositoryMetadata(RxJava1InterfaceWithGenerics.class); - DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, null); + DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, + Optional.empty()); Method reference = information.getTargetClassMethod(method); assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass()); @@ -73,7 +75,8 @@ public class ReactiveRepositoryInformationUnitTests { Method method = ReactiveSortingRepository.class.getMethod("save", Publisher.class); RepositoryMetadata metadata = new DefaultRepositoryMetadata(ReactiveJavaInterfaceWithGenerics.class); - DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, null); + DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, + Optional.empty()); Method reference = information.getTargetClassMethod(method); assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass()); @@ -86,7 +89,8 @@ public class ReactiveRepositoryInformationUnitTests { Method method = ReactiveJavaInterfaceWithGenerics.class.getMethod("save", Iterable.class); RepositoryMetadata metadata = new DefaultRepositoryMetadata(ReactiveJavaInterfaceWithGenerics.class); - DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, null); + DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, + Optional.empty()); Method reference = information.getTargetClassMethod(method); assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass()); @@ -99,7 +103,8 @@ public class ReactiveRepositoryInformationUnitTests { Method method = ReactiveJavaInterfaceWithGenerics.class.getMethod("save", Object.class); RepositoryMetadata metadata = new DefaultRepositoryMetadata(ReactiveJavaInterfaceWithGenerics.class); - DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, null); + DefaultRepositoryInformation information = new ReactiveRepositoryInformation(metadata, REPOSITORY, + Optional.empty()); Method reference = information.getTargetClassMethod(method); assertEquals(ReactiveCrudRepository.class, reference.getDeclaringClass()); diff --git a/src/test/java/org/springframework/data/repository/core/support/ReactiveWrapperRepositoryFactorySupportUnitTests.java b/src/test/java/org/springframework/data/repository/core/support/ReactiveWrapperRepositoryFactorySupportUnitTests.java index c5304c121..0d9b1f349 100644 --- a/src/test/java/org/springframework/data/repository/core/support/ReactiveWrapperRepositoryFactorySupportUnitTests.java +++ b/src/test/java/org/springframework/data/repository/core/support/ReactiveWrapperRepositoryFactorySupportUnitTests.java @@ -15,7 +15,7 @@ */ package org.springframework.data.repository.core.support; -import static org.mockito.Matchers.*; +import static org.mockito.ArgumentMatchers.*; import static org.mockito.Mockito.*; import reactor.core.publisher.Mono;