Browse Source

DATACMNS-1173 - Fixed value lookup of AnnotationRevisionMetadata.

Fixed handling of absent values. Minor refactorings. More unit tests.
pull/245/head
Oliver Gierke 8 years ago
parent
commit
65e21344ac
  1. 6
      src/main/java/org/springframework/data/history/AnnotationRevisionMetadata.java
  2. 8
      src/main/java/org/springframework/data/history/RevisionMetadata.java
  3. 82
      src/test/java/org/springframework/data/history/AnnotationRevisionMetadataUnitTests.java

6
src/main/java/org/springframework/data/history/AnnotationRevisionMetadata.java

@ -85,9 +85,9 @@ public class AnnotationRevisionMetadata<N extends Number & Comparable<N>> implem @@ -85,9 +85,9 @@ public class AnnotationRevisionMetadata<N extends Number & Comparable<N>> implem
return Lazy.of(() -> {
AnnotationDetectionFieldCallback numberCallback = new AnnotationDetectionFieldCallback(annotationType);
ReflectionUtils.doWithFields(entity.getClass(), numberCallback);
return numberCallback.getValue(entity);
AnnotationDetectionFieldCallback callback = new AnnotationDetectionFieldCallback(annotationType);
ReflectionUtils.doWithFields(entity.getClass(), callback);
return Optional.ofNullable(callback.getValue(entity));
});
}
}

8
src/main/java/org/springframework/data/history/RevisionMetadata.java

@ -40,8 +40,8 @@ public interface RevisionMetadata<N extends Number & Comparable<N>> { @@ -40,8 +40,8 @@ public interface RevisionMetadata<N extends Number & Comparable<N>> {
* @throws IllegalStateException if no revision number is available.
*/
default N getRequiredRevisionNumber() {
return getRevisionNumber()
.orElseThrow(() -> new IllegalStateException(String.format("No revision number found on %s!", getDelegate())));
return getRevisionNumber().orElseThrow(
() -> new IllegalStateException(String.format("No revision number found on %s!", (Object) getDelegate())));
}
/**
@ -58,8 +58,8 @@ public interface RevisionMetadata<N extends Number & Comparable<N>> { @@ -58,8 +58,8 @@ public interface RevisionMetadata<N extends Number & Comparable<N>> {
* @throw IllegalStateException if no revision date is available.
*/
default LocalDateTime getRequiredRevisionDate() {
return getRevisionDate()
.orElseThrow(() -> new IllegalStateException(String.format("No revision date found on %s!", getDelegate())));
return getRevisionDate().orElseThrow(
() -> new IllegalStateException(String.format("No revision date found on %s!", (Object) getDelegate())));
}
/**

82
src/test/java/org/springframework/data/history/AnnotationRevisionMetadataUnitTests.java

@ -0,0 +1,82 @@ @@ -0,0 +1,82 @@
/*
* Copyright 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.data.history;
import static org.assertj.core.api.Assertions.*;
import java.time.LocalDateTime;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.annotation.Reference;
/**
* Unit tests for {@link AnnotationRevisionMetadata}.
*
* @author Oliver Gierke
*/
public class AnnotationRevisionMetadataUnitTests {
@Test // DATACMNS-1173
public void exposesNoInformationOnEmptyProbe() {
Sample sample = new Sample();
RevisionMetadata<Long> metadata = getMetadata(sample);
assertThat(metadata.getRevisionNumber()).isEmpty();
assertThat(metadata.getRevisionDate()).isEmpty();
assertThatExceptionOfType(IllegalStateException.class) //
.isThrownBy(() -> metadata.getRequiredRevisionNumber());
assertThatExceptionOfType(IllegalStateException.class) //
.isThrownBy(() -> metadata.getRequiredRevisionDate());
}
@Test // DATACMNS-1173
public void exposesRevisionNumber() {
Sample sample = new Sample();
sample.revisionNumber = 1L;
RevisionMetadata<Long> metadata = getMetadata(sample);
assertThat(metadata.getRevisionNumber()).hasValue(1L);
assertThat(metadata.getRequiredRevisionNumber()).isEqualTo(1L);
}
@Test // DATACMNS-1173
public void exposesRevisionDate() {
Sample sample = new Sample();
sample.revisionDate = LocalDateTime.now();
RevisionMetadata<Long> metadata = getMetadata(sample);
assertThat(metadata.getRevisionDate()).hasValue(sample.revisionDate);
assertThat(metadata.getRequiredRevisionDate()).isEqualTo(sample.revisionDate);
}
private static RevisionMetadata<Long> getMetadata(Sample sample) {
return new AnnotationRevisionMetadata<>(sample, Autowired.class, Reference.class);
}
static class Sample {
@Autowired Long revisionNumber;
@Reference LocalDateTime revisionDate;
}
}
Loading…
Cancel
Save