From c30bd6caa75287552bfb424d07ac717c90c9ca13 Mon Sep 17 00:00:00 2001 From: Nikhil Khatwani Date: Sat, 12 Nov 2016 13:42:01 +0530 Subject: [PATCH 1/3] BAEL-29 Refactored Code to comeup with Unit Test --- .../java/conversion/StringConversion.java | 60 ---------- .../java/conversion/StringConversionTest.java | 104 ++++++++++++++++++ 2 files changed, 104 insertions(+), 60 deletions(-) delete mode 100644 core-java/src/main/java/com/baeldung/java/conversion/StringConversion.java create mode 100644 core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java diff --git a/core-java/src/main/java/com/baeldung/java/conversion/StringConversion.java b/core-java/src/main/java/com/baeldung/java/conversion/StringConversion.java deleted file mode 100644 index 0a0f79566e..0000000000 --- a/core-java/src/main/java/com/baeldung/java/conversion/StringConversion.java +++ /dev/null @@ -1,60 +0,0 @@ -package com.baeldung.java.conversion; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.time.LocalDateTime; -import java.util.Date; - -import com.baeldung.datetime.UseLocalDateTime; - -public class StringConversion { - - public static int getInt(String str) { - return Integer.parseInt(str); - } - - public static int getInteger(String str) { - return Integer.valueOf(str); - } - - public static long getLongPrimitive(String str) { - return Long.parseLong(str); - } - - public static Long getLong(String str) { - return Long.valueOf(str); - } - - public static double getDouble(String str) { - return Double.parseDouble(str); - } - - public static double getDoublePrimitive(String str) { - return Double.valueOf(str); - } - - public static byte[] getByteArray(String str) { - return str.getBytes(); - } - - public static char[] getCharArray(String str) { - return str.toCharArray(); - } - - public static boolean getBooleanPrimitive(String str) { - return Boolean.parseBoolean(str); - } - - public static boolean getBoolean(String str) { - return Boolean.valueOf(str); - } - - public static Date getJava6Date(String str, String format) throws ParseException { - SimpleDateFormat formatter = new SimpleDateFormat(format); - return formatter.parse(str); - } - - public static LocalDateTime getJava8Date(String str) throws ParseException { - return new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str); - } -} diff --git a/core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java b/core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java new file mode 100644 index 0000000000..731f9a2a6e --- /dev/null +++ b/core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java @@ -0,0 +1,104 @@ +package com.baeldung.java.conversion; + +import static org.junit.Assert.assertEquals; + +import java.io.UnsupportedEncodingException; +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.time.LocalDateTime; +import java.time.Month; +import java.util.Arrays; +import java.util.Calendar; +import java.util.Date; +import java.util.GregorianCalendar; + +import org.junit.Test; + +import com.baeldung.datetime.UseLocalDateTime; + +public class StringConversionTest { + + @Test + public void givenString_whenConvertedToint_thenCorrect() { + String str = "1"; + assertEquals(Integer.parseInt(str), 1); + } + + @Test + public void givenString_whenConvertedToInteger_thenCorrect() { + String str = "12"; + Integer number = Integer.valueOf(str); + assertEquals(number.intValue(), 12); + } + + @Test + public void givenString_whenConvertedTolong_thenCorrect() { + String str = "12345"; + assertEquals(Long.parseLong(str), 12345); + } + + @Test + public void givenString_whenConvertedToLong_thenCorrect() { + String str = "14567"; + Long number = Long.valueOf(str); + assertEquals(number.longValue(), 14567); + } + + @Test + public void givenString_whenConvertedTodouble_thenCorrect() { + String str = "1.4"; + assertEquals(Double.parseDouble(str), 1.4, 0.0); + } + + @Test + public void givenString_whenConvertedToDouble_thenCorrect() { + String str = "145.67"; + Double number = Double.valueOf(str); + assertEquals(number.doubleValue(), 145.67, 0.0); + } + + @Test + public void givenString_whenConvertedToByteArray_thenCorrect() throws UnsupportedEncodingException { + byte[] byteArray1 = new byte[] { 'a', 'b', 'c' }; + String str = new String(byteArray1, "UTF-8"); + assertEquals(Arrays.equals(str.getBytes(), byteArray1), true); + } + + @Test + public void givenString_whenConvertedToboolean_thenCorrect() { + String str = "true"; + assertEquals(Boolean.parseBoolean(str), true); + } + + @Test + public void givenString_whenConvertedToBoolean_thenCorrect() { + String str = "true"; + assertEquals(Boolean.valueOf(str), true); + } + + @Test + public void givenString_whenConvertedToCharArray_thenCorrect() { + String str = "hello"; + char[] charArray = { 'h', 'e', 'l', 'l', 'o' }; + assertEquals(Arrays.equals(charArray, str.toCharArray()), true); + } + + @Test + public void givenString_whenConvertedToJava6Date_thenCorrect() throws ParseException { + String str = "15/10/2013"; + SimpleDateFormat formatter = new SimpleDateFormat("dd/M/yyyy"); + Date date1 = formatter.parse(str); + Calendar calendar = new GregorianCalendar(2013, 9, 15); + Date date2 = calendar.getTime(); + assertEquals(date1.compareTo(date2), 0); + } + + @Test + public void givenString_whenConvertedToJava8Date_thenCorrect() throws ParseException { + String str = "2007-12-03T10:15:30"; + LocalDateTime localDateTime = new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str); + assertEquals(localDateTime.getDayOfMonth(), 3); + assertEquals(localDateTime.getMonth(), Month.DECEMBER); + assertEquals(localDateTime.getYear(), 2007); + } +} From 633162a04bc5317dbe949085eae62e337e677320 Mon Sep 17 00:00:00 2001 From: Nikhil Khatwani Date: Sat, 12 Nov 2016 15:03:07 +0530 Subject: [PATCH 2/3] Refactoring Code - short and simple --- .../java/conversion/StringConversionTest.java | 59 ++++++++----------- 1 file changed, 26 insertions(+), 33 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java b/core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java index 731f9a2a6e..60daf21091 100644 --- a/core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java +++ b/core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java @@ -19,84 +19,77 @@ import com.baeldung.datetime.UseLocalDateTime; public class StringConversionTest { @Test - public void givenString_whenConvertedToint_thenCorrect() { - String str = "1"; - assertEquals(Integer.parseInt(str), 1); + public void whenConvertedToint_thenCorrect() { + assertEquals(Integer.parseInt("1"), 1); } @Test - public void givenString_whenConvertedToInteger_thenCorrect() { - String str = "12"; - Integer number = Integer.valueOf(str); - assertEquals(number.intValue(), 12); + public void whenConvertedToInteger_thenCorrect() { + assertEquals(Integer.valueOf("12").equals(12), true); } @Test - public void givenString_whenConvertedTolong_thenCorrect() { - String str = "12345"; - assertEquals(Long.parseLong(str), 12345); + public void whenConvertedTolong_thenCorrect() { + assertEquals(Long.parseLong("12345"), 12345); } @Test - public void givenString_whenConvertedToLong_thenCorrect() { - String str = "14567"; - Long number = Long.valueOf(str); - assertEquals(number.longValue(), 14567); + public void whenConvertedToLong_thenCorrect() { + assertEquals(Long.valueOf("14567").equals(14567L), true); } @Test - public void givenString_whenConvertedTodouble_thenCorrect() { - String str = "1.4"; - assertEquals(Double.parseDouble(str), 1.4, 0.0); + public void whenConvertedTodouble_thenCorrect() { + assertEquals(Double.parseDouble("1.4"), 1.4, 0.0); } @Test - public void givenString_whenConvertedToDouble_thenCorrect() { - String str = "145.67"; - Double number = Double.valueOf(str); - assertEquals(number.doubleValue(), 145.67, 0.0); + public void whenConvertedToDouble_thenCorrect() { + assertEquals(Double.valueOf("145.67").equals(145.67d), true); } @Test - public void givenString_whenConvertedToByteArray_thenCorrect() throws UnsupportedEncodingException { + public void whenConvertedToByteArray_thenCorrect() throws UnsupportedEncodingException { byte[] byteArray1 = new byte[] { 'a', 'b', 'c' }; - String str = new String(byteArray1, "UTF-8"); - assertEquals(Arrays.equals(str.getBytes(), byteArray1), true); + String string = new String(byteArray1, "UTF-8"); + + assertEquals(Arrays.equals(string.getBytes(), byteArray1), true); } @Test - public void givenString_whenConvertedToboolean_thenCorrect() { - String str = "true"; - assertEquals(Boolean.parseBoolean(str), true); + public void whenConvertedToboolean_thenCorrect() { + assertEquals(Boolean.parseBoolean("true"), true); } @Test - public void givenString_whenConvertedToBoolean_thenCorrect() { - String str = "true"; - assertEquals(Boolean.valueOf(str), true); + public void whenConvertedToBoolean_thenCorrect() { + assertEquals(Boolean.valueOf("true"), true); } @Test - public void givenString_whenConvertedToCharArray_thenCorrect() { + public void whenConvertedToCharArray_thenCorrect() { String str = "hello"; char[] charArray = { 'h', 'e', 'l', 'l', 'o' }; + assertEquals(Arrays.equals(charArray, str.toCharArray()), true); } @Test - public void givenString_whenConvertedToJava6Date_thenCorrect() throws ParseException { + public void whenConvertedToJava6Date_thenCorrect() throws ParseException { String str = "15/10/2013"; SimpleDateFormat formatter = new SimpleDateFormat("dd/M/yyyy"); Date date1 = formatter.parse(str); Calendar calendar = new GregorianCalendar(2013, 9, 15); Date date2 = calendar.getTime(); + assertEquals(date1.compareTo(date2), 0); } @Test - public void givenString_whenConvertedToJava8Date_thenCorrect() throws ParseException { + public void whenConvertedToJava8Date_thenCorrect() throws ParseException { String str = "2007-12-03T10:15:30"; LocalDateTime localDateTime = new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str); + assertEquals(localDateTime.getDayOfMonth(), 3); assertEquals(localDateTime.getMonth(), Month.DECEMBER); assertEquals(localDateTime.getYear(), 2007); From b58c2341da2c216cedacab8003797e29f544ea8d Mon Sep 17 00:00:00 2001 From: Nikhil Khatwani Date: Sat, 12 Nov 2016 16:01:53 +0530 Subject: [PATCH 3/3] Refactoring Code - Naming corrections --- .../com/baeldung/java/conversion/StringConversionTest.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java b/core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java index 60daf21091..d4786f54ac 100644 --- a/core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java +++ b/core-java/src/test/java/com/baeldung/java/conversion/StringConversionTest.java @@ -19,7 +19,7 @@ import com.baeldung.datetime.UseLocalDateTime; public class StringConversionTest { @Test - public void whenConvertedToint_thenCorrect() { + public void whenConvertedToInt_thenCorrect() { assertEquals(Integer.parseInt("1"), 1); } @@ -75,7 +75,7 @@ public class StringConversionTest { } @Test - public void whenConvertedToJava6Date_thenCorrect() throws ParseException { + public void whenConvertedToDate_thenCorrect() throws ParseException { String str = "15/10/2013"; SimpleDateFormat formatter = new SimpleDateFormat("dd/M/yyyy"); Date date1 = formatter.parse(str); @@ -86,7 +86,7 @@ public class StringConversionTest { } @Test - public void whenConvertedToJava8Date_thenCorrect() throws ParseException { + public void whenConvertedToLocalDateTime_thenCorrect() throws ParseException { String str = "2007-12-03T10:15:30"; LocalDateTime localDateTime = new UseLocalDateTime().getLocalDateTimeUsingParseMethod(str);