Build a simple PhoneBook program.

PhoneBookTest.java 1.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. private PhoneBook pb = new PhoneBook();
  15. @Before
  16. public void setup() {
  17. pb = new PhoneBook();
  18. }
  19. @Test
  20. public void addNameStringNumbers() {
  21. assertEquals(
  22. pb.add("John", "101-101-1011"),
  23. new ArrayList<String>(
  24. Arrays.asList("101-101-1011")
  25. )
  26. );
  27. }
  28. @Test
  29. public void addNameArrayList() {
  30. ArrayList<String> numbers = new ArrayList<String>(Arrays.asList(
  31. "101-101-1011",
  32. "202-202-2022"
  33. ));
  34. assertEquals(
  35. pb.add("John", numbers),
  36. numbers
  37. );
  38. };
  39. @Test
  40. public void removeByName() {
  41. pb.add("John", "101-101-1011");
  42. pb.remove("John");
  43. assertEquals(
  44. pb.lookup("John"),
  45. null
  46. );
  47. }
  48. @Test
  49. public void removeMultipleNumbers() {
  50. pb.add("John", "101-101-1011");
  51. pb.add("John", "201-101-1011");
  52. pb.remove("John", "201-101-1011");
  53. assertEquals(
  54. pb.lookup("John"),
  55. new ArrayList<String>(Arrays.asList("101-101-1011"))
  56. );
  57. }
  58. @Test
  59. public void lookup() {
  60. pb.add("John", "101-101-1011");
  61. assertEquals(
  62. pb.lookup("John"),
  63. new ArrayList<String>(Arrays.asList("101-101-1011"))
  64. );
  65. }
  66. @Test
  67. public void reverseLookup() {
  68. pb.add("John", "101-101-1011");
  69. assertEquals(
  70. pb.reverseLookup("101-101-1011"),
  71. "John"
  72. );
  73. }
  74. }