Skip to content

Commit 0989c9b

Browse files
committed
Merge pull request iluwatar#240 from themoffster/master
Improved unit tests so assertions are used.
2 parents db11dc4 + 5956873 commit 0989c9b

File tree

11 files changed

+148
-44
lines changed

11 files changed

+148
-44
lines changed
Lines changed: 52 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.iluwatar.abstractfactory;
22

3+
34
/**
45
*
56
* The essence of the Abstract Factory pattern is a factory interface
@@ -12,26 +13,61 @@
1213
*/
1314
public class App {
1415

15-
/**
16-
* Program entry point
17-
* @param args command line arguments
18-
*/
19-
public static void main(String[] args) {
20-
createKingdom(new ElfKingdomFactory());
21-
createKingdom(new OrcKingdomFactory());
22-
}
16+
private King king;
17+
private Castle castle;
18+
private Army army;
2319

2420
/**
2521
* Creates kingdom
2622
* @param factory
2723
*/
28-
public static void createKingdom(KingdomFactory factory) {
29-
King king = factory.createKing();
30-
Castle castle = factory.createCastle();
31-
Army army = factory.createArmy();
32-
System.out.println("The kingdom was created.");
33-
System.out.println(king);
34-
System.out.println(castle);
35-
System.out.println(army);
24+
public void createKingdom(final KingdomFactory factory) {
25+
setKing(factory.createKing());
26+
setCastle(factory.createCastle());
27+
setArmy(factory.createArmy());
28+
}
29+
30+
ElfKingdomFactory getElfKingdomFactory() {
31+
return new ElfKingdomFactory();
32+
}
33+
34+
OrcKingdomFactory getOrcKingdomFactory() {
35+
return new OrcKingdomFactory();
36+
}
37+
38+
King getKing(final KingdomFactory factory) {
39+
return factory.createKing();
40+
}
41+
42+
Castle getCastle(final KingdomFactory factory) {
43+
return factory.createCastle();
44+
}
45+
46+
Army getArmy(final KingdomFactory factory) {
47+
return factory.createArmy();
48+
}
49+
50+
public King getKing() {
51+
return king;
52+
}
53+
54+
private void setKing(final King king) {
55+
this.king = king;
56+
}
57+
58+
public Castle getCastle() {
59+
return castle;
60+
}
61+
62+
private void setCastle(final Castle castle) {
63+
this.castle = castle;
64+
}
65+
66+
public Army getArmy() {
67+
return army;
68+
}
69+
70+
private void setArmy(final Army army) {
71+
this.army = army;
3672
}
3773
}

abstract-factory/src/main/java/com/iluwatar/abstractfactory/Army.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
*/
88
public interface Army {
99

10+
String getDescription();
1011
}

abstract-factory/src/main/java/com/iluwatar/abstractfactory/Castle.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
*/
88
public interface Castle {
99

10+
String getDescription();
1011
}

abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfArmy.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
*/
88
public class ElfArmy implements Army {
99

10+
static final String DESCRIPTION = "This is the Elven Army!";
11+
1012
@Override
11-
public String toString() {
12-
return "This is the Elven Army!";
13+
public String getDescription() {
14+
return DESCRIPTION;
1315
}
14-
1516
}

abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfCastle.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
*/
88
public class ElfCastle implements Castle {
99

10+
static final String DESCRIPTION = "This is the Elven castle!";
11+
1012
@Override
11-
public String toString() {
12-
return "This is the Elven castle!";
13+
public String getDescription() {
14+
return DESCRIPTION;
1315
}
14-
1516
}

abstract-factory/src/main/java/com/iluwatar/abstractfactory/ElfKing.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
*/
88
public class ElfKing implements King {
99

10+
static final String DESCRIPTION = "This is the Elven king!";
11+
1012
@Override
11-
public String toString() {
12-
return "This is the Elven king!";
13+
public String getDescription() {
14+
return DESCRIPTION;
1315
}
14-
1516
}

abstract-factory/src/main/java/com/iluwatar/abstractfactory/King.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
*/
88
public interface King {
99

10+
String getDescription();
1011
}

abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcArmy.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
*/
88
public class OrcArmy implements Army {
99

10+
static final String DESCRIPTION = "This is the Orc Army!";
11+
1012
@Override
11-
public String toString() {
12-
return "This is the Orcish Army!";
13+
public String getDescription() {
14+
return DESCRIPTION;
1315
}
14-
1516
}

abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcCastle.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
*/
88
public class OrcCastle implements Castle {
99

10+
static final String DESCRIPTION = "This is the Orc castle!";
11+
1012
@Override
11-
public String toString() {
12-
return "This is the Orcish castle!";
13+
public String getDescription() {
14+
return DESCRIPTION;
1315
}
14-
1516
}

abstract-factory/src/main/java/com/iluwatar/abstractfactory/OrcKing.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,10 @@
77
*/
88
public class OrcKing implements King {
99

10+
static final String DESCRIPTION = "This is the Orc king!";
11+
1012
@Override
11-
public String toString() {
12-
return "This is the Orc king!";
13+
public String getDescription() {
14+
return DESCRIPTION;
1315
}
14-
1516
}
Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,77 @@
11
package com.iluwatar.abstractfactory;
2-
import org.junit.Test;
2+
import static org.junit.Assert.assertEquals;
3+
import static org.junit.Assert.assertTrue;
34

4-
import com.iluwatar.abstractfactory.App;
5+
import org.junit.Before;
6+
import org.junit.Test;
57

6-
/**
7-
*
8-
* Application test
9-
*
10-
*/
118
public class AppTest {
129

10+
private App app = new App();;
11+
private KingdomFactory elfFactory;
12+
private KingdomFactory orcFactory;
13+
14+
@Before
15+
public void setUp() {
16+
elfFactory = app.getElfKingdomFactory();
17+
orcFactory = app.getOrcKingdomFactory();
18+
}
19+
20+
@Test
21+
public void king() {
22+
final King elfKing = app.getKing(elfFactory);
23+
assertTrue(elfKing instanceof ElfKing);
24+
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
25+
final King orcKing = app.getKing(orcFactory);
26+
assertTrue(orcKing instanceof OrcKing);
27+
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
28+
}
29+
30+
@Test
31+
public void castle() {
32+
final Castle elfCastle = app.getCastle(elfFactory);
33+
assertTrue(elfCastle instanceof ElfCastle);
34+
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
35+
final Castle orcCastle = app.getCastle(orcFactory);
36+
assertTrue(orcCastle instanceof OrcCastle);
37+
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
38+
}
39+
40+
@Test
41+
public void army() {
42+
final Army elfArmy = app.getArmy(elfFactory);
43+
assertTrue(elfArmy instanceof ElfArmy);
44+
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
45+
final Army orcArmy = app.getArmy(orcFactory);
46+
assertTrue(orcArmy instanceof OrcArmy);
47+
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
48+
}
49+
50+
@Test
51+
public void createElfKingdom() {
52+
app.createKingdom(elfFactory);
53+
final King king = app.getKing();
54+
final Castle castle = app.getCastle();
55+
final Army army = app.getArmy();
56+
assertTrue(king instanceof ElfKing);
57+
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
58+
assertTrue(castle instanceof ElfCastle);
59+
assertEquals(ElfCastle.DESCRIPTION, castle.getDescription());
60+
assertTrue(army instanceof ElfArmy);
61+
assertEquals(ElfArmy.DESCRIPTION, army.getDescription());
62+
}
63+
1364
@Test
14-
public void test() {
15-
String[] args = {};
16-
App.main(args);
65+
public void createOrcKingdom() {
66+
app.createKingdom(orcFactory);
67+
final King king = app.getKing();
68+
final Castle castle = app.getCastle();
69+
final Army army = app.getArmy();
70+
assertTrue(king instanceof OrcKing);
71+
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
72+
assertTrue(castle instanceof OrcCastle);
73+
assertEquals(OrcCastle.DESCRIPTION, castle.getDescription());
74+
assertTrue(army instanceof OrcArmy);
75+
assertEquals(OrcArmy.DESCRIPTION, army.getDescription());
1776
}
1877
}

0 commit comments

Comments
 (0)