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.junit.Assert.assertEquals;
46 import static org.eclipse.jgit.util.RelativeDateFormatter.YEAR_IN_MILLIS;
47 import static org.eclipse.jgit.util.RelativeDateFormatter.SECOND_IN_MILLIS;
48 import static org.eclipse.jgit.util.RelativeDateFormatter.MINUTE_IN_MILLIS;
49 import static org.eclipse.jgit.util.RelativeDateFormatter.HOUR_IN_MILLIS;
50 import static org.eclipse.jgit.util.RelativeDateFormatter.DAY_IN_MILLIS;
51
52 import java.util.Date;
53
54 import org.eclipse.jgit.junit.MockSystemReader;
55 import org.eclipse.jgit.util.RelativeDateFormatter;
56 import org.junit.After;
57 import org.junit.Before;
58 import org.junit.Test;
59
60 public class RelativeDateFormatterTest {
61
62 @Before
63 public void setUp() {
64 SystemReader.setInstance(new MockSystemReader());
65 }
66
67 @After
68 public void tearDown() {
69 SystemReader.setInstance(null);
70 }
71
72 private static void assertFormat(long ageFromNow, long timeUnit,
73 String expectedFormat) {
74 Date d = new Date(SystemReader.getInstance().getCurrentTime()
75 - ageFromNow * timeUnit);
76 String s = RelativeDateFormatter.format(d);
77 assertEquals(expectedFormat, s);
78 }
79
80 @Test
81 public void testFuture() {
82 assertFormat(-100, YEAR_IN_MILLIS, "in the future");
83 assertFormat(-1, SECOND_IN_MILLIS, "in the future");
84 }
85
86 @Test
87 public void testFormatSeconds() {
88 assertFormat(1, SECOND_IN_MILLIS, "1 seconds ago");
89 assertFormat(89, SECOND_IN_MILLIS, "89 seconds ago");
90 }
91
92 @Test
93 public void testFormatMinutes() {
94 assertFormat(90, SECOND_IN_MILLIS, "2 minutes ago");
95 assertFormat(3, MINUTE_IN_MILLIS, "3 minutes ago");
96 assertFormat(60, MINUTE_IN_MILLIS, "60 minutes ago");
97 assertFormat(89, MINUTE_IN_MILLIS, "89 minutes ago");
98 }
99
100 @Test
101 public void testFormatHours() {
102 assertFormat(90, MINUTE_IN_MILLIS, "2 hours ago");
103 assertFormat(149, MINUTE_IN_MILLIS, "2 hours ago");
104 assertFormat(35, HOUR_IN_MILLIS, "35 hours ago");
105 }
106
107 @Test
108 public void testFormatDays() {
109 assertFormat(36, HOUR_IN_MILLIS, "2 days ago");
110 assertFormat(13, DAY_IN_MILLIS, "13 days ago");
111 }
112
113 @Test
114 public void testFormatWeeks() {
115 assertFormat(14, DAY_IN_MILLIS, "2 weeks ago");
116 assertFormat(69, DAY_IN_MILLIS, "10 weeks ago");
117 }
118
119 @Test
120 public void testFormatMonths() {
121 assertFormat(70, DAY_IN_MILLIS, "2 months ago");
122 assertFormat(75, DAY_IN_MILLIS, "3 months ago");
123 assertFormat(364, DAY_IN_MILLIS, "12 months ago");
124 }
125
126 @Test
127 public void testFormatYearsMonths() {
128 assertFormat(366, DAY_IN_MILLIS, "1 year ago");
129 assertFormat(380, DAY_IN_MILLIS, "1 year, 1 month ago");
130 assertFormat(410, DAY_IN_MILLIS, "1 year, 2 months ago");
131 assertFormat(2, YEAR_IN_MILLIS, "2 years ago");
132 assertFormat(1824, DAY_IN_MILLIS, "4 years, 12 months ago");
133 }
134
135 @Test
136 public void testFormatYears() {
137 assertFormat(5, YEAR_IN_MILLIS, "5 years ago");
138 assertFormat(60, YEAR_IN_MILLIS, "60 years ago");
139 }
140 }