From 5bb2977eb063a73c81dd7fc99cc2c6ecefdc1c32 Mon Sep 17 00:00:00 2001 From: sergio41 Date: Fri, 24 Apr 2020 13:03:41 +0200 Subject: [PATCH] [BAEL-3944] Move to a separate test class and fix other issues --- .../regex/PhoneNumbersRegexUnitTest.java | 55 +++++++++++++++++++ .../com/baeldung/regex/RegexUnitTest.java | 49 ----------------- 2 files changed, 55 insertions(+), 49 deletions(-) create mode 100644 core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/PhoneNumbersRegexUnitTest.java diff --git a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/PhoneNumbersRegexUnitTest.java b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/PhoneNumbersRegexUnitTest.java new file mode 100644 index 0000000000..edfa7a2c5e --- /dev/null +++ b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/PhoneNumbersRegexUnitTest.java @@ -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()); + } + } +} diff --git a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/RegexUnitTest.java b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/RegexUnitTest.java index 909894ec55..77052b79ac 100644 --- a/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/RegexUnitTest.java +++ b/core-java-modules/core-java-regex/src/test/java/com/baeldung/regex/RegexUnitTest.java @@ -26,7 +26,6 @@ public class RegexUnitTest { while (matcher.find()) matches++; assertEquals(matches, 2); - } @Test @@ -452,7 +451,6 @@ public class RegexUnitTest { Matcher matcher = pattern.matcher("dogs are friendly"); assertTrue(matcher.lookingAt()); assertFalse(matcher.matches()); - } @Test @@ -460,7 +458,6 @@ public class RegexUnitTest { Pattern pattern = Pattern.compile("dog"); Matcher matcher = pattern.matcher("dog"); assertTrue(matcher.matches()); - } @Test @@ -469,7 +466,6 @@ public class RegexUnitTest { 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 @@ -478,51 +474,6 @@ public class RegexUnitTest { 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); - - } - - @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) {