diff --git a/java-strings/src/main/java/com/baeldung/string/MatchWords.java b/java-strings/src/main/java/com/baeldung/string/MatchWords.java index 647b60af9a..d322d192fa 100644 --- a/java-strings/src/main/java/com/baeldung/string/MatchWords.java +++ b/java-strings/src/main/java/com/baeldung/string/MatchWords.java @@ -12,22 +12,7 @@ import java.util.stream.Stream; public class MatchWords { - public static void main(String[] args) { - String[] words = {"hello", "Baeldung"}; - String inputString = "hello there, Baeldung"; - - containsWords(inputString, words); - - containsWordsJava8(inputString, words); - - containsWordsPatternMatch(inputString, words); - - containsWordsAhoCorasick(inputString, words); - - containsWordsIndexOf(inputString, words); - } - - private static boolean containsWordsIndexOf(String inputString, String[] words) { + public static boolean containsWordsIndexOf(String inputString, String[] words) { boolean found = true; for (String word : words) { int index = inputString.indexOf(word); @@ -39,7 +24,7 @@ public class MatchWords { return found; } - private static boolean containsWords(String inputString, String[] items) { + public static boolean containsWords(String inputString, String[] items) { boolean found = true; for (String item : items) { if (!inputString.contains(item)) { @@ -50,7 +35,7 @@ public class MatchWords { return found; } - private static boolean containsWordsAhoCorasick(String inputString, String[] words) { + public static boolean containsWordsAhoCorasick(String inputString, String[] words) { Trie trie = Trie.builder() .onlyWholeWords() .addKeywords(words) @@ -71,7 +56,7 @@ public class MatchWords { return found; } - private static boolean containsWordsPatternMatch(String inputString, String[] words) { + public static boolean containsWordsPatternMatch(String inputString, String[] words) { StringBuilder regexp = new StringBuilder(); for (String word : words) { @@ -85,14 +70,14 @@ public class MatchWords { return false; } - private static boolean containsWordsJava8(String inputString, String[] words) { + public static boolean containsWordsJava8(String inputString, String[] words) { ArrayList inputStringList = new ArrayList<>(Arrays.asList(inputString.split(" "))); ArrayList wordsList = new ArrayList<>(Arrays.asList(words)); return wordsList.stream().allMatch(inputStringList::contains); } - private static boolean containsWordsArray(String inputString, String[] words) { + public static boolean containsWordsArray(String inputString, String[] words) { ArrayList inputStringList = new ArrayList<>(Arrays.asList(inputString.split(" "))); ArrayList wordsList = new ArrayList<>(Arrays.asList(words)); diff --git a/java-strings/src/test/java/com/baeldung/string/MatchWordsUnitTest.java b/java-strings/src/test/java/com/baeldung/string/MatchWordsUnitTest.java new file mode 100644 index 0000000000..0b25a265b3 --- /dev/null +++ b/java-strings/src/test/java/com/baeldung/string/MatchWordsUnitTest.java @@ -0,0 +1,59 @@ +package com.baeldung.string; + +import org.junit.Test; + +import static org.assertj.core.api.Assertions.assertThat; + +public class MatchWordsUnitTest { + + private final String[] words = {"hello", "Baeldung"}; + private final String inputString = "hello there, Baeldung"; + + @Test + public void givenText_whenCallingStringContains_shouldMatchWords() { + + final boolean result = MatchWords.containsWords(inputString, words); + + assertThat(result).isEqualTo(true); + } + + @Test + public void givenText_whenCallingJava8_shouldMatchWords() { + + final boolean result = MatchWords.containsWordsJava8(inputString, words); + + assertThat(result).isEqualTo(true); + } + + @Test + public void givenText_whenCallingPattern_shouldMatchWords() { + + final boolean result = MatchWords.containsWordsPatternMatch(inputString, words); + + assertThat(result).isEqualTo(true); + } + + @Test + public void givenText_whenCallingAhoCorasick_shouldMatchWords() { + + final boolean result = MatchWords.containsWordsAhoCorasick(inputString, words); + + assertThat(result).isEqualTo(true); + } + + @Test + public void givenText_whenCallingIndexOf_shouldMatchWords() { + + final boolean result = MatchWords.containsWordsIndexOf(inputString, words); + + assertThat(result).isEqualTo(true); + } + + @Test + public void givenText_whenCallingArrayList_shouldMatchWords() { + + final boolean result = MatchWords.containsWordsArray(inputString, words); + + assertThat(result).isEqualTo(true); + } +}