1
1
package com .vogella .android .test .juntexamples ;
2
2
3
+ import com .vogella .android .test .juntexamples .model .Movie ;
4
+ import com .vogella .android .test .juntexamples .model .Race ;
5
+ import com .vogella .android .test .juntexamples .model .Ring ;
3
6
import com .vogella .android .test .juntexamples .model .TolkienCharacter ;
4
7
5
- import org .junit .Assume ;
6
8
import org .junit .Test ;
7
9
8
10
import java .util .List ;
11
+ import java .util .Map ;
9
12
10
13
import static com .vogella .android .test .juntexamples .model .Race .HOBBIT ;
11
14
import static org .junit .Assert .assertEquals ;
15
+ import static org .junit .Assert .assertNotEquals ;
12
16
import static org .junit .Assert .assertTrue ;
13
17
14
18
/**
17
21
18
22
public class DataModelTests {
19
23
24
+ private DataService dataService ;
25
+
20
26
@ Test
21
27
public void validateTolkeinCharactorsInitializationWorks () {
22
28
TolkienCharacter frodo = new TolkienCharacter ("Frodo" , 33 , HOBBIT );
23
- // age is 33
24
- //name is "Frodo"
25
- //name is not "Frodon"
29
+ assertEquals ( frodo . age , 33 );
30
+ assertEquals ( frodo . getName (), "Frodo" );
31
+ assertNotEquals ( frodo . getName (), "Frodon" );
26
32
}
27
33
28
34
@ Test
29
35
public void checkEquals (){
30
36
Object jake = new TolkienCharacter ("Jake" , 43 , HOBBIT );
31
37
Object sameJake = jake ;
32
- Object jakeClone = new TolkienCharacter ("Jake" ,12 , HOBBIT );
33
- // check that:
34
- // jack is equal to sameJake
35
- // jack is equal to jakeClone
38
+ Object jakeClone = new TolkienCharacter ("Jake" ,43 , HOBBIT );
36
39
assertEquals (jake , jakeClone );
37
40
}
38
41
@@ -44,7 +47,7 @@ public void checkInheritance() {
44
47
TolkienCharacter tolkienCharacter = dataService .getFellowship ().get (0 );
45
48
// checkthat tolkienCharacter.getClass is not a movie class
46
49
47
- assertTrue (false );
50
+ assertTrue (! tolkienCharacter . getClass (). equals ( Movie . class ) );
48
51
}
49
52
50
53
@ Test
@@ -53,16 +56,22 @@ public void ensureThatFrodoAndGandalfArePartOfTheFellowsip() {
53
56
List <TolkienCharacter > fellowship = dataService .getFellowship ();
54
57
55
58
// ensure that Frodo and Gandalf are part of the fellowship
56
- assertTrue (false );
59
+ assertTrue (fellowship . contains ( dataService . frodo )&& fellowship . contains ( dataService . gandalf ) );
57
60
}
58
61
59
62
@ Test
60
63
public void testThatOneRingBearerIsPartOfTheFellowship () {
61
64
DataService dataService = new DataService ();
62
65
List <TolkienCharacter > fellowship = dataService .getFellowship ();
63
-
64
- // ensure that Frodo and Gandalf are part of the fellowship
65
- assertTrue (false );
66
+ Map <Ring , TolkienCharacter > ringBearers = dataService .getRingBearers ();
67
+ boolean oneRingBearer = false ;
68
+ for (TolkienCharacter character : ringBearers .values ()) {
69
+ if (fellowship .contains (character )) {
70
+ oneRingBearer = true ;
71
+ }
72
+ }
73
+ // test that at least one ring bearer is part of the fellowship
74
+ assertTrue (oneRingBearer );
66
75
}
67
76
68
77
@ Test
@@ -72,22 +81,33 @@ public void ensureOrdering() {
72
81
73
82
// ensure that the order of the fellowship is:
74
83
//frodo, sam, merry,pippin, gandalf,legolas,gimli,aragorn,boromir
75
- assertTrue (false );
84
+
85
+ assertEquals (fellowship .get (0 ),dataService .frodo );
86
+ assertEquals (fellowship .get (1 ),dataService .sam );
87
+ assertEquals (fellowship .get (2 ),dataService .merry );
88
+ assertEquals (fellowship .get (3 ),dataService .pippin );
89
+ assertEquals (fellowship .get (4 ),dataService .gandalf );
90
+ assertEquals (fellowship .get (5 ),dataService .legolas );
91
+ assertEquals (fellowship .get (6 ),dataService .gimli );
92
+ assertEquals (fellowship .get (7 ),dataService .aragorn );
93
+ assertEquals (fellowship .get (8 ),dataService .boromir );
76
94
}
77
95
78
96
@ Test
79
97
public void ensureAge () {
80
98
DataService dataService = new DataService ();
81
99
List <TolkienCharacter > fellowship = dataService .getFellowship ();
82
-
83
- // ensure that all hobbits and men are younger than 100 years
84
-
85
-
86
- // also ensure that the elfs, dwars the maia are all older than 100 years
87
- assertTrue (false );
100
+ for (TolkienCharacter character : fellowship ) {
101
+ if (character .getRace ().equals (Race .HOBBIT ) || character .getRace ().equals (Race .MAN ) ) {
102
+ assertTrue (character .age < 100 );
103
+ } else {
104
+ assertTrue (character .age > 100 );
105
+
106
+ }
107
+ }
88
108
}
89
109
90
- @ Test
110
+ @ Test ( expected = IndexOutOfBoundsException . class )
91
111
public void ensureThatFellowsStayASmallGroup () {
92
112
DataService dataService = new DataService ();
93
113
List <TolkienCharacter > fellowship = dataService .getFellowship ();
0 commit comments