moved the code to a different module
This commit is contained in:
Deboshree 2023-04-03 15:11:48 +05:30
parent a7e7286515
commit 60eb348df5
5 changed files with 95 additions and 54 deletions

View File

@ -0,0 +1,6 @@
## Java String Conversions
This module contains articles about string conversions from/to another type.
### Relevant Articles:
- More articles: [[next -->]](/core-java-modules/core-java-string-conversions-2)

View File

@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-string-conversions-3</artifactId>
<version>0.1.0-SNAPSHOT</version>
<name>core-java-string-conversions-3</name>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
<dependencies>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>${commons-lang3.version}</version>
</dependency>
</dependencies>
<build>
<finalName>core-java-string-conversions-3</finalName>
<resources>
<resource>
<directory>src/main/resources</directory>
<filtering>true</filtering>
</resource>
</resources>
</build>
</project>

View File

@ -0,0 +1,53 @@
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 StringToIntConverterUnitTest {
@Test
void whenConvertingIntToString_thenInvalidCasesReturnIntegerMinValue() {
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, 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));
});
}
@Test
void whenConvertingIntToString_thenValidCasesReturnUnboxedInt() {
List<TestData> testData = Arrays.asList(
new TestData("23", 23),
new TestData("-23", -23)
);
testData.forEach(data -> {
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));
});
}
public static class TestData{
String input;
Integer expectedOutput;
TestData(String input, Integer expectedOutput){
this.input = input;
this.expectedOutput = expectedOutput;
}
}
}

View File

@ -1,53 +0,0 @@
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 StringToIntConverterUnitTest {
@Test
void whenConvertingIntToString_thenInvalidCasesReturnIntegerMinValue() {
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, 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));
});
}
@Test
void whenConvertingIntToString_thenValidCasesReturnUnboxedInt() {
List<TestData> testData = Arrays.asList(
new TestData("23", 23),
new TestData("-23", -23)
);
testData.forEach(data -> {
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));
});
}
public static class TestData{
String input;
Integer expectedOutput;
TestData(String input, Integer expectedOutput){
this.input = input;
this.expectedOutput = expectedOutput;
}
}
}