11package com .vogella .android .test .juntexamples ;
22
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 ;
36import com .vogella .android .test .juntexamples .model .TolkienCharacter ;
47
5- import org .junit .Assume ;
68import org .junit .Test ;
79
810import java .util .List ;
11+ import java .util .Map ;
912
1013import static com .vogella .android .test .juntexamples .model .Race .HOBBIT ;
1114import static org .junit .Assert .assertEquals ;
15+ import static org .junit .Assert .assertNotEquals ;
1216import static org .junit .Assert .assertTrue ;
1317
1418/**
1721
1822public class DataModelTests {
1923
24+ private DataService dataService ;
25+
2026 @ Test
2127 public void validateTolkeinCharactorsInitializationWorks () {
2228 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" );
2632 }
2733
2834 @ Test
2935 public void checkEquals (){
3036 Object jake = new TolkienCharacter ("Jake" , 43 , HOBBIT );
3137 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 );
3639 assertEquals (jake , jakeClone );
3740 }
3841
@@ -44,7 +47,7 @@ public void checkInheritance() {
4447 TolkienCharacter tolkienCharacter = dataService .getFellowship ().get (0 );
4548 // checkthat tolkienCharacter.getClass is not a movie class
4649
47- assertTrue (false );
50+ assertTrue (! tolkienCharacter . getClass (). equals ( Movie . class ) );
4851 }
4952
5053 @ Test
@@ -53,16 +56,22 @@ public void ensureThatFrodoAndGandalfArePartOfTheFellowsip() {
5356 List <TolkienCharacter > fellowship = dataService .getFellowship ();
5457
5558 // ensure that Frodo and Gandalf are part of the fellowship
56- assertTrue (false );
59+ assertTrue (fellowship . contains ( dataService . frodo )&& fellowship . contains ( dataService . gandalf ) );
5760 }
5861
5962 @ Test
6063 public void testThatOneRingBearerIsPartOfTheFellowship () {
6164 DataService dataService = new DataService ();
6265 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 );
6675 }
6776
6877 @ Test
@@ -72,22 +81,33 @@ public void ensureOrdering() {
7281
7382 // ensure that the order of the fellowship is:
7483 //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 );
7694 }
7795
7896 @ Test
7997 public void ensureAge () {
8098 DataService dataService = new DataService ();
8199 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+ }
88108 }
89109
90- @ Test
110+ @ Test ( expected = IndexOutOfBoundsException . class )
91111 public void ensureThatFellowsStayASmallGroup () {
92112 DataService dataService = new DataService ();
93113 List <TolkienCharacter > fellowship = dataService .getFellowship ();
0 commit comments