Skip to content

Commit c60f0f4

Browse files
committed
Durations of length zero caused an exception.
1 parent 5df3ab7 commit c60f0f4

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/main/java/org/culturegraph/mf/util/TimeUtil.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ public final class TimeUtil {
2525
public static final String[] UNIT_SYMBOLS = { "ns", "µs", "ms", "s", "min", "h" };
2626
public static final long[] UNIT_FACTORS = { 1L, 1000L, 1000L, 1000L, 60L, 60L };
2727

28+
public static final int BASE_UNIT_INDEX = 3;
29+
2830
public static final long HOURS = 60L * 60L * 1000L * 1000L * 1000L;
2931
public static final long MINUTES = 60L * 1000L * 1000L * 1000L;
3032
public static final long SECONDS = 1000L * 1000L * 1000L;
@@ -51,6 +53,9 @@ public static String formatDuration(final long duration) {
5153
}
5254

5355
if (i == 0 || minor == 0) {
56+
if (i < 0) {
57+
i = BASE_UNIT_INDEX; // Use seconds as default unit
58+
}
5459
return String.format("%d%s", Long.valueOf(major), UNIT_SYMBOLS[i]);
5560
}
5661
return String.format("%d%s %d%s", Long.valueOf(major), UNIT_SYMBOLS[i], Long.valueOf(minor), UNIT_SYMBOLS[i - 1]);

src/test/java/org/culturegraph/mf/util/TimeUtilTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,15 @@ public void testShouldRoundUpIfRemainderIsMidway() {
141141
assertEquals("42min 2s", formattedDuration);
142142
}
143143

144+
@Test
145+
public void testShouldNotFailIfDurationIsZero() {
146+
final long duration = 0L;
147+
148+
final String formattedDuration = TimeUtil.formatDuration(duration);
149+
150+
assertEquals("0s", formattedDuration);
151+
}
152+
144153
/**
145154
* Test for issue #82: TimerBase throws ArrayIndexOutOfBoundsException
146155
* if the measured duration is longer than one hour.

0 commit comments

Comments
 (0)