lots of exercises in java... from https://github.com/exercism/java

SchoolTest.java 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. import org.junit.Ignore;
  2. import org.junit.Test;
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.List;
  7. import java.util.Collection;
  8. import org.hamcrest.Matcher;
  9. import org.hamcrest.collection.IsIterableContainingInOrder;
  10. import static org.hamcrest.CoreMatchers.*;
  11. import static org.junit.Assert.assertThat;
  12. import static org.junit.Assert.assertTrue;
  13. import static org.junit.Assert.assertEquals;
  14. public class SchoolTest {
  15. private final School school = new School();
  16. @Test
  17. public void startsWithNoStudents() {
  18. assertThat(school.numberOfStudents(), is(0));
  19. }
  20. @Ignore
  21. @Test
  22. public void addsStudents() {
  23. school.add("Aimee", 2);
  24. assertThat(school.grade(2), hasItem("Aimee"));
  25. }
  26. @Ignore
  27. @Test
  28. public void addsMoreStudentsInSameGrade() {
  29. final int grade = 2;
  30. school.add("James", grade);
  31. school.add("Blair", grade);
  32. school.add("Paul", grade);
  33. assertThat(school.grade(grade).size(), is(3));
  34. assertThat(school.grade(grade), allOf(hasItem("James"), hasItem("Blair"), hasItem("Paul")));
  35. }
  36. @Ignore
  37. @Test
  38. public void addsStudentsInMultipleGrades() {
  39. school.add("Chelsea", 3);
  40. school.add("Logan", 7);
  41. assertThat(school.numberOfStudents(), is(2));
  42. assertThat(school.grade(3).size(), is(1));
  43. assertThat(school.grade(3), hasItem("Chelsea"));
  44. assertThat(school.grade(7).size(), is(1));
  45. assertThat(school.grade(7), hasItem("Logan"));
  46. }
  47. @Ignore
  48. @Test
  49. public void getsStudentsInEmptyGrade() {
  50. assertTrue(school.grade(1).isEmpty());
  51. }
  52. @Ignore
  53. @Test
  54. public void sortsSchool() {
  55. school.add("Kyle", 4);
  56. school.add("Zed", 4);
  57. school.add("Adam", 4);
  58. school.add("Jennifer", 4);
  59. school.add("Kareem", 6);
  60. school.add("Christopher", 4);
  61. school.add("Kylie", 3);
  62. Map<Integer, Matcher> sortedStudents = new HashMap<Integer, Matcher>();
  63. sortedStudents.put(6, IsIterableContainingInOrder
  64. .contains("Kareem"));
  65. sortedStudents.put(4, IsIterableContainingInOrder
  66. .contains("Adam", "Christopher", "Jennifer", "Kyle", "Zed"));
  67. sortedStudents.put(3, IsIterableContainingInOrder
  68. .contains("Kylie"));
  69. Map schoolStudents = school.studentsByGradeAlphabetical();
  70. for (Map.Entry<?, Matcher> entry : sortedStudents.entrySet()) {
  71. assertThat((Collection) schoolStudents.get(entry.getKey()), entry.getValue());
  72. }
  73. }
  74. @Ignore
  75. @Test
  76. public void modifyingFetchedGradeShouldNotModifyInternalDatabase() {
  77. String shouldNotBeAdded = "Should not be added to school";
  78. int grade = 1;
  79. Collection students = school.grade(grade);
  80. try {
  81. students.add(shouldNotBeAdded);
  82. } catch (Exception exception) {
  83. // Also valid that the add operation throws an exception
  84. // Such as UnsupportedOperationException when an umodifiable collection type is used
  85. }
  86. assertThat(school.grade(grade), not(hasItem(shouldNotBeAdded)));
  87. }
  88. @Ignore
  89. @Test
  90. public void modifyingSortedStudentsShouldNotModifyInternalDatabase() {
  91. int grade = 2;
  92. String studentWhichShouldNotBeAdded = "Should not be added";
  93. List<String> listWhichShouldNotBeAdded = new ArrayList<>();
  94. listWhichShouldNotBeAdded.add(studentWhichShouldNotBeAdded);
  95. Map sortedStudents = school.studentsByGradeAlphabetical();
  96. try {
  97. sortedStudents.put(grade, listWhichShouldNotBeAdded);
  98. } catch (Exception exception) {
  99. // Also valid that the put operation throws an exception
  100. // Such as UnsupportedOperationException when an unmodifiableMap is used
  101. }
  102. assertThat(school.studentsByGradeAlphabetical().get(grade),
  103. not(hasItem(studentWhichShouldNotBeAdded)));
  104. }
  105. }