From bbdf2ae68606b19dedd331453375fadc9dbc315c Mon Sep 17 00:00:00 2001 From: "mateusz.szablak" Date: Thu, 15 Oct 2020 16:08:49 +0200 Subject: [PATCH 1/3] BAEL-4212 (String) or .toString()? - code + tests --- .../baeldung/tostring/StringCastUtils.java | 10 ++ .../baeldung/tostring/ToStringUnitTest.java | 92 +++++++++++++++++++ 2 files changed, 102 insertions(+) create mode 100644 core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java create mode 100644 core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java diff --git a/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java new file mode 100644 index 0000000000..056f961ea4 --- /dev/null +++ b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java @@ -0,0 +1,10 @@ +package com.baeldung.tostring; + +public class StringCastUtils { + public static String castToString(Object object) { + if (object instanceof String) { + return (String) object; + } + return null; + } +} diff --git a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java new file mode 100644 index 0000000000..88202e1797 --- /dev/null +++ b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java @@ -0,0 +1,92 @@ +package com.baeldung.tostring; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class ToStringUnitTest { + @Test + public void givenString_whenCastToObjectAndString_thenSameAndNoException() { + String input = "baeldung"; + + Object obj = input; + String str = (String) obj; + + assertEquals(obj, str); + assertEquals(str, input); + assertSame(input, str); + } + + @Test(expected = ClassCastException.class) + public void givenIntegerObject_whenCastToObjectAndString_thenCastClassException() { + Integer input = 1234; + + Object obj = input; + String str = (String) obj; + } + + @Test + public void givenNullInteger_whenCastToObjectAndString_thenSameAndNoException() { + Integer input = null; + + Object obj = input; + String str = (String) obj; + + assertEquals(obj, str); + assertEquals(str, input); + assertSame(input, str); + } + + @Test(expected = NullPointerException.class) + public void givenNullInteger_whenToString_thenNullPointerException() { + Integer input = null; + + String str = input.toString(); + } + + @Test + public void givenInteger_whenCastToObject_thenToStringEquals() { + Integer input = 1234; + + Object obj = input; + + assertEquals("1234", input.toString()); + assertEquals("1234", obj.toString()); + assertEquals(input.toString(), obj.toString()); + } + + @Test + public void givenString_whenToString_thenSame() { + String str = "baeldung"; + + assertEquals("baeldung", str.toString()); + assertSame(str, str.toString()); + } + + @Test + public void givenString_whenCastToObject_thenCastToStringReturnsSame() { + String input = "baeldung"; + + Object obj = input; + + assertSame(input, StringCastUtils.castToString(obj)); + } + + @Test + public void givenInteger_whenCastToObject_thenCastToStringReturnsNull() { + Integer input = 1234; + + Object obj = input; + + assertEquals(null, StringCastUtils.castToString(obj)); + } + + @Test + public void givenIntegerNull_whenCastToObject_thenCastToStringReturnsNull() { + Integer input = null; + + Object obj = input; + + assertEquals(null, StringCastUtils.castToString(obj)); + } +} From dd98392dbe80e319aa61ca747a3d7f376d7c354b Mon Sep 17 00:00:00 2001 From: "mateusz.szablak" Date: Mon, 26 Oct 2020 16:35:41 +0100 Subject: [PATCH 2/3] added after review --- .../java/com/baeldung/tostring/StringCastUtils.java | 9 +++++++-- .../java/com/baeldung/tostring/ToStringUnitTest.java | 10 ++++++++++ 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java index 056f961ea4..f59e494845 100644 --- a/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java +++ b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java @@ -2,9 +2,14 @@ package com.baeldung.tostring; public class StringCastUtils { public static String castToString(Object object) { - if (object instanceof String) { + if (object instanceof String) return (String) object; - } + return null; + } + + public static String getToString(Object object) { + if (object != null) + return object.toString(); return null; } } diff --git a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java index 88202e1797..b8af7e4968 100644 --- a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java +++ b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java @@ -89,4 +89,14 @@ public class ToStringUnitTest { assertEquals(null, StringCastUtils.castToString(obj)); } + + @Test + public void givenIntegerNotNull_whenCastToObject_thenGetToStringReturnsString() { + Integer input = 1234; + + Object obj = input; + + assertEquals("1234", StringCastUtils.getToString(obj)); + assertNotSame("1234", StringCastUtils.getToString(obj)); + } } From 46c257f00f718dcd157bd7bfaf8bd8dc254271fe Mon Sep 17 00:00:00 2001 From: "mateusz.szablak" Date: Thu, 29 Oct 2020 16:44:28 +0100 Subject: [PATCH 3/3] review #2 --- .../main/java/com/baeldung/tostring/StringCastUtils.java | 8 +++++--- .../test/java/com/baeldung/tostring/ToStringUnitTest.java | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java index f59e494845..e96c87dfe4 100644 --- a/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java +++ b/core-java-modules/core-java-string-operations-3/src/main/java/com/baeldung/tostring/StringCastUtils.java @@ -2,14 +2,16 @@ package com.baeldung.tostring; public class StringCastUtils { public static String castToString(Object object) { - if (object instanceof String) + if (object instanceof String) { return (String) object; + } return null; } - public static String getToString(Object object) { - if (object != null) + public static String getStringRepresentation(Object object) { + if (object != null) { return object.toString(); + } return null; } } diff --git a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java index b8af7e4968..51a27e1b5c 100644 --- a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java +++ b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/tostring/ToStringUnitTest.java @@ -2,7 +2,9 @@ package com.baeldung.tostring; import org.junit.Test; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotSame; +import static org.junit.Assert.assertSame; public class ToStringUnitTest { @Test @@ -96,7 +98,7 @@ public class ToStringUnitTest { Object obj = input; - assertEquals("1234", StringCastUtils.getToString(obj)); - assertNotSame("1234", StringCastUtils.getToString(obj)); + assertEquals("1234", StringCastUtils.getStringRepresentation(obj)); + assertNotSame("1234", StringCastUtils.getStringRepresentation(obj)); } }