You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

RelativeDateFormatterTest.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. /*
  2. * Copyright (C) 2011, Matthias Sohn <matthias.sohn@sap.com> and others
  3. *
  4. * This program and the accompanying materials are made available under the
  5. * terms of the Eclipse Distribution License v. 1.0 which is available at
  6. * https://www.eclipse.org/org/documents/edl-v10.php.
  7. *
  8. * SPDX-License-Identifier: BSD-3-Clause
  9. */
  10. package org.eclipse.jgit.util;
  11. import static org.eclipse.jgit.util.RelativeDateFormatter.DAY_IN_MILLIS;
  12. import static org.eclipse.jgit.util.RelativeDateFormatter.HOUR_IN_MILLIS;
  13. import static org.eclipse.jgit.util.RelativeDateFormatter.MINUTE_IN_MILLIS;
  14. import static org.eclipse.jgit.util.RelativeDateFormatter.SECOND_IN_MILLIS;
  15. import static org.eclipse.jgit.util.RelativeDateFormatter.YEAR_IN_MILLIS;
  16. import static org.junit.Assert.assertEquals;
  17. import java.util.Date;
  18. import org.eclipse.jgit.junit.MockSystemReader;
  19. import org.junit.After;
  20. import org.junit.Before;
  21. import org.junit.Test;
  22. public class RelativeDateFormatterTest {
  23. @Before
  24. public void setUp() {
  25. SystemReader.setInstance(new MockSystemReader());
  26. }
  27. @After
  28. public void tearDown() {
  29. SystemReader.setInstance(null);
  30. }
  31. private static void assertFormat(long ageFromNow, long timeUnit,
  32. String expectedFormat) {
  33. Date d = new Date(SystemReader.getInstance().getCurrentTime()
  34. - ageFromNow * timeUnit);
  35. String s = RelativeDateFormatter.format(d);
  36. assertEquals(expectedFormat, s);
  37. }
  38. @Test
  39. public void testFuture() {
  40. assertFormat(-100, YEAR_IN_MILLIS, "in the future");
  41. assertFormat(-1, SECOND_IN_MILLIS, "in the future");
  42. }
  43. @Test
  44. public void testFormatSeconds() {
  45. assertFormat(1, SECOND_IN_MILLIS, "1 seconds ago");
  46. assertFormat(89, SECOND_IN_MILLIS, "89 seconds ago");
  47. }
  48. @Test
  49. public void testFormatMinutes() {
  50. assertFormat(90, SECOND_IN_MILLIS, "2 minutes ago");
  51. assertFormat(3, MINUTE_IN_MILLIS, "3 minutes ago");
  52. assertFormat(60, MINUTE_IN_MILLIS, "60 minutes ago");
  53. assertFormat(89, MINUTE_IN_MILLIS, "89 minutes ago");
  54. }
  55. @Test
  56. public void testFormatHours() {
  57. assertFormat(90, MINUTE_IN_MILLIS, "2 hours ago");
  58. assertFormat(149, MINUTE_IN_MILLIS, "2 hours ago");
  59. assertFormat(35, HOUR_IN_MILLIS, "35 hours ago");
  60. }
  61. @Test
  62. public void testFormatDays() {
  63. assertFormat(36, HOUR_IN_MILLIS, "2 days ago");
  64. assertFormat(13, DAY_IN_MILLIS, "13 days ago");
  65. }
  66. @Test
  67. public void testFormatWeeks() {
  68. assertFormat(14, DAY_IN_MILLIS, "2 weeks ago");
  69. assertFormat(69, DAY_IN_MILLIS, "10 weeks ago");
  70. }
  71. @Test
  72. public void testFormatMonths() {
  73. assertFormat(70, DAY_IN_MILLIS, "2 months ago");
  74. assertFormat(75, DAY_IN_MILLIS, "3 months ago");
  75. assertFormat(364, DAY_IN_MILLIS, "12 months ago");
  76. }
  77. @Test
  78. public void testFormatYearsMonths() {
  79. assertFormat(366, DAY_IN_MILLIS, "1 year ago");
  80. assertFormat(380, DAY_IN_MILLIS, "1 year, 1 month ago");
  81. assertFormat(410, DAY_IN_MILLIS, "1 year, 2 months ago");
  82. assertFormat(2, YEAR_IN_MILLIS, "2 years ago");
  83. }
  84. @Test
  85. public void testFullYearMissingSomeDays() {
  86. // avoid "x year(s), 12 months", as humans would always round this up to
  87. // "x+1 years"
  88. assertFormat(5 * 365 + 1, DAY_IN_MILLIS, "5 years ago");
  89. assertFormat(2 * 365 - 10, DAY_IN_MILLIS, "2 years ago");
  90. }
  91. @Test
  92. public void testFormatYears() {
  93. assertFormat(5, YEAR_IN_MILLIS, "5 years ago");
  94. assertFormat(60, YEAR_IN_MILLIS, "60 years ago");
  95. }
  96. }