001/* 002 * ModeShape (http://www.modeshape.org) 003 * 004 * Licensed under the Apache License, Version 2.0 (the "License"); 005 * you may not use this file except in compliance with the License. 006 * You may obtain a copy of the License at 007 * 008 * http://www.apache.org/licenses/LICENSE-2.0 009 * 010 * Unless required by applicable law or agreed to in writing, software 011 * distributed under the License is distributed on an "AS IS" BASIS, 012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016package org.modeshape.common.math; 017 018import static org.junit.Assert.assertEquals; 019import static org.junit.Assert.assertNotNull; 020import java.text.DecimalFormat; 021import java.util.concurrent.TimeUnit; 022import org.junit.Before; 023import org.junit.Test; 024 025public class DurationTest { 026 027 private Duration duration; 028 029 @Before 030 public void beforeEach() { 031 this.duration = new Duration(0); 032 } 033 034 @Test 035 public void shouldBeEmptyWhenInitialized() { 036 assertEquals(0, this.duration.getComponents().getHours()); 037 assertEquals(0, this.duration.getComponents().getMinutes()); 038 assertEquals(0.0d, this.duration.getComponents().getSeconds(), 0.00001d); 039 } 040 041 @Test 042 public void shouldHaveComponentsWhenInitialized() { 043 assertNotNull(this.duration.getComponents()); 044 } 045 046 @Test 047 public void shouldBeAllowedToAddSeconds() { 048 this.duration = this.duration.add(1, TimeUnit.SECONDS); 049 assertEquals(0, this.duration.getComponents().getHours()); 050 assertEquals(0, this.duration.getComponents().getMinutes()); 051 assertEquals(1.0d, this.duration.getComponents().getSeconds(), 0.00001d); 052 } 053 054 /** 055 * Need to be careful about comparing strings, because {@link DecimalFormat} is used and relies upon the default locale. What 056 * we do know is: 057 * <ul> 058 * <li>the hours will be at least 2 digits (zero-padded at beginning);</li> 059 * <li>the minutes will be 2 digits (zero-padded at beginning);</li> 060 * <li>the seconds will be formatted according to "00.000,###" using DecimalFormat and the default locale (which may use a 061 * different decimal point delimiter than '.') and where the ',' is only used if there are more than 3 digits used in the 062 * fractional part;</li> 063 * <li>the hours, minutes and seconds will be delimited with a ':';</li> 064 */ 065 @Test 066 public void shouldRepresentTimeInProperFormat() { 067 this.duration = this.duration.add(2, TimeUnit.SECONDS); 068 assertEquals(this.duration.toString().startsWith("00:00:02"), true); 069 assertEquals(this.duration.toString().endsWith("000"), true); 070 071 this.duration = new Duration(1100, TimeUnit.MILLISECONDS); 072 this.duration = this.duration.add(1 * 60, TimeUnit.SECONDS); 073 this.duration = this.duration.add(1 * 60 * 60, TimeUnit.SECONDS); 074 assertEquals(this.duration.toString().startsWith("01:01:01"), true); 075 assertEquals(this.duration.toString().endsWith("100"), true); 076 077 this.duration = new Duration(30100123, TimeUnit.MICROSECONDS); 078 this.duration = this.duration.add(20 * 60, TimeUnit.SECONDS); 079 this.duration = this.duration.add(10 * 60 * 60, TimeUnit.SECONDS); 080 assertEquals(this.duration.toString().startsWith("10:20:30"), true); 081 assertEquals(this.duration.toString().endsWith("100,123"), true); 082 } 083 084}