Merge pull request #823 from khatwaniNikhil/BAEL-29-StringConversions-UnitTest

BAEL-29 Refactored Code to comeup with Unit Test
This commit is contained in:
Jim Kerak 2016-11-12 12:42:57 -05:00 committed by GitHub
commit 106177b3e6
2 changed files with 97 additions and 60 deletions

View File

@ -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);
}
}

View File

@ -0,0 +1,97 @@
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 whenConvertedToInt_thenCorrect() {
assertEquals(Integer.parseInt("1"), 1);
}
@Test
public void whenConvertedToInteger_thenCorrect() {
assertEquals(Integer.valueOf("12").equals(12), true);
}
@Test
public void whenConvertedTolong_thenCorrect() {
assertEquals(Long.parseLong("12345"), 12345);
}
@Test
public void whenConvertedToLong_thenCorrect() {
assertEquals(Long.valueOf("14567").equals(14567L), true);
}
@Test
public void whenConvertedTodouble_thenCorrect() {
assertEquals(Double.parseDouble("1.4"), 1.4, 0.0);
}
@Test
public void whenConvertedToDouble_thenCorrect() {
assertEquals(Double.valueOf("145.67").equals(145.67d), true);
}
@Test
public void whenConvertedToByteArray_thenCorrect() throws UnsupportedEncodingException {
byte[] byteArray1 = new byte[] { 'a', 'b', 'c' };
String string = new String(byteArray1, "UTF-8");
assertEquals(Arrays.equals(string.getBytes(), byteArray1), true);
}
@Test
public void whenConvertedToboolean_thenCorrect() {
assertEquals(Boolean.parseBoolean("true"), true);
}
@Test
public void whenConvertedToBoolean_thenCorrect() {
assertEquals(Boolean.valueOf("true"), true);
}
@Test
public void whenConvertedToCharArray_thenCorrect() {
String str = "hello";
char[] charArray = { 'h', 'e', 'l', 'l', 'o' };
assertEquals(Arrays.equals(charArray, str.toCharArray()), true);
}
@Test
public void whenConvertedToDate_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 whenConvertedToLocalDateTime_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);
}
}