Skip to content

Commit 2c3243c

Browse files
committed
Trim string input in PropertyEditors where whitespace is irrelevant
Closes gh-28755
1 parent 2bf5f7a commit 2c3243c

File tree

5 files changed

+37
-13
lines changed

5 files changed

+37
-13
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
}

0 commit comments

Comments
 (0)