Fix test of RelativeDateFormatter RelativeDateFormatterTest was testing the relative date formatter from jgit (org.eclipse.jgit.util.RelativeDateFormatter) instead of the relative date formatter that we use on the client (com.google.gerrit.client.RelativeDateFormatter). When testing com.google.gerrit.client.RelativeDateFormatter we can't access the GWT constants and messages (since they are not available outside the GWT context). Hence these constants and messages are abstracted out. Change-Id: Ia16f355ba3d646b3b7c696bdc58ea974334ef6d5 Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/RelativeDateFormatter.java b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/RelativeDateFormatter.java index 7c62ed7..2f0df8c 100644 --- a/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/RelativeDateFormatter.java +++ b/gerrit-gwtui-common/src/main/java/com/google/gerrit/client/RelativeDateFormatter.java
@@ -14,9 +14,6 @@ package com.google.gerrit.client; -import static com.google.gerrit.client.CommonConstants.C; -import static com.google.gerrit.client.CommonMessages.M; - import java.util.Date; /** @@ -24,6 +21,9 @@ * defined by {@code git log --relative-date}. */ public class RelativeDateFormatter { + private static CommonConstants constants; + private static CommonMessages messages; + static final long SECOND_IN_MILLIS = 1000; static final long MINUTE_IN_MILLIS = 60 * SECOND_IN_MILLIS; static final long HOUR_IN_MILLIS = 60 * MINUTE_IN_MILLIS; @@ -32,6 +32,19 @@ static final long MONTH_IN_MILLIS = 30 * DAY_IN_MILLIS; static final long YEAR_IN_MILLIS = 365 * DAY_IN_MILLIS; + static void setConstants(CommonConstants c, CommonMessages m) { + constants = c; + messages = m; + } + + private static CommonConstants c() { + return constants != null ? constants : CommonConstants.C; + } + + private static CommonMessages m() { + return messages != null ? messages : CommonMessages.M; + } + /** * @param when {@link Date} to format * @return age of given {@link Date} compared to now formatted in the same relative format as @@ -42,81 +55,81 @@ // shouldn't happen in a perfect world if (ageMillis < 0) { - return C.inTheFuture(); + return c().inTheFuture(); } // seconds if (ageMillis < upperLimit(MINUTE_IN_MILLIS)) { long seconds = round(ageMillis, SECOND_IN_MILLIS); if (seconds == 1) { - return C.oneSecondAgo(); + return c().oneSecondAgo(); } - return M.secondsAgo(seconds); + return m().secondsAgo(seconds); } // minutes if (ageMillis < upperLimit(HOUR_IN_MILLIS)) { long minutes = round(ageMillis, MINUTE_IN_MILLIS); if (minutes == 1) { - return C.oneMinuteAgo(); + return c().oneMinuteAgo(); } - return M.minutesAgo(minutes); + return m().minutesAgo(minutes); } // hours if (ageMillis < upperLimit(DAY_IN_MILLIS)) { long hours = round(ageMillis, HOUR_IN_MILLIS); if (hours == 1) { - return C.oneHourAgo(); + return c().oneHourAgo(); } - return M.hoursAgo(hours); + return m().hoursAgo(hours); } // up to 14 days use days if (ageMillis < 14 * DAY_IN_MILLIS) { long days = round(ageMillis, DAY_IN_MILLIS); if (days == 1) { - return C.oneDayAgo(); + return c().oneDayAgo(); } - return M.daysAgo(days); + return m().daysAgo(days); } // up to 10 weeks use weeks if (ageMillis < 10 * WEEK_IN_MILLIS) { long weeks = round(ageMillis, WEEK_IN_MILLIS); if (weeks == 1) { - return C.oneWeekAgo(); + return c().oneWeekAgo(); } - return M.weeksAgo(weeks); + return m().weeksAgo(weeks); } // months if (ageMillis < YEAR_IN_MILLIS) { long months = round(ageMillis, MONTH_IN_MILLIS); if (months == 1) { - return C.oneMonthAgo(); + return c().oneMonthAgo(); } - return M.monthsAgo(months); + return m().monthsAgo(months); } // up to 5 years use "year, months" rounded to months if (ageMillis < 5 * YEAR_IN_MILLIS) { long years = ageMillis / YEAR_IN_MILLIS; - String yearLabel = (years > 1) ? C.years() : C.year(); + String yearLabel = (years > 1) ? c().years() : c().year(); long months = round(ageMillis % YEAR_IN_MILLIS, MONTH_IN_MILLIS); - String monthLabel = (months > 1) ? C.months() : (months == 1 ? C.month() : ""); + String monthLabel = (months > 1) ? c().months() : (months == 1 ? c().month() : ""); if (months == 0) { - return M.years0MonthsAgo(years, yearLabel); + return m().years0MonthsAgo(years, yearLabel); } - return M.yearsMonthsAgo(years, yearLabel, months, monthLabel); + return m().yearsMonthsAgo(years, yearLabel, months, monthLabel); } // years long years = round(ageMillis, YEAR_IN_MILLIS); if (years == 1) { - return C.oneYearAgo(); + return c().oneYearAgo(); } - return M.yearsAgo(years); + return m().yearsAgo(years); } private static long upperLimit(long unit) {
diff --git a/gerrit-gwtui-common/src/test/java/com/google/gerrit/client/RelativeDateFormatterTest.java b/gerrit-gwtui-common/src/test/java/com/google/gerrit/client/RelativeDateFormatterTest.java index 937fc96..513d95a 100644 --- a/gerrit-gwtui-common/src/test/java/com/google/gerrit/client/RelativeDateFormatterTest.java +++ b/gerrit-gwtui-common/src/test/java/com/google/gerrit/client/RelativeDateFormatterTest.java
@@ -22,11 +22,23 @@ import static org.junit.Assert.assertEquals; import java.util.Date; -import org.eclipse.jgit.util.RelativeDateFormatter; +import org.junit.AfterClass; +import org.junit.BeforeClass; import org.junit.Test; public class RelativeDateFormatterTest { + @BeforeClass + public static void setConstants() { + Constants c = new Constants(); + RelativeDateFormatter.setConstants(c, c); + } + + @AfterClass + public static void unsetConstants() { + RelativeDateFormatter.setConstants(null, null); + } + private static void assertFormat(long ageFromNow, long timeUnit, String expectedFormat) { Date d = new Date(System.currentTimeMillis() - ageFromNow * timeUnit); String s = RelativeDateFormatter.format(d); @@ -41,7 +53,7 @@ @Test public void formatSeconds() { - assertFormat(1, SECOND_IN_MILLIS, "1 seconds ago"); + assertFormat(1, SECOND_IN_MILLIS, "1 second ago"); assertFormat(89, SECOND_IN_MILLIS, "89 seconds ago"); } @@ -93,4 +105,111 @@ assertFormat(5, YEAR_IN_MILLIS, "5 years ago"); assertFormat(60, YEAR_IN_MILLIS, "60 years ago"); } + + private static class Constants implements CommonConstants, CommonMessages { + @Override + public String inTheFuture() { + return "in the future"; + } + + @Override + public String month() { + return "month"; + } + + @Override + public String months() { + return "months"; + } + + @Override + public String year() { + return "year"; + } + + @Override + public String years() { + return "years"; + } + + @Override + public String oneSecondAgo() { + return "1 second ago"; + } + + @Override + public String oneMinuteAgo() { + return "1 minute ago"; + } + + @Override + public String oneHourAgo() { + return "1 hour ago"; + } + + @Override + public String oneDayAgo() { + return "1 day ago"; + } + + @Override + public String oneWeekAgo() { + return "1 week ago"; + } + + @Override + public String oneMonthAgo() { + return "1 month ago"; + } + + @Override + public String oneYearAgo() { + return "1 year ago"; + } + + @Override + public String secondsAgo(long seconds) { + return seconds + " seconds ago"; + } + + @Override + public String minutesAgo(long minutes) { + return minutes + " minutes ago"; + } + + @Override + public String hoursAgo(long hours) { + return hours + " hours ago"; + } + + @Override + public String daysAgo(long days) { + return days + " days ago"; + } + + @Override + public String weeksAgo(long weeks) { + return weeks + " weeks ago"; + } + + @Override + public String monthsAgo(long months) { + return months + " months ago"; + } + + @Override + public String yearsAgo(long years) { + return years + " years ago"; + } + + @Override + public String years0MonthsAgo(long years, String yearLabel) { + return years + " " + yearLabel + " ago"; + } + + @Override + public String yearsMonthsAgo(long years, String yearLabel, long months, String monthLabel) { + return years + " " + yearLabel + ", " + months + " " + monthLabel + " ago"; + } + } }