/*
 * ModeShape (http://www.modeshape.org)
 * See the COPYRIGHT.txt file distributed with this work for information
 * regarding copyright ownership.  Some portions may be licensed
 * to Red Hat, Inc. under one or more contributor license agreements.
 * See the AUTHORS.txt file in the distribution for a full listing of 
 * individual contributors. 
 *
 * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
 * is licensed to you under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2.1 of
 * the License, or (at your option) any later version.
 *
 * ModeShape is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

package org.modeshape.common.math;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import java.util.concurrent.TimeUnit;
import org.junit.Before;
import org.junit.Test;

public class DurationTest {

    private Duration duration;

    @Before
    public void beforeEach() {
        this.duration = new Duration(0);
    }

    @Test
    public void shouldBeEmptyWhenInitialized() {
        assertEquals(0, this.duration.getComponents().getHours());
        assertEquals(0, this.duration.getComponents().getMinutes());
        assertEquals(0.0d, this.duration.getComponents().getSeconds(), 0.00001d);
    }

    @Test
    public void shouldHaveComponentsWhenInitialized() {
        assertNotNull(this.duration.getComponents());
    }

    @Test
    public void shouldBeAllowedToAddSeconds() {
        this.duration = this.duration.add(1, TimeUnit.SECONDS);
        assertEquals(0, this.duration.getComponents().getHours());
        assertEquals(0, this.duration.getComponents().getMinutes());
        assertEquals(1.0d, this.duration.getComponents().getSeconds(), 0.00001d);
    }

    @Test
    public void shouldRepresentTimeInProperFormat() {
        this.duration = this.duration.add(2, TimeUnit.SECONDS);
        assertEquals("00:00:02.000", this.duration.toString());

        this.duration = new Duration(1100, TimeUnit.MILLISECONDS);
        this.duration = this.duration.add(1 * 60, TimeUnit.SECONDS);
        this.duration = this.duration.add(1 * 60 * 60, TimeUnit.SECONDS);
        assertEquals("01:01:01.100", this.duration.toString());

        this.duration = new Duration(30100123, TimeUnit.MICROSECONDS);
        this.duration = this.duration.add(20 * 60, TimeUnit.SECONDS);
        this.duration = this.duration.add(10 * 60 * 60, TimeUnit.SECONDS);
        assertEquals("10:20:30.100,123", this.duration.toString());
    }

}

@@ -1281,16 +1281,48 @@
 otNull;%0A
+import java.text.DecimalFormat;%0A
 import j
@@ -2302,32 +2302,724 @@
 00001d);%0A    %7D%0A%0A
+    /**%0A     * Need to be careful about comparing strings, because %7B@link DecimalFormat%7D is used and relies upon the default locale. What%0A     * we do know is:%0A     * %3Cul%3E%0A     * %3Cli%3Ethe hours will be at least 2 digits (zero-padded at beginning);%3C/li%3E%0A     * %3Cli%3Ethe minutes will be 2 digits (zero-padded at beginning);%3C/li%3E%0A     * %3Cli%3Ethe seconds will be formatted according to %2200.000,###%22 using DecimalFormat and the default locale (which may use a%0A     * different decimal point delimiter than '.') and where the ',' is only used if there are more than 3 digits used in the%0A     * fractional part;%3C/li%3E%0A     * %3Cli%3Ethe hours, minutes and seconds will be delimited with a ':';%3C/li%3E%0A     */%0A
     @Test%0A    pu
@@ -3155,24 +3155,85 @@
 als(
-%2200:00:02.000%22, 
+this.duration.toString().startsWith(%2200:00:02%22), true);%0A        assertEquals(
 this
@@ -3244,32 +3244,54 @@
 ation.toString()
+.endsWith(%22000%22), true
 );%0A%0A        this
@@ -3513,24 +3513,8 @@
 als(
-%2201:01:01.100%22, 
 this
@@ -3533,16 +3533,115 @@
 String()
+.startsWith(%2201:01:01%22), true);%0A        assertEquals(this.duration.toString().endsWith(%22100%22), true
 );%0A%0A    
@@ -3877,28 +3877,8 @@
 als(
-%2210:20:30.100,123%22, 
 this
@@ -3897,16 +3897,119 @@
 String()
+.startsWith(%2210:20:30%22), true);%0A        assertEquals(this.duration.toString().endsWith(%22100,123%22), true
 );%0A    %7D

MODE-656 Fixed the Duration test case so that it works in Locales that use something other than '.' for the decimal point delimiter (e.g., Germany).