package cz.muni.fi.pa165;
import cz.muni.fi.pa165.library.backend.ReaderDAOImpl;
import cz.muni.fi.pa165.library.backend.Reader;
import cz.muni.fi.pa165.library.backend.Book;
import cz.muni.fi.pa165.library.backend.BookDAOImpl;
import cz.muni.fi.pa165.library.backend.ReservationDAOImpl;
import cz.muni.fi.pa165.library.backend.Reservation;
import cz.muni.fi.pa165.library.api.Available;
import cz.muni.fi.pa165.library.api.State;
import cz.muni.fi.pa165.library.api.Genre;
import java.sql.DriverManager;
import java.sql.SQLNonTransientConnectionException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import junit.framework.TestCase;
/**
*
* @author Filip Ligac
*/
public class ReservationDAOImplTest extends TestCase {
private EntityManagerFactory emf;
private EntityManager em;
private ReaderDAOImpl readerDAOImpl;
private BookDAOImpl bookDAOImpl;
private ReservationDAOImpl reservationDAOImpl;
private Book book;
public ReservationDAOImplTest(String testName) {
super(testName);
}
@Override
protected void setUp() throws Exception {
super.setUp();
try {
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
DriverManager.getConnection("jdbc:derby:memory:testDB;create=true").close();
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception during database startup.");
}
try {
emf = Persistence.createEntityManagerFactory("testPU");
em = emf.createEntityManager();
readerDAOImpl = new ReaderDAOImpl();
bookDAOImpl = new BookDAOImpl();
reservationDAOImpl = new ReservationDAOImpl();
reservationDAOImpl.setEntityManager(em);
createReaderData();
createBookData();
book = new Book("Da Vinciho kod", "Dan Brown", Genre.DRAMA, "2005", "USA",
"Foursick", "95860948503", State.NEW, Available.AVAILABLE);
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception during JPA EntityManager instantiation.");
}
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
emf.close();
try {
DriverManager.getConnection("jdbc:derby:memory:testDB;drop=true").close();
} catch (SQLNonTransientConnectionException ex) {
if (ex.getErrorCode() != 45000) {
throw ex;
}
}
}
private void createReaderData() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
em.persist(new Reader("Anton", "Bernolak", "10.4.1875", "Bernolakovo 453, SR",
"anton.bernolak@gmail.com", "0903 560 987", "aaa"));
em.persist(new Reader("Martin", "Skrecok", "5.10.1990", "Jurkovicova 16, SR",
"martin.skrecok@gmail.com", "903 875 349", "bbb"));
em.persist(new Reader("Leon", "Powe", "20.5.1950", "Golden Street, Boston, USA",
"leon.powe@gmail.com", "740 320 853", "ccc"));
em.getTransaction().commit();
if (em != null) {
em.close();
}
}
public void createBookData() {
EntityManager em = emf.createEntityManager();
em.getTransaction().begin();
// ID 1
em.persist(new Book("Da Vinciho kod", "Dan Brown", Genre.DRAMA, "2005", "USA",
"Foursick", "95860948503", State.NEW, Available.AVAILABLE));
// ID 2
em.persist(new Book("Cesta", "Brandon Boys", Genre.DRAMA, "2007", "Great Britain",
"Eminent", "8072813001", State.NEW, Available.AVAILABLE));
// ID 3
em.persist(new Book("Peniaze", "Arnold Doughty", Genre.NOVEL, "2000", "USA",
"Mackpublishing", "43543459834", State.USED, Available.BORROWED));
// ID 4
em.persist(new Book("Smely zajko v Afrike", "Ludmila Podjavorinska", Genre.ADVENTURE, "1993", "Slovakia",
"Ikar", "49684586044", State.USED, Available.AVAILABLE));
// ID 5
em.persist(new Book("Kazisvet Jurko", "Lubomir Feldek", Genre.ADVENTURE, "1992", "Slovakia",
"Mlade leta", "548684030023", State.USED, Available.RESERVED));
// ID 6
em.persist(new Book("Mierny vanok", "Juraj Lauko", Genre.CHILDREN, "2001", "Slovakia",
"Ikar", "9788055151191", State.USED, Available.BORROWED));
// ID 7
em.persist(new Book("Neposlusny Janko", "Peter Konecny", Genre.CHILDREN, "2012", "Slovakia",
"Mlade leta", "9788055161191", State.DAMAGED, Available.BORROWED));
// ID 8
em.persist(new Book("Cap zahradnik", "Anton Maly", Genre.ADVENTURE, "2011", "Slovakia",
"Ikar", "9788055171191", State.USED, Available.RESERVED));
// ID 9
em.persist(new Book("Snehuliak", "Jo Nesbo", Genre.ADVENTURE, "2010", "Slovakia",
"Mlade leta", "9788055181191", State.USED, Available.RESERVED));
em.getTransaction().commit();
em.close();
}
public void testInsertReservationWithNullArgument() {
try {
reservationDAOImpl.insertReservation(null);
fail("Method should fail on null parameter");
} catch (IllegalArgumentException ex) {
// Ok
}
}
public void testInsertReservationWithIncorrectBorrowAttributes() {
List<Book> reservationList = new ArrayList<Book>();
reservationList.add(em.find(Book.class, new Long(1)));
reservationList.add(em.find(Book.class, new Long(2)));
Reservation incorrectReservation;
long currentTime = System.currentTimeMillis();
long afterMonth = currentTime + 2592000000l;
}
public void testInsertReservationWithIncorrectReaderID() {
try {
reservationDAOImpl.insertReservation(new Reservation(em.find(Reader.class, new Long(-5)),
em.find(Book.class, new Long(1)), new Date(System.currentTimeMillis())));
fail("Cannot insert reservation with reader's id lesser than or equals to 0");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testInsertReservationWithNullReaderID() {
try {
reservationDAOImpl.insertReservation(new Reservation(null, em.find(Book.class, new Long(1)),
new Date(System.currentTimeMillis())));
fail("Cannot insert reservation without reader's id");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testInsertReservationWithIncorrectBookID() {
try {
reservationDAOImpl.insertReservation(new Reservation(em.find(Reader.class, new Long(1)),
em.find(Book.class, new Long(-5)), new Date(System.currentTimeMillis())));
fail("Cannot insert reservation with book's id lesser than or equals to 0");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testInsertReservationWithNullBookID() {
try {
reservationDAOImpl.insertReservation(new Reservation(em.find(Reader.class, new Long(1)),
null, new Date(System.currentTimeMillis())));
fail("Cannot make a reservation for book which is null");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testInsertReservationWithIncorrectReservationDate() {
try {
reservationDAOImpl.insertReservation(new Reservation(em.find(Reader.class, new Long(1)), em.find(Book.class, new Long(1)), null));
fail("Cannot make a reservation without specifying a reservation date");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testInsertCorrectReservation() {
em.getTransaction().begin();
Reservation reservation = new Reservation(em.find(Reader.class, new Long(1)),
em.find(Book.class, new Long(8)), new Date(System.currentTimeMillis()));
em.persist(reservation);
em.getTransaction().commit();
assertNotNull(reservationDAOImpl.findReservationByID(reservation.getReservationID()));
if (em != null) {
em.close();
}
}
public void testFindAllReservations() {
em.getTransaction().begin();
Reservation reservation1 = new Reservation(em.find(Reader.class, new Long(1)),
em.find(Book.class, new Long(8)), new Date(System.currentTimeMillis()));
Reservation reservation2 = new Reservation(em.find(Reader.class, new Long(1)),
em.find(Book.class, new Long(9)), new Date(System.currentTimeMillis()));
em.persist(reservation1);
em.persist(reservation2);
em.getTransaction().commit();
assertEquals("Reservation list should have size equals to 2", 2, reservationDAOImpl.findAllReservations().size());
if (em != null) {
em.close();
}
}
public void testFindReservationByID() {
em.getTransaction().begin();
Reservation reservation1 = new Reservation(em.find(Reader.class, new Long(1)),
em.find(Book.class, new Long(8)), new Date(System.currentTimeMillis()));
Reservation reservation2 = new Reservation(em.find(Reader.class, 1l),
em.find(Book.class, new Long(9)), new Date(System.currentTimeMillis()));
em.persist(reservation1);
em.persist(reservation2);
em.getTransaction().commit();
reservation1.setReservationID(new Long(1));
reservation2.setReservationID(new Long(2));
assertEquals(reservation1, reservationDAOImpl.findReservationByID(new Long(1)));
assertEquals(reservation2, reservationDAOImpl.findReservationByID(new Long(2)));
if (em != null) {
em.close();
}
}
public void testFindReservationByNullID() {
try {
reservationDAOImpl.findReservationByID(null);
fail("ID cannot be null");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testFindReservationByIncorrectID() {
try {
reservationDAOImpl.findReservationByID(new Long(-100));
fail("ID must be greater than 0");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testFindReservationsByReader() {
em.getTransaction().begin();
Reservation reservation1 = new Reservation(em.find(Reader.class, new Long(1)),
em.find(Book.class, new Long(8)), new Date(System.currentTimeMillis()));
Reservation reservation2 = new Reservation(em.find(Reader.class, new Long(1)),
em.find(Book.class, new Long(9)), new Date(System.currentTimeMillis()));
em.persist(reservation1);
em.persist(reservation2);
em.getTransaction().commit();
List<Reservation> allReservations = new ArrayList<Reservation>();
allReservations.add(reservation1);
allReservations.add(reservation2);
assertEquals(allReservations, reservationDAOImpl.findReservationsByReader(em.find(Reader.class, new Long(1))));
if (em != null) {
em.close();
}
}
public void testFindReservationsWithNullReader() {
try {
reservationDAOImpl.findReservationsByReader(null);
fail("Reader cannot be null");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testFindReservationsWithIncorrectReader() {
try {
reservationDAOImpl.findReservationsByReader(em.find(Reader.class, new Long(-5)));
fail("Reader ID must be greater than 0");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testFindReservationByBook() {
em.getTransaction().begin();
Reservation reservation1 = new Reservation(em.find(Reader.class, new Long(1)),
em.find(Book.class, new Long(8)), new Date(System.currentTimeMillis()));
Reservation reservation2 = new Reservation(em.find(Reader.class, new Long(1)),
em.find(Book.class, new Long(9)), new Date(System.currentTimeMillis()));
em.persist(reservation1);
em.persist(reservation2);
em.getTransaction().commit();
assertEquals(reservation2, reservationDAOImpl.findReservationByBook(em.find(Book.class, new Long(9))));
if (em != null) {
em.close();
}
}
public void testFindReservationWithNullBook() {
try {
reservationDAOImpl.findReservationByBook(null);
fail("Book cannot be null");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testFindReservationWithIcorrectBook() {
try {
reservationDAOImpl.findReservationByBook(em.find(Book.class, new Long(-5)));
fail("Book ID must be greater than 0");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testDeleteReservation() {
em.getTransaction().begin();
Reservation reservation1 = new Reservation(em.find(Reader.class, new Long(1)),
em.find(Book.class, new Long(8)), new Date(System.currentTimeMillis()));
Reservation reservation2 = new Reservation(em.find(Reader.class, new Long(1)),
em.find(Book.class, new Long(9)), new Date(System.currentTimeMillis()));
em.persist(reservation1);
em.persist(reservation2);
em.getTransaction().commit();
em.getTransaction().begin();
reservationDAOImpl.deleteReservation(reservation1);
em.getTransaction().commit();
// reservation1 is deleted
assertEquals(null, reservationDAOImpl.findReservationByID(new Long(1)));
// reservation2 remains
assertEquals(reservation2, reservationDAOImpl.findReservationByID(new Long(2)));
if (em != null) {
em.close();
}
}
public void testDeleteNullReservation() {
try {
reservationDAOImpl.deleteReservation(null);
fail("Cannot delete a reservation which is null.");
} catch (IllegalArgumentException ex) {
// OK
}
}
}