1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.util;
11
12 import static org.eclipse.jgit.util.RelativeDateFormatter.DAY_IN_MILLIS;
13 import static org.eclipse.jgit.util.RelativeDateFormatter.HOUR_IN_MILLIS;
14 import static org.eclipse.jgit.util.RelativeDateFormatter.MINUTE_IN_MILLIS;
15 import static org.eclipse.jgit.util.RelativeDateFormatter.SECOND_IN_MILLIS;
16 import static org.eclipse.jgit.util.RelativeDateFormatter.YEAR_IN_MILLIS;
17 import static org.junit.Assert.assertEquals;
18
19 import java.util.Date;
20
21 import org.eclipse.jgit.junit.MockSystemReader;
22 import org.junit.After;
23 import org.junit.Before;
24 import org.junit.Test;
25
26 public class RelativeDateFormatterTest {
27
28 @Before
29 public void setUp() {
30 SystemReader.setInstance(new MockSystemReader());
31 }
32
33 @After
34 public void tearDown() {
35 SystemReader.setInstance(null);
36 }
37
38 private static void assertFormat(long ageFromNow, long timeUnit,
39 String expectedFormat) {
40 Date d = new Date(SystemReader.getInstance().getCurrentTime()
41 - ageFromNow * timeUnit);
42 String s = RelativeDateFormatter.format(d);
43 assertEquals(expectedFormat, s);
44 }
45
46 @Test
47 public void testFuture() {
48 assertFormat(-100, YEAR_IN_MILLIS, "in the future");
49 assertFormat(-1, SECOND_IN_MILLIS, "in the future");
50 }
51
52 @Test
53 public void testFormatSeconds() {
54 assertFormat(1, SECOND_IN_MILLIS, "1 seconds ago");
55 assertFormat(89, SECOND_IN_MILLIS, "89 seconds ago");
56 }
57
58 @Test
59 public void testFormatMinutes() {
60 assertFormat(90, SECOND_IN_MILLIS, "2 minutes ago");
61 assertFormat(3, MINUTE_IN_MILLIS, "3 minutes ago");
62 assertFormat(60, MINUTE_IN_MILLIS, "60 minutes ago");
63 assertFormat(89, MINUTE_IN_MILLIS, "89 minutes ago");
64 }
65
66 @Test
67 public void testFormatHours() {
68 assertFormat(90, MINUTE_IN_MILLIS, "2 hours ago");
69 assertFormat(149, MINUTE_IN_MILLIS, "2 hours ago");
70 assertFormat(35, HOUR_IN_MILLIS, "35 hours ago");
71 }
72
73 @Test
74 public void testFormatDays() {
75 assertFormat(36, HOUR_IN_MILLIS, "2 days ago");
76 assertFormat(13, DAY_IN_MILLIS, "13 days ago");
77 }
78
79 @Test
80 public void testFormatWeeks() {
81 assertFormat(14, DAY_IN_MILLIS, "2 weeks ago");
82 assertFormat(69, DAY_IN_MILLIS, "10 weeks ago");
83 }
84
85 @Test
86 public void testFormatMonths() {
87 assertFormat(70, DAY_IN_MILLIS, "2 months ago");
88 assertFormat(75, DAY_IN_MILLIS, "3 months ago");
89 assertFormat(364, DAY_IN_MILLIS, "12 months ago");
90 }
91
92 @Test
93 public void testFormatYearsMonths() {
94 assertFormat(366, DAY_IN_MILLIS, "1 year ago");
95 assertFormat(380, DAY_IN_MILLIS, "1 year, 1 month ago");
96 assertFormat(410, DAY_IN_MILLIS, "1 year, 2 months ago");
97 assertFormat(2, YEAR_IN_MILLIS, "2 years ago");
98 }
99
100 @Test
101 public void testFullYearMissingSomeDays() {
102
103
104 assertFormat(5 * 365 + 1, DAY_IN_MILLIS, "5 years ago");
105 assertFormat(2 * 365 - 10, DAY_IN_MILLIS, "2 years ago");
106 }
107
108 @Test
109 public void testFormatYears() {
110 assertFormat(5, YEAR_IN_MILLIS, "5 years ago");
111 assertFormat(60, YEAR_IN_MILLIS, "60 years ago");
112 }
113 }