[BAEL-3944] Move to a separate test class and fix other issues

This commit is contained in:
sergio41 2020-04-24 13:03:41 +02:00
parent 3233f80c5b
commit 5bb2977eb0
2 changed files with 55 additions and 49 deletions

View File

@ -0,0 +1,55 @@
package com.baeldung.regex;
import static org.junit.Assert.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.junit.Test;
public class PhoneNumbersRegexUnitTest {
@Test
public void whenMatchesTenDigitsNumber_thenCorrect() {
Pattern pattern = Pattern.compile("^\\d{10}$");
Matcher matcher = pattern.matcher("1234567890");
assertTrue(matcher.matches());
}
@Test
public void whenMatchesTenDigitsNumberWhitespacesHyphen_thenCorrect() {
Pattern pattern = Pattern.compile("^(\\d{3}[- ]?){2}\\d{4}$");
Matcher matcher = pattern.matcher("123 456 7890");
assertTrue(matcher.matches());
}
@Test
public void whenMatchesTenDigitsNumberParenthesis_thenCorrect() {
Pattern pattern = Pattern.compile("^\\(?\\d{3}\\)?[- ]?\\d{3}[- ]?\\d{4}$");
Matcher matcher = pattern.matcher("(123)-456-7890");
assertTrue(matcher.matches());
}
@Test
public void whenMatchesTenDigitsNumberPrefix_thenCorrect() {
Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?\\(?\\d{3}\\)?[- ]?\\d{3}[- ]?\\d{4}$");
Matcher matcher = pattern.matcher("+111 (123)-456-7890");
assertTrue(matcher.matches());
}
@Test
public void whenMatchesPhoneNumber_thenCorrect() {
String patterns = "^(\\+\\d{1,3}( )?)?\\(?\\d{3}\\)?[- ]?\\d{3}[- ]?\\d{4}$" +
"|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?){2}\\d{3}$" +
"|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?)(\\d{2}[ ]?){2}\\d{2}$";
String[] validPhoneNumbers = {"1234567890","123 456 7890", "(123)-456-7890", "+111 (123)-456-7890",
"123 456 789", "+111 123 456 789", "123 45 67 89", "+111 123 45 67 89"};
Pattern pattern = Pattern.compile(patterns);
for(String phoneNumber : validPhoneNumbers) {
Matcher matcher = pattern.matcher(phoneNumber);
assertTrue(matcher.matches());
}
}
}

View File

@ -26,7 +26,6 @@ public class RegexUnitTest {
while (matcher.find()) while (matcher.find())
matches++; matches++;
assertEquals(matches, 2); assertEquals(matches, 2);
} }
@Test @Test
@ -452,7 +451,6 @@ public class RegexUnitTest {
Matcher matcher = pattern.matcher("dogs are friendly"); Matcher matcher = pattern.matcher("dogs are friendly");
assertTrue(matcher.lookingAt()); assertTrue(matcher.lookingAt());
assertFalse(matcher.matches()); assertFalse(matcher.matches());
} }
@Test @Test
@ -460,7 +458,6 @@ public class RegexUnitTest {
Pattern pattern = Pattern.compile("dog"); Pattern pattern = Pattern.compile("dog");
Matcher matcher = pattern.matcher("dog"); Matcher matcher = pattern.matcher("dog");
assertTrue(matcher.matches()); assertTrue(matcher.matches());
} }
@Test @Test
@ -469,7 +466,6 @@ public class RegexUnitTest {
Matcher matcher = pattern.matcher("dogs are domestic animals, dogs are friendly"); Matcher matcher = pattern.matcher("dogs are domestic animals, dogs are friendly");
String newStr = matcher.replaceFirst("cat"); String newStr = matcher.replaceFirst("cat");
assertEquals("cats are domestic animals, dogs are friendly", newStr); assertEquals("cats are domestic animals, dogs are friendly", newStr);
} }
@Test @Test
@ -478,51 +474,6 @@ public class RegexUnitTest {
Matcher matcher = pattern.matcher("dogs are domestic animals, dogs are friendly"); Matcher matcher = pattern.matcher("dogs are domestic animals, dogs are friendly");
String newStr = matcher.replaceAll("cat"); String newStr = matcher.replaceAll("cat");
assertEquals("cats are domestic animals, cats are friendly", newStr); assertEquals("cats are domestic animals, cats are friendly", newStr);
}
@Test
public void whenMatchesTenDigitsNumber_thenCorrect() {
Pattern pattern = Pattern.compile("^\\d{10}$");
Matcher matcher = pattern.matcher("1234567890");
assertTrue(matcher.matches());
}
@Test
public void whenMatchesTenDigitsNumberWhitespacesHyphen_thenCorrect() {
Pattern pattern = Pattern.compile("^(\\d{3}[- ]?){2}\\d{4}$");
Matcher matcher = pattern.matcher("123 456 7890");
assertTrue(matcher.matches());
}
@Test
public void whenMatchesTenDigitsNumberParenthesis_thenCorrect() {
Pattern pattern = Pattern.compile("^\\(?\\d{3}\\)?[- ]?\\d{3}[- ]?\\d{4}$");
Matcher matcher = pattern.matcher("(123)-456-7890");
assertTrue(matcher.matches());
}
@Test
public void whenMatchesTenDigitsNumberPrefix_thenCorrect() {
Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?\\(?\\d{3}\\)?[- ]?\\d{3}[- ]?\\d{4}$");
Matcher matcher = pattern.matcher("+111 (123)-456-7890");
assertTrue(matcher.matches());
}
@Test
public void whenMatchesPhoneNumber_thenCorrect() {
String patterns = "^(\\+\\d{1,3}( )?)?\\(?\\d{3}\\)?[- ]?\\d{3}[- ]?\\d{4}$" +
"|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?){2}\\d{3}$" +
"|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?)(\\d{2}[ ]?){2}\\d{2}$";
String[] validPhoneNumbers = {"1234567890","123 456 7890", "(123)-456-7890", "+111 (123)-456-7890",
"123 456 789", "+111 123 456 789", "123 45 67 89", "+111 123 45 67 89"};
Pattern pattern = Pattern.compile(patterns);
for(String phoneNumber : validPhoneNumbers) {
Matcher matcher = pattern.matcher(phoneNumber);
assertTrue(matcher.matches());
}
} }
public synchronized static int runTest(String regex, String text) { public synchronized static int runTest(String regex, String text) {