DATACMNS-1141 - Add missing NonNullApi and Nullable annotations.

Add Nullable annotation to ParsingUtils, Sort.Order and PropertyValueProvider.

Add getRequiredAnnotation(…) to PersistentEntity and PersistentProperty to provide methods returning required, non-null annotations.
This commit is contained in:
Mark Paluch
2017-08-22 10:24:23 +02:00
parent d657b17e74
commit 3a2b6b601b
7 changed files with 90 additions and 10 deletions

View File

@@ -25,8 +25,6 @@ import java.lang.annotation.Target;
import java.util.Map;
import java.util.Optional;
import org.springframework.lang.Nullable;
import org.junit.Before;
import org.junit.Test;
import org.springframework.core.annotation.AliasFor;
@@ -37,8 +35,10 @@ import org.springframework.data.annotation.Id;
import org.springframework.data.annotation.ReadOnlyProperty;
import org.springframework.data.annotation.Transient;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentProperty;
import org.springframework.data.mapping.context.SampleMappingContext;
import org.springframework.data.mapping.context.SamplePersistentProperty;
import org.springframework.lang.Nullable;
import org.springframework.test.util.ReflectionTestUtils;
/**
@@ -46,6 +46,7 @@ import org.springframework.test.util.ReflectionTestUtils;
*
* @author Oliver Gierke
* @author Christoph Strobl
* @author Mark Paluch
*/
public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBasedPersistentProperty<P>> {
@@ -217,9 +218,25 @@ public class AnnotationBasedPersistentPropertyUnitTests<P extends AnnotationBase
assertThat(entity.getPersistentProperty("setter")).satisfies(
property -> assertThat(property.findAnnotation(RevisedAnnnotationWithAliasFor.class)).satisfies(annotation -> {
assertThat(annotation.name()).isEqualTo("my-value");
assertThat(annotation.value()).isEqualTo("my-value");
}));
assertThat(annotation.name()).isEqualTo("my-value");
assertThat(annotation.value()).isEqualTo("my-value");
}));
}
@Test // DATACMNS-1141
public void getRequiredAnnotationReturnsAnnotation() {
PersistentProperty property = getProperty(Sample.class, "id");
assertThat(property.getRequiredAnnotation(Id.class)).isNotNull();
}
@Test // DATACMNS-1141
public void getRequiredAnnotationThrowsException() {
PersistentProperty property = getProperty(Sample.class, "id");
assertThatThrownBy(() -> property.getRequiredAnnotation(Transient.class)).isInstanceOf(IllegalStateException.class);
}
@SuppressWarnings("unchecked")

View File

@@ -39,6 +39,7 @@ import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedBy;
import org.springframework.data.annotation.TypeAlias;
import org.springframework.data.mapping.Alias;
import org.springframework.data.mapping.Document;
import org.springframework.data.mapping.MappingException;
import org.springframework.data.mapping.PersistentEntity;
import org.springframework.data.mapping.PersistentEntitySpec;
@@ -258,6 +259,23 @@ public class BasicPersistentEntityUnitTests<T extends PersistentProperty<T>> {
assertThatExceptionOfType(IllegalArgumentException.class).isThrownBy(() -> entity.addAssociation(null));
}
@Test // DATACMNS-1141
public void getRequiredAnnotationReturnsAnnotation() {
PersistentEntity<AliasEntityUsingComposedAnnotation, T> entity = createEntity(
AliasEntityUsingComposedAnnotation.class);
assertThat(entity.getRequiredAnnotation(TypeAlias.class).value()).isEqualTo("bar");
}
@Test // DATACMNS-1141
public void getRequiredAnnotationThrowsException() {
PersistentEntity<AliasEntityUsingComposedAnnotation, T> entity = createEntity(
AliasEntityUsingComposedAnnotation.class);
assertThatThrownBy(() -> entity.getRequiredAnnotation(Document.class)).isInstanceOf(IllegalStateException.class);
}
private <S> BasicPersistentEntity<S, T> createEntity(Class<S> type) {
return createEntity(type, null);
}