You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

GitDateParserTest.java 8.2KB

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