View Javadoc
1   /*
2    * Copyright (C) 2012, Christian Halstrick
3    * and other copyright owners as documented in the project's IP log.
4    *
5    * This program and the accompanying materials are made available
6    * under the terms of the Eclipse Distribution License v1.0 which
7    * accompanies this distribution, is reproduced below, and is
8    * available at http://www.eclipse.org/org/documents/edl-v10.php
9    *
10   * All rights reserved.
11   *
12   * Redistribution and use in source and binary forms, with or
13   * without modification, are permitted provided that the following
14   * conditions are met:
15   *
16   * - Redistributions of source code must retain the above copyright
17   *   notice, this list of conditions and the following disclaimer.
18   *
19   * - Redistributions in binary form must reproduce the above
20   *   copyright notice, this list of conditions and the following
21   *   disclaimer in the documentation and/or other materials provided
22   *   with the distribution.
23   *
24   * - Neither the name of the Eclipse Foundation, Inc. nor the
25   *   names of its contributors may be used to endorse or promote
26   *   products derived from this software without specific prior
27   *   written permission.
28   *
29   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
30   * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
31   * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
32   * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
33   * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
34   * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
35   * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
36   * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37   * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
38   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
39   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
40   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
41   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
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 }