1 package org.apache.archiva.common.utils;
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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
22 import java.text.ParseException;
23 import java.text.SimpleDateFormat;
24 import java.util.Date;
26 import junit.framework.TestCase;
33 public class DateUtilTest extends TestCase
35 private void assertDuration( String expectedDuration, String startTimestamp, String endTimestamp )
38 SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss SSS" );
39 Date startDate = sdf.parse( startTimestamp );
40 Date endDate = sdf.parse( endTimestamp );
42 // System.out.println( "Date: " + endTimestamp + " - " + startTimestamp + " = "
43 // + ( endDate.getTime() - startDate.getTime() ) + " ms" );
45 assertEquals( expectedDuration, DateUtil.getDuration( startDate, endDate ) );
48 public void testGetDurationDifference() throws ParseException
50 assertDuration( "2 Seconds", "2006-08-22 13:00:02 0000",
51 "2006-08-22 13:00:04 0000" );
53 assertDuration( "12 Minutes 12 Seconds 234 Milliseconds", "2006-08-22 13:12:02 0000",
54 "2006-08-22 13:24:14 0234" );
56 assertDuration( "12 Minutes 501 Milliseconds", "2006-08-22 13:12:01 0500",
57 "2006-08-22 13:24:02 0001" );
60 public void testGetDurationDirect() throws ParseException
62 assertEquals( "2 Seconds", DateUtil.getDuration( 2000 ) );
64 assertEquals( "12 Minutes 12 Seconds 234 Milliseconds", DateUtil.getDuration( 732234 ) );
66 assertEquals( "12 Minutes 501 Milliseconds", DateUtil.getDuration( 720501 ) );