More unit tests and bugfixes. The currently failing test is commented out, so more bugfixes to come.

git-svn-id: https://svn.apache.org/repos/asf/jakarta/commons/proper/lang/trunk@486523 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Henri Yandell 2006-12-13 07:09:24 +00:00
parent 9b0b19a916
commit dd23ae863a
2 changed files with 90 additions and 15 deletions

View File

@ -20,6 +20,7 @@ import org.apache.commons.lang.StringUtils;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
/**
@ -312,6 +313,9 @@ public class DurationFormatUtils {
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 @@ public class DurationFormatUtils {
// 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 @@ public class DurationFormatUtils {
// 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;

View File

@ -441,9 +441,61 @@ public class DurationFormatUtilsTest extends TestCase {
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 @@ public class DurationFormatUtilsTest extends TestCase {
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) {