svn commit: r910827 - in /ofbiz/trunk/framework/base: build.xml src/org/ofbiz/base/util/test/TimeDurationTests.java testdef/basetests.xml

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

svn commit: r910827 - in /ofbiz/trunk/framework/base: build.xml src/org/ofbiz/base/util/test/TimeDurationTests.java testdef/basetests.xml

doogie-3
Author: doogie
Date: Wed Feb 17 05:15:59 2010
New Revision: 910827

URL: http://svn.apache.org/viewvc?rev=910827&view=rev
Log:
Add TimeDurationTests; full line coverage, almost full branch coverage.
There are some bugs not fixed by this commit, those will come shortly.

Added:
    ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/TimeDurationTests.java
Modified:
    ofbiz/trunk/framework/base/build.xml
    ofbiz/trunk/framework/base/testdef/basetests.xml

Modified: ofbiz/trunk/framework/base/build.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/build.xml?rev=910827&r1=910826&r2=910827&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/build.xml (original)
+++ ofbiz/trunk/framework/base/build.xml Wed Feb 17 05:15:59 2010
@@ -39,6 +39,7 @@
 
     <filelist id="test.classes" dir="${src.dir}">
         <file name="org/ofbiz/base/util/test/IndentingWriterTests.java"/>
+        <file name="org/ofbiz/base/util/test/TimeDurationTests.java"/>
         <file name="org/ofbiz/base/json/test/JSONTests.java"/>
         <file name="org/ofbiz/base/conversion/test/DateTimeTests.java"/>
         <file name="org/ofbiz/base/conversion/test/MiscTests.java"/>

