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 7.5KB

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