Parcourir la source

Issue #253: Tried to improve grade school structure. (#259)

* Issue #253: Tried to improve grade school structure. Removed .db() function, added numberOfStudents functions and renamed the sort function to studentsByGradeAlphabetical. Added test to check that grade() gets the students in the order they were inserted. Removed getsStudentsInAGrade() test as what it was testing was already being covered by the tests before it.

* Added tests for protection agains mutation of the list object
FridaTveit il y a 9 ans
Parent
révision
c3968d5066

+ 20
- 19
exercises/grade-school/src/example/java/School.java Voir le fichier

1
-import java.util.ArrayList;
2
-import java.util.HashMap;
3
-import java.util.List;
4
-import java.util.Map;
5
-import java.util.Set;
6
-import java.util.TreeSet;
1
+import java.util.*;
7
 
2
 
8
 public class School {
3
 public class School {
9
 
4
 
10
-  private final Map<Integer, Set<String>> database = new HashMap<Integer, Set<String>>();
5
+  private final Map<Integer, List<String>> database = new HashMap<>();
11
 
6
 
12
-  public Map<Integer, Set<String>> db() {
13
-    // Leaks internal storage to caller
14
-    return database;
7
+  public int numberOfStudents() {
8
+    int result = 0;
9
+    for (List<String> studentsInGrade: database.values()) {
10
+      result += studentsInGrade.size();
11
+    }
12
+    return result;
15
   }
13
   }
16
 
14
 
17
   public void add(String student, int grade) {
15
   public void add(String student, int grade) {
18
-    Set<String> students = grade(grade);
16
+    List<String> students = fetchGradeFromDatabase(grade);
19
     students.add(student);
17
     students.add(student);
20
   }
18
   }
21
 
19
 
22
-  public Set<String> grade(int grade) {
23
-    // Leaks internal storage to caller
20
+  public List<String> grade(int grade) {
21
+    return new ArrayList<>(fetchGradeFromDatabase(grade));
22
+  }
23
+
24
+  private List<String> fetchGradeFromDatabase(int grade) {
24
     if (!database.containsKey(grade)) {
25
     if (!database.containsKey(grade)) {
25
-      database.put(grade, new TreeSet<String>());
26
+      database.put(grade, new LinkedList<>());
26
     }
27
     }
27
     return database.get(grade);
28
     return database.get(grade);
28
   }
29
   }
29
 
30
 
30
-  public Map<Integer, List<String>> sort() {
31
-    Map<Integer, List<String>> sortedStudents = new HashMap<Integer, List<String>>();
31
+  public Map<Integer, List<String>> studentsByGradeAlphabetical() {
32
+    Map<Integer, List<String>> sortedStudents = new HashMap<>();
32
     for (Integer grade : database.keySet()) {
33
     for (Integer grade : database.keySet()) {
33
-      // Relies on using a TreeSet internally
34
-      List<String> sortedGrade = new ArrayList<String>(database.get(grade));
35
-      sortedStudents.put(grade, sortedGrade);
34
+      List<String> studentsInGrade = database.get(grade);
35
+      Collections.sort(studentsInGrade);
36
+      sortedStudents.put(grade, studentsInGrade);
36
     }
37
     }
37
     return sortedStudents;
38
     return sortedStudents;
38
   }
39
   }

+ 53
- 20
exercises/grade-school/src/test/java/SchoolTest.java Voir le fichier

1
 import org.junit.Ignore;
1
 import org.junit.Ignore;
2
 import org.junit.Test;
2
 import org.junit.Test;
3
 
3
 
4
+import java.lang.Integer;
5
+import java.util.*;
6
+import java.util.ArrayList;
4
 import java.util.Arrays;
7
 import java.util.Arrays;
5
 import java.util.HashMap;
8
 import java.util.HashMap;
6
 import java.util.List;
9
 import java.util.List;
7
 import java.util.Map;
10
 import java.util.Map;
8
 
11
 
9
 import static org.hamcrest.CoreMatchers.*;
12
 import static org.hamcrest.CoreMatchers.*;
10
-import static org.junit.Assert.*;
13
+import static org.junit.Assert.assertThat;
14
+import static org.junit.Assert.assertTrue;
15
+import static org.junit.Assert.assertEquals;
11
 
16
 
12
 public class SchoolTest {
17
 public class SchoolTest {
13
   private final School school = new School();
18
   private final School school = new School();
14
 
19
 
15
-
16
   @Test
20
   @Test
17
   public void startsWithNoStudents() {
21
   public void startsWithNoStudents() {
18
-    assertTrue(school.db().isEmpty());
22
+    assertThat(school.numberOfStudents(), is(0));
19
   }
23
   }
20
 
24
 
21
   @Ignore
25
   @Ignore
22
   @Test
26
   @Test
23
   public void addsStudents() {
27
   public void addsStudents() {
24
     school.add("Aimee", 2);
28
     school.add("Aimee", 2);
25
-    assertThat(school.db().get(2), hasItem("Aimee"));
29
+    assertThat(school.grade(2), hasItem("Aimee"));
26
   }
30
   }
27
 
31
 
28
   @Ignore
32
   @Ignore
33
     school.add("Blair", grade);
37
     school.add("Blair", grade);
34
     school.add("Paul", grade);
38
     school.add("Paul", grade);
35
 
39
 
36
-    assertThat(school.db().get(grade).size(), is(3));
37
-    assertThat(school.db().get(grade), allOf(hasItem("James"), hasItem("Blair"), hasItem("Paul")));
40
+    assertThat(school.grade(grade).size(), is(3));
41
+    assertThat(school.grade(grade), allOf(hasItem("James"), hasItem("Blair"), hasItem("Paul")));
38
   }
42
   }
39
 
43
 
40
   @Ignore
44
   @Ignore
43
     school.add("Chelsea", 3);
47
     school.add("Chelsea", 3);
44
     school.add("Logan", 7);
48
     school.add("Logan", 7);
45
 
49
 
46
-    assertThat(school.db().size(), is(2));
47
-    assertThat(school.db().get(3).size(), is(1));
48
-    assertThat(school.db().get(3), hasItem("Chelsea"));
49
-    assertThat(school.db().get(7).size(), is(1));
50
-    assertThat(school.db().get(7), hasItem("Logan"));
50
+    assertThat(school.numberOfStudents(), is(2));
51
+    assertThat(school.grade(3).size(), is(1));
52
+    assertThat(school.grade(3), hasItem("Chelsea"));
53
+    assertThat(school.grade(7).size(), is(1));
54
+    assertThat(school.grade(7), hasItem("Logan"));
51
   }
55
   }
