Simple Phonebook

PhoneBookTest.java 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. import java.util.*;
  2. import static org.junit.Assert.*;
  3. import org.junit.After;
  4. import org.junit.Before;
  5. import org.junit.Test;
  6. /**
  7. * The test class PhoneBookTest.
  8. *
  9. * @author (your name)
  10. * @version (a version number or a date)
  11. */
  12. public class PhoneBookTest
  13. {
  14. @Test
  15. public void testAddEntry(){
  16. //given
  17. PhoneBook phoneBook = new PhoneBook();
  18. //actual
  19. phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
  20. String actual = phoneBook.lookup("eric");
  21. //expected
  22. String expected = "3025884804, 3025884805, 3025884806";
  23. //test
  24. assertEquals(expected, actual);
  25. }
  26. @Test
  27. public void testAddEntryMultiple(){
  28. //given
  29. PhoneBook phoneBook = new PhoneBook();
  30. //actual
  31. phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
  32. phoneBook.addEntry("john", "3025844804", "3025884705");
  33. phoneBook.addEntry("jane", "3025884704");
  34. String actual = phoneBook.lookup("eric");
  35. String actual1 = phoneBook.lookup("john");
  36. String actual2 = phoneBook.lookup("jane");
  37. //expected
  38. String expected = "3025884804, 3025884805, 3025884806";
  39. String expected1 = "3025844804, 3025884705";
  40. String expected2 = "3025884704";
  41. //test
  42. assertEquals(expected, actual);
  43. }
  44. @Test
  45. public void testRemoveEntry(){
  46. //given
  47. PhoneBook phoneBook = new PhoneBook();
  48. //actual
  49. phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
  50. phoneBook.removeEntry("eric");
  51. String actual = phoneBook.lookup("eric");
  52. //expected
  53. String expected = null;
  54. //test
  55. assertEquals(expected, actual);
  56. }
  57. @Test
  58. public void testRemoveEntryMultiple(){
  59. //given
  60. PhoneBook phoneBook = new PhoneBook();
  61. //actual
  62. phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
  63. phoneBook.addEntry("john", "3025844804", "3025884705");
  64. phoneBook.addEntry("jane", "3025884704");
  65. phoneBook.removeEntry("john");
  66. phoneBook.removeEntry("eric");
  67. String actual = phoneBook.lookup("john");
  68. String actual1 = phoneBook.lookup("eric");
  69. String actual2 = phoneBook.lookup("jane");
  70. //expected
  71. String expected = null;
  72. String expected1 = null;
  73. String expected2 = "3025884704";
  74. //test
  75. assertEquals(expected, actual);
  76. assertEquals(expected1, actual1);
  77. assertEquals(expected2, actual2);
  78. }
  79. @Test
  80. public void testReverseLookup(){
  81. //given
  82. PhoneBook phoneBook = new PhoneBook();
  83. //actual
  84. phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
  85. phoneBook.addEntry("john", "3025844804", "3025884705");
  86. phoneBook.addEntry("jane", "3025884704");
  87. String actual = phoneBook.reverseLookup("3025884804", "3025884805", "3025884806");
  88. String actual1 = phoneBook.reverseLookup("3025844804", "3025884705");
  89. String actual2 = phoneBook.reverseLookup("3025884704");
  90. String actual3 = phoneBook.reverseLookup("4025884704");
  91. //expected
  92. String expected = "eric";
  93. String expected1 = "john";
  94. String expected2 = "jane";
  95. String expected3 = null;
  96. //test
  97. assertEquals(expected, actual);
  98. assertEquals(expected1, actual1);
  99. assertEquals(expected2, actual2);
  100. assertEquals(expected3, actual3);
  101. }
  102. @Test
  103. public void testAddNumberToEntry(){
  104. //given
  105. PhoneBook phoneBook = new PhoneBook();
  106. //actual
  107. phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
  108. phoneBook.addNumberToEntry("eric", "3025884807");
  109. String actual = phoneBook.lookup("eric");
  110. //expected
  111. String expected = "3025884804, 3025884805, 3025884806, 3025884807";
  112. //test
  113. assertEquals(expected, actual);
  114. }
  115. @Test
  116. public void testAddNumberToEntryMultiple(){
  117. //given
  118. PhoneBook phoneBook = new PhoneBook();
  119. //actual
  120. phoneBook.addEntry("john", "3025884804", "3025884705");
  121. phoneBook.addNumberToEntry("john", "3025884788", "3025884789");
  122. String actual = phoneBook.lookup("john");
  123. //expected
  124. String expected = "3025884804, 3025884705, 3025884788, 3025884789";
  125. //test
  126. assertEquals(expected, actual);
  127. }
  128. @Test
  129. public void testRemoveNumberFromEntry(){
  130. //given
  131. PhoneBook phoneBook = new PhoneBook();
  132. //actual
  133. phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806");
  134. phoneBook.removeNumberFromEntry("eric", "3025884805");
  135. String actual = phoneBook.lookup("eric");
  136. //expected
  137. String expected = "3025884804, 3025884806";
  138. //test
  139. assertEquals(expected, actual);
  140. }
  141. @Test
  142. public void testRemoveNumberFromEntryMultiple(){
  143. //given
  144. PhoneBook phoneBook = new PhoneBook();
  145. //actual
  146. phoneBook.addEntry("eric", "3025884804", "3025884805", "3025884806", "3025884807");
  147. phoneBook.removeNumberFromEntry("eric", "3025884804", "3025884806");
  148. String actual = phoneBook.lookup("eric");
  149. //expected
  150. String expected = "3025884805, 3025884807";
  151. //test
  152. assertEquals(expected, actual);
  153. }
  154. }