Skip to content

Commit 338609a

Browse files
committed
Merge branch '5.3.x'
2 parents a92b427 + 2c3243c commit 338609a

File tree

8 files changed

+84
-29
lines changed

8 files changed

+84
-29
lines changed

spring-beans/src/main/java/org/springframework/beans/propertyeditors/CharsetEditor.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 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.
@@ -29,6 +29,7 @@
2929
* e.g. {@code UTF-8}, {@code ISO-8859-16}, etc.
3030
*
3131
* @author Arjen Poutsma
32+
* @author Sam Brannen
3233
* @since 2.5.4
3334
* @see Charset
3435
*/
@@ -37,7 +38,7 @@ public class CharsetEditor extends PropertyEditorSupport {
3738
@Override
3839
public void setAsText(String text) throws IllegalArgumentException {
3940
if (StringUtils.hasText(text)) {
40-
setValue(Charset.forName(text));
41+
setValue(Charset.forName(text.trim()));
4142
}
4243
else {
4344
setValue(null);

spring-beans/src/main/java/org/springframework/beans/propertyeditors/CurrencyEditor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2012 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.
@@ -19,18 +19,24 @@
1919
import java.beans.PropertyEditorSupport;
2020
import java.util.Currency;
2121

22+
import org.springframework.util.StringUtils;
23+
2224
/**
2325
* Editor for {@code java.util.Currency}, translating currency codes into Currency
2426
* objects. Exposes the currency code as text representation of a Currency object.
2527
*
2628
* @author Juergen Hoeller
29+
* @author Sam Brannen
2730
* @since 3.0
2831
* @see java.util.Currency
2932
*/
3033
public class CurrencyEditor extends PropertyEditorSupport {
3134

3235
@Override
3336
public void setAsText(String text) throws IllegalArgumentException {
37+
if (StringUtils.hasText(text)) {
38+
text = text.trim();
39+
}
3440
setValue(Currency.getInstance(text));
3541
}
3642

spring-beans/src/main/java/org/springframework/beans/propertyeditors/TimeZoneEditor.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 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.
@@ -28,6 +28,7 @@
2828
*
2929
* @author Juergen Hoeller
3030
* @author Nicholas Williams
31+
* @author Sam Brannen
3132
* @since 3.0
3233
* @see java.util.TimeZone
3334
* @see ZoneIdEditor
@@ -36,6 +37,9 @@ public class TimeZoneEditor extends PropertyEditorSupport {
3637

3738
@Override
3839
public void setAsText(String text) throws IllegalArgumentException {
40+
if (StringUtils.hasText(text)) {
41+
text = text.trim();
42+
}
3943
setValue(StringUtils.parseTimeZoneString(text));
4044
}
4145

spring-beans/src/main/java/org/springframework/beans/propertyeditors/ZoneIdEditor.java

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 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.
@@ -19,11 +19,14 @@
1919
import java.beans.PropertyEditorSupport;
2020
import java.time.ZoneId;
2121

22+
import org.springframework.util.StringUtils;
23+
2224
/**
2325
* Editor for {@code java.time.ZoneId}, translating zone ID Strings into {@code ZoneId}
2426
* objects. Exposes the {@code TimeZone} ID as a text representation.
2527
*
2628
* @author Nicholas Williams
29+
* @author Sam Brannen
2730
* @since 4.0
2831
* @see java.time.ZoneId
2932
* @see TimeZoneEditor
@@ -32,6 +35,9 @@ public class ZoneIdEditor extends PropertyEditorSupport {
3235

3336
@Override
3437
public void setAsText(String text) throws IllegalArgumentException {
38+
if (StringUtils.hasText(text)) {
39+
text = text.trim();
40+
}
3541
setValue(ZoneId.of(text));
3642
}
3743

spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java

Lines changed: 15 additions & 8 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.
@@ -19,19 +19,26 @@
1919
import java.time.ZoneId;
2020

2121
import org.junit.jupiter.api.Test;
22+
import org.junit.jupiter.params.ParameterizedTest;
23+
import org.junit.jupiter.params.provider.ValueSource;
2224

2325
import static org.assertj.core.api.Assertions.assertThat;
2426

2527
/**
2628
* @author Nicholas Williams
29+
* @author Sam Brannen
2730
*/
28-
public class ZoneIdEditorTests {
31+
class ZoneIdEditorTests {
2932

3033
private final ZoneIdEditor editor = new ZoneIdEditor();
3134

32-
@Test
33-
public void americaChicago() {
34-
editor.setAsText("America/Chicago");
35+
@ParameterizedTest(name = "[{index}] text = ''{0}''")
36+
@ValueSource(strings = {
37+
"America/Chicago",
38+
" America/Chicago ",
39+
})
40+
void americaChicago(String text) {
41+
editor.setAsText(text);
3542

3643
ZoneId zoneId = (ZoneId) editor.getValue();
3744
assertThat(zoneId).as("The zone ID should not be null.").isNotNull();
@@ -41,7 +48,7 @@ public void americaChicago() {
4148
}
4249

4350
@Test
44-
public void americaLosAngeles() {
51+
void americaLosAngeles() {
4552
editor.setAsText("America/Los_Angeles");
4653

4754
ZoneId zoneId = (ZoneId) editor.getValue();
@@ -52,12 +59,12 @@ public void americaLosAngeles() {
5259
}
5360

5461
@Test
55-
public void getNullAsText() {
62+
void getNullAsText() {
5663
assertThat(editor.getAsText()).as("The returned value is not correct.").isEqualTo("");
5764
}
5865

5966
@Test
60-
public void getValueAsText() {
67+
void getValueAsText() {
6168
editor.setValue(ZoneId.of("America/New_York"));
6269
assertThat(editor.getAsText()).as("The text version is not correct.").isEqualTo("America/New_York");
6370
}

spring-core/src/main/java/org/springframework/util/StringUtils.java

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -232,28 +232,45 @@ public static String trimWhitespace(String str) {
232232
}
233233

234234
/**
235-
* Trim <i>all</i> whitespace from the given {@code String}:
235+
* Trim <em>all</em> whitespace from the given {@code CharSequence}:
236236
* leading, trailing, and in between characters.
237-
* @param str the {@code String} to check
238-
* @return the trimmed {@code String}
237+
* @param text the {@code CharSequence} to check
238+
* @return the trimmed {@code CharSequence}
239+
* @since 5.3.22
240+
* @see #trimAllWhitespace(String)
239241
* @see java.lang.Character#isWhitespace
240242
*/
241-
public static String trimAllWhitespace(String str) {
242-
if (!hasLength(str)) {
243-
return str;
243+
public static CharSequence trimAllWhitespace(CharSequence text) {
244+
if (!hasLength(text)) {
245+
return text;
244246
}
245247

246-
int len = str.length();
247-
StringBuilder sb = new StringBuilder(str.length());
248+
int len = text.length();
249+
StringBuilder sb = new StringBuilder(text.length());
248250
for (int i = 0; i < len; i++) {
249-
char c = str.charAt(i);
251+
char c = text.charAt(i);
250252
if (!Character.isWhitespace(c)) {
251253
sb.append(c);
252254
}
253255
}
254256
return sb.toString();
255257
}
256258

259+
/**
260+
* Trim <em>all</em> whitespace from the given {@code String}:
261+
* leading, trailing, and in between characters.
262+
* @param str the {@code String} to check
263+
* @return the trimmed {@code String}
264+
* @see #trimAllWhitespace(CharSequence)
265+
* @see java.lang.Character#isWhitespace
266+
*/
267+
public static String trimAllWhitespace(String str) {
268+
if (str == null) {
269+
return null;
270+
}
271+
return trimAllWhitespace((CharSequence) str).toString();
272+
}
273+
257274
/**
258275
* Trim leading whitespace from the given {@code String}.
259276
* @param str the {@code String} to check

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)