| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146 |
- import org.junit.Ignore;
- import org.junit.Rule;
- import org.junit.Test;
- import org.junit.rules.ExpectedException;
-
- import static org.junit.Assert.assertEquals;
-
- public class PhoneNumberTest {
- private static String wrongLengthExceptionMessage = "Number must be 10 or 11 digits";
- private static String numberIs11DigitsButDoesNotStartWith1ExceptionMessage =
- "Can only have 11 digits if number starts with '1'";
- private static String illegalCharacterExceptionMessage =
- "Illegal character in phone number. Only digits, spaces, parentheses, hyphens or dots accepted.";
- private static String illegalAreaOrExchangeCodeMessage =
- "Illegal Area Or Exchange Code. Only 2-9 are valid digits";
-
- @Rule
- public ExpectedException expectedException = ExpectedException.none();
-
- @Test
- public void cleansTheNumber() {
- String expectedNumber = "2234567890";
- String actualNumber = new PhoneNumber("(223) 456-7890").getNumber();
-
- assertEquals(
- expectedNumber, actualNumber
- );
- }
-
- @Ignore("Remove to run test")
- @Test
- public void cleansNumbersWithDots() {
- String expectedNumber = "2234567890";
- String actualNumber = new PhoneNumber("223.456.7890").getNumber();
-
- assertEquals(
- expectedNumber, actualNumber
- );
- }
-
- @Ignore("Remove to run test")
- @Test
- public void cleansNumbersWithMultipleSpaces() {
- String expectedNumber = "2234567890";
- String actualNumber = new PhoneNumber("223 456 7890 ").getNumber();
-
- assertEquals(
- expectedNumber, actualNumber
- );
- }
-
- @Ignore("Remove to run test")
- @Test
- public void invalidWhen9Digits() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(wrongLengthExceptionMessage);
- new PhoneNumber("123456789");
- }
-
- @Ignore("Remove to run test")
- @Test
- public void invalidWhen11DigitsDoesNotStartWith1() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(numberIs11DigitsButDoesNotStartWith1ExceptionMessage);
- new PhoneNumber("22234567890");
- }
-
- @Ignore("Remove to run test")
- @Test
- public void validWhen11DigitsAndStartingWith1() {
- String expectedNumber = "2234567890";
- String actualNumber = new PhoneNumber("12234567890").getNumber();
-
- assertEquals(
- expectedNumber, actualNumber
- );
- }
-
- @Ignore("Remove to run test")
- @Test
- public void validWhen11DigitsAndStartingWith1EvenWithPunctuation() {
- String expectedNumber = "2234567890";
- String actualNumber = new PhoneNumber("+1 (223) 456-7890").getNumber();
-
- assertEquals(
- expectedNumber, actualNumber
- );
- }
-
- @Ignore("Remove to run test")
- @Test
- public void invalidWhenMoreThan11Digits() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(wrongLengthExceptionMessage);
- new PhoneNumber("321234567890");
- }
-
- @Ignore("Remove to run test")
- @Test
- public void invalidWithLetters() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(illegalCharacterExceptionMessage);
- new PhoneNumber("123-abc-7890");
- }
-
- @Ignore("Remove to run test")
- @Test
- public void invalidWithPunctuations() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(illegalCharacterExceptionMessage);
- new PhoneNumber("123-@:!-7890");
- }
-
- @Ignore("Remove to run test")
- @Test
- public void invalidIfAreaCodeStartsWith0() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
- new PhoneNumber("(023) 456-7890");
- }
-
- @Ignore("Remove to run test")
- @Test
- public void invalidIfAreaCodeStartsWith1() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
- new PhoneNumber("(123) 456-7890");
- }
-
- @Ignore("Remove to run test")
- @Test
- public void invalidIfExchangeCodeStartsWith0() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
- new PhoneNumber("(223) 056-7890");
- }
-
- @Ignore("Remove to run test")
- @Test
- public void invalidIfExchangeCodeStartsWith1() {
- expectedException.expect(IllegalArgumentException.class);
- expectedException.expectMessage(illegalAreaOrExchangeCodeMessage);
- new PhoneNumber("(223) 156-7890");
- }
- }
|