[JGIT PATCH 1/2] Add time and timezone to SystemReader properties

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Having an uncontrollable variable time and timezone is
inconvenient for unit testing.

Signed-off-by: Robin Rosenberg <robin.rosenberg@xxxxxxxxxx>
---
 .../tst/org/spearce/jgit/lib/MockSystemReader.java |   11 ++++++++++
 .../src/org/spearce/jgit/lib/PersonIdent.java      |    6 +++-
 .../src/org/spearce/jgit/util/SystemReader.java    |   22 ++++++++++++++++++++
 3 files changed, 37 insertions(+), 2 deletions(-)

diff --git a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/MockSystemReader.java b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/MockSystemReader.java
index 7a65f99..e51df7e 100644
--- a/org.spearce.jgit.test/tst/org/spearce/jgit/lib/MockSystemReader.java
+++ b/org.spearce.jgit.test/tst/org/spearce/jgit/lib/MockSystemReader.java
@@ -39,6 +39,7 @@
 
 import java.util.HashMap;
 import java.util.Map;
+import java.util.TimeZone;
 
 import org.spearce.jgit.util.SystemReader;
 
@@ -75,4 +76,14 @@ public FileBasedConfig openUserConfig() {
 	public String getHostname() {
 		return "fake.host.example.com";
 	}
+
+	@Override
+	public long getCurrentTime() {
+		return 1250379778668L; // Sat Aug 15 20:12:58 GMT-03:30 2009 
+	}
+
+	@Override
+	public int getTimezone(long when) {
+		return TimeZone.getTimeZone("GMT-3:30").getOffset(when);
+	}
 }
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java b/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java
index 393e177..a0418ab 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/PersonIdent.java
@@ -44,6 +44,8 @@
 import java.util.Locale;
 import java.util.TimeZone;
 
+import org.spearce.jgit.util.SystemReader;
+
 /**
  * A combination of a person identity and time in Git.
  * 
@@ -70,8 +72,8 @@ public PersonIdent(final Repository repo) {
 		final RepositoryConfig config = repo.getConfig();
 		name = config.getCommitterName();
 		emailAddress = config.getCommitterEmail();
-		when = System.currentTimeMillis();
-		tzOffset = TimeZone.getDefault().getOffset(when) / (60 * 1000);
+		when = SystemReader.getInstance().getCurrentTime();
+		tzOffset = SystemReader.getInstance().getTimezone(when);
 	}
 
 	/**
diff --git a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
index 51a0d29..083d120 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/util/SystemReader.java
@@ -40,6 +40,7 @@
 import java.io.File;
 import java.net.InetAddress;
 import java.net.UnknownHostException;
+import java.util.TimeZone;
 
 import org.spearce.jgit.lib.FileBasedConfig;
 
@@ -81,6 +82,16 @@ public String getHostname() {
 			}
 			return hostname;
 		}
+
+		@Override
+		public long getCurrentTime() {
+			return System.currentTimeMillis();
+		}
+
+		@Override
+		public int getTimezone(long when) {
+			return TimeZone.getDefault().getOffset(when) / (60 * 1000);
+		}
 	};
 
 	/** @return the live instance to read system properties. */
@@ -120,4 +131,15 @@ public static void setInstance(SystemReader newReader) {
 	 * @return the git configuration found in the user home
 	 */
 	public abstract FileBasedConfig openUserConfig();
+
+	/**
+	 * @return the current system time
+	 */
+	public abstract long getCurrentTime();
+
+	/**
+	 * @param when TODO
+	 * @return the local time zone
+	 */
+	public abstract int getTimezone(long when);
 }
-- 
1.6.4.115.gc0eb0

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]