TestTimeUtil: Also stub out JGit's SystemReader [redux]

This is used in several places, most notably in the constructors for
PersonIdent that don't provide explicit timestamps. As a result of
this change, projects created in rapid succession in tests that use a
test clock should not share empty commit SHA-1s.

This was originally done in I6df1baa18, but was accidentally removed
by the merge done in I991be7faf.

Change-Id: I41445c6602ccfea349f303f6d5044bc9d56aec1d
This commit is contained in:
Dave Borowitz 2016-02-18 21:56:04 -05:00 committed by David Pursehouse
parent 74e439808e
commit 3222b751fa
1 changed files with 51 additions and 1 deletions

View File

@ -17,6 +17,10 @@ package com.google.gerrit.testutil;
import static com.google.common.base.Preconditions.checkState;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import org.eclipse.jgit.lib.Config;
import org.eclipse.jgit.storage.file.FileBasedConfig;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.SystemReader;
import org.joda.time.DateTime;
import org.joda.time.DateTimeUtils;
import org.joda.time.DateTimeUtils.MillisProvider;
@ -63,11 +67,57 @@ public class TestTimeUtil {
return clockMs.getAndAdd(clockStepMs);
}
});
SystemReader.setInstance(null);
final SystemReader defaultReader = SystemReader.getInstance();
SystemReader r = new SystemReader() {
@Override
public String getHostname() {
return defaultReader.getHostname();
}
@Override
public String getenv(String variable) {
return defaultReader.getenv(variable);
}
@Override
public String getProperty(String key) {
return defaultReader.getProperty(key);
}
@Override
public FileBasedConfig openUserConfig(Config parent, FS fs) {
return defaultReader.openUserConfig(parent, fs);
}
@Override
public FileBasedConfig openSystemConfig(Config parent, FS fs) {
return defaultReader.openSystemConfig(parent, fs);
}
@Override
public long getCurrentTime() {
return clockMs.getAndAdd(clockStepMs);
}
@Override
public int getTimezone(long when) {
return defaultReader.getTimezone(when);
}
};
SystemReader.setInstance(r);
}
/** Reset the clock to use the actual system clock. */
/**
* Reset the clock to use the actual system clock.
* <p>
* As a side effect, resets the {@link SystemReader} to the original default
* instance.
*/
public static synchronized void useSystemTime() {
DateTimeUtils.setCurrentMillisSystem();
SystemReader.setInstance(null);
}
private TestTimeUtil() {