Improved unit tests so assertions are used.
This commit is contained in:
parent
db11dc42c1
commit
5956873fc9
@ -1,5 +1,6 @@
|
|||||||
package com.iluwatar.abstractfactory;
|
package com.iluwatar.abstractfactory;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
* The essence of the Abstract Factory pattern is a factory interface
|
* The essence of the Abstract Factory pattern is a factory interface
|
||||||
@ -12,26 +13,61 @@ package com.iluwatar.abstractfactory;
|
|||||||
*/
|
*/
|
||||||
public class App {
|
public class App {
|
||||||
|
|
||||||
/**
|
private King king;
|
||||||
* Program entry point
|
private Castle castle;
|
||||||
* @param args command line arguments
|
private Army army;
|
||||||
*/
|
|
||||||
public static void main(String[] args) {
|
|
||||||
createKingdom(new ElfKingdomFactory());
|
|
||||||
createKingdom(new OrcKingdomFactory());
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates kingdom
|
* Creates kingdom
|
||||||
* @param factory
|
* @param factory
|
||||||
*/
|
*/
|
||||||
public static void createKingdom(KingdomFactory factory) {
|
public void createKingdom(final KingdomFactory factory) {
|
||||||
King king = factory.createKing();
|
setKing(factory.createKing());
|
||||||
Castle castle = factory.createCastle();
|
setCastle(factory.createCastle());
|
||||||
Army army = factory.createArmy();
|
setArmy(factory.createArmy());
|
||||||
System.out.println("The kingdom was created.");
|
}
|
||||||
System.out.println(king);
|
|
||||||
System.out.println(castle);
|
ElfKingdomFactory getElfKingdomFactory() {
|
||||||
System.out.println(army);
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,4 +7,5 @@ package com.iluwatar.abstractfactory;
|
|||||||
*/
|
*/
|
||||||
public interface Army {
|
public interface Army {
|
||||||
|
|
||||||
|
String getDescription();
|
||||||
}
|
}
|
||||||
|
@ -7,4 +7,5 @@ package com.iluwatar.abstractfactory;
|
|||||||
*/
|
*/
|
||||||
public interface Castle {
|
public interface Castle {
|
||||||
|
|
||||||
|
String getDescription();
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,10 @@ package com.iluwatar.abstractfactory;
|
|||||||
*/
|
*/
|
||||||
public class ElfArmy implements Army {
|
public class ElfArmy implements Army {
|
||||||
|
|
||||||
@Override
|
static final String DESCRIPTION = "This is the Elven Army!";
|
||||||
public String toString() {
|
|
||||||
return "This is the Elven Army!";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return DESCRIPTION;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,10 @@ package com.iluwatar.abstractfactory;
|
|||||||
*/
|
*/
|
||||||
public class ElfCastle implements Castle {
|
public class ElfCastle implements Castle {
|
||||||
|
|
||||||
@Override
|
static final String DESCRIPTION = "This is the Elven castle!";
|
||||||
public String toString() {
|
|
||||||
return "This is the Elven castle!";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return DESCRIPTION;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,10 @@ package com.iluwatar.abstractfactory;
|
|||||||
*/
|
*/
|
||||||
public class ElfKing implements King {
|
public class ElfKing implements King {
|
||||||
|
|
||||||
@Override
|
static final String DESCRIPTION = "This is the Elven king!";
|
||||||
public String toString() {
|
|
||||||
return "This is the Elven king!";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return DESCRIPTION;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,4 +7,5 @@ package com.iluwatar.abstractfactory;
|
|||||||
*/
|
*/
|
||||||
public interface King {
|
public interface King {
|
||||||
|
|
||||||
|
String getDescription();
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,10 @@ package com.iluwatar.abstractfactory;
|
|||||||
*/
|
*/
|
||||||
public class OrcArmy implements Army {
|
public class OrcArmy implements Army {
|
||||||
|
|
||||||
@Override
|
static final String DESCRIPTION = "This is the Orc Army!";
|
||||||
public String toString() {
|
|
||||||
return "This is the Orcish Army!";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return DESCRIPTION;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,10 @@ package com.iluwatar.abstractfactory;
|
|||||||
*/
|
*/
|
||||||
public class OrcCastle implements Castle {
|
public class OrcCastle implements Castle {
|
||||||
|
|
||||||
@Override
|
static final String DESCRIPTION = "This is the Orc castle!";
|
||||||
public String toString() {
|
|
||||||
return "This is the Orcish castle!";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return DESCRIPTION;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -7,9 +7,10 @@ package com.iluwatar.abstractfactory;
|
|||||||
*/
|
*/
|
||||||
public class OrcKing implements King {
|
public class OrcKing implements King {
|
||||||
|
|
||||||
@Override
|
static final String DESCRIPTION = "This is the Orc king!";
|
||||||
public String toString() {
|
|
||||||
return "This is the Orc king!";
|
|
||||||
}
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getDescription() {
|
||||||
|
return DESCRIPTION;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,18 +1,77 @@
|
|||||||
package com.iluwatar.abstractfactory;
|
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 org.junit.Test;
|
||||||
|
|
||||||
import com.iluwatar.abstractfactory.App;
|
|
||||||
|
|
||||||
/**
|
|
||||||
*
|
|
||||||
* Application test
|
|
||||||
*
|
|
||||||
*/
|
|
||||||
public class AppTest {
|
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
|
@Test
|
||||||
public void test() {
|
public void king() {
|
||||||
String[] args = {};
|
final King elfKing = app.getKing(elfFactory);
|
||||||
App.main(args);
|
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());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user