Package cz.muni.fi.pa165.ddtroops.persistence

Source Code of cz.muni.fi.pa165.ddtroops.persistence.HeroDAOTest

package cz.muni.fi.pa165.ddtroops.persistence;

import cz.muni.fi.pa165.ddtroops.daointerfaces.HeroDAO;
import cz.muni.fi.pa165.ddtroops.daointerfaces.RaceDAO;
import cz.muni.fi.pa165.ddtroops.daointerfaces.SkillDAO;
import cz.muni.fi.pa165.ddtroops.daointerfaces.SquadDAO;
import cz.muni.fi.pa165.ddtroops.entities.Gender;
import cz.muni.fi.pa165.ddtroops.entities.Hero;
import cz.muni.fi.pa165.ddtroops.entities.Race;
import cz.muni.fi.pa165.ddtroops.entities.Skill;
import java.util.List;
import java.util.logging.Logger;
import junit.framework.TestCase;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;

/**
* Testclass for HeroDAOImpl class.
*
* @author Jaromír Svoboda
*/
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = { "classpath:testAppContext.xml" })
@Transactional
public class HeroDAOTest extends TestCase {

    private Hero hero;
    @Autowired
    private HeroDAO dao;
    @Autowired
    private SquadDAO squadDao;
    @Autowired
    private RaceDAO raceDao;
    @Autowired
    private SkillDAO skillDao;
    private static final Logger logger = Logger.getLogger(HeroDAOTest.class.getName());

    private Hero getHero(){
        hero = new Hero();
        hero.setBelief("Neutral");
        hero.setBiography("unknown...");
        hero.setCharisma(new Byte("4"));
        hero.setName("Cohen the Barbarian");
        hero.setGender(Gender.MALE);
        hero.setProfession("Warrior");

        Race race = new Race();
        race.setName("jarda");
        race.setDescription("linux core developer");
        race.setCharisma(Byte.MIN_VALUE);
        race.setConstitution(Byte.MIN_VALUE);
        race.setDexterity(Byte.MIN_VALUE);
        race.setIntelligence(Byte.MIN_VALUE);
        race.setStrength(Byte.MIN_VALUE);
        race.setWisdom(Byte.MIN_VALUE);
        raceDao.create(race);

        hero.setRace(race);//TODO
        hero.setConstitution(new Byte("8"));
        hero.setDexterity(new Byte("7"));
        hero.setIntelligence(new Byte("6"));
        hero.setStrength(new Byte("9"));
        hero.setWisdom(new Byte("7"));
        hero.setXp(5000L);

        return hero;
    }

    @Test
    public void testCreate(){
        hero = getHero();
        dao.create(hero);
        assertNotNull(hero.getId());
    }

    @Test
    public void testUpdate(){
        hero = getHero();
        dao.create(hero);
        hero.setXp(10000L);
        dao.update(hero);
        Hero heroFromDB = dao.getById(hero.getId());
        assertEquals(heroFromDB.getXp(),hero.getXp());
    }

    @Test
    public void testDelete(){
        hero = getHero();
        dao.create(hero);
        List<Hero> heroes = dao.getAll();
        for (Hero h : heroes){
            dao.delete(h);
        }
        List<Hero> deletedHeroes = dao.getAll();
        assertTrue(deletedHeroes.isEmpty());
    }

    @Test
    public void testSkills(){
        hero = getHero();
        Skill newSkill = new Skill();
        newSkill.setDescription("Brand new skill");
        newSkill.setName("New skill");
        newSkill.setMinXP(1000L);
        newSkill.setProfession("Wizard");

        skillDao.create(newSkill);
        assertTrue(hero.getSkills().isEmpty());

        hero.addSkill(newSkill);
        assertTrue(hero.getSkills().contains(newSkill));
        assertTrue(hero.getSkills().size() == 1);

        hero.removeSkill(newSkill);
        assertTrue(hero.getSkills().isEmpty());
    }
}
TOP

Related Classes of cz.muni.fi.pa165.ddtroops.persistence.HeroDAOTest

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.