BAEL-4163 - Counting matches for a regex (#9466)

* BAEL-4163 - Counting matches for a regex

* BAEL-4163 - Fixing build problemns

* BAEL-4163 : Adding more test to assure quality of the examples

* BAEL-4163 : Fixing request changes

* BAEL-4163 : Minor adjustements to give more context about the commented out tests

* BAEL-4163 : Tests for overlap example
This commit is contained in:
Roque Santos 2020-07-04 06:29:58 -03:00 committed by GitHub
parent 78cacb4d2e
commit 535c88153d
1 changed files with 110 additions and 0 deletions

View File

@ -0,0 +1,110 @@
package com.baeldung.regex.countmatches;
import static org.junit.Assert.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
/**
* Unit Test intended to count number of matches of a RegEx using Java 8 and 9.
*
* Java 9 is needed to run the commented out tests.
*/
public class CountMatchesUnitTest {
private static final Pattern EMAIL_ADDRESS_PATTERN = Pattern.compile("([a-z0-9_.-]+)@([a-z0-9_.-]+[a-z])");
private static final String TEXT_CONTAINING_EMAIL_ADDRESSES = "You can contact me through: writer@baeldung.com, editor@baeldung.com and team@bealdung.com";
private static final String TEXT_CONTAINING_FIVE_EMAIL_ADDRESSES = "Valid emails are: me@gmail.com, you@baeldung.com, contact@hotmail.com, press@anysite.com and support@bealdung.com";
private static final String TEXT_CONTAINING_OVERLAP_EMAIL_ADDRESSES = "Try to contact us at team@baeldung.comeditor@baeldung.com, support@baeldung.com.";
@Test
public void givenContainingEmailString_whenJava8Match_thenCountMacthesFound() {
Matcher countEmailMatcher = EMAIL_ADDRESS_PATTERN.matcher(TEXT_CONTAINING_EMAIL_ADDRESSES);
int count = 0;
while (countEmailMatcher.find()) {
count++;
}
assertEquals(3, count);
}
@Test
public void givenContainingFiveEmailsString_whenJava8Match_thenCountMacthesFound() {
Matcher countFiveEmailsMatcher = EMAIL_ADDRESS_PATTERN.matcher(TEXT_CONTAINING_FIVE_EMAIL_ADDRESSES);
int count = 0;
while (countFiveEmailsMatcher.find()) {
count++;
}
assertEquals(5, count);
}
@Test
public void givenStringWithoutEmails_whenJava8Match_thenCountMacthesNotFound() {
Matcher noEmailMatcher = EMAIL_ADDRESS_PATTERN.matcher("Simple text without emails.");
int count = 0;
while (noEmailMatcher.find()) {
count++;
}
assertEquals(0, count);
}
@Test
public void givenStringWithOverlappingEmails_whenJava8Match_thenCountWrongMatches() {
Matcher countOverlappingEmailsMatcher = EMAIL_ADDRESS_PATTERN.matcher(TEXT_CONTAINING_OVERLAP_EMAIL_ADDRESSES);
int count = 0;
while (countOverlappingEmailsMatcher.find()) {
count++;
}
assertNotEquals(3, count);
}
@Test
public void givenContainingEmailString_whenStartingInJava9Match_thenCountMacthesFound() {
// uncomment to try with Java 9
// Matcher countEmailMatcher = EMAIL_ADDRESS_PATTERN.matcher(TEXT_CONTAINING_EMAIL_ADDRESSES);
// long count = countEmailMatcher.results().count();
// assertEquals(3, count);
}
@Test
public void givenContainingFiveEmailsString_whenStartingInJava9Match_thenCountMacthesFound() {
// uncomment to try with Java 9
// Matcher countFiveEmailsMatcher = EMAIL_ADDRESS_PATTERN.matcher(TEXT_CONTAINING_FIVE_EMAIL_ADDRESSES);
// long count = countFiveEmailsMatcher.results().count();
// assertEquals(5, count);
}
@Test
public void givenStringWithoutEmails_whenJava9Match_thenCountMacthesNotFound() {
// uncomment to try with Java 9
// Matcher noEmailMatcher = EMAIL_ADDRESS_PATTERN.matcher("Simple text without emails.");
// long count = noEmailMatcher.results().count();
// assertEquals(0, count);
}
@Test
public void givenStringWithOverlappingEmails_whenJava9Match_thenCountWrongMatches() {
// uncomment to try with Java 9
// Matcher countOverlappingEmailsMatcher = EMAIL_ADDRESS_PATTERN.matcher(TEXT_CONTAINING_OVERLAP_EMAIL_ADDRESSES);
// long count = countOverlappingEmailsMatcher.results().count();
// assertNotEquals(3, count);
}
}