Browse Source

Merge branch '5.3.x'

pull/28759/head
Sam Brannen 4 years ago
parent
commit
338609a168
  1. 5
      spring-beans/src/main/java/org/springframework/beans/propertyeditors/CharsetEditor.java
  2. 8
      spring-beans/src/main/java/org/springframework/beans/propertyeditors/CurrencyEditor.java
  3. 6
      spring-beans/src/main/java/org/springframework/beans/propertyeditors/TimeZoneEditor.java
  4. 8
      spring-beans/src/main/java/org/springframework/beans/propertyeditors/ZoneIdEditor.java
  5. 23
      spring-beans/src/test/java/org/springframework/beans/propertyeditors/ZoneIdEditorTests.java
  6. 35
      spring-core/src/main/java/org/springframework/util/StringUtils.java
  7. 2
      spring-core/src/main/java/org/springframework/util/unit/DataSize.java
  8. 26
      spring-core/src/test/java/org/springframework/util/unit/DataSizeTests.java

5
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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. * e.g. {@code UTF-8}, {@code ISO-8859-16}, etc.
* *
* @author Arjen Poutsma * @author Arjen Poutsma
* @author Sam Brannen
* @since 2.5.4 * @since 2.5.4
* @see Charset * @see Charset
*/ */
@ -37,7 +38,7 @@ public class CharsetEditor extends PropertyEditorSupport {
@Override @Override
public void setAsText(String text) throws IllegalArgumentException { public void setAsText(String text) throws IllegalArgumentException {
if (StringUtils.hasText(text)) { if (StringUtils.hasText(text)) {
setValue(Charset.forName(text)); setValue(Charset.forName(text.trim()));
} }
else { else {
setValue(null); setValue(null);

8
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.beans.PropertyEditorSupport;
import java.util.Currency; import java.util.Currency;
import org.springframework.util.StringUtils;
/** /**
* Editor for {@code java.util.Currency}, translating currency codes into Currency * Editor for {@code java.util.Currency}, translating currency codes into Currency
* objects. Exposes the currency code as text representation of a Currency object. * objects. Exposes the currency code as text representation of a Currency object.
* *
* @author Juergen Hoeller * @author Juergen Hoeller
* @author Sam Brannen
* @since 3.0 * @since 3.0
* @see java.util.Currency * @see java.util.Currency
*/ */
@ -31,6 +34,9 @@ public class CurrencyEditor extends PropertyEditorSupport {
@Override @Override
public void setAsText(String text) throws IllegalArgumentException { public void setAsText(String text) throws IllegalArgumentException {
if (StringUtils.hasText(text)) {
text = text.trim();
}
setValue(Currency.getInstance(text)); setValue(Currency.getInstance(text));
} }

6
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 Juergen Hoeller
* @author Nicholas Williams * @author Nicholas Williams
* @author Sam Brannen
* @since 3.0 * @since 3.0
* @see java.util.TimeZone * @see java.util.TimeZone
* @see ZoneIdEditor * @see ZoneIdEditor
@ -36,6 +37,9 @@ public class TimeZoneEditor extends PropertyEditorSupport {
@Override @Override
public void setAsText(String text) throws IllegalArgumentException { public void setAsText(String text) throws IllegalArgumentException {
if (StringUtils.hasText(text)) {
text = text.trim();
}
setValue(StringUtils.parseTimeZoneString(text)); setValue(StringUtils.parseTimeZoneString(text));
} }

8
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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.beans.PropertyEditorSupport;
import java.time.ZoneId; import java.time.ZoneId;
import org.springframework.util.StringUtils;
/** /**
* Editor for {@code java.time.ZoneId}, translating zone ID Strings into {@code ZoneId} * Editor for {@code java.time.ZoneId}, translating zone ID Strings into {@code ZoneId}
* objects. Exposes the {@code TimeZone} ID as a text representation. * objects. Exposes the {@code TimeZone} ID as a text representation.
* *
* @author Nicholas Williams * @author Nicholas Williams
* @author Sam Brannen
* @since 4.0 * @since 4.0
* @see java.time.ZoneId * @see java.time.ZoneId
* @see TimeZoneEditor * @see TimeZoneEditor
@ -32,6 +35,9 @@ public class ZoneIdEditor extends PropertyEditorSupport {
@Override @Override
public void setAsText(String text) throws IllegalArgumentException { public void setAsText(String text) throws IllegalArgumentException {
if (StringUtils.hasText(text)) {
text = text.trim();
}
setValue(ZoneId.of(text)); setValue(ZoneId.of(text));
} }

23
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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 java.time.ZoneId;
import org.junit.jupiter.api.Test; 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; import static org.assertj.core.api.Assertions.assertThat;
/** /**
* @author Nicholas Williams * @author Nicholas Williams
* @author Sam Brannen
*/ */
public class ZoneIdEditorTests { class ZoneIdEditorTests {
private final ZoneIdEditor editor = new ZoneIdEditor(); private final ZoneIdEditor editor = new ZoneIdEditor();
@Test @ParameterizedTest(name = "[{index}] text = ''{0}''")
public void americaChicago() { @ValueSource(strings = {
editor.setAsText("America/Chicago"); "America/Chicago",
" America/Chicago ",
})
void americaChicago(String text) {
editor.setAsText(text);
ZoneId zoneId = (ZoneId) editor.getValue(); ZoneId zoneId = (ZoneId) editor.getValue();
assertThat(zoneId).as("The zone ID should not be null.").isNotNull(); assertThat(zoneId).as("The zone ID should not be null.").isNotNull();
@ -41,7 +48,7 @@ public class ZoneIdEditorTests {
} }
@Test @Test
public void americaLosAngeles() { void americaLosAngeles() {
editor.setAsText("America/Los_Angeles"); editor.setAsText("America/Los_Angeles");
ZoneId zoneId = (ZoneId) editor.getValue(); ZoneId zoneId = (ZoneId) editor.getValue();
@ -52,12 +59,12 @@ public class ZoneIdEditorTests {
} }
@Test @Test
public void getNullAsText() { void getNullAsText() {
assertThat(editor.getAsText()).as("The returned value is not correct.").isEqualTo(""); assertThat(editor.getAsText()).as("The returned value is not correct.").isEqualTo("");
} }
@Test @Test
public void getValueAsText() { void getValueAsText() {
editor.setValue(ZoneId.of("America/New_York")); editor.setValue(ZoneId.of("America/New_York"));
assertThat(editor.getAsText()).as("The text version is not correct.").isEqualTo("America/New_York"); assertThat(editor.getAsText()).as("The text version is not correct.").isEqualTo("America/New_York");
} }

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

@ -232,21 +232,23 @@ public abstract class StringUtils {
} }
/** /**
* Trim <i>all</i> whitespace from the given {@code String}: * Trim <em>all</em> whitespace from the given {@code CharSequence}:
* leading, trailing, and in between characters. * leading, trailing, and in between characters.
* @param str the {@code String} to check * @param text the {@code CharSequence} to check
* @return the trimmed {@code String} * @return the trimmed {@code CharSequence}
* @since 5.3.22
* @see #trimAllWhitespace(String)
* @see java.lang.Character#isWhitespace * @see java.lang.Character#isWhitespace
*/ */
public static String trimAllWhitespace(String str) { public static CharSequence trimAllWhitespace(CharSequence text) {
if (!hasLength(str)) { if (!hasLength(text)) {
return str; return text;
} }
int len = str.length(); int len = text.length();
StringBuilder sb = new StringBuilder(str.length()); StringBuilder sb = new StringBuilder(text.length());
for (int i = 0; i < len; i++) { for (int i = 0; i < len; i++) {
char c = str.charAt(i); char c = text.charAt(i);
if (!Character.isWhitespace(c)) { if (!Character.isWhitespace(c)) {
sb.append(c); sb.append(c);
} }
@ -254,6 +256,21 @@ public abstract class StringUtils {
return sb.toString(); return sb.toString();
} }
/**
* Trim <em>all</em> whitespace from the given {@code String}:
* leading, trailing, and in between characters.
* @param str the {@code String} to check
* @return the trimmed {@code String}
* @see #trimAllWhitespace(CharSequence)
* @see java.lang.Character#isWhitespace
*/
public static String trimAllWhitespace(String str) {
if (str == null) {
return null;
}
return trimAllWhitespace((CharSequence) str).toString();
}
/** /**
* Trim leading whitespace from the given {@code String}. * Trim leading whitespace from the given {@code String}.
* @param str the {@code String} to check * @param str the {@code String} to check

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

@ -174,7 +174,7 @@ public final class DataSize implements Comparable<DataSize>, Serializable {
public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit) { public static DataSize parse(CharSequence text, @Nullable DataUnit defaultUnit) {
Assert.notNull(text, "Text must not be null"); Assert.notNull(text, "Text must not be null");
try { try {
Matcher matcher = DataSizeUtils.PATTERN.matcher(text); Matcher matcher = DataSizeUtils.PATTERN.matcher(StringUtils.trimAllWhitespace(text));
Assert.state(matcher.matches(), "Does not match data size pattern"); Assert.state(matcher.matches(), "Does not match data size pattern");
DataUnit unit = DataSizeUtils.determineDataUnit(matcher.group(2), defaultUnit); DataUnit unit = DataSizeUtils.determineDataUnit(matcher.group(2), defaultUnit);
long amount = Long.parseLong(matcher.group(1)); long amount = Long.parseLong(matcher.group(1));

26
spring-core/src/test/java/org/springframework/util/unit/DataSizeTests.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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -17,6 +17,8 @@
package org.springframework.util.unit; package org.springframework.util.unit;
import org.junit.jupiter.api.Test; 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; import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
@ -25,6 +27,7 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
* Tests for {@link DataSize}. * Tests for {@link DataSize}.
* *
* @author Stephane Nicoll * @author Stephane Nicoll
* @author Sam Brannen
*/ */
class DataSizeTests { class DataSizeTests {
@ -128,9 +131,17 @@ class DataSizeTests {
assertThat(DataSize.parse("-1", DataUnit.KILOBYTES)).isEqualTo(DataSize.ofKilobytes(-1)); assertThat(DataSize.parse("-1", DataUnit.KILOBYTES)).isEqualTo(DataSize.ofKilobytes(-1));
} }
@Test @ParameterizedTest(name = "[{index}] text = ''{0}''")
void parseWithBytes() { @ValueSource(strings = {
assertThat(DataSize.parse("1024B")).isEqualTo(DataSize.ofKilobytes(1)); "1024B",
"1024 B",
"1024B ",
" 1024B",
" 1024B ",
"\t1024 B\t"
})
void parseWithBytes(CharSequence text) {
assertThat(DataSize.parse(text)).isEqualTo(DataSize.ofKilobytes(1));
} }
@Test @Test
@ -210,9 +221,12 @@ class DataSizeTests {
@Test @Test
void parseWithUnsupportedUnit() { void parseWithUnsupportedUnit() {
assertThatIllegalArgumentException().isThrownBy(() -> assertThatIllegalArgumentException()
DataSize.parse("3WB")) .isThrownBy(() -> DataSize.parse("3WB"))
.withMessage("'3WB' is not a valid data size"); .withMessage("'3WB' is not a valid data size");
assertThatIllegalArgumentException()
.isThrownBy(() -> DataSize.parse("3 WB"))
.withMessage("'3 WB' is not a valid data size");
} }
} }

Loading…
Cancel
Save