diff options
author | Ivan Frade <ifrade@google.com> | 2024-11-04 15:23:47 -0800 |
---|---|---|
committer | Ivan Frade <ifrade@google.com> | 2024-12-13 13:23:12 -0800 |
commit | 320a95e37d3f791b0d16ff7753bc1d7b68b0681f (patch) | |
tree | f853bb9f777bc933e8f06a5478968d7943ab2d6a | |
parent | 9cf0a1f174886f844f61d056d4c3383415d27b9e (diff) | |
download | jgit-320a95e37d3f791b0d16ff7753bc1d7b68b0681f.tar.gz jgit-320a95e37d3f791b0d16ff7753bc1d7b68b0681f.zip |
TestRepository: Deprecate #getDate and use #getInstant instead
Errorprone recomments to use the more modern java.time API instead of
the java.util.Date family of classes.
This removes all uses of TestRepository#getDate and replace them with
getInstant. The method is marked as deprecated and doesn't have any
other usages.
Change-Id: I84ea63abf781d0d3c7c2724891fd2ef7afc2db1d
4 files changed, 29 insertions, 14 deletions
diff --git a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java index c6cdfafe84..33ec042930 100644 --- a/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java +++ b/org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java @@ -21,6 +21,7 @@ import java.io.IOException; import java.io.OutputStream; import java.security.MessageDigest; import java.time.Instant; +import java.time.ZoneId; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -198,7 +199,9 @@ public class TestRepository<R extends Repository> implements AutoCloseable { * * @return current date. * @since 4.2 + * @deprecated Use {@link #getInstant()} instead. */ + @Deprecated public Date getDate() { return new Date(mockSystemReader.getCurrentTime()); } @@ -210,18 +213,30 @@ public class TestRepository<R extends Repository> implements AutoCloseable { * @since 6.8 */ public Instant getInstant() { - return Instant.ofEpochMilli(mockSystemReader.getCurrentTime()); + return mockSystemReader.now(); } /** * Get timezone * * @return timezone used for default identities. + * @deprecated Use {@link #getTimeZoneId()} instead. */ + @Deprecated public TimeZone getTimeZone() { return mockSystemReader.getTimeZone(); } + + /** + * Get timezone + * + * @return timezone used for default identities. + */ + public ZoneId getTimeZoneId() { + return mockSystemReader.getTimeZoneId(); + } + /** * Adjust the current time that will used by the next commit. * @@ -233,14 +248,14 @@ public class TestRepository<R extends Repository> implements AutoCloseable { } /** - * Set the author and committer using {@link #getDate()}. + * Set the author and committer using {@link #getInstant()}. * * @param c * the commit builder to store. */ public void setAuthorAndCommitter(org.eclipse.jgit.lib.CommitBuilder c) { - c.setAuthor(new PersonIdent(defaultAuthor, getDate())); - c.setCommitter(new PersonIdent(defaultCommitter, getDate())); + c.setAuthor(new PersonIdent(defaultAuthor, getInstant())); + c.setCommitter(new PersonIdent(defaultCommitter, getInstant())); } /** @@ -488,8 +503,8 @@ public class TestRepository<R extends Repository> implements AutoCloseable { c = new org.eclipse.jgit.lib.CommitBuilder(); c.setTreeId(tree); c.setParentIds(parents); - c.setAuthor(new PersonIdent(defaultAuthor, getDate())); - c.setCommitter(new PersonIdent(defaultCommitter, getDate())); + c.setAuthor(new PersonIdent(defaultAuthor, getInstant())); + c.setCommitter(new PersonIdent(defaultCommitter, getInstant())); c.setMessage(""); ObjectId id; try (ObjectInserter ins = inserter) { @@ -529,7 +544,7 @@ public class TestRepository<R extends Repository> implements AutoCloseable { final TagBuilder t = new TagBuilder(); t.setObjectId(dst); t.setTag(name); - t.setTagger(new PersonIdent(defaultCommitter, getDate())); + t.setTagger(new PersonIdent(defaultCommitter, getInstant())); t.setMessage(""); ObjectId id; try (ObjectInserter ins = inserter) { @@ -798,7 +813,7 @@ public class TestRepository<R extends Repository> implements AutoCloseable { b.setParentId(head); b.setTreeId(merger.getResultTreeId()); b.setAuthor(commit.getAuthorIdent()); - b.setCommitter(new PersonIdent(defaultCommitter, getDate())); + b.setCommitter(new PersonIdent(defaultCommitter, getInstant())); b.setMessage(commit.getFullMessage()); ObjectId result; try (ObjectInserter ins = inserter) { @@ -1408,7 +1423,7 @@ public class TestRepository<R extends Repository> implements AutoCloseable { c.setAuthor(author); if (committer != null) { if (updateCommitterTime) - committer = new PersonIdent(committer, getDate()); + committer = new PersonIdent(committer, getInstant()); c.setCommitter(committer); } diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CloneTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CloneTest.java index a1fb9fb589..c56cc6bf3c 100644 --- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CloneTest.java +++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/CloneTest.java @@ -126,7 +126,7 @@ public class CloneTest extends CLIRepositoryTestCase { JGitTestUtil.writeTrashFile(db, "Test.txt", "Some change"); git.add().addFilepattern("Test.txt").call(); return git.commit() - .setCommitter(new PersonIdent(this.committer, tr.getDate())) + .setCommitter(new PersonIdent(this.committer, tr.getInstant())) .setMessage("Second commit").call(); } @@ -134,7 +134,7 @@ public class CloneTest extends CLIRepositoryTestCase { JGitTestUtil.writeTrashFile(db, "change.txt", "another change"); git.add().addFilepattern("change.txt").call(); return git.commit() - .setCommitter(new PersonIdent(this.committer, tr.getDate())) + .setCommitter(new PersonIdent(this.committer, tr.getInstant())) .setMessage("Third commit").call(); } diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java index 450b753d94..924703464b 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java @@ -275,9 +275,9 @@ public class TestRepositoryTest { RevCommit toPick = tr.commit() .parent(tr.commit().create()) // Can't cherry-pick root. .author(new PersonIdent("Cherrypick Author", "cpa@example.com", - tr.getDate(), tr.getTimeZone())) + tr.getInstant(), tr.getTimeZoneId())) .author(new PersonIdent("Cherrypick Committer", "cpc@example.com", - tr.getDate(), tr.getTimeZone())) + tr.getInstant(), tr.getTimeZoneId())) .message("message to cherry-pick") .add("bar", "bar contents\n") .create(); diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkTestCase.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkTestCase.java index ec0c0e7e84..b37a19dd7a 100644 --- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkTestCase.java +++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkTestCase.java @@ -39,7 +39,7 @@ public abstract class RevWalkTestCase extends RepositoryTestCase { } protected Date getDate() { - return util.getDate(); + return Date.from(util.getInstant()); } protected void tick(int secDelta) { |