]> source.dussan.org Git - jgit.git/commitdiff
Push control of time into MockSystemReader 26/58526/3
authorTerry Parker <tparker@google.com>
Tue, 20 Oct 2015 00:33:50 +0000 (17:33 -0700)
committerTerry Parker <tparker@google.com>
Tue, 20 Oct 2015 22:57:31 +0000 (15:57 -0700)
LocalDiskRepositoryTestCase and TestRepository have competing ideas
about time. Push them into MockSystemReader so they can
cooperate.

Rename getClock() methods that return Dates to getDate().

Change-Id: Ibbd9fe7f85d0064b0a19e3b675b9718a9e67c479
Signed-off-by: Terry Parker <tparker@google.com>
org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/LocalDiskRepositoryTestCase.java
org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/MockSystemReader.java
org.eclipse.jgit.junit/src/org/eclipse/jgit/junit/TestRepository.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/junit/TestRepositoryTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkFilterTest.java
org.eclipse.jgit.test/tst/org/eclipse/jgit/revwalk/RevWalkTestCase.java

index b5348f9980658081abc802e516464d1584f2fcdf..bb5f9efb8fd82b04c793109b28c76e4310d1941b 100644 (file)
@@ -51,7 +51,6 @@ import static org.junit.Assert.fail;
 import java.io.File;
 import java.io.IOException;
 import java.util.*;
-import java.util.concurrent.TimeUnit;
 
 import org.eclipse.jgit.dircache.DirCache;
 import org.eclipse.jgit.dircache.DirCacheEntry;
