Build a simple PhoneBook program.

PhoneBookTest.java 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. import static org.junit.Assert.*;
  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. public class PhoneBookTest
  6. {
  7. @Before
  8. public void setUp(){
  9. PhoneBook pb = new PhoneBook();
  10. }
  11. @Test
  12. public void testAdd1(){
  13. PhoneBook pb = new PhoneBook();
  14. String expected = "123, 456";
  15. pb.addEntrant("John" , "123", "456");
  16. String actual = pb.lookup("John");
  17. assertEquals(expected, actual);
  18. }
  19. @Test
  20. public void testAdd2(){
  21. PhoneBook pb = new PhoneBook();
  22. String expected = "123";
  23. pb.addEntrant("Josh" , "123");
  24. String actual = pb.lookup("Josh");
  25. assertEquals(expected, actual);
  26. }
  27. @Test
  28. public void testRemove(){
  29. PhoneBook pb = new PhoneBook();
  30. String expected = null;
  31. pb.addEntrant("Josh" , "123");
  32. pb.removeEntrant("Josh");
  33. String actual = pb.lookup("Josh");
  34. assertEquals(expected, actual);
  35. }
  36. @Test
  37. public void testLookup(){
  38. PhoneBook pb = new PhoneBook();
  39. String expected = "123, 456, 789";
  40. pb.addEntrant("Josh" , "123", "456", "789");
  41. String actual = pb.lookup("Josh");
  42. assertEquals(expected, actual);
  43. }
  44. @Test
  45. public void testReverseLookup1(){
  46. PhoneBook pb = new PhoneBook();
  47. String expected = "Joe";
  48. pb.addEntrant("Josh" , "123", "456", "789");
  49. pb.addEntrant("John" , "321", "654", "987");
  50. pb.addEntrant("Joe" , "111", "222", "333");
  51. String actual = pb.reverseLookup("222");
  52. assertEquals(expected, actual);
  53. }
  54. @Test
  55. public void testReverseLookup2(){
  56. PhoneBook pb = new PhoneBook();
  57. String expected = "John";
  58. pb.addEntrant("Josh" , "123", "456", "789");
  59. pb.addEntrant("John" , "321", "654", "987");
  60. pb.addEntrant("Joe" , "111", "222", "333");
  61. String actual = pb.reverseLookup("654");
  62. assertEquals(expected, actual);
  63. }
  64. @Test
  65. public void testAddNumber1(){
  66. PhoneBook pb = new PhoneBook();
  67. String expected = "123, 456";
  68. pb.addEntrant("Josh" , "123");
  69. pb.addNumber("Josh","456");
  70. String actual = pb.lookup("Josh");
  71. assertEquals(expected, actual);
  72. }
  73. @Test
  74. public void testAddNumber2(){
  75. PhoneBook pb = new PhoneBook();
  76. String expected = "123, 456, 789";
  77. pb.addEntrant("Jim" , "123");
  78. pb.addNumber("Jim","456");
  79. pb.addNumber("Jim","789");
  80. String actual = pb.lookup("Jim");
  81. assertEquals(expected, actual);
  82. }
  83. @Test
  84. public void testRemoveNumber1(){
  85. PhoneBook pb = new PhoneBook();
  86. String expected = "123, 456";
  87. pb.addEntrant("Jim" , "123", "456", "789");
  88. pb.removeNumber("Jim", "789");
  89. String actual = pb.lookup("Jim");
  90. assertEquals(expected, actual);
  91. }
  92. @Test
  93. public void testRemoveNumber2(){
  94. PhoneBook pb = new PhoneBook();
  95. String expected = "123, 789";
  96. pb.addEntrant("Jim" , "123", "456", "789");
  97. pb.removeNumber("Jim", "456");
  98. String actual = pb.lookup("Jim");
  99. assertEquals(expected, actual);
  100. }
  101. }