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
44 package org.eclipse.jgit.util;
45
46 import java.text.ParseException;
47 import java.text.SimpleDateFormat;
48 import java.util.Calendar;
49 import java.util.Date;
50 import java.util.GregorianCalendar;
51
52 import org.eclipse.jgit.junit.MockSystemReader;
53 import org.junit.After;
54 import org.junit.Assert;
55 import org.junit.Before;
56 import org.junit.Test;
57
58 public class GitDateParserTest {
59 @Before
60 public void setUp() {
61 MockSystemReader mockSystemReader = new MockSystemReader();
62 SystemReader.setInstance(mockSystemReader);
63 }
64
65 @After
66 public void tearDown() {
67 SystemReader.setInstance(null);
68 }
69
70 @Test
71 public void yesterday() throws ParseException {
72 GregorianCalendar cal = new GregorianCalendar(SystemReader
73 .getInstance().getTimeZone(), SystemReader.getInstance()
74 .getLocale());
75 Date parse = GitDateParser.parse("yesterday", cal, SystemReader
76 .getInstance().getLocale());
77 cal.add(Calendar.DATE, -1);
78 cal.set(Calendar.HOUR_OF_DAY, 0);
79 cal.set(Calendar.MINUTE, 0);
80 cal.set(Calendar.SECOND, 0);
81 cal.set(Calendar.MILLISECOND, 0);
82 cal.set(Calendar.MILLISECOND, 0);
83 Assert.assertEquals(cal.getTime(), parse);
84 }
85
86 @Test
87 public void never() throws ParseException {
88 GregorianCalendar cal = new GregorianCalendar(SystemReader
89 .getInstance().getTimeZone(), SystemReader.getInstance()
90 .getLocale());
91 Date parse = GitDateParser.parse("never", cal, SystemReader
92 .getInstance().getLocale());
93 Assert.assertEquals(GitDateParser.NEVER, parse);
94 parse = GitDateParser.parse("never", null);
95 Assert.assertEquals(GitDateParser.NEVER, parse);
96 }
97
98 @Test
99 public void now() throws ParseException {
100 String dateStr = "2007-02-21 15:35:00 +0100";
101 Date refDate = SystemReader.getInstance()
102 .getSimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(dateStr);
103
104 GregorianCalendar cal = new GregorianCalendar(SystemReader
105 .getInstance().getTimeZone(), SystemReader.getInstance()
106 .getLocale());
107 cal.setTime(refDate);
108
109 Date parse = GitDateParser.parse("now", cal, SystemReader.getInstance()
110 .getLocale());
111 Assert.assertEquals(refDate, parse);
112 long t1 = SystemReader.getInstance().getCurrentTime();
113 parse = GitDateParser.parse("now", null);
114 long t2 = SystemReader.getInstance().getCurrentTime();
115 Assert.assertTrue(t2 >= parse.getTime() && parse.getTime() >= t1);
116 }
117
118 @Test
119 public void weeksAgo() throws ParseException {
120 String dateStr = "2007-02-21 15:35:00 +0100";
121 SimpleDateFormat df = SystemReader.getInstance()
122 .getSimpleDateFormat("yyyy-MM-dd HH:mm:ss Z");
123 Date refDate = df.parse(dateStr);
124 GregorianCalendar cal = new GregorianCalendar(SystemReader
125 .getInstance().getTimeZone(), SystemReader.getInstance()
126 .getLocale());
127 cal.setTime(refDate);
128
129 Date parse = GitDateParser.parse("2 weeks ago", cal, SystemReader
130 .getInstance().getLocale());
131 Assert.assertEquals(df.parse("2007-02-07 15:35:00 +0100"), parse);
132 }
133
134 @Test
135 public void daysAndWeeksAgo() throws ParseException {
136 String dateStr = "2007-02-21 15:35:00 +0100";
137 SimpleDateFormat df = SystemReader.getInstance().getSimpleDateFormat(
138 "yyyy-MM-dd HH:mm:ss Z");
139 Date refDate = df.parse(dateStr);
140 GregorianCalendar cal = new GregorianCalendar(SystemReader
141 .getInstance().getTimeZone(), SystemReader.getInstance()
142 .getLocale());
143 cal.setTime(refDate);
144
145 Date parse = GitDateParser.parse("2 weeks ago", cal, SystemReader.getInstance()
146 .getLocale());
147 Assert.assertEquals(df.parse("2007-02-07 15:35:00 +0100"), parse);
148 parse = GitDateParser.parse("3 days 2 weeks ago", cal);
149 Assert.assertEquals(df.parse("2007-02-04 15:35:00 +0100"), parse);
150 parse = GitDateParser.parse("3.day.2.week.ago", cal);
151 Assert.assertEquals(df.parse("2007-02-04 15:35:00 +0100"), parse);
152 }
153
154 @Test
155 public void iso() throws ParseException {
156 String dateStr = "2007-02-21 15:35:00 +0100";
157 Date exp = SystemReader.getInstance()
158 .getSimpleDateFormat("yyyy-MM-dd HH:mm:ss Z").parse(dateStr);
159 Date parse = GitDateParser.parse(dateStr, null, SystemReader
160 .getInstance().getLocale());
161 Assert.assertEquals(exp, parse);
162 }
163
164 @Test
165 public void rfc() throws ParseException {
166 String dateStr = "Wed, 21 Feb 2007 15:35:00 +0100";
167 Date exp = SystemReader.getInstance()
168 .getSimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss Z")
169 .parse(dateStr);
170 Date parse = GitDateParser.parse(dateStr, null, SystemReader
171 .getInstance().getLocale());
172 Assert.assertEquals(exp, parse);
173 }
174
175 @Test
176 public void shortFmt() throws ParseException {
177 String dateStr = "2007-02-21";
178 Date exp = SystemReader.getInstance().getSimpleDateFormat("yyyy-MM-dd")
179 .parse(dateStr);
180 Date parse = GitDateParser.parse(dateStr, null, SystemReader
181 .getInstance().getLocale());
182 Assert.assertEquals(exp, parse);
183 }
184
185 @Test
186 public void shortWithDots() throws ParseException {
187 String dateStr = "2007.02.21";
188 Date exp = SystemReader.getInstance().getSimpleDateFormat("yyyy.MM.dd")
189 .parse(dateStr);
190 Date parse = GitDateParser.parse(dateStr, null, SystemReader
191 .getInstance().getLocale());
192 Assert.assertEquals(exp, parse);
193 }
194
195 @Test
196 public void shortWithSlash() throws ParseException {
197 String dateStr = "02/21/2007";
198 Date exp = SystemReader.getInstance().getSimpleDateFormat("MM/dd/yyyy")
199 .parse(dateStr);
200 Date parse = GitDateParser.parse(dateStr, null, SystemReader
201 .getInstance().getLocale());
202 Assert.assertEquals(exp, parse);
203 }
204
205 @Test
206 public void shortWithDotsReverse() throws ParseException {
207 String dateStr = "21.02.2007";
208 Date exp = SystemReader.getInstance().getSimpleDateFormat("dd.MM.yyyy")
209 .parse(dateStr);
210 Date parse = GitDateParser.parse(dateStr, null, SystemReader
211 .getInstance().getLocale());
212 Assert.assertEquals(exp, parse);
213 }
214
215 @Test
216 public void defaultFmt() throws ParseException {
217 String dateStr = "Wed Feb 21 15:35:00 2007 +0100";
218 Date exp = SystemReader.getInstance()
219 .getSimpleDateFormat("EEE MMM dd HH:mm:ss yyyy Z")
220 .parse(dateStr);
221 Date parse = GitDateParser.parse(dateStr, null, SystemReader
222 .getInstance().getLocale());
223 Assert.assertEquals(exp, parse);
224 }
225
226 @Test
227 public void local() throws ParseException {
228 String dateStr = "Wed Feb 21 15:35:00 2007";
229 Date exp = SystemReader.getInstance()
230 .getSimpleDateFormat("EEE MMM dd HH:mm:ss yyyy").parse(dateStr);
231 Date parse = GitDateParser.parse(dateStr, null, SystemReader
232 .getInstance().getLocale());
233 Assert.assertEquals(exp, parse);
234 }
235 }