1. moving modules to java-string-conversions 2. fixing old broken test
This commit is contained in:
parent
4abc9c1a39
commit
979b362ad1
|
@ -0,0 +1,53 @@
|
|||
package com.baeldung.stringtoint;
|
||||
|
||||
import java.util.Optional;
|
||||
import org.apache.commons.lang3.math.NumberUtils;
|
||||
|
||||
public class StringToIntEncapsulation {
|
||||
|
||||
private StringToIntEncapsulation() {
|
||||
}
|
||||
|
||||
public static Integer convertStringToIntUsingIntegerParseInt(String input){
|
||||
try {
|
||||
return Integer.parseInt(input);
|
||||
} catch (NumberFormatException e) {
|
||||
// log or handle the error
|
||||
return null; // or Integer.MIN_VALUE, or some other default value
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer convertStringToIntUsingIntegerValueOf(String input){
|
||||
try {
|
||||
return Integer.valueOf(input);
|
||||
} catch (NumberFormatException e) {
|
||||
// log or handle the error
|
||||
return null; // or Integer.MIN_VALUE, or some other default value
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer convertStringToIntUsingIntegerDecode(String input){
|
||||
try {
|
||||
return Integer.decode(input);
|
||||
} catch (Exception e) {
|
||||
// log or handle the error
|
||||
return null; // or Integer.MIN_VALUE, or some other default value
|
||||
}
|
||||
}
|
||||
|
||||
public static Integer converStringToIntUsingOptional(String input){
|
||||
Optional<Integer> parsedInt;
|
||||
try {
|
||||
parsedInt = Optional.of(Integer.parseInt(input));
|
||||
} catch (Exception e) {
|
||||
// log or handle the error
|
||||
parsedInt = Optional.empty();
|
||||
}
|
||||
return parsedInt.orElse(null);
|
||||
}
|
||||
|
||||
public static int convertStringToIntUsingNumberUtils(String input){
|
||||
//returns Integer.MIN_VALUE as the default value if conversion fails
|
||||
return NumberUtils.toInt(input, Integer.MIN_VALUE);
|
||||
}
|
||||
}
|
|
@ -19,7 +19,6 @@ 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
|
||||
|
@ -34,8 +33,8 @@ public class DateToStringFormatterUnitTest {
|
|||
public void whenDateConvertedUsingSimpleDateFormatToString_thenCorrect() {
|
||||
DateFormat formatter = new SimpleDateFormat(DATE_FORMAT);
|
||||
String formattedDate = formatter.format(date);
|
||||
|
||||
assertEquals(EXPECTED_STRING_DATE, formattedDate);
|
||||
String expectedDate = "Aug 1, 2018 12:00 pm";
|
||||
assertEquals(expectedDate, formattedDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -43,15 +42,15 @@ public class DateToStringFormatterUnitTest {
|
|||
String formattedDate = DateFormat
|
||||
.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.SHORT, Locale.US)
|
||||
.format(date);
|
||||
|
||||
assertEquals(EXPECTED_STRING_DATE, formattedDate);
|
||||
String expectedDate = "Aug 1, 2018, 12:00 PM";
|
||||
assertEquals(expectedDate, formattedDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDateConvertedUsingFormatterToString_thenCorrect() {
|
||||
String formattedDate = String.format("%1$tb %1$te, %1$tY %1$tI:%1$tM %1$Tp", date);
|
||||
|
||||
assertEquals(EXPECTED_STRING_DATE, formattedDate);
|
||||
String expectedDate = "Aug 1, 2018 12:00 PM";
|
||||
assertEquals(expectedDate, formattedDate);
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -62,7 +61,7 @@ public class DateToStringFormatterUnitTest {
|
|||
.atZone(ZoneId.of("CET"))
|
||||
.toLocalDateTime();
|
||||
String formattedDate = ldt.format(fmt);
|
||||
|
||||
assertEquals(EXPECTED_STRING_DATE, formattedDate);
|
||||
String expectedDate = "Aug 1, 2018 12:00 pm";
|
||||
assertEquals(expectedDate, formattedDate);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -0,0 +1,67 @@
|
|||
package com.baeldung.stringtoint;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import org.junit.jupiter.api.Assertions;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class StringToIntEncapsulationUnitTest {
|
||||
|
||||
@Test
|
||||
void whenConvertingIntToString_thenInvalidCasesReturnNull() {
|
||||
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)
|
||||
);
|
||||
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));
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
void whenConvertingIntToString_thenValidCasesReturnUnboxedInt() {
|
||||
List<TestData> testData = Arrays.asList(
|
||||
new TestData("23", 23),
|
||||
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));
|
||||
});
|
||||
}
|
||||
|
||||
@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;
|
||||
|
||||
TestData(String input, Integer expectedOutput){
|
||||
this.input = input;
|
||||
this.expectedOutput = expectedOutput;
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue