From e93151364afb32997b2261663995b9b9d0f35e5a Mon Sep 17 00:00:00 2001 From: Ken Partlow Date: Sun, 11 Feb 2024 16:43:52 -0500 Subject: [PATCH] Removed Trim on String -> Character conversion --- .../util/convert/StringConversions.java | 4 +-- .../util/convert/StringConversionsTests.java | 26 +++++++++++++++++++ 2 files changed, 28 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/cedarsoftware/util/convert/StringConversions.java b/src/main/java/com/cedarsoftware/util/convert/StringConversions.java index e7991697..351addd8 100644 --- a/src/main/java/com/cedarsoftware/util/convert/StringConversions.java +++ b/src/main/java/com/cedarsoftware/util/convert/StringConversions.java @@ -204,8 +204,8 @@ static Boolean toBoolean(Object from, Converter converter) { } static char toCharacter(Object from, Converter converter) { - String str = StringUtilities.trimToNull(asString(from)); - if (str == null) { + String str = (String)from; + if (str == null || str.isEmpty()) { return CommonValues.CHARACTER_ZERO; } if (str.length() == 1) { diff --git a/src/test/java/com/cedarsoftware/util/convert/StringConversionsTests.java b/src/test/java/com/cedarsoftware/util/convert/StringConversionsTests.java index 7ecf93b6..f3d2e786 100644 --- a/src/test/java/com/cedarsoftware/util/convert/StringConversionsTests.java +++ b/src/test/java/com/cedarsoftware/util/convert/StringConversionsTests.java @@ -81,6 +81,7 @@ private static Stream toYear_extremeParams() { ); } + @ParameterizedTest @MethodSource("toYear_extremeParams") void toYear_withExtremeParams_returnsValue(String source, int value) { @@ -89,6 +90,31 @@ void toYear_withExtremeParams_returnsValue(String source, int value) { assertThat(actual).isEqualTo(expected); } + private static Stream toCharParams() { + return Stream.of( + Arguments.of("0000", '\u0000'), + Arguments.of("65", 'A'), + Arguments.of("\t", '\t'), + Arguments.of("\u0005", '\u0005') + ); + } + + @ParameterizedTest + @MethodSource("toCharParams") + void toChar(String source, char value) { + char actual = this.converter.convert(source, char.class); + //System.out.println(Integer.toHexString(actual) + " = " + Integer.toHexString(value)); + assertThat(actual).isEqualTo(value); + } + + @ParameterizedTest + @MethodSource("toCharParams") + void toChar(String source, Character value) { + Character actual = this.converter.convert(source, Character.class); + //System.out.println(Integer.toHexString(actual) + " = " + Integer.toHexString(value)); + assertThat(actual).isEqualTo(value); + } + private static Stream toCharSequenceTypes() { return Stream.of( Arguments.of(StringBuffer.class),