Skip to content

Commit 02c990c

Browse files
Seungpangsnicoll
authored andcommitted
Reject empty strings in DurationFormatterUtils
See gh-33669
1 parent 7f7f65c commit 02c990c

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

spring-context/src/main/java/org/springframework/format/datetime/standard/DurationFormatterUtils.java

+4-1
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
* @author Phillip Webb
3636
* @author Valentine Wu
3737
* @author Simon Baslé
38+
* @author Kim Seungrae
3839
* @since 6.2
3940
*/
4041
public abstract class DurationFormatterUtils {
@@ -62,6 +63,7 @@ public static Duration parse(String value, DurationFormat.Style style) {
6263
* @return a duration
6364
*/
6465
public static Duration parse(String value, DurationFormat.Style style, @Nullable DurationFormat.Unit unit) {
66+
Assert.hasText(value, () -> "Value must not be empty");
6567
return switch (style) {
6668
case ISO8601 -> parseIso8601(value);
6769
case SIMPLE -> parseSimple(value, unit);
@@ -88,6 +90,7 @@ public static String print(Duration value, DurationFormat.Style style) {
8890
* @return the printed result
8991
*/
9092
public static String print(Duration value, DurationFormat.Style style, @Nullable DurationFormat.Unit unit) {
93+
Assert.notNull(value, "Value must not be null");
9194
return switch (style) {
9295
case ISO8601 -> value.toString();
9396
case SIMPLE -> printSimple(value, unit);
@@ -149,7 +152,7 @@ private static Duration parseIso8601(String value) {
149152
try {
150153
return Duration.parse(value);
151154
}
152-
catch (Throwable ex) {
155+
catch (Exception ex) {
153156
throw new IllegalArgumentException("'" + value + "' is not a valid ISO-8601 duration", ex);
154157
}
155158
}

spring-context/src/test/java/org/springframework/format/datetime/standard/DurationFormatterUtilsTests.java

+27
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,10 @@
2323

2424
import org.junit.jupiter.api.Nested;
2525
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.params.ParameterizedTest;
27+
import org.junit.jupiter.params.provider.EnumSource;
2628

29+
import org.springframework.format.annotation.DurationFormat;
2730
import org.springframework.format.annotation.DurationFormat.Unit;
2831

2932
import static org.assertj.core.api.Assertions.assertThat;
@@ -188,6 +191,22 @@ void parseCompositeBadUnit() {
188191
.havingCause().withMessage("Does not match composite duration pattern");
189192
}
190193

194+
@ParameterizedTest
195+
@EnumSource(DurationFormat.Style.class)
196+
void parseEmptyStringThrowsForAllStyles(DurationFormat.Style style) {
197+
assertThatIllegalArgumentException()
198+
.isThrownBy(() -> DurationFormatterUtils.parse("", style))
199+
.withMessage("Value must not be empty");
200+
}
201+
202+
@ParameterizedTest
203+
@EnumSource(DurationFormat.Style.class)
204+
void parseNullStringThrowsForAllStyles(DurationFormat.Style style) {
205+
assertThatIllegalArgumentException()
206+
.isThrownBy(() -> DurationFormatterUtils.parse(null, style))
207+
.withMessage("Value must not be empty");
208+
}
209+
191210
@Test
192211
void printSimple() {
193212
assertThat(DurationFormatterUtils.print(Duration.ofNanos(12345), SIMPLE, Unit.NANOS))
@@ -240,6 +259,14 @@ void printCompositeNegative() {
240259
.isEqualTo("-1d2h34m57s28ms3us2ns");
241260
}
242261

262+
@ParameterizedTest
263+
@EnumSource(DurationFormat.Style.class)
264+
void printNullDurationThrowsForAllStyles(DurationFormat.Style style) {
265+
assertThatIllegalArgumentException()
266+
.isThrownBy(() -> DurationFormatterUtils.print(null, style))
267+
.withMessage("Value must not be null");
268+
}
269+
243270
@Test
244271
void detectAndParse() {
245272
assertThat(DurationFormatterUtils.detectAndParse("PT1.234S", Unit.NANOS))

0 commit comments

Comments
 (0)