package at.fhj.itm.dao;
import static org.junit.Assert.*;
import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
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.DAOFactory;
import at.fhj.itm.dao.PointDAO;
import at.fhj.itm.model.Point;
/**
* Tests the MySql implementation of the PointDAO
* @author Seuchter
*
*/
public class MySqlPointDAOTest extends DatabaseTest{
private PointDAO dao;
private DAOFactory factory;
private Connection connection;
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
/**
* Creates a multiple Points entites and associates them with a trip. Then
* it is tested if the points can be found again by the dao. Afterwards both
* trip and previously created points are deleted.
*/
@Test
public void testInsertSelectByTripDeleteByTrip()
{
List<Point> points = new ArrayList<Point>();
for(int i = 0; i< 10; i++){
Point p = new Point(i+1, 10.343 * i, 8.343 * i, i*2, 1);
points.add(p);
}
for(Point p : points){
dao.update(p, connection);
}
List<Point> pointsByTrip = ((MySqlPointDAO) dao).getByTrip(1, connection);
assertEquals(points.size(), pointsByTrip.size());
((MySqlPointDAO) dao).deletePointsForTrip(1,connection);
points = ((MySqlPointDAO) dao).getByTrip(1, connection);
assertEquals(0, points.size());
}
/**
* Creates a single point which is persisted and again deleted
*/
@Test
public void testInsertDelete(){
int count = dao.selectAll(connection).size();
Point p = new Point(1, 1234.33, 4312.11, 100, 1);
assertEquals(-1, p.getId());
dao.update(p, connection);
int countAfter = dao.selectAll(connection).size();
assertTrue(p.getId() != -1);
assertTrue(countAfter > count);
Point p2 = dao.getByID(p.getId(), connection);
assertEquals(p, p2);
dao.delete(p, connection);
int countAfterDelete = dao.selectAll(connection).size();
assertEquals(-1, p.getId());
assertEquals(count, countAfterDelete);
}
/**
* Creates and persists a point entity and tests if updates are performed
* by the DAO
*/
@Test
public void testUpdate(){
Point p = new Point(1, 42.424242, 24.242424, 1230, 1);
dao.update(p, connection);
assertTrue(p.getId() != -1);
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
/**
* 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();
factory = new MySqlDAOFactory();
dao = factory.getPointDAO();
}
/**
* Closes the connection after an test case is executed.
* @throws Exception
*/
@After
public void tearDown() throws Exception {
connection.close();
}
}