52
 
56
 
53
   @Ignore
57
   @Ignore
54
   @Test
58
   @Test
55
-  public void getsStudentsInAGrade() {
56
-    school.add("Franklin", 5);
57
-    school.add("Bradley", 5);
58
-    school.add("Jeff", 1);
59
-    assertThat(school.grade(5).size(), is(2));
60
-    assertThat(school.grade(5), allOf(hasItem("Franklin"), hasItem("Bradley")));
59
+  public void getsStudentsInEmptyGrade() {
60
+    assertTrue(school.grade(1).isEmpty());
61
   }
61
   }
62
 
62
 
63
   @Ignore
63
   @Ignore
64
   @Test
64
   @Test
65
-  public void getsStudentsInEmptyGrade() {
66
-    assertTrue(school.grade(1).isEmpty());
65
+  public void gradeReturnsStudentsInTheOrderTheyWereInserted() {
66
+    int grade = 4;
67
+    school.add("Bartimaeus", grade);
68
+    school.add("Nathaniel", grade);
69
+    school.add("Faquarl", grade);
70
+    List<String> studentsInGrade = school.grade(grade);
71
+    assertThat(studentsInGrade.get(0), is("Bartimaeus"));
72
+    assertThat(studentsInGrade.get(1), is("Nathaniel"));
73
+    assertThat(studentsInGrade.get(2), is("Faquarl"));
67
   }
74
   }
68
 
75
 
69
   @Ignore
76
   @Ignore
77
     sortedStudents.put(6, Arrays.asList("Kareem"));
84
     sortedStudents.put(6, Arrays.asList("Kareem"));
78
     sortedStudents.put(4, Arrays.asList("Christopher", "Jennifer"));
85
     sortedStudents.put(4, Arrays.asList("Christopher", "Jennifer"));
79
     sortedStudents.put(3, Arrays.asList("Kyle"));
86
     sortedStudents.put(3, Arrays.asList("Kyle"));
80
-    assertEquals(school.sort(), sortedStudents);
87
+    assertEquals(school.studentsByGradeAlphabetical(), sortedStudents);
88
+  }
89
+
90
+  @Ignore
91
+  @Test
92
+  public void modifyingFetchedGradeShouldNotModifyInternalDatabase() {
93
+    String shouldNotBeAdded = "Should not be added to school";
94
+    int grade = 1;
95
+
96
+    List<String> students = school.grade(grade);
97
+    students.add(shouldNotBeAdded);
98
+
99
+    assertThat(school.grade(grade), not(hasItem(shouldNotBeAdded)));
100
+  }
101
+
102
+  @Ignore
103
+  @Test
104
+  public void modifyingSortedStudentsShouldNotModifyInternalDatabase() {
105
+    int grade = 2;
106
+    String studentWhichShouldNotBeAdded = "Should not be added";
107
+    List<String> listWhichShouldNotBeAdded = new ArrayList<>();
108
+    listWhichShouldNotBeAdded.add(studentWhichShouldNotBeAdded);
109
+
110
+    Map<Integer, List<String>> sortedStudents = school.studentsByGradeAlphabetical();
111
+    sortedStudents.put(grade,listWhichShouldNotBeAdded);
112
+
113
+    assertThat(school.studentsByGradeAlphabetical().get(grade), not(hasItem(studentWhichShouldNotBeAdded)));
81
   }
114
   }
82
 }
115
 }