Skip to content

Commit

Permalink
Merge pull request eugenp#9148 from sergio41/master
Browse files Browse the repository at this point in the history
[BAEL-3944] Validate Phone Numbers with Java Regex
  • Loading branch information
JonCook authored May 10, 2020
2 parents 899c7a2 + 6012eca commit 6cd135a
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ public void givenText_whenSimpleRegexMatchesTwice_thenCorrect() {
while (matcher.find())
matches++;
assertEquals(matches, 2);

}

@Test
Expand Down Expand Up @@ -452,15 +451,13 @@ public void whenStudyMethodsWork_thenCorrect() {
Matcher matcher = pattern.matcher("dogs are friendly");
assertTrue(matcher.lookingAt());
assertFalse(matcher.matches());

}

@Test
public void whenMatchesStudyMethodWorks_thenCorrect() {
Pattern pattern = Pattern.compile("dog");
Matcher matcher = pattern.matcher("dog");
assertTrue(matcher.matches());

}

@Test
Expand All @@ -469,7 +466,6 @@ public void whenReplaceFirstWorks_thenCorrect() {
Matcher matcher = pattern.matcher("dogs are domestic animals, dogs are friendly");
String newStr = matcher.replaceFirst("cat");
assertEquals("cats are domestic animals, dogs are friendly", newStr);

}

@Test
Expand All @@ -478,7 +474,6 @@ public void whenReplaceAllWorks_thenCorrect() {
Matcher matcher = pattern.matcher("dogs are domestic animals, dogs are friendly");
String newStr = matcher.replaceAll("cat");
assertEquals("cats are domestic animals, cats are friendly", newStr);

}

public synchronized static int runTest(String regex, String text) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
package com.baeldung.regex.phonenumbers;

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("2055550125");
assertTrue(matcher.matches());
}

@Test
public void whenMOreThanTenDigits_thenNotCorrect() {
Pattern pattern = Pattern.compile("^\\d{10}$");
Matcher matcher = pattern.matcher("20555501251");
assertFalse(matcher.matches());
}

@Test
public void whenMatchesTenDigitsNumberWhitespacesDotHyphen_thenCorrect() {
Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$");
Matcher matcher = pattern.matcher("202 555 0125");
assertTrue(matcher.matches());
}

@Test
public void whenIncludesBracket_thenNotCorrect() {
Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$");
Matcher matcher = pattern.matcher("202]555 0125");
assertFalse(matcher.matches());
}

@Test
public void whenNotStartsWithBatchesOfThreeDigits_thenNotCorrect() {
Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$");
Matcher matcher = pattern.matcher("2021 555 0125");
assertFalse(matcher.matches());
}

@Test
public void whenLastPartWithNoFourDigits_thenNotCorrect() {
Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$");
Matcher matcher = pattern.matcher("202 555 012");
assertFalse(matcher.matches());
}

@Test
public void whenMatchesTenDigitsNumberParenthesis_thenCorrect() {
Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$");
Matcher matcher = pattern.matcher("(202) 555-0125");
assertTrue(matcher.matches());
}

@Test
public void whenJustOpeningParenthesis_thenNotCorrect() {
Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$");
Matcher matcher = pattern.matcher("(202 555-0125");
assertFalse(matcher.matches());
}

@Test
public void whenJustClosingParenthesis_thenNotCorrect() {
Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$");
Matcher matcher = pattern.matcher("202) 555-0125");
assertFalse(matcher.matches());
}

@Test
public void whenMatchesTenDigitsNumberPrefix_thenCorrect() {
Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$");
Matcher matcher = pattern.matcher("+111 (202) 555-0125");
assertTrue(matcher.matches());
}

@Test
public void whenIncorrectPrefix_thenNotCorrect() {
Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$");
Matcher matcher = pattern.matcher("-111 (202) 555-0125");
assertFalse(matcher.matches());
}

@Test
public void whenTooLongPrefix_thenNotCorrect() {
Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$");
Matcher matcher = pattern.matcher("+1111 (202) 555-0125");
assertFalse(matcher.matches());
}

@Test
public void whenMatchesPhoneNumber_thenCorrect() {
String patterns
= "^(\\+\\d{1,3}( )?)?((\\(\\d{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
= {"2055550125","202 555 0125", "(202) 555-0125", "+111 (202) 555-0125", "636 856 789", "+111 636 856 789", "636 85 67 89", "+111 636 85 67 89"};

Pattern pattern = Pattern.compile(patterns);
for(String phoneNumber : validPhoneNumbers) {
Matcher matcher = pattern.matcher(phoneNumber);
assertTrue(matcher.matches());
}
}

@Test
public void whenNotMatchesPhoneNumber_thenNotCorrect() {
String patterns
= "^(\\+\\d{1,3}( )?)?((\\(\\d{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[] invalidPhoneNumbers
= {"20555501251","202]555 0125", "2021 555 012", "(202 555-0125", "202) 555-0125", "-111 (202) 555-0125", "+1111 (202) 555-0125", "636 85 789", "636 85 67 893"};

Pattern pattern = Pattern.compile(patterns);
for(String phoneNumber : invalidPhoneNumbers) {
Matcher matcher = pattern.matcher(phoneNumber);
assertFalse(matcher.matches());
}
}
}

0 comments on commit 6cd135a

Please sign in to comment.