@@ -92,11 +91,12 @@ public abstract class LocalDiskRepositoryTestCase {
        /** A fake (but stable) identity for committer fields in the test. */
        protected PersonIdent committer;
 
+       /** A {@link SystemReader} used to coordinate time, envars, etc. */
+       protected MockSystemReader mockSystemReader;
+
        private final List<Repository> toClose = new ArrayList<Repository>();
        private File tmp;
 
-       private MockSystemReader mockSystemReader;
-
        @Before
        public void setUp() throws Exception {
                tmp = File.createTempFile("jgit_test_", "_tmp");
@@ -171,9 +171,8 @@ public abstract class LocalDiskRepositoryTestCase {
 
        /** Increment the {@link #author} and {@link #committer} times. */
        protected void tick() {
-               final long delta = TimeUnit.MILLISECONDS.convert(5 * 60,
-                               TimeUnit.SECONDS);
-               final long now = author.getWhen().getTime() + delta;
+               mockSystemReader.tick(5 * 60);
+               final long now = mockSystemReader.getCurrentTime();
                final int tz = mockSystemReader.getTimezone(now);
 
                author = new PersonIdent(author, now, tz);
@@ -278,11 +277,10 @@ public abstract class LocalDiskRepositoryTestCase {
                        throws IllegalStateException, IOException {
                DirCache dc = repo.readDirCache();
                StringBuilder sb = new StringBuilder();
-               TreeSet<Long> timeStamps = null;
+               TreeSet<Long> timeStamps = new TreeSet<Long>();
 
                // iterate once over the dircache just to collect all time stamps
                if (0 != (includedOptions & MOD_TIME)) {
-                       timeStamps = new TreeSet<Long>();
                        for (int i=0; i<dc.getEntryCount(); ++i)
                                timeStamps.add(Long.valueOf(dc.getEntry(i).getLastModified()));
                }
index d24dd44fff08171e050b8c997861719f5ee225f8..03a2b1a58477e033d93106b66aae6efdc9296cae 100644 (file)
@@ -62,6 +62,9 @@ import org.eclipse.jgit.storage.file.FileBasedConfig;
 import org.eclipse.jgit.util.FS;
 import org.eclipse.jgit.util.SystemReader;
 
+/**
+ * Mock {@link SystemReader} for tests.
+ */
 public class MockSystemReader extends SystemReader {
        private final class MockConfig extends FileBasedConfig {
                private MockConfig(File cfgLocation, FS fs) {
@@ -79,6 +82,8 @@ public class MockSystemReader extends SystemReader {
                }
        }
 
+       long now = 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
+
        final Map<String, String> values = new HashMap<String, String>();
 
        FileBasedConfig userGitConfig;
@@ -138,7 +143,17 @@ public class MockSystemReader extends SystemReader {
 
        @Override
        public long getCurrentTime() {
-               return 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
+               return now;
+       }
+
+       /**
+        * Adjusts the current time in seconds.
+        *
+        * @param secDelta
+        *            number of seconds to add to the current time.
+        */
+       public void tick(final int secDelta) {
+               now += secDelta * 1000L;
        }
 
        @Override
index 925a6b0216dc62cad37c4bff4830ff70923b802c..98e29d708310dec820f261946cd532335ec2b3ce 100644 (file)
@@ -138,7 +138,7 @@ public class TestRepository<R extends Repository> {
 
        private final ObjectInserter inserter;
 
-       private long now;
+       private final MockSystemReader mockSystemReader;
 
        /**
         * Wrap a repository with test building tools.
@@ -148,7 +148,7 @@ public class TestRepository<R extends Repository> {
         * @throws IOException
         */
        public TestRepository(R db) throws IOException {
-               this(db, new RevWalk(db));
+               this(db, new RevWalk(db), new MockSystemReader());
        }
 
        /**
@@ -161,11 +161,28 @@ public class TestRepository<R extends Repository> {
         * @throws IOException
         */
        public TestRepository(R db, RevWalk rw) throws IOException {
+               this(db, rw, new MockSystemReader());
+       }
+
+       /**
+        * Wrap a repository with test building tools.
+        *
+        * @param db
+        *            the test repository to write into.
+        * @param rw
+        *            the RevObject pool to use for object lookup.
+        * @param reader
+        *            the MockSystemReader to use for clock and other system
+        *            operations.
+        * @throws IOException
+        */
+       public TestRepository(R db, RevWalk rw, MockSystemReader reader)
+                       throws IOException {
                this.db = db;
                this.git = Git.wrap(db);
                this.pool = rw;
                this.inserter = db.newObjectInserter();
-               this.now = 1236977987000L;
+               this.mockSystemReader = reader;
        }
 
        /** @return the repository this helper class operates against. */
@@ -186,14 +203,14 @@ public class TestRepository<R extends Repository> {
                return git;
        }
 
-       /** @return current time adjusted by {@link #tick(int)}. */
-       public Date getClock() {
-               return new Date(now);
+       /** @return current date. */
+       public Date getDate() {
+               return new Date(mockSystemReader.getCurrentTime());
        }
 
        /** @return timezone used for default identities. */
        public TimeZone getTimeZone() {
-               return defaultCommitter.getTimeZone();
+               return mockSystemReader.getTimeZone();
        }
 
        /**
@@ -203,18 +220,18 @@ public class TestRepository<R extends Repository> {
         *            number of seconds to add to the current time.
         */
        public void tick(final int secDelta) {
-               now += secDelta * 1000L;
+               mockSystemReader.tick(secDelta);
        }
 
        /**
-        * Set the author and committer using {@link #getClock()}.
+        * Set the author and committer using {@link #getDate()}.
         *
         * @param c
         *            the commit builder to store.
         */
        public void setAuthorAndCommitter(org.eclipse.jgit.lib.CommitBuilder c) {
-               c.setAuthor(new PersonIdent(defaultAuthor, new Date(now)));
-               c.setCommitter(new PersonIdent(defaultCommitter, new Date(now)));
+               c.setAuthor(new PersonIdent(defaultAuthor, getDate()));
+               c.setCommitter(new PersonIdent(defaultCommitter, getDate()));
        }
 
        /**
@@ -392,8 +409,8 @@ public class TestRepository<R extends Repository> {
                c = new org.eclipse.jgit.lib.CommitBuilder();
                c.setTreeId(tree);
                c.setParentIds(parents);
-               c.setAuthor(new PersonIdent(defaultAuthor, new Date(now)));
-               c.setCommitter(new PersonIdent(defaultCommitter, new Date(now)));
+               c.setAuthor(new PersonIdent(defaultAuthor, getDate()));
+               c.setCommitter(new PersonIdent(defaultCommitter, getDate()));
                c.setMessage("");
                ObjectId id;
                try (ObjectInserter ins = inserter) {
@@ -428,7 +445,7 @@ public class TestRepository<R extends Repository> {
                final TagBuilder t = new TagBuilder();
                t.setObjectId(dst);
                t.setTag(name);
-               t.setTagger(new PersonIdent(defaultCommitter, new Date(now)));
+               t.setTagger(new PersonIdent(defaultCommitter, getDate()));
                t.setMessage("");
                ObjectId id;
                try (ObjectInserter ins = inserter) {
@@ -663,7 +680,7 @@ public class TestRepository<R extends Repository> {
                        b.setParentId(head);
                        b.setTreeId(merger.getResultTreeId());
                        b.setAuthor(commit.getAuthorIdent());
-                       b.setCommitter(new PersonIdent(defaultCommitter, new Date(now)));
+                       b.setCommitter(new PersonIdent(defaultCommitter, getDate()));
                        b.setMessage(commit.getFullMessage());
                        ObjectId result;
                        try (ObjectInserter ins = inserter) {
@@ -1100,7 +1117,7 @@ public class TestRepository<R extends Repository> {
                                        c.setAuthor(author);
                                if (committer != null) {
                                        if (updateCommitterTime)
-                                               committer = new PersonIdent(committer, new Date(now));
+                                               committer = new PersonIdent(committer, getDate());
                                        c.setCommitter(committer);
                                }
 
index fbb9eecdff6198527e20b90736a3056104b28c24..b69b8e01e073d6f2cf26f34b397b88b89df87fb5 100644 (file)
@@ -307,9 +307,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.getClock(), tr.getTimeZone()))
+                                               tr.getDate(), tr.getTimeZone()))
                                .author(new PersonIdent("Cherrypick Committer", "cpc@example.com",
-                                               tr.getClock(), tr.getTimeZone()))
+                                               tr.getDate(), tr.getTimeZone()))
                                .message("message to cherry-pick")
                                .add("bar", "bar contents\n")
                                .create();
index 643ba26d9a463dabff12160813a68a6172227b4e..8ab972837cb4340dc9149266077a74157a6a105e 100644 (file)
@@ -250,14 +250,14 @@ public class RevWalkFilterTest extends RevWalkTestCase {
                final RevCommit b = commit(a);
                tick(100);
 
-               Date since = getClock();
+               Date since = getDate();
                final RevCommit c1 = commit(b);
                tick(100);
 
                final RevCommit c2 = commit(b);
                tick(100);
 
-               Date until = getClock();
+               Date until = getDate();
                final RevCommit d = commit(c1, c2);
                tick(100);
 
index 881deb1f5a31e39c84720c57fb40ff5aa9da938a..30586ee1e298d3ee58c2c96fd269b849c52d458f 100644 (file)
@@ -70,8 +70,8 @@ public abstract class RevWalkTestCase extends RepositoryTestCase {
                return new RevWalk(db);
        }
 
-       protected Date getClock() {
-               return util.getClock();
+       protected Date getDate() {
+               return util.getDate();
        }
 
        protected void tick(final int secDelta) {