diff --git a/core-java-modules/core-java-string-operations-3/pom.xml b/core-java-modules/core-java-string-operations-3/pom.xml index b73851f90c..642ade5ab3 100644 --- a/core-java-modules/core-java-string-operations-3/pom.xml +++ b/core-java-modules/core-java-string-operations-3/pom.xml @@ -16,6 +16,16 @@ + + org.springframework + spring-core + ${spring-core.version} + + + org.apache.commons + commons-lang3 + ${apache-commons-lang3.version} + org.assertj assertj-core @@ -68,10 +78,12 @@ 11 11 3.6.1 + 5.3.9 + 3.12.0 3.6.3 6.1.1 2.11.1 3.1.0 - \ No newline at end of file + diff --git a/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/countspaces/CountSpacesInStringUnitTest.java b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/countspaces/CountSpacesInStringUnitTest.java new file mode 100644 index 0000000000..0432b7dd24 --- /dev/null +++ b/core-java-modules/core-java-string-operations-3/src/test/java/com/baeldung/countspaces/CountSpacesInStringUnitTest.java @@ -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); + } +}