You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

DateUtilTest.java 2.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package org.apache.archiva.common.utils;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import junit.framework.TestCase;
  21. import java.text.ParseException;
  22. import java.text.SimpleDateFormat;
  23. import java.util.Date;
  24. /**
  25. * DateUtilTest
  26. *
  27. *
  28. */
  29. public class DateUtilTest extends TestCase
  30. {
  31. private void assertDuration( String expectedDuration, String startTimestamp, String endTimestamp )
  32. throws ParseException
  33. {
  34. SimpleDateFormat sdf = new SimpleDateFormat( "yyyy-MM-dd HH:mm:ss SSS" );
  35. Date startDate = sdf.parse( startTimestamp );
  36. Date endDate = sdf.parse( endTimestamp );
  37. // System.out.println( "Date: " + endTimestamp + " - " + startTimestamp + " = "
  38. // + ( endDate.getTime() - startDate.getTime() ) + " ms" );
  39. assertEquals( expectedDuration, DateUtil.getDuration( startDate, endDate ) );
  40. }
  41. public void testGetDurationDifference() throws ParseException
  42. {
  43. assertDuration( "2 Seconds", "2006-08-22 13:00:02 0000",
  44. "2006-08-22 13:00:04 0000" );
  45. assertDuration( "12 Minutes 12 Seconds 234 Milliseconds", "2006-08-22 13:12:02 0000",
  46. "2006-08-22 13:24:14 0234" );
  47. assertDuration( "12 Minutes 501 Milliseconds", "2006-08-22 13:12:01 0500",
  48. "2006-08-22 13:24:02 0001" );
  49. }
  50. public void testGetDurationDirect() throws ParseException
  51. {
  52. assertEquals( "2 Seconds", DateUtil.getDuration( 2000 ) );
  53. assertEquals( "12 Minutes 12 Seconds 234 Milliseconds", DateUtil.getDuration( 732234 ) );
  54. assertEquals( "12 Minutes 501 Milliseconds", DateUtil.getDuration( 720501 ) );
  55. }
  56. }