From 2c3243c93ce1d68151e17b6ee0aee64646cd38fd Mon Sep 17 00:00:00 2001 From: Sam Brannen Date: Mon, 4 Jul 2022 19:20:56 +0200 Subject: [PATCH] Trim string input in PropertyEditors where whitespace is irrelevant Closes gh-28755 --- .../beans/propertyeditors/CharsetEditor.java | 5 ++-- .../beans/propertyeditors/CurrencyEditor.java | 8 ++++++- .../beans/propertyeditors/TimeZoneEditor.java | 6 ++++- .../beans/propertyeditors/ZoneIdEditor.java | 8 ++++++- .../propertyeditors/ZoneIdEditorTests.java | 23 ++++++++++++------- 5 files changed, 37 insertions(+), 13 deletions(-) diff --git a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CharsetEditor.java b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CharsetEditor.java index adcb806e68..ef772db749 100644 --- a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CharsetEditor.java +++ b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CharsetEditor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -29,6 +29,7 @@ import org.springframework.util.StringUtils; * e.g. {@code UTF-8}, {@code ISO-8859-16}, etc. * * @author Arjen Poutsma + * @author Sam Brannen * @since 2.5.4 * @see Charset */ @@ -37,7 +38,7 @@ public class CharsetEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { if (StringUtils.hasText(text)) { - setValue(Charset.forName(text)); + setValue(Charset.forName(text.trim())); } else { setValue(null); diff --git a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CurrencyEditor.java b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CurrencyEditor.java index 0d044ffe11..b6b9d318af 100644 --- a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CurrencyEditor.java +++ b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/CurrencyEditor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2012 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,11 +19,14 @@ package org.springframework.beans.propertyeditors; import java.beans.PropertyEditorSupport; import java.util.Currency; +import org.springframework.util.StringUtils; + /** * Editor for {@code java.util.Currency}, translating currency codes into Currency * objects. Exposes the currency code as text representation of a Currency object. * * @author Juergen Hoeller + * @author Sam Brannen * @since 3.0 * @see java.util.Currency */ @@ -31,6 +34,9 @@ public class CurrencyEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { + if (StringUtils.hasText(text)) { + text = text.trim(); + } setValue(Currency.getInstance(text)); } diff --git a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/TimeZoneEditor.java b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/TimeZoneEditor.java index 6b809169b9..3833c02681 100644 --- a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/TimeZoneEditor.java +++ b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/TimeZoneEditor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2013 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import org.springframework.util.StringUtils; * * @author Juergen Hoeller * @author Nicholas Williams + * @author Sam Brannen * @since 3.0 * @see java.util.TimeZone * @see ZoneIdEditor @@ -36,6 +37,9 @@ public class TimeZoneEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { + if (StringUtils.hasText(text)) { + text = text.trim(); + } setValue(StringUtils.parseTimeZoneString(text)); } diff --git a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/ZoneIdEditor.java b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/ZoneIdEditor.java index 912bdd5fb7..4992e33aeb 100644 --- a/spring-beans/src/main/java/org/springframework/beans/propertyeditors/ZoneIdEditor.java +++ b/spring-beans/src/main/java/org/springframework/beans/propertyeditors/ZoneIdEditor.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2016 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,11 +19,14 @@ package org.springframework.beans.propertyeditors; import java.beans.PropertyEditorSupport; import java.time.ZoneId; +import org.springframework.util.StringUtils; + /** * Editor for {@code java.time.ZoneId}, translating zone ID Strings into {@code ZoneId} * objects. Exposes the {@code TimeZone} ID as a text representation. * * @author Nicholas Williams + * @author Sam Brannen * @since 4.0 * @see java.time.ZoneId * @see TimeZoneEditor @@ -32,6 +35,9 @@ public class ZoneIdEditor extends PropertyEditorSupport { @Override public void setAsText(String text) throws IllegalArgumentException { + if (StringUtils.hasText(text)) { + text = text.trim(); + } setValue(ZoneId.of(text)); } diff --git a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java index 7a9162314b..b62f952fdb 100644 --- a/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java +++ b/spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2019 the original author or authors. + * Copyright 2002-2022 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -19,19 +19,26 @@ package org.springframework.beans.propertyeditors; import java.time.ZoneId; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; import static org.assertj.core.api.Assertions.assertThat; /** * @author Nicholas Williams + * @author Sam Brannen */ -public class ZoneIdEditorTests { +class ZoneIdEditorTests { private final ZoneIdEditor editor = new ZoneIdEditor(); - @Test - public void americaChicago() { - editor.setAsText("America/Chicago"); + @ParameterizedTest(name = "[{index}] text = ''{0}''") + @ValueSource(strings = { + "America/Chicago", + " America/Chicago ", + }) + void americaChicago(String text) { + editor.setAsText(text); ZoneId zoneId = (ZoneId) editor.getValue(); assertThat(zoneId).as("The zone ID should not be null.").isNotNull(); @@ -41,7 +48,7 @@ public class ZoneIdEditorTests { } @Test - public void americaLosAngeles() { + void americaLosAngeles() { editor.setAsText("America/Los_Angeles"); ZoneId zoneId = (ZoneId) editor.getValue(); @@ -52,12 +59,12 @@ public class ZoneIdEditorTests { } @Test - public void getNullAsText() { + void getNullAsText() { assertThat(editor.getAsText()).as("The returned value is not correct.").isEqualTo(""); } @Test - public void getValueAsText() { + void getValueAsText() { editor.setValue(ZoneId.of("America/New_York")); assertThat(editor.getAsText()).as("The text version is not correct.").isEqualTo("America/New_York"); }