Package at.fhj.itm.dao

Source Code of at.fhj.itm.dao.MySqlTripDAOTest

package at.fhj.itm.dao;
import static org.junit.Assert.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.Date;

import java.util.List;
import java.util.UUID;


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.DatabaseTest;
import at.fhj.itm.dao.TripDAO;
import at.fhj.itm.model.Location;
import at.fhj.itm.model.Trip;
import at.fhj.itm.model.User;
import at.fhj.itm.model.Waypoint;
/**
* Tests the MySQL implementation of the TripDAO
* @author Seuchter
*
*/
public class MySqlTripDAOTest extends DatabaseTest {

  private Connection connection;
  private TripDAO dao;
 

 
  @BeforeClass
  public static void setUpBeforeClass() throws Exception {
    Class.forName("com.mysql.jdbc.Driver");

  }

  @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();
   
    DAOFactory factory = MySqlDAOFactory.getInstance();
    dao = factory.getTripDAO();
   
  }
  /**
   * Closes the connection after an test case is executed.
   * @throws Exception
   */
  @After
  public void tearDown() throws Exception {
    connection.close();
  }
  /**
   * Retrieves all trips which go to 'Graz' from the sample data stored in the
   * test database
   */
  @Test
  public void testGetByDestinationCity(){
    List<Trip> trips = dao.allTripsTo("Graz", connection);
   
   
    assertTrue(trips.size() > 0);
  }
  /**
   * Tests if any data sets can be returned by the DAO.
   * This test depends on the fact that sample data is contained in the rebuild script
   */
  @Test
  public void testGetAll(){
    List<Trip> trips = dao.selectAll(connection);
    assertTrue(trips.size() > 0);
 
  }
 
  /**
   * Helper method for creating a fully fledged trip object
   * @return a fully fledged trip object which can be used by testcases.
   */
  private Trip createTestTrip(){
    Location location = new Location(1234, "Unit City");
    User user = new User("Unit", "Test", "UnitTest", "1234", "Unit@Test.com", "123456",
        location, new Date(), UUID.randomUUID().toString().replace("-", ""));
    Location from = new Location(8190, "Birkfeld");
    Location to = new Location(8010, "Graz");
    Waypoint wp = new Waypoint(from, to, user, "", true);
    Trip t = new Trip(user, new Date(), 4, wp, "Copyright (C) Google");
   
    return t;
  }
 
  /**
   * Creates  a new Trip which is then persisted. After the trip was persisted it
   * is deleted again.
   * @throws SQLException
   */
  @Test
  public void testInsertDeleteTrip()throws SQLException{

 
    int before = dao.selectAll(connection).size();
   
    Trip t = createTestTrip();
   
    dao.update(t, connection);
    connection.commit();
    assertTrue(t.getId() != -1);
    assertTrue(t.getWaypoint().getId() != -1);
    assertTrue(t.getWaypoint().getFromLocation().getId() != -1);
    assertTrue(t.getWaypoint().getToLocation().getId() != -1);
    assertTrue(t.getWaypoint().getUser().getId() != -1);   
    int after = dao.selectAll(connection).size();
    assertTrue(after > before);
    dao.delete(t, connection);
   
    int afterDelete = dao.selectAll(connection).size();
    assertTrue(t.getId() == -1);
    assertTrue(t.getWaypoint().getId() == -1);
    assertTrue(t.getUser().getId() != -1);
    assertEquals(before, afterDelete);
  }
 
 
  /**
   * Creates a trip which is then persisted, afterwards it is checked if the
   * DAO can perform an update on the already created trip.
   * @throws SQLException
   */
  @Test
  public void testUpdate()throws SQLException{
    Trip t = createTestTrip();
    dao.update(t, connection);
    assertTrue(t.getId() != -1);
    t.setSeats(1);
    dao.update(t, connection);
    connection.commit();
    Trip newTrip = dao.getByID(t.getId(), connection);
    assertEquals(t.getSeats(), newTrip.getSeats());
   
   
   
  }
 
  /**
   * Tries to delete a non persisted trip which should throw an
   * DAOException
   */
  @Test(expected=DAOException.class)
  public void deleteNonPersisted(){
    Trip t = createTestTrip();
    dao.delete(t, connection);
  }

}
TOP

Related Classes of at.fhj.itm.dao.MySqlTripDAOTest

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.