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.

RelativeDateFormatterTest.java 4.7KB

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