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