add unit-test class for the issue 5155 (#11217)

This commit is contained in:
Kai Yuan 2021-09-21 03:37:35 +02:00 committed by GitHub
parent 32e5a9ccb0
commit bf1131eac5
2 changed files with 78 additions and 1 deletions

View File

@ -16,6 +16,16 @@
</parent>
<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>
<groupId>org.assertj</groupId>
<artifactId>assertj-core</artifactId>
@ -68,10 +78,12 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<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>
<gradle-core.version>6.1.1</gradle-core.version>
<jackson-core.version>2.11.1</jackson-core.version>
<semver4j.version>3.1.0</semver4j.version>
</properties>
</project>
</project>

View File

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