Skip to content

Commit e2238c0

Browse files
committed
Polish "Reject empty strings in DurationFormatterUtils"
See gh-33669
1 parent 02c990c commit e2238c0

File tree

2 files changed

+16
-26
lines changed

2 files changed

+16
-26
lines changed

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

-2
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
* @author Phillip Webb
3636
* @author Valentine Wu
3737
* @author Simon Baslé
38-
* @author Kim Seungrae
3938
* @since 6.2
4039
*/
4140
public abstract class DurationFormatterUtils {
@@ -90,7 +89,6 @@ public static String print(Duration value, DurationFormat.Style style) {
9089
* @return the printed result
9190
*/
9291
public static String print(Duration value, DurationFormat.Style style, @Nullable DurationFormat.Unit unit) {
93-
Assert.notNull(value, "Value must not be null");
9492
return switch (style) {
9593
case ISO8601 -> value.toString();
9694
case SIMPLE -> printSimple(value, unit);

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

+16-24
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,22 @@
4141
*/
4242
class DurationFormatterUtilsTests {
4343

44+
@ParameterizedTest
45+
@EnumSource(DurationFormat.Style.class)
46+
void parseEmptyStringFailsWithDedicatedException(DurationFormat.Style style) {
47+
assertThatIllegalArgumentException()
48+
.isThrownBy(() -> DurationFormatterUtils.parse("", style))
49+
.withMessage("Value must not be empty");
50+
}
51+
52+
@ParameterizedTest
53+
@EnumSource(DurationFormat.Style.class)
54+
void parseNullStringFailsWithDedicatedException(DurationFormat.Style style) {
55+
assertThatIllegalArgumentException()
56+
.isThrownBy(() -> DurationFormatterUtils.parse(null, style))
57+
.withMessage("Value must not be empty");
58+
}
59+
4460
@Test
4561
void parseSimpleWithUnits() {
4662
Duration nanos = DurationFormatterUtils.parse("1ns", SIMPLE, Unit.SECONDS);
@@ -191,22 +207,6 @@ void parseCompositeBadUnit() {
191207
.havingCause().withMessage("Does not match composite duration pattern");
192208
}
193209

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-
210210
@Test
211211
void printSimple() {
212212
assertThat(DurationFormatterUtils.print(Duration.ofNanos(12345), SIMPLE, Unit.NANOS))
@@ -259,14 +259,6 @@ void printCompositeNegative() {
259259
.isEqualTo("-1d2h34m57s28ms3us2ns");
260260
}
261261

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-
270262
@Test
271263
void detectAndParse() {
272264
assertThat(DurationFormatterUtils.detectAndParse("PT1.234S", Unit.NANOS))

0 commit comments

Comments
 (0)