View Javadoc
1   package org.apache.archiva.common.utils;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
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   * DateUtilTest 
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  //        System.out.println( "Date: " + endTimestamp + " - " + startTimestamp + " = "
43  //                        + ( endDate.getTime() - startDate.getTime() ) + " ms" );
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  }