View Javadoc
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  
45  import static org.eclipse.jgit.util.RelativeDateFormatter.DAY_IN_MILLIS;
46  import static org.eclipse.jgit.util.RelativeDateFormatter.HOUR_IN_MILLIS;
47  import static org.eclipse.jgit.util.RelativeDateFormatter.MINUTE_IN_MILLIS;
48  import static org.eclipse.jgit.util.RelativeDateFormatter.SECOND_IN_MILLIS;
49  import static org.eclipse.jgit.util.RelativeDateFormatter.YEAR_IN_MILLIS;
50  import static org.junit.Assert.assertEquals;
51  
52  import java.util.Date;
53  
54  import org.eclipse.jgit.junit.MockSystemReader;
55  import org.junit.After;
56  import org.junit.Before;
57  import org.junit.Test;
58  
59  public class RelativeDateFormatterTest {
60  
61  	@Before
62  	public void setUp() {
63  		SystemReader.setInstance(new MockSystemReader());
64  	}
65  
66  	@After
67  	public void tearDown() {
68  		SystemReader.setInstance(null);
69  	}
70  
71  	private static void assertFormat(long ageFromNow, long timeUnit,
72  			String expectedFormat) {
73  		Date d = new Date(SystemReader.getInstance().getCurrentTime()
74  				- ageFromNow * timeUnit);
75  		String s = RelativeDateFormatter.format(d);
76  		assertEquals(expectedFormat, s);
77  	}
78  
79  	@Test
80  	public void testFuture() {
81  		assertFormat(-100, YEAR_IN_MILLIS, "in the future");
82  		assertFormat(-1, SECOND_IN_MILLIS, "in the future");
83  	}
84  
85  	@Test
86  	public void testFormatSeconds() {
87  		assertFormat(1, SECOND_IN_MILLIS, "1 seconds ago");
88  		assertFormat(89, SECOND_IN_MILLIS, "89 seconds ago");
89  	}
90  
91  	@Test
92  	public void testFormatMinutes() {
93  		assertFormat(90, SECOND_IN_MILLIS, "2 minutes ago");
94  		assertFormat(3, MINUTE_IN_MILLIS, "3 minutes ago");
95  		assertFormat(60, MINUTE_IN_MILLIS, "60 minutes ago");
96  		assertFormat(89, MINUTE_IN_MILLIS, "89 minutes ago");
97  	}
98  
99  	@Test
100 	public void testFormatHours() {
101 		assertFormat(90, MINUTE_IN_MILLIS, "2 hours ago");
102 		assertFormat(149, MINUTE_IN_MILLIS, "2 hours ago");
103 		assertFormat(35, HOUR_IN_MILLIS, "35 hours ago");
104 	}
105 
106 	@Test
107 	public void testFormatDays() {
108 		assertFormat(36, HOUR_IN_MILLIS, "2 days ago");
109 		assertFormat(13, DAY_IN_MILLIS, "13 days ago");
110 	}
111 
112 	@Test
113 	public void testFormatWeeks() {
114 		assertFormat(14, DAY_IN_MILLIS, "2 weeks ago");
115 		assertFormat(69, DAY_IN_MILLIS, "10 weeks ago");
116 	}
117 
118 	@Test
119 	public void testFormatMonths() {
120 		assertFormat(70, DAY_IN_MILLIS, "2 months ago");
121 		assertFormat(75, DAY_IN_MILLIS, "3 months ago");
122 		assertFormat(364, DAY_IN_MILLIS, "12 months ago");
123 	}
124 
125 	@Test
126 	public void testFormatYearsMonths() {
127 		assertFormat(366, DAY_IN_MILLIS, "1 year ago");
128 		assertFormat(380, DAY_IN_MILLIS, "1 year, 1 month ago");
129 		assertFormat(410, DAY_IN_MILLIS, "1 year, 2 months ago");
130 		assertFormat(2, YEAR_IN_MILLIS, "2 years ago");
131 	}
132 
133 	@Test
134 	public void testFullYearMissingSomeDays() {
135 		// avoid "x year(s), 12 months", as humans would always round this up to
136 		// "x+1 years"
137 		assertFormat(5 * 365 + 1, DAY_IN_MILLIS, "5 years ago");
138 		assertFormat(2 * 365 - 10, DAY_IN_MILLIS, "2 years ago");
139 	}
140 
141 	@Test
142 	public void testFormatYears() {
143 		assertFormat(5, YEAR_IN_MILLIS, "5 years ago");
144 		assertFormat(60, YEAR_IN_MILLIS, "60 years ago");
145 	}
146 }