Author: britter
Date: Fri May 2 09:21:14 2014
New Revision: 1591840
URL: http://svn.apache.org/r1591840
Log:
LANG-1003: DurationFormatUtils are not able to handle negative
durations/periods. Reported by Michael Osipov.
Modified:
commons/proper/lang/trunk/src/changes/changes.xml
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java
Modified: commons/proper/lang/trunk/src/changes/changes.xml
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/changes/changes.xml?rev=1591840&r1=1591839&r2=1591840&view=diff
==============================================================================
--- commons/proper/lang/trunk/src/changes/changes.xml [utf-8] (original)
+++ commons/proper/lang/trunk/src/changes/changes.xml [utf-8] Fri May 2
09:21:14 2014
@@ -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>
Modified:
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java?rev=1591840&r1=1591839&r2=1591840&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java
(original)
+++
commons/proper/lang/trunk/src/main/java/org/apache/commons/lang3/time/DurationFormatUtils.java
Fri May 2 09:21:14 2014
@@ -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
Modified:
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java
URL:
http://svn.apache.org/viewvc/commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java?rev=1591840&r1=1591839&r2=1591840&view=diff
==============================================================================
---
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java
(original)
+++
commons/proper/lang/trunk/src/test/java/org/apache/commons/lang3/time/DurationFormatUtilsTest.java
Fri May 2 09:21:14 2014
@@ -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