@@ -1,4 +1,4 @@
/*******************************************************************************
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
@@ -15,46 +15,33 @@
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*******************************************************************************/
package org .apache .ofbiz .base .util .test ;
*/
package org .apache .ofbiz .base .util ;
import static org .junit .Assert .assertEquals ;
import static org .junit .Assert .assertFalse ;
import static org .junit .Assert .assertNotSame ;
import org .apache .ofbiz .base .lang .SourceMonitored ;
import org .apache .ofbiz .base .test .GenericTestCaseBase ;
import org .apache .ofbiz .base .util .TimeDuration ;
import org .junit .Test ;
//import com.ibm.icu.util.Calendar;
//import com.ibm.icu.util.TimeZone;
import com .ibm .icu .util .Calendar ;
import com .ibm .icu .util .TimeZone ;
@ SourceMonitored
public class TimeDurationTests extends GenericTestCaseBase {
public class TimeDurationTests {
private static final Calendar zero = Calendar .getInstance (TimeZone .getTimeZone ("GMT" ));
static {
zero .clear ();
zero .setTimeInMillis (0 );
}
public TimeDurationTests (String name ) {
super (name );
}
@ Override
protected void setUp () throws Exception {
super .setUp ();
}
@ Override
protected void tearDown () throws Exception {
super .tearDown ();
}
private static <T extends Comparable <T >> int doCompare (T comparable , T other ) {
return comparable .compareTo (other );
}
private static void assertDurationFields (String label , int years , int months , int days , int hours , int minutes , int seconds , int milliseconds , String string , TimeDuration duration , boolean isNegative , boolean isZero ) {
private static void assertDurationFields (String label , int years , int months , int days , int hours , int minutes ,
int seconds , int milliseconds , String string , TimeDuration duration , boolean isNegative , boolean isZero ) {
assertEquals (label + ".years()" , years , duration .years ());
assertEquals (label + ".months()" , months , duration .months ());
assertEquals (label + ".days()" , days , duration .days ());
@@ -65,7 +52,8 @@ private static void assertDurationFields(String label, int years, int months, in
assertEquals (label + ".isNegative()" , isNegative , duration .isNegative ());
assertEquals (label + ".toString()" , string , duration .toString ());
assertEquals (label + ".equals(from/to long)" , duration , TimeDuration .fromLong (TimeDuration .toLong (duration )));
assertEquals (label + ".equals(from/to number)" , duration , TimeDuration .fromNumber (TimeDuration .toLong (duration )));
assertEquals (label + ".equals(from/to number)" , duration ,
TimeDuration .fromNumber (TimeDuration .toLong (duration )));
assertEquals (label + ".isZero" , isZero , duration .isZero ());
if (isZero ) {
assertEquals (label + ".compareTo(zero) == 0" , 0 , doCompare (TimeDuration .ZeroTimeDuration , duration ));
@@ -76,7 +64,8 @@ private static void assertDurationFields(String label, int years, int months, in
}
}
private static TimeDuration assertDurationLoop (String label , Calendar right , int years , int months , int days , int hours , int minutes , int seconds , int milliseconds , TimeDuration lastString , boolean isNegative ) {
private static TimeDuration assertDurationLoop (String label , Calendar right , int years , int months , int days ,
int hours , int minutes , int seconds , int milliseconds , TimeDuration lastString , boolean isNegative ) {
StringBuilder sb = new StringBuilder ();
sb .append (years != 0 ? years : "" );
sb .append (':' ).append (months != 0 ? months : "" );
@@ -85,7 +74,8 @@ private static TimeDuration assertDurationLoop(String label, Calendar right, int
sb .append (':' ).append (minutes != 0 ? minutes : "" );
sb .append (':' ).append (seconds != 0 ? seconds : "" );
sb .append (':' ).append (milliseconds != 0 ? milliseconds : "" );
String durationString = years + ":" + months + ":" + days + ":" + hours + ":" + minutes + ":" + seconds + ":" + milliseconds ;
String durationString =
years + ":" + months + ":" + days + ":" + hours + ":" + minutes + ":" + seconds + ":" + milliseconds ;
TimeDuration stringDuration = TimeDuration .parseDuration (sb .toString ());
right .setTimeInMillis (0 );
if (years != 0 ) {
@@ -108,12 +98,16 @@ private static TimeDuration assertDurationLoop(String label, Calendar right, int
right .set (Calendar .MILLISECOND , Math .abs (milliseconds ));
}
TimeDuration calDuration = isNegative ? new TimeDuration (right , zero ) : new TimeDuration (zero , right );
assertDurationFields (label + "(parseString[0])" , years , months , days , hours , minutes , seconds , milliseconds , durationString , TimeDuration .parseDuration (durationString ), isNegative , false );
assertDurationFields (label + "(parseString)" , years , months , days , hours , minutes , seconds , milliseconds , durationString , stringDuration , isNegative , false );
assertDurationFields (label + "(cal)" , years , months , days , hours , minutes , seconds , milliseconds , durationString , calDuration , isNegative , false );
assertDurationFields (label + "(parseString[0])" , years , months , days , hours , minutes , seconds , milliseconds ,
durationString , TimeDuration .parseDuration (durationString ), isNegative , false );
assertDurationFields (label + "(parseString)" , years , months , days , hours , minutes , seconds , milliseconds ,
durationString , stringDuration , isNegative , false );
assertDurationFields (label + "(cal)" , years , months , days , hours , minutes , seconds , milliseconds ,
durationString , calDuration , isNegative , false );
Calendar added = calDuration .addToCalendar ((Calendar ) zero .clone ());
TimeDuration addDuration = new TimeDuration (zero , added );
assertDurationFields (label + "(cal[add])" , years , months , days , hours , minutes , seconds , milliseconds , durationString , addDuration , isNegative , false );
assertDurationFields (label + "(cal[add])" , years , months , days , hours , minutes , seconds , milliseconds ,
durationString , addDuration , isNegative , false );
assertEquals (label + ".compareTo(string, cal)" , 0 , doCompare (stringDuration , calDuration ));
assertEquals (label + ".compareTo(string, string)" , 0 , doCompare (stringDuration , stringDuration ));
assertEquals (label + ".compareTo(cal, cal)" , 0 , doCompare (calDuration , calDuration ));
@@ -128,31 +122,41 @@ private static TimeDuration assertDurationLoop(String label, Calendar right, int
return stringDuration ;
}
public static void assertDuration (String label , int years , int months , int days , int hours , int minutes , int seconds , int milliseconds ) {
public static void assertDuration (String label , int years , int months , int days , int hours , int minutes ,
int seconds , int milliseconds ) {
TimeDuration lastString = null ;
Calendar right = (Calendar ) zero .clone ();
for (int i = 1 ; i < 12 ; i ++) {
lastString = assertDurationLoop (i + " " + label , right , i * years , i * months , i * days , i * hours , i * minutes , i * seconds , i * milliseconds , lastString , false );
lastString = assertDurationLoop (i + " " + label , right , i * years , i * months , i * days , i * hours ,
i * minutes , i * seconds , i * milliseconds , lastString , false );
}
lastString = null ;
for (int i = -2 ; i > -12 ; i --) {
lastString = assertDurationLoop (i + " " + label , right , i * years , i * months , i * days , i * hours , i * minutes , i * seconds , i * milliseconds , lastString , true );
lastString = assertDurationLoop (i + " " + label , right , i * years , i * months , i * days , i * hours ,
i * minutes , i * seconds , i * milliseconds , lastString , true );
}
}
@ Test
public void testDuration () throws Exception {
Calendar now = Calendar .getInstance ();
TimeDuration zeroDuration = TimeDuration .ZeroTimeDuration ;
assertFalse ("zero equals null" , zeroDuration .equals (null ));
Calendar newTime = (Calendar ) now .clone ();
zeroDuration .addToCalendar (newTime );
assertEquals ("zero same calendar" , now , newTime );
assertDurationFields ("zero(same zero calendar)" , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "0:0:0:0:0:0:0" , new TimeDuration (zero , zero ), false , true );
assertDurationFields ("zero(same now calendar)" , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "0:0:0:0:0:0:0" , new TimeDuration (now , now ), false , true );
assertDurationFields ("zero(empty parse)" , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "0:0:0:0:0:0:0" , TimeDuration .parseDuration ("" ), false , true );
assertDurationFields ("zero(zero parse)" , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "0:0:0:0:0:0:0" , TimeDuration .parseDuration ("0:0:0:0:0:0:0" ), false , true );
assertDurationFields ("zero(from null number)" , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "0:0:0:0:0:0:0" , TimeDuration .fromNumber (null ), false , true );
assertDurationFields ("zero(from null number)" , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "0:0:0:0:0:0:0" , TimeDuration .fromNumber (null ), false , true );
assertDurationFields ("zero(same zero calendar)" , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "0:0:0:0:0:0:0" ,
new TimeDuration (zero , zero ), false , true );
assertDurationFields ("zero(same now calendar)" , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "0:0:0:0:0:0:0" ,
new TimeDuration (now , now ), false , true );
assertDurationFields ("zero(empty parse)" , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "0:0:0:0:0:0:0" ,
TimeDuration .parseDuration ("" ), false , true );
assertDurationFields ("zero(zero parse)" , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "0:0:0:0:0:0:0" ,
TimeDuration .parseDuration ("0:0:0:0:0:0:0" ), false , true );
assertDurationFields ("zero(from null number)" , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "0:0:0:0:0:0:0" ,
TimeDuration .fromNumber (null ), false , true );
assertDurationFields ("zero(from null number)" , 0 , 0 , 0 , 0 , 0 , 0 , 0 , "0:0:0:0:0:0:0" ,
TimeDuration .fromNumber (null ), false , true );
assertDuration ("millisecond" , 0 , 0 , 0 , 0 , 0 , 0 , 1 );
assertDuration ("second" , 0 , 0 ,0 ,0 , 0 , 1 , 0 );
assertDuration ("minute" , 0 , 0 , 0 , 0 , 1 , 0 , 0 );
@@ -170,6 +174,7 @@ public void testDuration() throws Exception {
end .add (Calendar .DAY_OF_MONTH , 1 );
end .add (Calendar .MONTH , 1 );
end .add (Calendar .YEAR , 1 );
assertDurationFields ("pre-epoch elapsed time" , 1 , 1 , 1 , 1 , 1 , 1 , 1 , "1:1:1:1:1:1:1" , new TimeDuration (start , end ), false , false );
assertDurationFields ("pre-epoch elapsed time" , 1 , 1 , 1 , 1 , 1 , 1 , 1 , "1:1:1:1:1:1:1" ,
new TimeDuration (start , end ), false , false );
}
}