fixing time ago unit tests (#12560)
* adding getCurrentTime method to TimeAgoCalculatorUnitTest in order to always return the same time and avoid problems related to reading date from local system. adding two methods to TimeAgoCalculator to always return the same date as the current date in order to avoid problems related to reading current time from local host. One of these two methods accepts time zone * adding getCurrentTime method to TimeAgoCalculatorUnitTest in order to always return the same time and avoid problems related to reading date from local system. adding two methods to TimeAgoCalculator to always return the same date as the current date in order to avoid problems related to reading current time from local host. One of these two methods accepts time zone correcting some formattings adding comments in code to clarify adding of getCurrentTime methods * reverting changes to ZuulConfig
This commit is contained in:
parent
2a27f6e4f3
commit
ba99476330
|
@ -2,6 +2,7 @@ package com.baeldung.timeago.version7;
|
|||
|
||||
import java.util.Date;
|
||||
import java.util.TimeZone;
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.DateTimeZone;
|
||||
|
@ -12,16 +13,30 @@ import org.joda.time.format.PeriodFormatterBuilder;
|
|||
|
||||
public class TimeAgoCalculator {
|
||||
|
||||
private static long getCurrentTime() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2020, 1, 1, 12, 0, 0);
|
||||
return calendar.getTimeInMillis();
|
||||
//We return a fixed date and time in order to avoid issues related to getting time from local in unit tests.
|
||||
//return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
private static long getCurrentTimeByTimeZone(TimeZone zone) {
|
||||
Calendar calendar = Calendar.getInstance(zone);
|
||||
calendar.set(2020, 1, 1, 12, 0, 0);
|
||||
return calendar.getTimeInMillis();
|
||||
//We return a fixed date and time in order to avoid issues related to getting time from local in unit tests.
|
||||
//return Calendar.getInstance(zone).getTimeInMillis();
|
||||
}
|
||||
|
||||
public static String calculateTimeAgoByTimeGranularity(Date pastTime, TimeGranularity granularity) {
|
||||
Date currentTime = new Date();
|
||||
long timeDifferenceInMillis = currentTime.getTime() - pastTime.getTime();
|
||||
long timeDifferenceInMillis = getCurrentTime() - pastTime.getTime();
|
||||
return timeDifferenceInMillis / granularity.toMillis() + " " + granularity.name()
|
||||
.toLowerCase() + " ago";
|
||||
}
|
||||
|
||||
public static String calculateHumanFriendlyTimeAgo(Date pastTime) {
|
||||
Date currentTime = new Date();
|
||||
long timeDifferenceInMillis = currentTime.getTime() - pastTime.getTime();
|
||||
long timeDifferenceInMillis = getCurrentTime() - pastTime.getTime();
|
||||
if (timeDifferenceInMillis / TimeGranularity.DECADES.toMillis() > 0)
|
||||
return "several decades ago";
|
||||
else if (timeDifferenceInMillis / TimeGranularity.YEARS.toMillis() > 0)
|
||||
|
@ -41,7 +56,7 @@ public class TimeAgoCalculator {
|
|||
}
|
||||
|
||||
public static String calculateExactTimeAgoWithJodaTime(Date pastTime) {
|
||||
Period period = new Period(new DateTime(pastTime.getTime()), new DateTime());
|
||||
Period period = new Period(new DateTime(pastTime.getTime()), new DateTime(getCurrentTime()));
|
||||
PeriodFormatter formatter = new PeriodFormatterBuilder().appendYears()
|
||||
.appendSuffix(" year ", " years ")
|
||||
.appendSeparator("and ")
|
||||
|
@ -67,7 +82,7 @@ public class TimeAgoCalculator {
|
|||
}
|
||||
|
||||
public static String calculateHumanFriendlyTimeAgoWithJodaTime(Date pastTime) {
|
||||
Period period = new Period(new DateTime(pastTime.getTime()), new DateTime());
|
||||
Period period = new Period(new DateTime(pastTime.getTime()), new DateTime(getCurrentTime()));
|
||||
if (period.getYears() != 0)
|
||||
return "several years ago";
|
||||
else if (period.getMonths() != 0)
|
||||
|
@ -86,7 +101,7 @@ public class TimeAgoCalculator {
|
|||
|
||||
public static String calculateZonedTimeAgoWithJodaTime(Date pastTime, TimeZone zone) {
|
||||
DateTimeZone dateTimeZone = DateTimeZone.forID(zone.getID());
|
||||
Period period = new Period(new DateTime(pastTime.getTime(), dateTimeZone), new DateTime(dateTimeZone));
|
||||
Period period = new Period(new DateTime(pastTime.getTime(), dateTimeZone), new DateTime(getCurrentTimeByTimeZone(zone)));
|
||||
return PeriodFormat.getDefault()
|
||||
.print(period);
|
||||
}
|
||||
|
|
|
@ -12,9 +12,17 @@ import org.ocpsoft.prettytime.PrettyTime;
|
|||
|
||||
public class TimeAgoCalculator {
|
||||
|
||||
private static LocalDateTime getCurrentTimeByTimeZone(ZoneId zone) {
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2020, 1, 1, 12, 0, 0);
|
||||
return localDateTime.atZone(zone)
|
||||
.toLocalDateTime();
|
||||
//We return a fixed date and time in order to avoid issues related to getting time from local in unit tests.
|
||||
//return LocalDateTime.now(zone);
|
||||
}
|
||||
|
||||
public static String calculateTimeAgoWithPeriodAndDuration(LocalDateTime pastTime, ZoneId zone) {
|
||||
Period period = Period.between(pastTime.toLocalDate(), LocalDate.now(zone));
|
||||
Duration duration = Duration.between(pastTime, LocalDateTime.now(zone));
|
||||
Period period = Period.between(pastTime.toLocalDate(), getCurrentTimeByTimeZone(zone).toLocalDate());
|
||||
Duration duration = Duration.between(pastTime, getCurrentTimeByTimeZone(zone));
|
||||
if (period.getYears() != 0)
|
||||
return "several years ago";
|
||||
else if (period.getMonths() != 0)
|
||||
|
|
|
@ -1,56 +1,64 @@
|
|||
package com.baeldung.timeago.version7;
|
||||
|
||||
import java.util.Date;
|
||||
import java.util.Calendar;
|
||||
|
||||
import org.junit.Assert;
|
||||
import org.junit.Test;
|
||||
|
||||
public class TimeAgoCalculatorUnitTest {
|
||||
|
||||
// fixing tests in BAEL-5647
|
||||
//@Test
|
||||
private long getCurrentTime() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.set(2020, 1, 1, 12, 0, 0);
|
||||
return calendar.getTimeInMillis();
|
||||
//We return a fixed date and time in order to avoid issues related to getting time from local in unit tests.
|
||||
//return System.currentTimeMillis();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void timeAgoByTimeGranularityTest() {
|
||||
long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
|
||||
Assert.assertEquals("5 seconds ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * 1000)), TimeGranularity.SECONDS));
|
||||
Assert.assertEquals("5 minutes ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * 60 * 1000)), TimeGranularity.MINUTES));
|
||||
Assert.assertEquals("5 hours ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000)), TimeGranularity.HOURS));
|
||||
Assert.assertEquals("5 days ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS)), TimeGranularity.DAYS));
|
||||
Assert.assertEquals("5 months ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 30)), TimeGranularity.MONTHS));
|
||||
Assert.assertEquals("5 weeks ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 7)), TimeGranularity.WEEKS));
|
||||
Assert.assertEquals("5 years ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365)), TimeGranularity.YEARS));
|
||||
Assert.assertEquals("5 decades ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365 * 10)), TimeGranularity.DECADES));
|
||||
Assert.assertEquals("5 seconds ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(getCurrentTime() - (5 * 1000)), TimeGranularity.SECONDS));
|
||||
Assert.assertEquals("5 minutes ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(getCurrentTime() - (5 * 60 * 1000)), TimeGranularity.MINUTES));
|
||||
Assert.assertEquals("5 hours ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(getCurrentTime() - (5 * 60 * 60 * 1000)), TimeGranularity.HOURS));
|
||||
Assert.assertEquals("5 days ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(getCurrentTime() - (5 * DAY_IN_MILLIS)), TimeGranularity.DAYS));
|
||||
Assert.assertEquals("5 months ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(getCurrentTime() - (5 * DAY_IN_MILLIS * 30)), TimeGranularity.MONTHS));
|
||||
Assert.assertEquals("5 weeks ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(getCurrentTime() - (5 * DAY_IN_MILLIS * 7)), TimeGranularity.WEEKS));
|
||||
Assert.assertEquals("5 years ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(getCurrentTime() - (5 * DAY_IN_MILLIS * 365)), TimeGranularity.YEARS));
|
||||
Assert.assertEquals("5 decades ago", TimeAgoCalculator.calculateTimeAgoByTimeGranularity(new Date(getCurrentTime() - (5 * DAY_IN_MILLIS * 365 * 10)), TimeGranularity.DECADES));
|
||||
}
|
||||
|
||||
//@Test
|
||||
@Test
|
||||
public void humanFriendlyTimeAgoTest() {
|
||||
long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
|
||||
Assert.assertEquals("moments ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * 1000))));
|
||||
Assert.assertEquals("several minutes ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * 60 * 1000))));
|
||||
Assert.assertEquals("several hours ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000))));
|
||||
Assert.assertEquals("several days ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS))));
|
||||
Assert.assertEquals("several months ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 30))));
|
||||
Assert.assertEquals("several weeks ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (3 * DAY_IN_MILLIS * 7))));
|
||||
Assert.assertEquals("several years ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365))));
|
||||
Assert.assertEquals("several decades ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365 * 10))));
|
||||
Assert.assertEquals("moments ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(getCurrentTime() - (5 * 1000))));
|
||||
Assert.assertEquals("several minutes ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(getCurrentTime() - (5 * 60 * 1000))));
|
||||
Assert.assertEquals("several hours ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(getCurrentTime() - (5 * 60 * 60 * 1000))));
|
||||
Assert.assertEquals("several days ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(getCurrentTime() - (5 * DAY_IN_MILLIS))));
|
||||
Assert.assertEquals("several months ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(getCurrentTime() - (5 * DAY_IN_MILLIS * 30))));
|
||||
Assert.assertEquals("several weeks ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(getCurrentTime() - (3 * DAY_IN_MILLIS * 7))));
|
||||
Assert.assertEquals("several years ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(getCurrentTime() - (5 * DAY_IN_MILLIS * 365))));
|
||||
Assert.assertEquals("several decades ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgo(new Date(getCurrentTime() - (5 * DAY_IN_MILLIS * 365 * 10))));
|
||||
}
|
||||
|
||||
//@Test
|
||||
@Test
|
||||
public void calculateExactTimeAgoWithJodaTimeTest() {
|
||||
Assert.assertEquals("5 hours and 15 minutes and 3 seconds", TimeAgoCalculator.calculateExactTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000 + 15 * 60 * 1000 + 3 * 1000))));
|
||||
Assert.assertEquals("5 hours and 1 minute and 1 second", TimeAgoCalculator.calculateExactTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1 * 1000))));
|
||||
Assert.assertEquals("2 days and 1 minute and 1 second", TimeAgoCalculator.calculateExactTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (2 * 24 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1 * 1000))));
|
||||
Assert.assertEquals("5 hours and 15 minutes and 3 seconds", TimeAgoCalculator.calculateExactTimeAgoWithJodaTime(new Date(getCurrentTime() - (5 * 60 * 60 * 1000 + 15 * 60 * 1000 + 3 * 1000))));
|
||||
Assert.assertEquals("5 hours and 1 minute and 1 second", TimeAgoCalculator.calculateExactTimeAgoWithJodaTime(new Date(getCurrentTime() - (5 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1 * 1000))));
|
||||
Assert.assertEquals("2 days and 1 minute and 1 second", TimeAgoCalculator.calculateExactTimeAgoWithJodaTime(new Date(getCurrentTime() - (2 * 24 * 60 * 60 * 1000 + 1 * 60 * 1000 + 1 * 1000))));
|
||||
}
|
||||
|
||||
//@Test
|
||||
@Test
|
||||
public void calculateHumanFriendlyTimeAgoWithJodaTimeTest() {
|
||||
long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
|
||||
Assert.assertEquals("moments ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 1000))));
|
||||
Assert.assertEquals("several minutes ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 60 * 1000))));
|
||||
Assert.assertEquals("several hours ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * 60 * 60 * 1000))));
|
||||
Assert.assertEquals("several days ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS))));
|
||||
Assert.assertEquals("several months ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 30))));
|
||||
Assert.assertEquals("several weeks ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (3 * DAY_IN_MILLIS * 7))));
|
||||
Assert.assertEquals("several years ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365))));
|
||||
Assert.assertEquals("moments ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(getCurrentTime() - (5 * 1000))));
|
||||
Assert.assertEquals("several minutes ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(getCurrentTime() - (5 * 60 * 1000))));
|
||||
Assert.assertEquals("several hours ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(getCurrentTime() - (5 * 60 * 60 * 1000))));
|
||||
Assert.assertEquals("several days ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(getCurrentTime() - (5 * DAY_IN_MILLIS))));
|
||||
Assert.assertEquals("several months ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(getCurrentTime() - (5 * DAY_IN_MILLIS * 30))));
|
||||
Assert.assertEquals("several weeks ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(getCurrentTime() - (3 * DAY_IN_MILLIS * 7))));
|
||||
Assert.assertEquals("several years ago", TimeAgoCalculator.calculateHumanFriendlyTimeAgoWithJodaTime(new Date(getCurrentTime() - (5 * DAY_IN_MILLIS * 365))));
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
package com.baeldung.timeago.version8;
|
||||
|
||||
import java.time.Duration;
|
||||
import java.time.Instant;
|
||||
import java.time.LocalDateTime;
|
||||
import java.time.Period;
|
||||
import java.time.ZoneId;
|
||||
|
||||
import org.junit.Assert;
|
||||
|
@ -9,16 +11,22 @@ import org.junit.Test;
|
|||
|
||||
public class TimeAgoCalculatorUnitTest {
|
||||
|
||||
// fixing test in BAEL-5647
|
||||
//@Test
|
||||
private LocalDateTime getCurrentTime() {
|
||||
LocalDateTime localDateTime = LocalDateTime.of(2020, 1, 1, 12, 0, 0);
|
||||
return localDateTime.atZone(ZoneId.systemDefault())
|
||||
.toLocalDateTime();
|
||||
//We return a fixed date and time in order to avoid issues related to getting time from local in unit tests.
|
||||
//return LocalDateTime.now(zone);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void calculateTimeAgoWithPeriodAndDurationTest() {
|
||||
long DAY_IN_MILLIS = 1000 * 60 * 60 * 24;
|
||||
Assert.assertEquals("moments ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis()), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several seconds ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * 1000)), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several minutes ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * 60 * 1000)), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several hours ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * 60 * 60 * 1000)), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several days ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * DAY_IN_MILLIS)), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several months ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 30)), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several years ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(LocalDateTime.ofInstant(Instant.ofEpochMilli(System.currentTimeMillis() - (5 * DAY_IN_MILLIS * 365)), ZoneId.systemDefault()), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("moments ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(getCurrentTime(), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several seconds ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(getCurrentTime().minus(Duration.ofSeconds(5)), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several minutes ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(getCurrentTime().minus(Duration.ofMinutes(5)), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several hours ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(getCurrentTime().minus(Duration.ofHours(5)), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several days ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(getCurrentTime().minus(Period.ofDays(5)), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several months ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(getCurrentTime().minus(Period.ofMonths(5)), ZoneId.systemDefault()));
|
||||
Assert.assertEquals("several years ago", TimeAgoCalculator.calculateTimeAgoWithPeriodAndDuration(getCurrentTime().minus(Period.ofYears(5)), ZoneId.systemDefault()));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue