addressing PR comments
This commit is contained in:
Deboshree 2023-03-24 23:17:33 +05:30
parent 979b362ad1
commit a7e7286515
3 changed files with 34 additions and 48 deletions

View File

@ -3,9 +3,9 @@ package com.baeldung.stringtoint;
import java.util.Optional;
import org.apache.commons.lang3.math.NumberUtils;
public class StringToIntEncapsulation {
public class StringToIntConverter {
private StringToIntEncapsulation() {
private StringToIntConverter() {
}
public static Integer convertStringToIntUsingIntegerParseInt(String input){
@ -13,7 +13,7 @@ public class StringToIntEncapsulation {
return Integer.parseInt(input);
} catch (NumberFormatException e) {
// log or handle the error
return null; // or Integer.MIN_VALUE, or some other default value
return Integer.MIN_VALUE;
}
}
@ -22,7 +22,7 @@ public class StringToIntEncapsulation {
return Integer.valueOf(input);
} catch (NumberFormatException e) {
// log or handle the error
return null; // or Integer.MIN_VALUE, or some other default value
return Integer.MIN_VALUE;
}
}
@ -31,11 +31,11 @@ public class StringToIntEncapsulation {
return Integer.decode(input);
} catch (Exception e) {
// log or handle the error
return null; // or Integer.MIN_VALUE, or some other default value
return Integer.MIN_VALUE;
}
}
public static Integer converStringToIntUsingOptional(String input){
public static Integer convertStringToIntUsingOptional(String input){
Optional<Integer> parsedInt;
try {
parsedInt = Optional.of(Integer.parseInt(input));
@ -43,11 +43,10 @@ public class StringToIntEncapsulation {
// log or handle the error
parsedInt = Optional.empty();
}
return parsedInt.orElse(null);
return parsedInt.orElse(Integer.MIN_VALUE);
}
public static int convertStringToIntUsingNumberUtils(String input){
//returns Integer.MIN_VALUE as the default value if conversion fails
return NumberUtils.toInt(input, Integer.MIN_VALUE);
}
}

View File

@ -19,6 +19,7 @@ import static org.junit.Assert.assertEquals;
public class DateToStringFormatterUnitTest {
private static final String DATE_FORMAT = "MMM d, yyyy HH:mm a";
private static final String EXPECTED_STRING_DATE = "Aug 1, 2018 12:00 PM";
private static Date date;
@BeforeClass
@ -33,8 +34,8 @@ public class DateToStringFormatterUnitTest {
public void whenDateConvertedUsingSimpleDateFormatToString_thenCorrect() {
DateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
String formattedDate = formatter.format(date);
String expectedDate = "Aug 1, 2018 12:00 pm";
assertEquals(expectedDate, formattedDate);
assertEquals(EXPECTED_STRING_DATE, formattedDate);
}
@Test
@ -42,15 +43,15 @@ public class DateToStringFormatterUnitTest {
String formattedDate = DateFormat
.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US)
.format(date);
String expectedDate = "Aug 1, 2018, 12:00 PM";
assertEquals(expectedDate, formattedDate);
assertEquals(EXPECTED_STRING_DATE, formattedDate);
}
@Test
public void whenDateConvertedUsingFormatterToString_thenCorrect() {
String formattedDate = String.format("%1$tb %1$te, %1$tY %1$tI:%1$tM %1$Tp", date);
String expectedDate = "Aug 1, 2018 12:00 PM";
assertEquals(expectedDate, formattedDate);
assertEquals(EXPECTED_STRING_DATE, formattedDate);
}
@Test
@ -61,7 +62,7 @@ public class DateToStringFormatterUnitTest {
.atZone(ZoneId.of("CET"))
.toLocalDateTime();
String formattedDate = ldt.format(fmt);
String expectedDate = "Aug 1, 2018 12:00 pm";
assertEquals(expectedDate, formattedDate);
assertEquals(EXPECTED_STRING_DATE, formattedDate);
}
}

View File

@ -5,23 +5,24 @@ import java.util.List;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
class StringToIntEncapsulationUnitTest {
class StringToIntConverterUnitTest {
@Test
void whenConvertingIntToString_thenInvalidCasesReturnNull() {
void whenConvertingIntToString_thenInvalidCasesReturnIntegerMinValue() {
List<TestData> testData = Arrays.asList(
new TestData("", null),
new TestData(null, null),
new TestData("23,56", null),
new TestData("2147483648", null),
new TestData("-2147483649", null),
new TestData("hello", null)
new TestData("", Integer.MIN_VALUE),
new TestData(null, Integer.MIN_VALUE),
new TestData("23,56", Integer.MIN_VALUE),
new TestData("2147483648", Integer.MIN_VALUE),
new TestData("-2147483649", Integer.MIN_VALUE),
new TestData("hello", Integer.MIN_VALUE)
);
testData.forEach(data -> {
Assertions.assertEquals(data.expectedOutput, StringToIntEncapsulation.convertStringToIntUsingIntegerParseInt(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntEncapsulation.convertStringToIntUsingIntegerValueOf(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntEncapsulation.converStringToIntUsingOptional(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntEncapsulation.convertStringToIntUsingIntegerDecode(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerParseInt(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerValueOf(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingOptional(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerDecode(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingNumberUtils(data.input));
});
}
@ -32,29 +33,14 @@ class StringToIntEncapsulationUnitTest {
new TestData("-23", -23)
);
testData.forEach(data -> {
Assertions.assertEquals(data.expectedOutput, StringToIntEncapsulation.convertStringToIntUsingIntegerParseInt(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntEncapsulation.convertStringToIntUsingIntegerValueOf(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntEncapsulation.converStringToIntUsingOptional(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntEncapsulation.convertStringToIntUsingNumberUtils(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntEncapsulation.convertStringToIntUsingIntegerDecode(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerParseInt(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerValueOf(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingOptional(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingNumberUtils(data.input));
Assertions.assertEquals(data.expectedOutput, StringToIntConverter.convertStringToIntUsingIntegerDecode(data.input));
});
}
@Test
void whenConvertingStringToIntUsingNumberUtils_ThenInValidConversionReturnIntegerMin() {
List<TestData> testData = Arrays.asList(
new TestData("", Integer.MIN_VALUE),
new TestData(null, Integer.MIN_VALUE),
new TestData("23,56", Integer.MIN_VALUE),
new TestData("2147483648", Integer.MIN_VALUE),
new TestData("-2147483649", Integer.MIN_VALUE),
new TestData("hello", Integer.MIN_VALUE)
);
testData.forEach(data ->
Assertions.assertEquals(data.expectedOutput, StringToIntEncapsulation.convertStringToIntUsingNumberUtils(data.input)));
}
public static class TestData{
String input;
Integer expectedOutput;