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

SchoolTest.java 3.7KB

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