Build a simple PhoneBook program.

PhoneBookTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. import static org.junit.Assert.*;
  2. import org.junit.After;
  3. import org.junit.Before;
  4. import org.junit.Test;
  5. import java.util.*;
  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. PhoneBook test = new PhoneBook();
  15. /**
  16. * Default constructor for test class PhoneBookTest
  17. */
  18. public PhoneBookTest()
  19. {
  20. }
  21. /**
  22. * Sets up the test fixture.
  23. *
  24. * Called before every test case method.
  25. */
  26. @Before
  27. public void setUp()
  28. {
  29. }
  30. @Test
  31. public void addTest(){
  32. //Given
  33. ArrayList<String> list = new ArrayList<String>();
  34. list.add("5704282076");
  35. //When
  36. int expected = 1;
  37. TreeMap actual = test.add("Chad", list);
  38. //Result
  39. assertEquals(expected, actual.size());
  40. }
  41. @Test
  42. public void removeRecordTest(){
  43. //Given
  44. ArrayList<String> list = new ArrayList<String>();
  45. list.add("5704282076");
  46. list.add("5707429337");
  47. test.add("Chad", list);
  48. //When
  49. int expected = 0;
  50. TreeMap actual = test.removeRecord("Chad");
  51. //Result
  52. assertEquals(expected, actual.size());
  53. }
  54. @Test
  55. public void lookUpTest(){
  56. //Given
  57. ArrayList<String> list = new ArrayList<String>();
  58. list.add("5704282076");
  59. test.add("Chad", list);
  60. //When
  61. ArrayList<String> expected = list;
  62. ArrayList<String> actual = test.lookUp("Chad");
  63. //Result
  64. assertEquals(expected, actual);
  65. }
  66. @Test
  67. public void invertMapTest(){
  68. //Given
  69. ArrayList<String> list = new ArrayList<String>();
  70. list.add("5704282076");
  71. test.add("Chad", list);
  72. test.invertMap();
  73. //When
  74. String expected = "Chad";
  75. String actual = test.reverseLookUp("5704282076");
  76. //Result
  77. assertEquals(expected, actual);
  78. }
  79. @Test
  80. public void displayTest(){
  81. //Given
  82. ArrayList<String> list = new ArrayList<String>();
  83. list.add("5704282076");
  84. ArrayList<String> list1 = new ArrayList<String>();
  85. list1.add("111-222-333");
  86. ArrayList<String> list2 = new ArrayList<String>();
  87. list2.add("222-444-4444");
  88. test.add("Chad", list);
  89. test.add("Zebra", list1);
  90. test.add("Dog", list2);
  91. //When
  92. String expected = "Chad 5704282076\nDog 222-444-4444\nZebra 111-222-333\n";
  93. String actual = test.display();
  94. //Result
  95. assertEquals(expected, actual);
  96. }
  97. @Test
  98. public void arrayContainsTest(){
  99. //Given
  100. ArrayList<String> list = new ArrayList<String>();
  101. list.add("5704282076");
  102. //When
  103. boolean actual = test.arrayContains(list,"5704282076");
  104. //Result
  105. assertTrue(actual);
  106. }
  107. @Test
  108. public void removeTest(){
  109. //Given
  110. ArrayList<String> list = new ArrayList<String>();
  111. list.add("5704282076");
  112. list.add("5707429337");
  113. ArrayList<String> list1 = new ArrayList<String>();
  114. list1.add("111-222-333");
  115. ArrayList<String> list2 = new ArrayList<String>();
  116. list2.add("222-444-4444");
  117. test.add("Chad", list);
  118. test.add("Zebra", list1);
  119. test.add("Dog", list2);
  120. test.remove("Chad","5704282076");
  121. //When
  122. boolean actual = test.arrayContains(list,"5704282076");
  123. //Result
  124. assertFalse(actual);
  125. }
  126. /**
  127. * Tears down the test fixture.
  128. *
  129. * Called after every test case method.
  130. */
  131. @After
  132. public void tearDown()
  133. {
  134. }
  135. }