Author: bayard
Date: Tue Dec 12 23:09:24 2006
New Revision: 486523
URL: http://svn.apache.org/viewvc?view=rev&rev=486523
Log:
More unit tests and bugfixes. The currently failing test is commented out, so
more bugfixes to come.
Modified:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DurationFormatUtils.java
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DurationFormatUtilsTest.java
Modified:
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DurationFormatUtils.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DurationFormatUtils.java?view=diff&rev=486523&r1=486522&r2=486523
==============================================================================
---
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DurationFormatUtils.java
(original)
+++
jakarta/commons/proper/lang/trunk/src/java/org/apache/commons/lang/time/DurationFormatUtils.java
Tue Dec 12 23:09:24 2006
@@ -20,6 +20,7 @@
import java.util.Calendar;
import java.util.Date;
+import java.util.GregorianCalendar;
import java.util.TimeZone;
/**
@@ -312,6 +313,9 @@
hours += 24;
days -= 1;
}
+ // TODO: Create a test to see if this should be while. ie) one that
makes hours above
+ // overflow and pushes this above the maximum # of days in a
month?
+ int leapDays = 0;
if (days < 0) {
days += start.getActualMaximum(Calendar.DAY_OF_MONTH);
// It's a tricky subject. Jan 15th to March 10th. If I count
days-first it is
@@ -320,10 +324,25 @@
// Also it's contextual - if asked for no M in the format then I
should probably
// be doing no calculating here.
months -= 1;
+ start.add(Calendar.MONTH, 1);
}
while (months < 0) {
months += 12;
years -= 1;
+ if (start instanceof GregorianCalendar) {
+ if ( ((GregorianCalendar)
start).isLeapYear(start.get(Calendar.YEAR) + 1) &&
+ ( end.get(Calendar.MONTH) > 1) )
+ {
+ leapDays += 1;
+ }
+ }
+ if (end instanceof GregorianCalendar) {
+ if ( ((GregorianCalendar)
end).isLeapYear(end.get(Calendar.YEAR)) &&
+ ( end.get(Calendar.MONTH) < 1) )
+ {
+ leapDays -= 1;
+ }
+ }
start.add(Calendar.YEAR, 1);
}
@@ -331,31 +350,31 @@
// aren't requested. This allows the user to ask for the
// number of months and get the real count and not just 0->11.
- if (!Token.containsTokenWithValue(tokens, y)) {
+ if (!Token.containsTokenWithValue(tokens, y) && years != 0) {
if (Token.containsTokenWithValue(tokens, M)) {
months += 12 * years;
years = 0;
} else {
- while(start.get(Calendar.YEAR) != end.get(Calendar.YEAR)) {
- days += start.getActualMaximum(Calendar.DAY_OF_YEAR);
- start.add(Calendar.YEAR, 1);
+ while ( (start.get(Calendar.YEAR) != end.get(Calendar.YEAR))) {
+ days += start.getActualMaximum(Calendar.DAY_OF_YEAR);
+ start.add(Calendar.YEAR, 1);
}
years = 0;
}
}
+ start.set(Calendar.YEAR, end.get(Calendar.YEAR));
- if (!Token.containsTokenWithValue(tokens, M) && months != 0) {
- start.set(start.get(Calendar.YEAR), start.get(Calendar.MONTH),
0, 0, 0, 0);
+ if (!Token.containsTokenWithValue(tokens, M) && months != 0) {
+ while(start.get(Calendar.MONTH) != end.get(Calendar.MONTH)) {
+ String date = start.getTime().toString();
+ days += start.getActualMaximum(Calendar.DAY_OF_MONTH);
start.add(Calendar.MONTH, 1);
- end.set(end.get(Calendar.YEAR), end.get(Calendar.MONTH), 0, 0,
0, 0);
- days += end.get(Calendar.DAY_OF_YEAR) -
start.get(Calendar.DAY_OF_YEAR);
- months = 0;
-
- // WARNING: For performance sake the Calendar instances are not
being
- // cloned but modified inline. They should not be trusted after
this point
- start = null;
- end = null;
+ }
+ days += leapDays;
+ months = 0;
}
+ start.set(Calendar.MONTH, end.get(Calendar.MONTH));
+
if (!Token.containsTokenWithValue(tokens, d)) {
hours += 24 * days;
days = 0;
Modified:
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DurationFormatUtilsTest.java
URL:
http://svn.apache.org/viewvc/jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DurationFormatUtilsTest.java?view=diff&rev=486523&r1=486522&r2=486523
==============================================================================
---
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DurationFormatUtilsTest.java
(original)
+++
jakarta/commons/proper/lang/trunk/src/test/org/apache/commons/lang/time/DurationFormatUtilsTest.java
Tue Dec 12 23:09:24 2006
@@ -441,9 +441,61 @@
assertEqualDuration( "365", new int[] { 2006, 0, 1, 0, 0, 0 },
new int[] { 2007, 0, 1, 0, 0, 0 }, "dd");
+ assertEqualDuration( "31", new int[] { 2006, 0, 1, 0, 0, 0 },
+ new int[] { 2006, 1, 1, 0, 0, 0 }, "dd");
+
+ assertEqualDuration( "92", new int[] { 2005, 9, 1, 0, 0, 0 },
+ new int[] { 2006, 0, 1, 0, 0, 0 }, "dd");
+ assertEqualDuration( "77", new int[] { 2005, 9, 16, 0, 0, 0 },
+ new int[] { 2006, 0, 1, 0, 0, 0 }, "dd");
+
+ // test month larger in start than end
+ assertEqualDuration( "136", new int[] { 2005, 9, 16, 0, 0, 0 },
+ new int[] { 2006, 2, 1, 0, 0, 0 }, "dd");
+ // test when start in leap year
+ assertEqualDuration( "136", new int[] { 2004, 9, 16, 0, 0, 0 },
+ new int[] { 2005, 2, 1, 0, 0, 0 }, "dd");
+ // test when end in leap year
+ assertEqualDuration( "137", new int[] { 2003, 9, 16, 0, 0, 0 },
+ new int[] { 2004, 2, 1, 0, 0, 0 }, "dd");
+ // test when end in leap year but less than end of feb
+ assertEqualDuration( "135", new int[] { 2003, 9, 16, 0, 0, 0 },
+ new int[] { 2004, 1, 28, 0, 0, 0 }, "dd");
+
+ assertEqualDuration( "364", new int[] { 2007, 0, 2, 0, 0, 0 },
+ new int[] { 2008, 0, 1, 0, 0, 0 }, "dd");
+ assertEqualDuration( "729", new int[] { 2006, 0, 2, 0, 0, 0 },
+ new int[] { 2008, 0, 1, 0, 0, 0 }, "dd");
+
+ assertEqualDuration( "365", new int[] { 2007, 2, 2, 0, 0, 0 },
+ new int[] { 2008, 2, 1, 0, 0, 0 }, "dd");
+ }
+
+ public void testDurationsByBruteForce() {
+ bruteForce(2006, 0, 1);
+ bruteForce(2006, 0, 2);
+// bruteForce(2006, 1, 2);
+ }
+
+ private void bruteForce(int year, int month, int day) {
+ String msg = year + "-" + month + "-" + day + " at ";
+ Calendar c = Calendar.getInstance();
+ c.set(year, month, day, 0, 0, 0);
+ int[] array1 = new int[] { year, month, day, 0, 0, 0 };
+ int[] array2 = new int[] { year, month, day, 0, 0, 0 };
+ for (int i=0; i < 1500; i++) {
+ array2[0] = c.get(Calendar.YEAR);
+ array2[1] = c.get(Calendar.MONTH);
+ array2[2] = c.get(Calendar.DAY_OF_MONTH);
+ assertEqualDuration( msg + i, Integer.toString(i), array1, array2,
"d" );
+ c.add(Calendar.DAY_OF_MONTH, 1);
+ }
}
private void assertEqualDuration(String expected, int[] start, int[] end,
String format) {
+ assertEqualDuration(null, expected, start, end, format);
+ }
+ private void assertEqualDuration(String message, String expected, int[]
start, int[] end, String format) {
Calendar cal1 = Calendar.getInstance();
cal1.set(start[0], start[1], start[2], start[3], start[4], start[5]);
cal1.set(Calendar.MILLISECOND, 0);
@@ -453,7 +505,11 @@
long milli1 = cal1.getTime().getTime();
long milli2 = cal2.getTime().getTime();
String result = DurationFormatUtils.formatPeriod(milli1, milli2,
format);
- assertEquals(expected, result);
+ if (message == null) {
+ assertEquals(expected, result);
+ } else {
+ assertEquals(message, expected, result);
+ }
}
private void assertArrayEquals(DurationFormatUtils.Token[] obj1,
DurationFormatUtils.Token[] obj2) {
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]