Simple Phonebook

PhoneBookTest.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. import 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. TreeMap<String, ArrayList<String>> phoneBook = new TreeMap<>();
  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. PhoneBook pb = new PhoneBook();
  30. }
  31. /**
  32. * Tears down the test fixture.
  33. *
  34. * Called after every test case method.
  35. */
  36. @After
  37. public void tearDown()
  38. {
  39. }
  40. // add entries
  41. @Test
  42. public void testAdd() {
  43. String expected = "Zebra 111-222-3333 222-444-5555\n";
  44. PhoneBook pb = new PhoneBook();
  45. pb.add("Zebra", "111-222-3333");
  46. pb.add("Zebra", "222-444-5555");
  47. String actual = pb.display();
  48. Assert.assertEquals(expected, actual);
  49. }
  50. @Test
  51. public void testRemove() {
  52. String expected = "Zebra 222-444-5555\n";
  53. PhoneBook pb = new PhoneBook(phoneBook);
  54. pb.add("Zebra", "111-222-3333");
  55. pb.add("Zebra", "222-444-5555");
  56. pb.remove("Zebra", "111-222-3333");
  57. String actual = pb.display();
  58. Assert.assertEquals(expected, actual);
  59. }
  60. @Test
  61. public void testRemoveRecord() {
  62. String expected = "Zebra 222-444-5555\n";
  63. PhoneBook pb = new PhoneBook(phoneBook);
  64. pb.add("Dog", "111-222-3333");
  65. pb.add("Dog", "333-444-5555");
  66. pb.add("Zebra", "222-444-5555");
  67. pb.removeRecord("Dog");
  68. String actual = pb.display();
  69. Assert.assertEquals(expected, actual);
  70. }
  71. @Test
  72. public void testLookup() {
  73. ArrayList<String> expected = new ArrayList<>();
  74. expected.add("111-222-3333");
  75. expected.add("222-444-5555");
  76. PhoneBook pb = new PhoneBook(phoneBook);
  77. pb.add("Zebra", "111-222-3333");
  78. pb.add("Zebra", "222-444-5555");
  79. ArrayList<String> actual = pb.lookup("Zebra");
  80. Assert.assertEquals(expected, actual);
  81. }
  82. @Test
  83. public void testReverseLookup() {
  84. String expected = "Zebra";
  85. PhoneBook pb = new PhoneBook(phoneBook);
  86. pb.add("Zebra", "111-222-3333");
  87. pb.add("Zebra", "222-444-5555");
  88. String actual = pb.reverseLookup("222-444-5555");
  89. Assert.assertEquals(expected, actual);
  90. }
  91. }