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