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;
/** 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");
/** 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);
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()));
}
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) {
}
}
+ long now = 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009
+
final Map<String, String> values = new HashMap<String, String>();
FileBasedConfig userGitConfig;
@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
private final ObjectInserter inserter;
- private long now;
+ private final MockSystemReader mockSystemReader;
/**
* Wrap a repository with test building tools.
* @throws IOException
*/
public TestRepository(R db) throws IOException {
- this(db, new RevWalk(db));
+ this(db, new RevWalk(db), new MockSystemReader());
}
/**
* @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. */
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();
}
/**
* 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()));
}
/**
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) {
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) {
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) {
c.setAuthor(author);
if (committer != null) {
if (updateCommitterTime)
- committer = new PersonIdent(committer, new Date(now));
+ committer = new PersonIdent(committer, getDate());
c.setCommitter(committer);
}