package at.fhj.itm.dao;
import java.sql.Connection;
import java.util.List;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import at.fhj.itm.dao.LocationDAO;
import at.fhj.itm.model.Location;
/**
* Tests for the MySql implementation of the Location DAO
* @author Seuchter
*
*/
public class MySqlLocationDAOTest extends DatabaseTest {
private LocationDAO dao;
private Connection connection;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
/**
* Tests wether any data can be returned by the DAO
*/
@Test
public void testGetAll(){
List<Location> all = dao.selectAll(connection);
assertTrue(all.size() > 0);
}
/**
* Performes an update operation an a already existing Location object and checks
* wether changes to entity are stored in the databse.
*/
@Test
public void testUpdate(){
List<Location> all= dao.selectAll(connection);
assertTrue(all.size() > 0);
Location oldLocation = all.get(0);
long newZip = 3456;
String newCity = "Update Town";
long oldZip = oldLocation.getZip();
String oldCity = oldLocation.getCity();
oldLocation.setZip(newZip);
oldLocation.setCity(newCity);
dao.update(oldLocation,connection);
Location uptLocation = dao.getByID(oldLocation.getId(),connection);
assertEquals(newZip, uptLocation.getZip());
assertEquals(newCity, uptLocation.getCity());
uptLocation.setZip(oldZip);
uptLocation.setCity(oldCity);
dao.update(uptLocation,connection);
}
/**
* Tests if a new entity can be inserted into the database and if it's ID gets
* updated properly. After a sucessfull insert the location is deleted.
*/
@Test
public void testInsertDelete(){
Location location = new Location(1234, "Unit Town");
assertEquals(-1, location.getId());
dao.update(location, connection);
assertTrue(location.getId() >= 0);
dao.delete(location,connection);
assertEquals(-1, location.getId());
}
/**
* Rebuilds the unit test database and creates a connection object for use
* by the unit test
* @throws Exception if there was an error creating the conenction or rebuilding the database
*/
@Before
public void setUp() throws Exception {
rebuildDatabase();
connection = getUnitTestConnection();
dao = new MySqlLocationDAO();
}
/**
* Closes the connection after an test case is executed.
* @throws Exception
*/
@After
public void tearDown() throws Exception {
connection.close();
}
}