1 package org.apache.archiva.common.utils;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.text.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
25
26 import junit.framework.TestCase;
27
28
29
30
31
32
33 public class DateUtilTest extends TestCase
34 {
35 private void assertDuration( String expectedDuration, String startTimestamp, String endTimestamp )
36 throws ParseException
37 {
38 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss SSS" );
39 Date startDate = sdf.parse( startTimestamp );
40 Date endDate = sdf.parse( endTimestamp );
41
42
43
44
45 assertEquals( expectedDuration, DateUtil.getDuration( startDate, endDate ) );
46 }
47
48 public void testGetDurationDifference() throws ParseException
49 {
50 assertDuration( "2 Seconds", "2006-08-22 13:00:02 0000",
51 "2006-08-22 13:00:04 0000" );
52
53 assertDuration( "12 Minutes 12 Seconds 234 Milliseconds", "2006-08-22 13:12:02 0000",
54 "2006-08-22 13:24:14 0234" );
55
56 assertDuration( "12 Minutes 501 Milliseconds", "2006-08-22 13:12:01 0500",
57 "2006-08-22 13:24:02 0001" );
58 }
59
60 public void testGetDurationDirect() throws ParseException
61 {
62 assertEquals( "2 Seconds", DateUtil.getDuration( 2000 ) );
63
64 assertEquals( "12 Minutes 12 Seconds 234 Milliseconds", DateUtil.getDuration( 732234 ) );
65
66 assertEquals( "12 Minutes 501 Milliseconds", DateUtil.getDuration( 720501 ) );
67 }
68 }