package ch.fusun.baron.basic.turntask.test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import org.junit.Before;
import org.junit.Test;
import ch.fusun.baron.basic.turntask.HeritageTurnTask;
import ch.fusun.baron.core.injection.ReInjector;
import ch.fusun.baron.player.Gender;
import ch.fusun.baron.player.MarriageServiceImpl;
import ch.fusun.baron.player.Player;
import ch.fusun.baron.player.api.Country;
import ch.fusun.baron.property.PropertyServiceImpl;
/**
* Tests the {@link HeritageTurnTask}
*/
@SuppressWarnings("nls")
public class HeritageTurnTaskTest {
private HeritageTurnTask turntask;
private PropertyServiceImpl propertyService;
private MarriageServiceImpl marriageService;
/**
*
*/
@Before
public void setUp() {
propertyService = new PropertyServiceImpl();
marriageService = new MarriageServiceImpl();
turntask = new HeritageTurnTask();
}
void reinject() {
ReInjector.getInstance().updateInstance(PropertyServiceImpl.class,
propertyService);
ReInjector.getInstance().updateInstance(MarriageServiceImpl.class,
marriageService);
ReInjector.getInstance().reInject(turntask);
}
/**
* Tests the scenario without children and without spouse and the person is
* dead
*/
@Test
public void testSimpleDead() {
Player person = new Player(0, "Albert", Gender.MALE);
person.kill();
Country country = new Country(0, "Country");
propertyService.setOwnership(person, country);
reinject();
turntask.execute();
Object owner = propertyService.getOwnership(country);
assertNull("The country should not be owned; Current owner: " + owner,
owner);
}
/**
* Tests the scenario without children and with spouse and the person is
* dead
*/
@Test
public void testSimpleFamily() {
Player male = new Player(0, "Albert", Gender.MALE);
Player female = new Player(1, "Alberta", Gender.FEMALE);
marriageService.marry(male, female);
male.kill();
Country country = new Country(0, "Country");
propertyService.setOwnership(male, country);
reinject();
turntask.execute();
Object owner = propertyService.getOwnership(country);
assertNotNull("The country should be owned; Current owner: " + owner,
owner);
assertEquals("The owner should be the married wife", owner, female);
}
/**
* Tests the scenario with a single male child and with spouse and the
* person is dead
*/
@Test
public void testSimpleFamilyWithChild() {
Player male = new Player(0, "Albert", Gender.MALE);
Player female = new Player(1, "Alberta", Gender.FEMALE);
marriageService.marry(male, female);
Player son = new Player(2, "Albertino", Gender.MALE);
giveBirth(male, female, son);
male.kill();
Country country = new Country(0, "Country");
propertyService.setOwnership(male, country);
reinject();
turntask.execute();
Object owner = propertyService.getOwnership(country);
assertNotNull("The country should be owned; Current owner: " + owner,
owner);
assertEquals("The owner should be the married wife", owner, son);
}
/**
* Tests the scenario with a single male child and with spouse and the
* person is dead and the spouse as well
*/
@Test
public void testSimpleFamilyWithChildWomanDies() {
Player male = new Player(0, "Albert", Gender.MALE);
Player female = new Player(1, "Alberta", Gender.FEMALE);
marriageService.marry(male, female);
Player son = new Player(2, "Albertino", Gender.MALE);
giveBirth(male, female, son);
male.kill();
female.kill();
Country country = new Country(0, "Country");
propertyService.setOwnership(female, country);
reinject();
turntask.execute();
Object owner = propertyService.getOwnership(country);
assertNotNull("The country should be owned; Current owner: " + owner,
owner);
assertEquals("The owner should be the son", owner, son);
}
private static void giveBirth(Player male, Player female, Player child) {
child.setFather(male);
child.setMother(female);
male.getChildren().add(child);
female.getChildren().add(child);
}
/**
* Tests the scenario without children and without spouse
*/
@Test
public void testSimple() {
Player person = new Player(0, "Albert", Gender.MALE);
Country country = new Country(0, "Country");
propertyService.setOwnership(person, country);
reinject();
turntask.execute();
assertNotNull("The country should be owned",
propertyService.getOwnership(country));
assertEquals("Wrong person owns the country", person,
propertyService.getOwnership(country));
}
}