]> source.dussan.org Git - gitblit.git/commitdiff
TimeUtils: Increase testability and add tests
authorFlorian Zschocke <f.zschocke+git@gmail.com>
Sat, 23 Oct 2021 21:20:47 +0000 (23:20 +0200)
committerFlorian Zschocke <f.zschocke+git@gmail.com>
Sat, 23 Oct 2021 21:20:47 +0000 (23:20 +0200)
Add tests for `timeAgo` to analyse issue #1248.
The tests are dependent on when they run as they time functions use the
current date and time. To make them testable in a reproducible way, we
need the ability to pass in what we think is "now". So add overloaded
methods that take a `now` parameter so that we can pass in the current
time.

src/main/java/com/gitblit/utils/TimeUtils.java
src/test/java/com/gitblit/tests/TimeUtilsTest.java

index c0e98e5d325223fec567bb9904a46f29808164b4..001e6be6afe6b90f9745f935e66663ef2fda4402 100644 (file)
@@ -59,7 +59,11 @@ public class TimeUtils {
         * @return true if date is today\r
         */\r
        public static boolean isToday(Date date, TimeZone timezone) {\r
-               Date now = new Date();\r
+               return isToday(date, timezone, new Date());\r
+       }\r
+\r
+\r
+       public static boolean isToday(Date date, TimeZone timezone, Date now) {\r
                SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");\r
                if (timezone != null) {\r
                        df.setTimeZone(timezone);\r
@@ -74,8 +78,13 @@ public class TimeUtils {
         * @return true if date is yesterday\r
         */\r
        public static boolean isYesterday(Date date, TimeZone timezone) {\r
+               return isYesterday(date, timezone, new Date());\r
+       }\r
+\r
+\r
+       public static boolean isYesterday(Date date, TimeZone timezone, Date now) {\r
                Calendar cal = Calendar.getInstance();\r
-               cal.setTime(new Date());\r
+               cal.setTime(now);\r
                cal.add(Calendar.DATE, -1);\r
                SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd");\r
                if (timezone != null) {\r
@@ -157,7 +166,12 @@ public class TimeUtils {
         * @return hours ago\r
         */\r
        public static int hoursAgo(Date date, boolean roundup) {\r
-               long diff = System.currentTimeMillis() - date.getTime();\r
+               return hoursAgo(date, System.currentTimeMillis(), roundup);\r
+       }\r
+\r
+\r
+       public static int hoursAgo(Date date, long now, boolean roundup) {\r
+               long diff = now - date.getTime();\r
                int hours = (int) (diff / ONEHOUR);\r
                if (roundup && (diff % ONEHOUR) >= HALFHOUR) {\r
                        hours++;\r
@@ -172,7 +186,11 @@ public class TimeUtils {
         * @return days ago\r
         */\r
        public static int daysAgo(Date date) {\r
-               long today = ONEDAY * (System.currentTimeMillis()/ONEDAY);\r
+               return daysAgo(date, System.currentTimeMillis());\r
+       }\r
+\r
+       public static int daysAgo(Date date, long now) {\r
+               long today = ONEDAY * (now/ONEDAY);\r
                long day = ONEDAY * (date.getTime()/ONEDAY);\r
                long diff = today - day;\r
                int days = (int) (diff / ONEDAY);\r
@@ -217,13 +235,19 @@ public class TimeUtils {
         * @return the string representation of the duration OR the css class\r
         */\r
        private String timeAgo(Date date, boolean css) {\r
-               if (isToday(date, timezone) || isYesterday(date, timezone)) {\r
-                       int mins = minutesAgo(date, true);\r
+               return timeAgo(date, css, System.currentTimeMillis());\r
+       }\r
+\r
+\r
+       public String timeAgo(Date date, boolean css, long now) {\r
+               Date dNow = new Date(now);\r
+               if (isToday(date, timezone, dNow) || isYesterday(date, timezone, dNow)) {\r
+                       int mins = minutesAgo(date, now, true);\r
                        if (mins >= 120) {\r
                                if (css) {\r
                                        return "age1";\r
                                }\r
-                               int hours = hoursAgo(date, true);\r
+                               int hours = hoursAgo(date, now, true);\r
                                if (hours > 23) {\r
                                        return yesterday();\r
                                } else {\r
@@ -238,7 +262,7 @@ public class TimeUtils {
                        }\r
                        return translate("gb.time.justNow", "just now");\r
                } else {\r
-                       int days = daysAgo(date);\r
+                       int days = daysAgo(date, now);\r
                        if (css) {\r
                                if (days <= 7) {\r
                                        return "age2";\r
index ef506ef4a48622fe4044a643fb29708d2d0db845..2dde6d5122931b6c739ca60299dfff171dca4526 100644 (file)
@@ -15,7 +15,9 @@
  */\r
 package com.gitblit.tests;\r
 \r
+import java.util.Calendar;\r
 import java.util.Date;\r
+import java.util.TimeZone;\r
 \r
 import org.junit.Test;\r
 \r
@@ -27,6 +29,10 @@ public class TimeUtilsTest extends GitblitUnitTest {
                return new Date(System.currentTimeMillis() - subtract);\r
        }\r
 \r
+       private Date offset(long now, long subtract) {\r
+               return new Date(now - subtract);\r
+       }\r
+\r
        @Test\r
        public void testBasicTimeFunctions() throws Exception {\r
                assertEquals(2, TimeUtils.minutesAgo(offset(2 * TimeUtils.MIN), false));\r
@@ -89,8 +95,12 @@ public class TimeUtilsTest extends GitblitUnitTest {
                assertEquals("1 year ago", timeUtils.timeAgo(offset(365 * TimeUtils.ONEDAY)));\r
                assertEquals("13 months ago", timeUtils.timeAgo(offset(395 * TimeUtils.ONEDAY)));\r
                assertEquals("2 years ago", timeUtils.timeAgo(offset((2 * 365 + 30) * TimeUtils.ONEDAY)));\r
+       }\r
 \r
+       @Test\r
+       public void testTimeAgoCss() throws Exception {\r
                // css class tests\r
+               TimeUtils timeUtils = new TimeUtils();\r
                assertEquals("age0", timeUtils.timeAgoCss(offset(1 * TimeUtils.MIN)));\r
                assertEquals("age0", timeUtils.timeAgoCss(offset(60 * TimeUtils.MIN)));\r
                assertEquals("age1", timeUtils.timeAgoCss(offset(120 * TimeUtils.MIN)));\r
@@ -98,6 +108,74 @@ public class TimeUtilsTest extends GitblitUnitTest {
                assertEquals("age2", timeUtils.timeAgoCss(offset(2 * TimeUtils.ONEDAY)));\r
        }\r
 \r
+\r
+       @Test\r
+       public void testTimeAgoYesterday() throws Exception {\r
+               TimeZone myTimezone = TimeZone.getTimeZone("GMT");\r
+               TimeUtils timeUtils = new TimeUtils(null, myTimezone);\r
+\r
+               Calendar myCal = Calendar.getInstance(myTimezone);\r
+               myCal.set(Calendar.HOUR_OF_DAY, 12);\r
+               myCal.set(Calendar.MINUTE, 0);\r
+               long now = myCal.getTime().getTime();\r
+\r
+               assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,23 * TimeUtils.ONEHOUR), false, now));\r
+               assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,(23 * TimeUtils.ONEHOUR) + (29 * TimeUtils.MIN)), false, now));\r
+\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,(23 * TimeUtils.ONEHOUR) + (31 * TimeUtils.MIN)), false, now));\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,24 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,35 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,36 * TimeUtils.ONEHOUR), false, now));\r
+\r
+               assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,37 * TimeUtils.ONEHOUR), false, now));\r
+               assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,44 * TimeUtils.ONEHOUR), false, now));\r
+       }\r
+\r
+       @Test\r
+       public void testTimeAgoYesterdayCET() throws Exception {\r
+               TimeZone myTimezone = TimeZone.getTimeZone("CET");\r
+               TimeUtils timeUtils = new TimeUtils(null, myTimezone);\r
+\r
+               Calendar myCal = Calendar.getInstance(myTimezone);\r
+               myCal.set(Calendar.HOUR_OF_DAY, 22);\r
+               myCal.set(Calendar.MINUTE, 0);\r
+               long now = myCal.getTime().getTime();\r
+\r
+               assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,23 * TimeUtils.ONEHOUR), false, now));\r
+               assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,(23 * TimeUtils.ONEHOUR) + (29 * TimeUtils.MIN)), false, now));\r
+\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,(23 * TimeUtils.ONEHOUR) + (31 * TimeUtils.MIN)), false, now));\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,24 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,36 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,46 * TimeUtils.ONEHOUR), false, now));\r
+\r
+               assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,47 * TimeUtils.ONEHOUR), false, now));\r
+               assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,56 * TimeUtils.ONEHOUR), false, now));\r
+       }\r
+\r
+\r
+       @Test\r
+       public void testTimeAgoYesterdayPST() throws Exception {\r
+               TimeZone myTimezone = TimeZone.getTimeZone("PST");\r
+               TimeUtils timeUtils = new TimeUtils(null, myTimezone);\r
+\r
+               Calendar myCal = Calendar.getInstance(myTimezone);\r
+               myCal.set(Calendar.HOUR_OF_DAY, 8);\r
+               myCal.set(Calendar.MINUTE, 0);\r
+               long now = myCal.getTime().getTime();\r
+\r
+               assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,23 * TimeUtils.ONEHOUR), false, now));\r
+               assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,(23 * TimeUtils.ONEHOUR) + (29 * TimeUtils.MIN)), false, now));\r
+\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,(23 * TimeUtils.ONEHOUR) + (31 * TimeUtils.MIN)), false, now));\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,24 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,30 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,32 * TimeUtils.ONEHOUR), false, now));\r
+\r
+               assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,33 * TimeUtils.ONEHOUR), false, now));\r
+               assertNotEquals("yesterday", timeUtils.timeAgo(offset(now,48 * TimeUtils.ONEHOUR), false, now));\r
+       }\r
+\r
        @Test\r
        public void testFrequency() {\r
                assertEquals(5, TimeUtils.convertFrequencyToMinutes("2 mins", 5));\r
@@ -105,4 +183,83 @@ public class TimeUtilsTest extends GitblitUnitTest {
                assertEquals(600, TimeUtils.convertFrequencyToMinutes("10 hours", 5));\r
                assertEquals(14400, TimeUtils.convertFrequencyToMinutes(" 10 days ", 5));\r
        }\r
+\r
+\r
+       @Test\r
+       public void testTimeAgoDaysAgo() throws Exception {\r
+               TimeZone myTimezone = TimeZone.getTimeZone("GMT");\r
+               TimeUtils timeUtils = new TimeUtils(null, myTimezone);\r
+\r
+               Calendar myCal = Calendar.getInstance(myTimezone);\r
+               myCal.set(Calendar.HOUR_OF_DAY, 12);\r
+               myCal.set(Calendar.MINUTE, 0);\r
+               long now = myCal.getTime().getTime();\r
+\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,24 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,36 * TimeUtils.ONEHOUR), false, now));\r
+\r
+               assertEquals("2 days ago", timeUtils.timeAgo(offset(now,37 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("2 days ago", timeUtils.timeAgo(offset(now,48 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("2 days ago", timeUtils.timeAgo(offset(now,60 * TimeUtils.ONEHOUR), false, now));\r
+\r
+               assertEquals("3 days ago", timeUtils.timeAgo(offset(now,61 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("3 days ago", timeUtils.timeAgo(offset(now,72 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("3 days ago", timeUtils.timeAgo(offset(now,84 * TimeUtils.ONEHOUR), false, now));\r
+\r
+               assertEquals("4 days ago", timeUtils.timeAgo(offset(now,85 * TimeUtils.ONEHOUR), false, now));\r
+       }\r
+\r
+\r
+\r
+       @Test\r
+       public void testTimeAgoDaysAgoCET() throws Exception {\r
+               TimeZone myTimezone = TimeZone.getTimeZone("CET");\r
+               TimeUtils timeUtils = new TimeUtils(null, myTimezone);\r
+\r
+               Calendar myCal = Calendar.getInstance(myTimezone);\r
+               myCal.set(Calendar.HOUR_OF_DAY, 8);\r
+               myCal.set(Calendar.MINUTE, 0);\r
+               long now = myCal.getTime().getTime();\r
+\r
+\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,24 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,32 * TimeUtils.ONEHOUR), false, now));\r
+\r
+               assertEquals("2 days ago", timeUtils.timeAgo(offset(now,33 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("2 days ago", timeUtils.timeAgo(offset(now,48 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("2 days ago", timeUtils.timeAgo(offset(now,56 * TimeUtils.ONEHOUR), false, now));\r
+\r
+               assertEquals("3 days ago", timeUtils.timeAgo(offset(now,57 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("3 days ago", timeUtils.timeAgo(offset(now,72 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("3 days ago", timeUtils.timeAgo(offset(now,80 * TimeUtils.ONEHOUR), false, now));\r
+\r
+               assertEquals("4 days ago", timeUtils.timeAgo(offset(now,81 * TimeUtils.ONEHOUR), false, now));\r
+       }\r
+\r
+\r
+\r
+       @Test\r
+       public void testTimeAgoDaysAgoPST() throws Exception {\r
+               TimeZone myTimezone = TimeZone.getTimeZone("PST");\r
+               TimeUtils timeUtils = new TimeUtils(null, myTimezone);\r
+\r
+               Calendar myCal = Calendar.getInstance(myTimezone);\r
+               myCal.set(Calendar.HOUR_OF_DAY, 22);\r
+               myCal.set(Calendar.MINUTE, 0);\r
+               long now = myCal.getTime().getTime();\r
+\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,24 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("yesterday", timeUtils.timeAgo(offset(now,46 * TimeUtils.ONEHOUR), false, now));\r
+\r
+               assertEquals("2 days ago", timeUtils.timeAgo(offset(now,47 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("2 days ago", timeUtils.timeAgo(offset(now,48 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("2 days ago", timeUtils.timeAgo(offset(now,70 * TimeUtils.ONEHOUR), false, now));\r
+\r
+               assertEquals("3 days ago", timeUtils.timeAgo(offset(now,71 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("3 days ago", timeUtils.timeAgo(offset(now,72 * TimeUtils.ONEHOUR), false, now));\r
+               assertEquals("3 days ago", timeUtils.timeAgo(offset(now,94 * TimeUtils.ONEHOUR), false, now));\r
+\r
+               assertEquals("4 days ago", timeUtils.timeAgo(offset(now,95 * TimeUtils.ONEHOUR), false, now));\r
+       }\r
+\r
 }\r