Skip to content

Commit

Permalink
DATACMNS-1384 - Support for j.u.Date and j.s.Timestamp in AnnotationR…
Browse files Browse the repository at this point in the history
…evisionMetadata.

We now properly convert legacy Date instances into Instants to expose the revision date. Support for Timestamp is transparent as it extends Date.
  • Loading branch information
odrotbohm committed Sep 1, 2018
1 parent 7f8087a commit b52e343
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;
import java.util.Optional;

import org.springframework.data.util.AnnotationDetectionFieldCallback;
Expand Down Expand Up @@ -117,6 +118,10 @@ private static Instant convertToInstant(Object timestamp) {
return Instant.ofEpochMilli((Long) timestamp);
}

if (Date.class.isInstance(timestamp)) {
return Date.class.cast(timestamp).toInstant();
}

throw new IllegalArgumentException(String.format("Can't convert %s to Instant!", timestamp));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,11 @@

import static org.assertj.core.api.Assertions.*;

import java.sql.Timestamp;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.Date;

import org.assertj.core.api.SoftAssertions;
import org.junit.Test;
Expand Down Expand Up @@ -108,6 +110,31 @@ public void exposesRevisionDateForLong() {
softly.assertAll();
}

@Test // DATACMNS-1384
public void supportsTimestampRevisionInstant() {

SampleWithTimestamp sample = new SampleWithTimestamp();
Instant now = Instant.now();
sample.revision = Timestamp.from(now);

RevisionMetadata<Long> metadata = getMetadata(sample);

assertThat(metadata.getRevisionDate()).hasValue(LocalDateTime.ofInstant(now, ZoneOffset.systemDefault()));
}

@Test // DATACMNS-1384
public void supportsDateRevisionInstant() {

SampleWithDate sample = new SampleWithDate();
Date date = new Date();
sample.revision = date;

RevisionMetadata<Long> metadata = getMetadata(sample);

assertThat(metadata.getRevisionDate())
.hasValue(LocalDateTime.ofInstant(date.toInstant(), ZoneOffset.systemDefault()));
}

private static RevisionMetadata<Long> getMetadata(Object sample) {
return new AnnotationRevisionMetadata<>(sample, Autowired.class, Reference.class);
}
Expand All @@ -129,4 +156,14 @@ static class SampleWithLong {
@Autowired Long revisionNumber;
@Reference long revisionLong;
}

// DATACMNS-1384

static class SampleWithTimestamp {
@Reference Timestamp revision;
}

static class SampleWithDate {
@Reference Date revision;
}
}

0 comments on commit b52e343

Please sign in to comment.