|
| 1 | +package com.baeldung.regex.phonenumbers; |
| 2 | + |
| 3 | +import static org.junit.Assert.*; |
| 4 | + |
| 5 | +import java.util.regex.Matcher; |
| 6 | +import java.util.regex.Pattern; |
| 7 | + |
| 8 | +import org.junit.Test; |
| 9 | + |
| 10 | +public class PhoneNumbersRegexUnitTest { |
| 11 | + |
| 12 | + @Test |
| 13 | + public void whenMatchesTenDigitsNumber_thenCorrect() { |
| 14 | + Pattern pattern = Pattern.compile("^\\d{10}$"); |
| 15 | + Matcher matcher = pattern.matcher("2055550125"); |
| 16 | + assertTrue(matcher.matches()); |
| 17 | + } |
| 18 | + |
| 19 | + @Test |
| 20 | + public void whenMOreThanTenDigits_thenNotCorrect() { |
| 21 | + Pattern pattern = Pattern.compile("^\\d{10}$"); |
| 22 | + Matcher matcher = pattern.matcher("20555501251"); |
| 23 | + assertFalse(matcher.matches()); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + public void whenMatchesTenDigitsNumberWhitespacesDotHyphen_thenCorrect() { |
| 28 | + Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$"); |
| 29 | + Matcher matcher = pattern.matcher("202 555 0125"); |
| 30 | + assertTrue(matcher.matches()); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + public void whenIncludesBracket_thenNotCorrect() { |
| 35 | + Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$"); |
| 36 | + Matcher matcher = pattern.matcher("202]555 0125"); |
| 37 | + assertFalse(matcher.matches()); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void whenNotStartsWithBatchesOfThreeDigits_thenNotCorrect() { |
| 42 | + Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$"); |
| 43 | + Matcher matcher = pattern.matcher("2021 555 0125"); |
| 44 | + assertFalse(matcher.matches()); |
| 45 | + } |
| 46 | + |
| 47 | + @Test |
| 48 | + public void whenLastPartWithNoFourDigits_thenNotCorrect() { |
| 49 | + Pattern pattern = Pattern.compile("^(\\d{3}[- .]?){2}\\d{4}$"); |
| 50 | + Matcher matcher = pattern.matcher("202 555 012"); |
| 51 | + assertFalse(matcher.matches()); |
| 52 | + } |
| 53 | + |
| 54 | + @Test |
| 55 | + public void whenMatchesTenDigitsNumberParenthesis_thenCorrect() { |
| 56 | + Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); |
| 57 | + Matcher matcher = pattern.matcher("(202) 555-0125"); |
| 58 | + assertTrue(matcher.matches()); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void whenJustOpeningParenthesis_thenNotCorrect() { |
| 63 | + Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); |
| 64 | + Matcher matcher = pattern.matcher("(202 555-0125"); |
| 65 | + assertFalse(matcher.matches()); |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + public void whenJustClosingParenthesis_thenNotCorrect() { |
| 70 | + Pattern pattern = Pattern.compile("^((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); |
| 71 | + Matcher matcher = pattern.matcher("202) 555-0125"); |
| 72 | + assertFalse(matcher.matches()); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + public void whenMatchesTenDigitsNumberPrefix_thenCorrect() { |
| 77 | + Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); |
| 78 | + Matcher matcher = pattern.matcher("+111 (202) 555-0125"); |
| 79 | + assertTrue(matcher.matches()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void whenIncorrectPrefix_thenNotCorrect() { |
| 84 | + Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); |
| 85 | + Matcher matcher = pattern.matcher("-111 (202) 555-0125"); |
| 86 | + assertFalse(matcher.matches()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void whenTooLongPrefix_thenNotCorrect() { |
| 91 | + Pattern pattern = Pattern.compile("^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$"); |
| 92 | + Matcher matcher = pattern.matcher("+1111 (202) 555-0125"); |
| 93 | + assertFalse(matcher.matches()); |
| 94 | + } |
| 95 | + |
| 96 | + @Test |
| 97 | + public void whenMatchesPhoneNumber_thenCorrect() { |
| 98 | + String patterns |
| 99 | + = "^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$" |
| 100 | + + "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?){2}\\d{3}$" |
| 101 | + + "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?)(\\d{2}[ ]?){2}\\d{2}$"; |
| 102 | + |
| 103 | + String[] validPhoneNumbers |
| 104 | + = {"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"}; |
| 105 | + |
| 106 | + Pattern pattern = Pattern.compile(patterns); |
| 107 | + for(String phoneNumber : validPhoneNumbers) { |
| 108 | + Matcher matcher = pattern.matcher(phoneNumber); |
| 109 | + assertTrue(matcher.matches()); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + public void whenNotMatchesPhoneNumber_thenNotCorrect() { |
| 115 | + String patterns |
| 116 | + = "^(\\+\\d{1,3}( )?)?((\\(\\d{3}\\))|\\d{3})[- .]?\\d{3}[- .]?\\d{4}$" |
| 117 | + + "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?){2}\\d{3}$" |
| 118 | + + "|^(\\+\\d{1,3}( )?)?(\\d{3}[ ]?)(\\d{2}[ ]?){2}\\d{2}$"; |
| 119 | + |
| 120 | + String[] invalidPhoneNumbers |
| 121 | + = {"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"}; |
| 122 | + |
| 123 | + Pattern pattern = Pattern.compile(patterns); |
| 124 | + for(String phoneNumber : invalidPhoneNumbers) { |
| 125 | + Matcher matcher = pattern.matcher(phoneNumber); |
| 126 | + assertFalse(matcher.matches()); |
| 127 | + } |
| 128 | + } |
| 129 | +} |
0 commit comments