Merge pull request #240 from themoffster/master

Improved unit tests so assertions are used.
This commit is contained in:
Ilkka Seppälä 2015-09-05 08:53:05 +03:00
commit 0989c9b498
11 changed files with 152 additions and 48 deletions

View File

@ -1,5 +1,6 @@
package com.iluwatar.abstractfactory;
/**
*
* The essence of the Abstract Factory pattern is a factory interface
@ -12,26 +13,61 @@ package com.iluwatar.abstractfactory;
*/
public class App {
/**
* Program entry point
* @param args command line arguments
*/
public static void main(String[] args) {
createKingdom(new ElfKingdomFactory());
createKingdom(new OrcKingdomFactory());
}
private King king;
private Castle castle;
private Army army;
/**
* Creates kingdom
* @param factory
*/
public static void createKingdom(KingdomFactory factory) {
King king = factory.createKing();
Castle castle = factory.createCastle();
Army army = factory.createArmy();
System.out.println("The kingdom was created.");
System.out.println(king);
System.out.println(castle);
System.out.println(army);
public void createKingdom(final KingdomFactory factory) {
setKing(factory.createKing());
setCastle(factory.createCastle());
setArmy(factory.createArmy());
}
ElfKingdomFactory getElfKingdomFactory() {
return new ElfKingdomFactory();
}
OrcKingdomFactory getOrcKingdomFactory() {
return new OrcKingdomFactory();
}
King getKing(final KingdomFactory factory) {
return factory.createKing();
}
Castle getCastle(final KingdomFactory factory) {
return factory.createCastle();
}
Army getArmy(final KingdomFactory factory) {
return factory.createArmy();
}
public King getKing() {
return king;
}
private void setKing(final King king) {
this.king = king;
}
public Castle getCastle() {
return castle;
}
private void setCastle(final Castle castle) {
this.castle = castle;
}
public Army getArmy() {
return army;
}
private void setArmy(final Army army) {
this.army = army;
}
}

View File

@ -7,4 +7,5 @@ package com.iluwatar.abstractfactory;
*/
public interface Army {
String getDescription();
}

View File

@ -7,4 +7,5 @@ package com.iluwatar.abstractfactory;
*/
public interface Castle {
String getDescription();
}

View File

@ -7,9 +7,10 @@ package com.iluwatar.abstractfactory;
*/
public class ElfArmy implements Army {
@Override
public String toString() {
return "This is the Elven Army!";
}
static final String DESCRIPTION = "This is the Elven Army!";
@Override
public String getDescription() {
return DESCRIPTION;
}
}

View File

@ -7,9 +7,10 @@ package com.iluwatar.abstractfactory;
*/
public class ElfCastle implements Castle {
@Override
public String toString() {
return "This is the Elven castle!";
}
static final String DESCRIPTION = "This is the Elven castle!";
@Override
public String getDescription() {
return DESCRIPTION;
}
}

View File

@ -7,9 +7,10 @@ package com.iluwatar.abstractfactory;
*/
public class ElfKing implements King {
static final String DESCRIPTION = "This is the Elven king!";
@Override
public String toString() {
return "This is the Elven king!";
public String getDescription() {
return DESCRIPTION;
}
}

View File

@ -7,4 +7,5 @@ package com.iluwatar.abstractfactory;
*/
public interface King {
String getDescription();
}

View File

@ -7,9 +7,10 @@ package com.iluwatar.abstractfactory;
*/
public class OrcArmy implements Army {
@Override
public String toString() {
return "This is the Orcish Army!";
}
static final String DESCRIPTION = "This is the Orc Army!";
@Override
public String getDescription() {
return DESCRIPTION;
}
}

View File

@ -7,9 +7,10 @@ package com.iluwatar.abstractfactory;
*/
public class OrcCastle implements Castle {
@Override
public String toString() {
return "This is the Orcish castle!";
}
static final String DESCRIPTION = "This is the Orc castle!";
@Override
public String getDescription() {
return DESCRIPTION;
}
}

View File

@ -7,9 +7,10 @@ package com.iluwatar.abstractfactory;
*/
public class OrcKing implements King {
static final String DESCRIPTION = "This is the Orc king!";
@Override
public String toString() {
return "This is the Orc king!";
public String getDescription() {
return DESCRIPTION;
}
}

View File

@ -1,18 +1,77 @@
package com.iluwatar.abstractfactory;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import com.iluwatar.abstractfactory.App;
/**
*
* Application test
*
*/
public class AppTest {
private App app = new App();;
private KingdomFactory elfFactory;
private KingdomFactory orcFactory;
@Before
public void setUp() {
elfFactory = app.getElfKingdomFactory();
orcFactory = app.getOrcKingdomFactory();
}
@Test
public void test() {
String[] args = {};
App.main(args);
public void king() {
final King elfKing = app.getKing(elfFactory);
assertTrue(elfKing instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, elfKing.getDescription());
final King orcKing = app.getKing(orcFactory);
assertTrue(orcKing instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, orcKing.getDescription());
}
@Test
public void castle() {
final Castle elfCastle = app.getCastle(elfFactory);
assertTrue(elfCastle instanceof ElfCastle);
assertEquals(ElfCastle.DESCRIPTION, elfCastle.getDescription());
final Castle orcCastle = app.getCastle(orcFactory);
assertTrue(orcCastle instanceof OrcCastle);
assertEquals(OrcCastle.DESCRIPTION, orcCastle.getDescription());
}
@Test
public void army() {
final Army elfArmy = app.getArmy(elfFactory);
assertTrue(elfArmy instanceof ElfArmy);
assertEquals(ElfArmy.DESCRIPTION, elfArmy.getDescription());
final Army orcArmy = app.getArmy(orcFactory);
assertTrue(orcArmy instanceof OrcArmy);
assertEquals(OrcArmy.DESCRIPTION, orcArmy.getDescription());
}
@Test
public void createElfKingdom() {
app.createKingdom(elfFactory);
final King king = app.getKing();
final Castle castle = app.getCastle();
final Army army = app.getArmy();
assertTrue(king instanceof ElfKing);
assertEquals(ElfKing.DESCRIPTION, king.getDescription());
assertTrue(castle instanceof ElfCastle);
assertEquals(ElfCastle.DESCRIPTION, castle.getDescription());
assertTrue(army instanceof ElfArmy);
assertEquals(ElfArmy.DESCRIPTION, army.getDescription());
}
@Test
public void createOrcKingdom() {
app.createKingdom(orcFactory);
final King king = app.getKing();
final Castle castle = app.getCastle();
final Army army = app.getArmy();
assertTrue(king instanceof OrcKing);
assertEquals(OrcKing.DESCRIPTION, king.getDescription());
assertTrue(castle instanceof OrcCastle);
assertEquals(OrcCastle.DESCRIPTION, castle.getDescription());
assertTrue(army instanceof OrcArmy);
assertEquals(OrcArmy.DESCRIPTION, army.getDescription());
}
}