Skip to content

Commit 2bf5f7a

Browse files
committed
Introduce lenient parsing in DataSize regarding whitespace
Prior to this commit, a DataSize input string could not be parsed if it contained any whitespace. With this commit, a DataSize input string can contain leading, trailing, or 'in between' whitespace. For example, the following will be parsed to the same DataSize value. - "1024B" - "1024 B" - " 1024B " - " 1024 B " Closes gh-28643
1 parent bf39492 commit 2bf5f7a

File tree

2 files changed

+21
-7
lines changed

2 files changed

+21
-7
lines changed

spring-core/src/main/java/org/springframework/util/unit/DataSize.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public static DataSize parse(CharSequence text) {
174174
public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit) {
175175
Assert.notNull(text, "Text must not be null");
176176
try {
177-
Matcher matcher = DataSizeUtils.PATTERN.matcher(text);
177+
Matcher matcher = DataSizeUtils.PATTERN.matcher(StringUtils.trimAllWhitespace(text));
178178
Assert.state(matcher.matches(), "Does not match data size pattern");
179179
DataUnit unit = DataSizeUtils.determineDataUnit(matcher.group(2), defaultUnit);
180180
long amount = Long.parseLong(matcher.group(1));

spring-core/src/test/java/org/springframework/util/unit/DataSizeTests.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2022 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,6 +17,8 @@
1717
package org.springframework.util.unit;
1818

1919
import org.junit.jupiter.api.Test;
20+
import org.junit.jupiter.params.ParameterizedTest;
21+
import org.junit.jupiter.params.provider.ValueSource;
2022

2123
import static org.assertj.core.api.Assertions.assertThat;
2224
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@@ -25,6 +27,7 @@
2527
* Tests for {@link DataSize}.
2628
*
2729
* @author Stephane Nicoll
30+
* @author Sam Brannen
2831
*/
2932
class DataSizeTests {
3033

@@ -128,9 +131,17 @@ void parseNegativeNumberWithCustomDefaultUnit() {
128131
assertThat(DataSize.parse("-1", DataUnit.KILOBYTES)).isEqualTo(DataSize.ofKilobytes(-1));
129132
}
130133

131-
@Test
132-
void parseWithBytes() {
133-
assertThat(DataSize.parse("1024B")).isEqualTo(DataSize.ofKilobytes(1));
134+
@ParameterizedTest(name = "[{index}] text = ''{0}''")
135+
@ValueSource(strings = {
136+
"1024B",
137+
"1024 B",
138+
"1024B ",
139+
" 1024B",
140+
" 1024B ",
141+
"\t1024 B\t"
142+
})
143+
void parseWithBytes(CharSequence text) {
144+
assertThat(DataSize.parse(text)).isEqualTo(DataSize.ofKilobytes(1));
134145
}
135146

136147
@Test
@@ -210,9 +221,12 @@ void toStringWithNegativeBytes() {
210221

211222
@Test
212223
void parseWithUnsupportedUnit() {
213-
assertThatIllegalArgumentException().isThrownBy(() ->
214-
DataSize.parse("3WB"))
224+
assertThatIllegalArgumentException()
225+
.isThrownBy(() -> DataSize.parse("3WB"))
215226
.withMessage("'3WB' is not a valid data size");
227+
assertThatIllegalArgumentException()
228+
.isThrownBy(() -> DataSize.parse("3 WB"))
229+
.withMessage("'3 WB' is not a valid data size");
216230
}
217231

218232
}

0 commit comments

Comments
 (0)