Added: ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/TimeDurationTests.java
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/TimeDurationTests.java?rev=910827&view=auto
==============================================================================
--- ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/TimeDurationTests.java (added)
+++ ofbiz/trunk/framework/base/src/org/ofbiz/base/util/test/TimeDurationTests.java Wed Feb 17 05:15:59 2010
@@ -0,0 +1,162 @@
+/*******************************************************************************
+ * 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
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *******************************************************************************/
+package org.ofbiz.base.util.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;
+
+import org.ofbiz.base.util.TimeDuration;
+import org.ofbiz.base.test.GenericTestCaseBase;
+
+public class TimeDurationTests extends GenericTestCaseBase {
+    private static final Calendar zero = Calendar.getInstance(TimeZone.getTimeZone("GMT"));
+
+    static {
+        zero.clear();
+        zero.setTimeInMillis(0);
+    }
+
+    public TimeDurationTests(String name) {
+        super(name);
+    }
+
+    protected void setUp() throws Exception {
+        super.setUp();
+    }
+
+    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) {
+        assertEquals(label + ".years()", years, duration.years());
+        assertEquals(label + ".months()", months, duration.months());
+        assertEquals(label + ".days()", days, duration.days());
+        assertEquals(label + ".hours()", hours, duration.hours());
+        assertEquals(label + ".minutes()", minutes, duration.minutes());
+        assertEquals(label + ".seconds()", seconds, duration.seconds());
+        assertEquals(label + ".milliseconds()", milliseconds, duration.millis());
+        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 + ".isZero", isZero, duration.isZero());
+        if (isZero) {
+            assertEquals(label + ".compareTo(zero) == 0", 0, doCompare(TimeDuration.ZeroTimeDuration, duration));
+            assertEquals(label + ".compareTo(zero) == 0", 0, doCompare(duration, TimeDuration.ZeroTimeDuration));
+        } else {
+            assertNotSame(label + ".compareTo(zero) != 0", 0, doCompare(TimeDuration.ZeroTimeDuration, duration));
+            assertNotSame(label + ".compareTo(zero) != 0", 0, doCompare(duration, TimeDuration.ZeroTimeDuration));
+        }
+    }
+
+    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 || sb.length() > 0 ? months : "");
+        sb.append(':').append(days != 0 || sb.length() > 1 ? days : "");
+        sb.append(':').append(hours != 0 || sb.length() > 2 ? hours : "");
+        sb.append(':').append(minutes != 0 || sb.length() > 3 ? minutes : "");
+        sb.append(':').append(seconds != 0 || sb.length() > 4 ? seconds : "");
+        sb.append(':').append(milliseconds != 0 || sb.length() > 5 ? milliseconds : "");
+        String durationString = years + ":" + months + ":" + days + ":" + hours + ":" + minutes + ":" + seconds + ":" + milliseconds;
+        TimeDuration stringDuration = TimeDuration.parseDuration(sb.toString());
+        right.setTimeInMillis(0);
+        if (years != 0) {
+            right.set(Calendar.YEAR, 1970 + Math.abs(years));
+        }
+        if (months != 0) {
+            right.set(Calendar.MONTH, Math.abs(months));
+        }
+        right.set(Calendar.DAY_OF_MONTH, Math.abs(days) + 1);
+        if (hours != 0) {
+            right.set(Calendar.HOUR, Math.abs(hours));
+        }
+        if (minutes != 0) {
+            right.set(Calendar.MINUTE, Math.abs(minutes));
+        }
+        if (seconds != 0) {
+            right.set(Calendar.SECOND, Math.abs(seconds));
+        }
+        if (milliseconds != 0) {
+            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);
+        if (!isNegative) {
+            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);
+        }
+        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));
+        assertEquals(label + ".compareTo(cal, string)", 0, doCompare(calDuration, stringDuration));
+        assertEquals(label + ".equals(cal, cal)", calDuration, calDuration);
+        assertEquals(label + ".equals(cal, string)", calDuration, stringDuration);
+        assertEquals(label + ".equals(string, cal)", stringDuration, calDuration);
+        assertEquals(label + ".equals(string, string)", stringDuration, stringDuration);
+        assertFalse(label + ".not-equals(string, this)", stringDuration.equals(TimeDurationTests.class));
+        if (lastString != null) {
+            assertFalse(label + ".not-equals(string, lastString)", stringDuration.equals(lastString));
+        }
+        return stringDuration;
+    }
+
+    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 = 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);
+        }
+    }
+
+    public void testDuration() throws Exception {
+        Calendar now = Calendar.getInstance();
+        TimeDuration zeroDuration = TimeDuration.ZeroTimeDuration;
+        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);
+        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);
+        assertDuration("hour", 0, 0, 0, 1, 0, 0, 0);
+        assertDuration("day",  0, 0, 1, 0, 0, 0, 0);
+        assertDuration("month", 0, 1, 0, 0, 0, 0, 0);
+        assertDuration("year", 1, 0, 0, 0, 0, 0, 0);
+    }
+}

Modified: ofbiz/trunk/framework/base/testdef/basetests.xml
URL: http://svn.apache.org/viewvc/ofbiz/trunk/framework/base/testdef/basetests.xml?rev=910827&r1=910826&r2=910827&view=diff
==============================================================================
--- ofbiz/trunk/framework/base/testdef/basetests.xml (original)
+++ ofbiz/trunk/framework/base/testdef/basetests.xml Wed Feb 17 05:15:59 2010
@@ -22,6 +22,7 @@
         xsi:noNamespaceSchemaLocation="http://ofbiz.apache.org/dtds/test-suite.xsd">
     <test-case case-name="basetests">
         <junit-test-suite class-name="org.ofbiz.base.util.test.IndentingWriterTests"/>
+        <junit-test-suite class-name="org.ofbiz.base.util.test.TimeDurationTests"/>
         <junit-test-suite class-name="org.ofbiz.base.conversion.test.DateTimeTests.java"/>
         <junit-test-suite class-name="org.ofbiz.base.conversion.test.MiscTests.java"/>
         <junit-test-suite class-name="org.ofbiz.base.util.test.UtilIOTests"/>