PhoneNumberFactoryTest.java 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. package com.zipcodewilmington;
  2. import com.zipcodewilmington.exceptions.InvalidPhoneNumberFormatException;
  3. import com.zipcodewilmington.phone.PhoneNumber;
  4. import com.zipcodewilmington.phone.PhoneNumberFactory;
  5. import org.junit.Assert;
  6. import org.junit.Before;
  7. import org.junit.Test;
  8. import java.util.logging.Level;
  9. import java.util.logging.Logger;
  10. /**
  11. * Created by leon on 5/9/17.
  12. */
  13. public class PhoneNumberFactoryTest {
  14. @Test(expected = InvalidPhoneNumberFormatException.class)
  15. public void testInvalidPhoneNumberFormatException() throws InvalidPhoneNumberFormatException {
  16. PhoneNumberFactory.createPhoneNumber("-1");
  17. }
  18. @Test
  19. public void testCreatePhoneNumberSafely() throws InvalidPhoneNumberFormatException {
  20. // : Given
  21. int areaCode = 0;
  22. int centralOfficeCode = 0;
  23. int phoneLineCode = 0;
  24. // : When
  25. PhoneNumber phoneNumber = null;
  26. phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
  27. // : Then
  28. Assert.assertEquals(null, phoneNumber);
  29. }
  30. @Test
  31. public void testGetAreaCode() throws InvalidPhoneNumberFormatException {
  32. // : Given
  33. Integer areaCode = 302;
  34. int centralOfficeCode = 312;
  35. int phoneLineCode = 5555;
  36. // : When
  37. PhoneNumber phoneNumber = null;
  38. phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
  39. // : Then
  40. Assert.assertEquals(phoneNumber.getAreaCode(), areaCode.toString());
  41. }
  42. @Test
  43. public void testGetCentralOfficeCode() throws InvalidPhoneNumberFormatException {
  44. // : Given
  45. int areaCode = 302;
  46. Integer centralOfficeCode = 312;
  47. int phoneLineCode = 5555;
  48. // : When
  49. PhoneNumber phoneNumber = null;
  50. phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
  51. // : Then
  52. Assert.assertEquals(phoneNumber.getCentralOfficeCode(), centralOfficeCode.toString());
  53. }
  54. @Test
  55. public void testPhoneLineCode() throws InvalidPhoneNumberFormatException {
  56. // : Given
  57. int areaCode = 302;
  58. int centralOfficeCode = 312;
  59. Integer phoneLineCode = 5555;
  60. // : When
  61. PhoneNumber phoneNumber = null;
  62. phoneNumber = PhoneNumberFactory.createPhoneNumberSafely(areaCode, centralOfficeCode, phoneLineCode);
  63. // : Then
  64. Assert.assertEquals(phoneNumber.getPhoneLineCode(), phoneLineCode.toString());
  65. }
  66. @Test
  67. public void testCreateRandomPhoneNumber() throws InvalidPhoneNumberFormatException {
  68. for (int i = 0; i < 999; i++) {
  69. // : Given
  70. // : When
  71. PhoneNumber phoneNumber = null;
  72. phoneNumber = PhoneNumberFactory.createRandomPhoneNumber();
  73. // : Then
  74. Assert.assertTrue(phoneNumber != null);
  75. }
  76. }
  77. }