package flex.spring.samples.company;

import java.util.List;

import org.springframework.test.AbstractDependencyInjectionSpringContextTests;

public class IbatisCompanyDAOTests extends AbstractDependencyInjectionSpringContextTests{

	private ICompanyDAO dao;
	
	public void testFindById() {
		Company company = dao.findById(1);
		assertNotNull(company);
		assertNotNull(company.getIndustry());
		assertEquals(1, company.getId());
	}
	
	public void testFindAll() {
		List<Company> companies = dao.findAll();
		assertEquals(6, companies.size());
	}
	
	public void testFindByName() {
		List<Company> companies = dao.findByName("SpringSource");
		assertEquals(1, companies.size());
		assertEquals("SpringSource", companies.get(0).getName());
	}
	
	public void testUpdate() {
		Company company = dao.findById(1);
		assertNotNull(company);
		String oldCity = company.getCity();
		company.setCity("Melbourne");
		assertTrue(dao.update(company));
		company = dao.findById(1);
		assertEquals("Melbourne",company.getCity());
		company.setCity(oldCity);
		assertTrue(dao.update(company));
	}
	
	public void testRemoveAndInsert() {
		Company company = dao.findByName("Alpha Corp").get(0);
		int originalId = company.getId();
		assertNotNull(company);
		assertTrue(dao.remove(company));
		company.setId(-1);
		company = dao.create(company);
		assertNotNull(company);
		assertEquals(originalId+1, company.getId());
	}
	
	@Override
	protected String[] getConfigLocations() {
		return new String[] {"classpath:flex/spring/samples/company/company-dao-test-context.xml"};
	}



	public ICompanyDAO getDao() {
		return dao;
	}



	public void setDao(ICompanyDAO dao) {
		this.dao = dao;
	}
	
	
}
