add unit-test class for the issue 5155 (#11217)
This commit is contained in:
parent
32e5a9ccb0
commit
bf1131eac5
|
@ -16,6 +16,16 @@
|
||||||
</parent>
|
</parent>
|
||||||
|
|
||||||
<dependencies>
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-core</artifactId>
|
||||||
|
<version>${spring-core.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.commons</groupId>
|
||||||
|
<artifactId>commons-lang3</artifactId>
|
||||||
|
<version>${apache-commons-lang3.version}</version>
|
||||||
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.assertj</groupId>
|
<groupId>org.assertj</groupId>
|
||||||
<artifactId>assertj-core</artifactId>
|
<artifactId>assertj-core</artifactId>
|
||||||
|
@ -68,6 +78,8 @@
|
||||||
<maven.compiler.source>11</maven.compiler.source>
|
<maven.compiler.source>11</maven.compiler.source>
|
||||||
<maven.compiler.target>11</maven.compiler.target>
|
<maven.compiler.target>11</maven.compiler.target>
|
||||||
<assertj.version>3.6.1</assertj.version>
|
<assertj.version>3.6.1</assertj.version>
|
||||||
|
<spring-core.version>5.3.9</spring-core.version>
|
||||||
|
<apache-commons-lang3.version>3.12.0</apache-commons-lang3.version>
|
||||||
<maven-artifact.version>3.6.3</maven-artifact.version>
|
<maven-artifact.version>3.6.3</maven-artifact.version>
|
||||||
<gradle-core.version>6.1.1</gradle-core.version>
|
<gradle-core.version>6.1.1</gradle-core.version>
|
||||||
<jackson-core.version>2.11.1</jackson-core.version>
|
<jackson-core.version>2.11.1</jackson-core.version>
|
||||||
|
|
|
@ -0,0 +1,65 @@
|
||||||
|
package com.baeldung.countspaces;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.util.regex.Matcher;
|
||||||
|
import java.util.regex.Pattern;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
|
||||||
|
class CountSpacesInStringUnitTest {
|
||||||
|
private static final String INPUT_STRING = " This string has nine spaces and a Tab:' '";
|
||||||
|
private static final int EXPECTED_COUNT = 9;
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenCountSpaceByLooping_thenReturnsExpectedCount() {
|
||||||
|
int spaceCount = 0;
|
||||||
|
for (char c : INPUT_STRING.toCharArray()) {
|
||||||
|
if (c == ' ') {
|
||||||
|
spaceCount++;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
assertThat(spaceCount).isEqualTo(EXPECTED_COUNT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenCountSpaceByJava8StreamFilter_thenReturnsExpectedCount() {
|
||||||
|
long spaceCount = INPUT_STRING.chars().filter(c -> c == (int) ' ').count();
|
||||||
|
assertThat(spaceCount).isEqualTo(EXPECTED_COUNT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenCountSpaceByRegexMatcher_thenReturnsExpectedCount() {
|
||||||
|
Pattern pattern = Pattern.compile(" ");
|
||||||
|
Matcher matcher = pattern.matcher(INPUT_STRING);
|
||||||
|
int spaceCount = 0;
|
||||||
|
while (matcher.find()) {
|
||||||
|
spaceCount++;
|
||||||
|
}
|
||||||
|
assertThat(spaceCount).isEqualTo(EXPECTED_COUNT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenCountSpaceByReplaceAll_thenReturnsExpectedCount() {
|
||||||
|
int spaceCount = INPUT_STRING.replaceAll("[^ ]", "").length();
|
||||||
|
assertThat(spaceCount).isEqualTo(EXPECTED_COUNT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenCountSpaceBySplit_thenReturnsExpectedCount() {
|
||||||
|
int spaceCount = INPUT_STRING.split(" ").length - 1;
|
||||||
|
assertThat(spaceCount).isEqualTo(EXPECTED_COUNT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenCountSpaceUsingApacheCommons_thenReturnsExpectedCount() {
|
||||||
|
int spaceCount = StringUtils.countMatches(INPUT_STRING, " ");
|
||||||
|
assertThat(spaceCount).isEqualTo(EXPECTED_COUNT);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void givenString_whenCountSpaceUsingSpring_thenReturnsExpectedCount() {
|
||||||
|
int spaceCount = org.springframework.util.StringUtils.countOccurrencesOf(INPUT_STRING, " ");
|
||||||
|
assertThat(spaceCount).isEqualTo(EXPECTED_COUNT);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue