commit | author | age
|
ec97f7
|
1 |
/*
|
JM |
2 |
* Copyright 2011 gitblit.com.
|
|
3 |
*
|
|
4 |
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
5 |
* you may not use this file except in compliance with the License.
|
|
6 |
* You may obtain a copy of the License at
|
|
7 |
*
|
|
8 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
9 |
*
|
|
10 |
* Unless required by applicable law or agreed to in writing, software
|
|
11 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
12 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
13 |
* See the License for the specific language governing permissions and
|
|
14 |
* limitations under the License.
|
|
15 |
*/
|
28d6b2
|
16 |
package com.gitblit.tests;
|
JM |
17 |
|
|
18 |
import java.util.Date;
|
|
19 |
|
|
20 |
import junit.framework.TestCase;
|
|
21 |
|
|
22 |
import com.gitblit.utils.TimeUtils;
|
|
23 |
|
|
24 |
public class TimeUtilsTest extends TestCase {
|
|
25 |
|
ec97f7
|
26 |
private Date offset(long subtract) {
|
JM |
27 |
return new Date(System.currentTimeMillis() - subtract);
|
|
28 |
}
|
|
29 |
|
|
30 |
public void testBasicTimeFunctions() throws Exception {
|
|
31 |
assertTrue(TimeUtils.minutesAgo(offset(2 * TimeUtils.MIN), false) == 2);
|
|
32 |
assertTrue(TimeUtils.minutesAgo(offset((2 * TimeUtils.MIN) + (35 * 1000L)), true) == 3);
|
|
33 |
|
|
34 |
assertTrue(TimeUtils.hoursAgo(offset(2 * TimeUtils.ONEHOUR), false) == 2);
|
|
35 |
assertTrue(TimeUtils.hoursAgo(offset(5 * TimeUtils.HALFHOUR), true) == 3);
|
|
36 |
|
|
37 |
assertTrue(TimeUtils.daysAgo(offset(4 * TimeUtils.ONEDAY), false) == 4);
|
|
38 |
assertTrue(TimeUtils.daysAgo(offset(4 * TimeUtils.ONEDAY + 12 * TimeUtils.ONEHOUR), true) == 5);
|
|
39 |
}
|
|
40 |
|
28d6b2
|
41 |
public void testToday() throws Exception {
|
ec97f7
|
42 |
assertTrue(TimeUtils.isToday(new Date()));
|
28d6b2
|
43 |
}
|
JM |
44 |
|
|
45 |
public void testYesterday() throws Exception {
|
ec97f7
|
46 |
assertTrue(TimeUtils.isYesterday(offset(TimeUtils.ONEDAY)));
|
28d6b2
|
47 |
}
|
JM |
48 |
|
|
49 |
public void testDurations() throws Exception {
|
ec97f7
|
50 |
assertTrue(TimeUtils.duration(1).equals("1 day"));
|
28d6b2
|
51 |
assertTrue(TimeUtils.duration(5).equals("5 days"));
|
ec97f7
|
52 |
assertTrue(TimeUtils.duration(75).equals("3 months"));
|
28d6b2
|
53 |
assertTrue(TimeUtils.duration(364).equals("12 months"));
|
JM |
54 |
assertTrue(TimeUtils.duration(365 + 0).equals("1 year"));
|
|
55 |
assertTrue(TimeUtils.duration(365 + 10).equals("1 year"));
|
|
56 |
assertTrue(TimeUtils.duration(365 + 15).equals("1 year, 1 month"));
|
|
57 |
assertTrue(TimeUtils.duration(365 + 30).equals("1 year, 1 month"));
|
|
58 |
assertTrue(TimeUtils.duration(365 + 44).equals("1 year, 1 month"));
|
|
59 |
assertTrue(TimeUtils.duration(365 + 45).equals("1 year, 2 months"));
|
|
60 |
assertTrue(TimeUtils.duration(365 + 60).equals("1 year, 2 months"));
|
ec97f7
|
61 |
|
JM |
62 |
assertTrue(TimeUtils.duration(2 * 365 + 0).equals("2 years"));
|
|
63 |
assertTrue(TimeUtils.duration(2 * 365 + 10).equals("2 years"));
|
|
64 |
assertTrue(TimeUtils.duration(2 * 365 + 15).equals("2 years, 1 month"));
|
|
65 |
assertTrue(TimeUtils.duration(2 * 365 + 30).equals("2 years, 1 month"));
|
|
66 |
assertTrue(TimeUtils.duration(2 * 365 + 44).equals("2 years, 1 month"));
|
|
67 |
assertTrue(TimeUtils.duration(2 * 365 + 45).equals("2 years, 2 months"));
|
|
68 |
assertTrue(TimeUtils.duration(2 * 365 + 60).equals("2 years, 2 months"));
|
28d6b2
|
69 |
}
|
ec97f7
|
70 |
|
28d6b2
|
71 |
public void testTimeAgo() throws Exception {
|
ec97f7
|
72 |
// standard time ago tests
|
JM |
73 |
assertTrue(TimeUtils.timeAgo(offset(1 * TimeUtils.MIN)).equals("1 min ago"));
|
|
74 |
assertTrue(TimeUtils.timeAgo(offset(60 * TimeUtils.MIN)).equals("60 mins ago"));
|
|
75 |
assertTrue(TimeUtils.timeAgo(offset(120 * TimeUtils.MIN)).equals("2 hours ago"));
|
|
76 |
assertTrue(TimeUtils.timeAgo(offset(15 * TimeUtils.ONEHOUR)).equals("15 hours ago"));
|
|
77 |
assertTrue(TimeUtils.timeAgo(offset(24 * TimeUtils.ONEHOUR)).equals("yesterday"));
|
|
78 |
assertTrue(TimeUtils.timeAgo(offset(2 * TimeUtils.ONEDAY)).equals("2 days ago"));
|
|
79 |
assertTrue(TimeUtils.timeAgo(offset(35 * TimeUtils.ONEDAY)).equals("5 weeks ago"));
|
|
80 |
assertTrue(TimeUtils.timeAgo(offset(84 * TimeUtils.ONEDAY)).equals("3 months ago"));
|
|
81 |
assertTrue(TimeUtils.timeAgo(offset(95 * TimeUtils.ONEDAY)).equals("3 months ago"));
|
|
82 |
assertTrue(TimeUtils.timeAgo(offset(104 * TimeUtils.ONEDAY)).equals("4 months ago"));
|
|
83 |
assertTrue(TimeUtils.timeAgo(offset(365 * TimeUtils.ONEDAY)).equals("1 year ago"));
|
|
84 |
assertTrue(TimeUtils.timeAgo(offset(395 * TimeUtils.ONEDAY)).equals("13 months ago"));
|
|
85 |
assertTrue(TimeUtils.timeAgo(offset((2 * 365 + 30) * TimeUtils.ONEDAY)).equals(
|
|
86 |
"2 years ago"));
|
|
87 |
|
|
88 |
// css class tests
|
|
89 |
assertTrue(TimeUtils.timeAgoCss(offset(1 * TimeUtils.MIN)).equals("age0"));
|
|
90 |
assertTrue(TimeUtils.timeAgoCss(offset(60 * TimeUtils.MIN)).equals("age0"));
|
|
91 |
assertTrue(TimeUtils.timeAgoCss(offset(120 * TimeUtils.MIN)).equals("age1"));
|
|
92 |
assertTrue(TimeUtils.timeAgoCss(offset(24 * TimeUtils.ONEHOUR)).equals("age1"));
|
|
93 |
assertTrue(TimeUtils.timeAgoCss(offset(2 * TimeUtils.ONEDAY)).equals("age2"));
|
|
94 |
}
|
28d6b2
|
95 |
}
|