LANG-1003: DurationFormatUtils are not able to handle negative durations/periods. Reported by Michael Osipov.

git-svn-id: https://svn.apache.org/repos/asf/commons/proper/lang/trunk@1591840 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Benedikt Ritter 2014-05-02 09:21:14 +00:00
parent 358f139d13
commit 64ef8a8022
3 changed files with 37 additions and 0 deletions

View File

@ -22,6 +22,7 @@
<body>
<release version="3.4" date="tba" description="tba">
<action issue="LANG-1003" type="update" dev="britter">DurationFormatUtils are not able to handle negative durations/periods</action>
<action issue="LANG-1001" type="fix" dev="ggregory" due-to="Michael Osipov">ISO 8601 misspelled throughout the Javadocs</action>
<action issue="LANG-994" type="add" dev="britter" due-to="Mikhail Mazursky">Add zero copy read method to StrBuilder</action>
<action issue="LANG-993" type="add" dev="britter" due-to="Mikhail Mazursky">Add zero copy write method to StrBuilder</action>

View File

@ -124,6 +124,9 @@ public class DurationFormatUtils {
* @return the formatted duration, not null
*/
public static String formatDuration(final long durationMillis, final String format, final boolean padWithZeros) {
if(durationMillis < 0) {
throw new IllegalArgumentException("Duration must not be less than 0");
}
final Token[] tokens = lexx(format);
@ -269,6 +272,9 @@ public class DurationFormatUtils {
*/
public static String formatPeriod(final long startMillis, final long endMillis, final String format, final boolean padWithZeros,
final TimeZone timezone) {
if(startMillis > endMillis) {
throw new IllegalArgumentException("endMillis must be greater than startMillis");
}
// Used to optimise for differences under 28 days and
// called formatDuration(millis, format); however this did not work

View File

@ -157,6 +157,11 @@ public class DurationFormatUtilsTest {
assertEquals("1 day 1 hour 1 minute 1 second", text);
}
@Test(expected = IllegalArgumentException.class)
public void testFormatNegativeDurationWords() throws Exception {
DurationFormatUtils.formatDurationWords(-5000, true, true);
}
@Test
public void testFormatDurationHMS() {
long time = 0;
@ -187,6 +192,11 @@ public class DurationFormatUtilsTest {
assertEquals("1:02:12.789", DurationFormatUtils.formatDurationHMS(time));
}
@Test(expected = IllegalArgumentException.class)
public void testFormatNegativeDurationHMS() throws Exception {
DurationFormatUtils.formatDurationHMS(-5000);
}
@Test
public void testFormatDurationISO() {
assertEquals("P0Y0M0DT0H0M0.000S", DurationFormatUtils.formatDurationISO(0L));
@ -196,6 +206,11 @@ public class DurationFormatUtilsTest {
assertEquals("P0Y0M0DT0H1M15.321S", DurationFormatUtils.formatDurationISO(75321L));
}
@Test(expected = IllegalArgumentException.class)
public void testFormatNegativeDurationISO() throws Exception {
DurationFormatUtils.formatDurationISO(-5000);
}
@Test
public void testFormatDuration() {
long duration = 0;
@ -234,6 +249,11 @@ public class DurationFormatUtilsTest {
assertEquals("0 0 " + days, DurationFormatUtils.formatDuration(duration, "y M d"));
}
@Test(expected = IllegalArgumentException.class)
public void testFormatNegativeDuration() throws Exception {
DurationFormatUtils.formatDuration(-5000, "S", true);
}
@Test
public void testFormatPeriodISO() {
final TimeZone timeZone = TimeZone.getTimeZone("GMT-3");
@ -262,6 +282,11 @@ public class DurationFormatUtilsTest {
// assertEquals("P1Y2M3DT10H30M", text);
}
@Test(expected = IllegalArgumentException.class)
public void testFormatPeriodISOStartGreaterEnd() throws Exception {
DurationFormatUtils.formatPeriodISO(5000, 2000);
}
@Test
public void testFormatPeriodISOMethod() {
assertEquals("P0Y0M0DT0H0M0.000S", DurationFormatUtils.formatPeriodISO(0L, 0L));
@ -323,6 +348,11 @@ public class DurationFormatUtilsTest {
assertEquals("048", DurationFormatUtils.formatPeriod(time1970, time, "MMM"));
}
@Test(expected = IllegalArgumentException.class)
public void testFormatPeriodeStartGreaterEnd() throws Exception {
DurationFormatUtils.formatPeriod(5000, 2500, "yy/MM");
}
@Test
public void testLexx() {
// tests each constant