package cz.muni.fi.pa165;
import cz.muni.fi.pa165.library.backend.Reader;
import cz.muni.fi.pa165.library.backend.BorrowDAO;
import cz.muni.fi.pa165.library.backend.Borrow;
import cz.muni.fi.pa165.library.backend.Book;
import cz.muni.fi.pa165.library.backend.BorrowDAOImpl;
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 Marián Labuda
* @version 1.2
*/
public class BorrowDAOImplTest extends TestCase {
EntityManagerFactory entityManagerFactory;
EntityManager em;
BorrowDAO borrowDAO;
public BorrowDAOImplTest() {
}
@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 {
entityManagerFactory = Persistence.createEntityManagerFactory("testPU");
createReaderData();
createBookData();
createReservationData();
borrowDAO = new BorrowDAOImpl();
em = entityManagerFactory.createEntityManager();
borrowDAO.setEntityManager(em);
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception during JPA EntityManager instanciation.");
}
}
@Override
protected void tearDown() throws Exception {
super.tearDown();
entityManagerFactory.close();
try {
DriverManager.getConnection("jdbc:derby:memory:testDB;drop=true").close();
} catch (SQLNonTransientConnectionException ex) {
if (ex.getErrorCode() != 45000) {
throw ex;
}
// Shutdown success
}
}
private void createReaderData() {
// ID start from 1 incremented by 1, because of in memory DB
// ID 1
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(new Reader("Emil", "Slovak", "8.9.1973", "Velke Cierne 23, SR",
"e.slovak@gmail.com", "0949 123 542", "aaa"));
// ID 2
entityManager.persist(new Reader("Karel", "Novak", "7.11.1981", "Grohova 2, Brno, CR",
"karel.novak@gmail.com", "735 945 248", "ccc"));
// ID 3
entityManager.persist(new Reader("Jack", "Black", "6.8.1972", "1234/1 5th Avenue, NY, USA",
"jack.black@gmail.com", "482 451 354", "ddd"));
entityManager.getTransaction().commit();
entityManager.close();
}
public void createBookData() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
// ID 1
entityManager.persist(new Book("Cesta", "Brandon Boys", Genre.DRAMA, "2007", "Great Britan",
"Eminent", "8072813001", State.NEW, Available.AVAILABLE));
// ID 2
entityManager.persist(new Book("Cesta", "Brandon Boys", Genre.DRAMA, "2007", "Great Britan",
"Eminent", "8072813001", State.NEW, Available.AVAILABLE));
// ID 3
entityManager.persist(new Book("Cesta", "Brandon Boys", Genre.DRAMA, "2007", "Great Britan",
"Eminent", "8072813001", State.USED, Available.AVAILABLE));
// ID 4
entityManager.persist(new Book("Snehuliak", "Jo Nesbo", Genre.ADVENTURE, "2012", "Slovakia",
"Ikar", "9788055131191", State.NEW, Available.AVAILABLE));
// ID 5
entityManager.persist(new Book("Snehuliak", "Jo Nesbo", Genre.ADVENTURE, "2012", "Slovakia",
"Ikar", "9788055131191", State.NEW, Available.AVAILABLE));
// ID 6
entityManager.persist(new Book("Snehuliak", "Jo Nesbo", Genre.ADVENTURE, "2012", "Slovakia",
"Ikar", "9788055131191", State.USED, Available.AVAILABLE));
// ID 7
entityManager.persist(new Book("Snehuliak", "Jo Nesbo", Genre.ADVENTURE, "2012", "Slovakia",
"Ikar", "9788055131191", State.USED, Available.AVAILABLE));
// ID 8
entityManager.persist(new Book("Snehuliak", "Jo Nesbo", Genre.ADVENTURE, "2012", "Slovakia",
"Ikar", "9788055131191", State.USED, Available.RESERVED));
// ID 9
entityManager.persist(new Book("Snehuliak", "Jo Nesbo", Genre.ADVENTURE, "2012", "Slovakia",
"Ikar", "9788055131191", State.USED, Available.RESERVED));
// ID 10
entityManager.persist(new Book("Snehuliak", "Jo Nesbo", Genre.ADVENTURE, "2012", "Slovakia",
"Ikar", "9788055131191", State.USED, Available.BORROWED));
// ID 11
entityManager.persist(new Book("Snehuliak", "Jo Nesbo", Genre.ADVENTURE, "2012", "Slovakia",
"Ikar", "9788055131191", State.DAMAGED, Available.BORROWED));
entityManager.getTransaction().commit();
entityManager.close();
}
private void createReservationData() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(new Reservation(
entityManager.find(Reader.class, 1l),
entityManager.find(Book.class, 8l),
new Date(System.currentTimeMillis())));
entityManager.persist(new Reservation(
entityManager.find(Reader.class, 1l),
entityManager.find(Book.class, 9l),
new Date(System.currentTimeMillis())));
entityManager.getTransaction().commit();
entityManager.close();
}
private List<Book> prepareBorrowList() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
List<Book> borrowList = new ArrayList<Book>();
borrowList.add(entityManager.find(Book.class, new Long(1)));
borrowList.add(entityManager.find(Book.class, new Long(2)));
entityManager.close();
return borrowList;
}
public void testInsertCorrectBorrow() {
long currentTime = System.currentTimeMillis();
EntityManager entityManager = entityManagerFactory.createEntityManager();
Borrow borrow = new Borrow(entityManager.find(Reader.class, 1l), prepareBorrowList(),
new Date(currentTime), new Date(currentTime + 2592000000l));
entityManager.close();
em.getTransaction().begin();
borrowDAO.createBorrow(borrow);
em.getTransaction().commit();
entityManager = entityManagerFactory.createEntityManager();
assertEquals(borrow, entityManager.find(Borrow.class, 1l));
entityManager.close();
}
public void testInsertBorrowWithNullArgument() {
try {
borrowDAO.createBorrow(null);
fail("Method should failed on null parameter");
} catch (IllegalArgumentException ex) {
// Ok
}
}
public void testInsertBorrowWithNullReaderID() {
List<Book> borrowList = prepareBorrowList();
long currentTime = System.currentTimeMillis();
try {
borrowDAO.createBorrow(new Borrow(null, borrowList,
new Date(currentTime), new Date(currentTime + 2592000000l)));
fail("Cannot insert borrow without reader's id");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testInsertBorrowWithNullBorrowList() {
long currentTime = System.currentTimeMillis();
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
borrowDAO.createBorrow(new Borrow(entityManager.find(Reader.class, 1l), null, new Date(currentTime), new Date(currentTime + 2592000000l)));
fail("Cannot insert borrow without borrowed books");
} catch (IllegalArgumentException ex) {
// OK
} finally {
entityManager.close();
}
}
public void testInsertBorrowWithoutBorrowDate() {
List<Book> borrowList = prepareBorrowList();
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
borrowDAO.createBorrow(new Borrow(entityManager.find(Reader.class, 1l), borrowList, null, new Date(System.currentTimeMillis() + 2592000000l)));
fail("Cannot insert borrow without borrow date");
} catch (IllegalArgumentException ex) {
// OK
} finally {
entityManager.close();
}
}
public void testInsertBorrowWithoutReturnDate() {
List<Book> borrowList = prepareBorrowList();
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
borrowDAO.createBorrow(new Borrow(entityManager.find(Reader.class, 1l), borrowList, new Date(System.currentTimeMillis()), null));
fail("Cannot insert borrow without borrowed books");
} catch (IllegalArgumentException ex) {
// OK
} finally {
entityManager.close();
}
}
public void testInsertBorrowWithEmptyBorrowList() {
long currentTime = System.currentTimeMillis();
EntityManager entityManager = entityManagerFactory.createEntityManager();
try {
borrowDAO.createBorrow(new Borrow(entityManager.find(Reader.class, 1l), new ArrayList<Book>(),
new Date(currentTime), new Date(currentTime + 2592000000l)));
fail("Cannot insert empty borrow list");
} catch (IllegalArgumentException ex) {
// OK
} finally {
entityManager.close();
}
}
public void testInsertBorrowWithMoreBookThanAllowed() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
long currentTime = System.currentTimeMillis();
List<Book> borrowList = new ArrayList<Book>();
for (long id = 1l; id < 8; id++) {
borrowList.add(entityManager.find(Book.class, id));
}
try {
borrowDAO.createBorrow(new Borrow(entityManager.find(Reader.class, 1l), borrowList,
new Date(currentTime), new Date(currentTime + 2592000000l)));
fail("Cannot insert empty borrow list");
} catch (IllegalArgumentException ex) {
// OK
} finally {
entityManager.close();
}
}
public void testInsertBorrowBookStateUpdate() {
long currentTime = System.currentTimeMillis();
EntityManager entityManager = entityManagerFactory.createEntityManager();
Reader reader = entityManager.find(Reader.class, 1l);
entityManager.close();
em.getTransaction().begin();
borrowDAO.createBorrow(new Borrow(reader, prepareBorrowList(),
new Date(currentTime), new Date(currentTime + 2592000000l)));
em.getTransaction().commit();
entityManager = entityManagerFactory.createEntityManager();
Borrow borrow = entityManager.find(Borrow.class, 1l);
Book firstBook = entityManager.find(Book.class, 1l);
Book secondBook = entityManager.find(Book.class, 2l);
entityManager.close();
assertEquals(Available.BORROWED, firstBook.getAvailability());
assertEquals(Available.BORROWED, secondBook.getAvailability());
}
public void testInsertBorrowWithAlreadyBorrowedBook() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
List<Book> borrowedBook = new ArrayList<Book>();
borrowedBook.add(entityManager.find(Book.class, 10l));
long currentTime = System.currentTimeMillis();
try {
borrowDAO.createBorrow(new Borrow(entityManager.find(Reader.class, 1l), borrowedBook,
new Date(currentTime), new Date(currentTime + 2592000000l)));
fail("Cannot insert borrow list with borrowed book");
} catch (IllegalArgumentException ex) {
// OK
} finally {
entityManager.close();
}
}
public void testInsertBorrowWithBookReservedByAnotherReader() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
List<Book> borrowedBook = new ArrayList<Book>();
borrowedBook.add(entityManager.find(Book.class, 8l));
long currentTime = System.currentTimeMillis();
try {
borrowDAO.createBorrow(new Borrow(entityManager.find(Reader.class, 2l), borrowedBook,
new Date(currentTime), new Date(currentTime + 2592000000l)));
fail("Cannot insert borrow list with book reserved by another reader");
} catch (IllegalArgumentException ex) {
// OK
} finally {
entityManager.close();
}
}
public void testFindAllBorrows() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
assertEquals("At start borrow list should have size 0", 0, borrowDAO.findAllBorrows().size());
List<Book> borrowList1 = new ArrayList<Book>();
borrowList1.add(entityManager.find(Book.class, 1l));
borrowList1.add(entityManager.find(Book.class, 2l));
List<Book> borrowList2 = new ArrayList<Book>();
borrowList2.add(entityManager.find(Book.class, 3l));
borrowList2.add(entityManager.find(Book.class, 4l));
// ID 1
Borrow borrow1 = new Borrow(entityManager.find(Reader.class, 1l), borrowList1, new Date(System.currentTimeMillis()),
new Date(System.currentTimeMillis()));
// ID 2
Borrow borrow2 = new Borrow(entityManager.find(Reader.class, 2l), borrowList2, new Date(System.currentTimeMillis()),
new Date(System.currentTimeMillis()));
entityManager.getTransaction().begin();
entityManager.persist(borrow1);
entityManager.persist(borrow2);
entityManager.getTransaction().commit();
entityManager.close();
assertEquals("Borrow list should have size equals to 2", 2, borrowDAO.findAllBorrows().size());
}
public void testFindBorrowByID() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
List<Book> borrowList1 = new ArrayList<Book>();
borrowList1.add(entityManager.find(Book.class, 1l));
borrowList1.add(entityManager.find(Book.class, 2l));
List<Book> borrowList2 = new ArrayList<Book>();
borrowList2.add(entityManager.find(Book.class, 3l));
borrowList2.add(entityManager.find(Book.class, 4l));
// ID 1
Borrow borrow1 = new Borrow(entityManager.find(Reader.class, 1l), borrowList1, new Date(System.currentTimeMillis()),
new Date(System.currentTimeMillis()));
// ID 2
Borrow borrow2 = new Borrow(entityManager.find(Reader.class, 2l), borrowList2, new Date(System.currentTimeMillis()),
new Date(System.currentTimeMillis()));
entityManager.close();
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(borrow1);
entityManager.getTransaction().commit();
entityManager.getTransaction().begin();
entityManager.persist(borrow2);
entityManager.getTransaction().commit();
entityManager.close();
borrow1.setBorrowID(1l);
borrow2.setBorrowID(2l);
assertEquals(borrow1, borrowDAO.findBorrowById(1l));
assertEquals(borrow2, borrowDAO.findBorrowById(2l));
}
public void testFindBorrowByIDWithNullArgument() {
try {
borrowDAO.findBorrowById(null);
fail("ID cannot be null");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testFindBorrowByIDWithArgumentLesserThan1() {
try {
borrowDAO.findBorrowById(0l);
fail("ID must be greater than 0");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testFindBorrowByReader() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
List<Book> borrowList1 = new ArrayList<Book>();
borrowList1.add(entityManager.find(Book.class, 1l));
borrowList1.add(entityManager.find(Book.class, 2l));
List<Book> borrowList2 = new ArrayList<Book>();
borrowList2.add(entityManager.find(Book.class, 3l));
borrowList2.add(entityManager.find(Book.class, 4l));
Reader reader1 = entityManager.find(Reader.class, 1l);
Reader reader2 = entityManager.find(Reader.class, 2l);
entityManager.close();
// ID 1
Borrow borrow1 = new Borrow(reader1, borrowList1,
new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()));
// ID 2
Borrow borrow2 = new Borrow(reader2, borrowList2,
new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()));
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(borrow1);
entityManager.persist(borrow2);
entityManager.getTransaction().commit();
entityManager.close();
borrow1.setBorrowID(1l);
borrow2.setBorrowID(2l);
em.getTransaction().begin();
assertEquals(borrow1, borrowDAO.findBorrowsByReader(reader1).get(0));
assertEquals(borrow2, borrowDAO.findBorrowsByReader(reader2).get(0));
em.getTransaction().commit();
}
public void testFindBorrowByReaderWithNullArgument() {
try {
borrowDAO.findBorrowsByReader(null);
fail("Cannot find borrow withou reader's id");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testFindBorrowByBookID() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
List<Book> borrowList1 = new ArrayList<Book>();
borrowList1.add(entityManager.find(Book.class, 1l));
borrowList1.add(entityManager.find(Book.class, 2l));
List<Book> borrowList2 = new ArrayList<Book>();
borrowList2.add(entityManager.find(Book.class, 3l));
borrowList2.add(entityManager.find(Book.class, 4l));
// ID 1
Borrow borrow1 = new Borrow(entityManager.find(Reader.class, 1l), borrowList1,
new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()));
// ID 2
Borrow borrow2 = new Borrow(entityManager.find(Reader.class, 2l), borrowList2,
new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()));
entityManager.close();
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(borrow1);
entityManager.getTransaction().commit();
entityManager.getTransaction().begin();
entityManager.persist(borrow2);
entityManager.getTransaction().commit();
entityManager.close();
entityManager = entityManagerFactory.createEntityManager();
assertEquals(borrow1, borrowDAO.findBorrowByTitle(entityManager.find(Book.class, 1l)));
assertEquals(borrow1, borrowDAO.findBorrowByTitle(entityManager.find(Book.class, 2l)));
assertEquals(borrow2, borrowDAO.findBorrowByTitle(entityManager.find(Book.class, 3l)));
assertEquals(borrow2, borrowDAO.findBorrowByTitle(entityManager.find(Book.class, 4l)));
entityManager.close();
}
public void testFindBorrowByBookWithNullArgument() {
try {
borrowDAO.findBorrowByTitle(null);
fail("title ID cannot be null");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testUpdateExpirationDate() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
List<Book> borrowList1 = new ArrayList<Book>();
borrowList1.add(entityManager.find(Book.class, 1l));
borrowList1.add(entityManager.find(Book.class, 2l));
Date firstTime = new Date(System.currentTimeMillis());
Date afterMonth = new Date(firstTime.getTime() + 2592000000l);
// ID 1
Borrow borrow1 = new Borrow(entityManager.find(Reader.class, 1l), borrowList1, firstTime, firstTime);
entityManager.getTransaction().begin();
entityManager.persist(borrow1);
entityManager.getTransaction().commit();
entityManager.close();
EntityManager entityManager2 = entityManagerFactory.createEntityManager();
Borrow borrow2 = entityManager2.find(Borrow.class, 1l);
borrowDAO.updateExpirationDate(borrow2, afterMonth);
assertEquals(afterMonth, entityManager2.find(Borrow.class, 1l).getExpirationDate());
entityManager2.close();
}
public void testUpdateExpirationDateWithNullIDArgument() {
try {
borrowDAO.updateExpirationDate((Borrow) null, new Date(System.currentTimeMillis()));
fail("ID cannot be null");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testUpdateExpirationDateWithNullDateArgument() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
List<Book> borrowList1 = new ArrayList<Book>();
borrowList1.add(entityManager.find(Book.class, 1l));
borrowList1.add(entityManager.find(Book.class, 2l));
Date firstTime = new Date(System.currentTimeMillis());
Date afterMonth = new Date(firstTime.getTime() + 2592000000l);
// ID 1
Borrow borrow1 = new Borrow(entityManager.find(Reader.class, 1l), borrowList1, firstTime, firstTime);
entityManager.close();
try {
borrowDAO.updateExpirationDate(borrow1, null);
fail("Date cannot be null");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testReturnBooks() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Book book1 = entityManager.find(Book.class, 1l);
Book book2 = entityManager.find(Book.class, 2l);
List<Book> borrowList = new ArrayList<Book>();
borrowList.add(book1);
borrowList.add(book2);
book1.setAvailability(Available.BORROWED);
book2.setAvailability(Available.BORROWED);
// Borrow with ID 1
Borrow borrow = new Borrow(entityManager.find(Reader.class, 1l), borrowList,
new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()));
entityManager.close();
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(borrow);
// because of lazy loading of collections in hibernate
borrow.getTitles().get(0).getId();
entityManager.getTransaction().commit();
entityManager.close();
List<Book> returnList = new ArrayList<Book>();
returnList.add(book1);
em.getTransaction().begin();
borrowDAO.returnBooks(borrow, returnList);
em.getTransaction().commit();
entityManager = entityManagerFactory.createEntityManager();
Borrow testBorrow = entityManager.find(Borrow.class, 1l);
assertFalse("Borrow list should not contain book with id 1", testBorrow.getTitles().contains(book1));
entityManager.close();
}
public void testReturnBooksWithUnborrowedBookList() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Book book1 = entityManager.find(Book.class, 1l);
Book book2 = entityManager.find(Book.class, 2l);
Book book3 = entityManager.find(Book.class, 3l);
List<Book> borrowList = new ArrayList<Book>();
borrowList.add(book1);
borrowList.add(book2);
List<Book> returnList = new ArrayList<Book>();
returnList.add(book3);
Borrow borrow = new Borrow(entityManager.find(Reader.class, 1l), borrowList,
new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()));
entityManager.getTransaction().begin();
// Borrow with ID 1
entityManager.persist(borrow);
entityManager.getTransaction().commit();
borrow.getTitles().get(0).getId();
entityManager.close();
try {
borrowDAO.returnBooks(borrow, returnList);
fail("cannot return unborrowed book");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testReturnBooksWithNullIDArgument() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Book book1 = entityManager.find(Book.class, 1l);
List<Book> returnList = new ArrayList<Book>();
returnList.add(book1);
entityManager.close();
try {
borrowDAO.returnBooks((Borrow) null, returnList);
fail("ID cannot be null");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testReturnBookWithNullReturnList() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
List<Book> borrowList1 = new ArrayList<Book>();
borrowList1.add(entityManager.find(Book.class, 1l));
borrowList1.add(entityManager.find(Book.class, 2l));
Date firstTime = new Date(System.currentTimeMillis());
Date afterMonth = new Date(firstTime.getTime() + 2592000000l);
// ID 1
Borrow borrow1 = new Borrow(entityManager.find(Reader.class, 1l), borrowList1, firstTime, firstTime);
entityManager.close();
try {
borrowDAO.returnBooks(borrow1, null);
fail("Return book list cannot be null");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testReturnBooksWithEmptyReturnList() {
List<Book> returnList = new ArrayList<Book>();
EntityManager entityManager = entityManagerFactory.createEntityManager();
List<Book> borrowList1 = new ArrayList<Book>();
borrowList1.add(entityManager.find(Book.class, 1l));
borrowList1.add(entityManager.find(Book.class, 2l));
Date firstTime = new Date(System.currentTimeMillis());
Date afterMonth = new Date(firstTime.getTime() + 2592000000l);
// ID 1
Borrow borrow1 = new Borrow(entityManager.find(Reader.class, 1l), borrowList1, firstTime, afterMonth);
entityManager.close();
try {
borrowDAO.returnBooks(borrow1, returnList);
fail("Return book list cannot be empty");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testBorrowBooks() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Book book1 = entityManager.find(Book.class, 1l);
Book book2 = entityManager.find(Book.class, 2l);
Book book3 = entityManager.find(Book.class, 3l);
List<Book> borrowList1 = new ArrayList<Book>();
borrowList1.add(book1);
borrowList1.add(book2);
List<Book> borrowAnotherBooks = new ArrayList<Book>();
borrowAnotherBooks.add(book3);
Borrow borrow = new Borrow(entityManager.find(Reader.class, 1l),borrowList1,
new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()));
entityManager.close();
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(borrow);
entityManager.getTransaction().commit();
borrow.getTitles().get(0).getId();
entityManager.close();
em.getTransaction().begin();
borrowDAO.borrowBooks(borrow, borrowAnotherBooks);
em.getTransaction().commit();
entityManager = entityManagerFactory.createEntityManager();
assertEquals(3, entityManager.find(Borrow.class, 1l).getTitles().size());
assertTrue(entityManager.find(Borrow.class, 1l).getTitles().contains(book3));
entityManager.close();
}
public void testBorrowBookWithNullBorrowID() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Book book = entityManager.find(Book.class, 1l);
List<Book> borrowBook = new ArrayList<Book>();
borrowBook.add(book);
entityManager.close();
try {
borrowDAO.borrowBooks((Borrow) null, borrowBook);
fail("ID cannot be null");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testBorrowBookWithNullBorrowList() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
List<Book> borrowList1 = new ArrayList<Book>();
borrowList1.add(entityManager.find(Book.class, 1l));
borrowList1.add(entityManager.find(Book.class, 2l));
Date firstTime = new Date(System.currentTimeMillis());
Date afterMonth = new Date(firstTime.getTime() + 2592000000l);
// ID 1
Borrow borrow1 = new Borrow(entityManager.find(Reader.class, 1l), borrowList1, firstTime, firstTime);
entityManager.close();
try {
borrowDAO.borrowBooks(borrow1, null);
fail("Borrow list cannot be null");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testBorrowMoreBooksThanAllowedCount() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Book book1 = entityManager.find(Book.class, 1l);
Book book2 = entityManager.find(Book.class, 2l);
Book book3 = entityManager.find(Book.class, 3l);
Book book4 = entityManager.find(Book.class, 4l);
Book book5 = entityManager.find(Book.class, 5l);
Book book6 = entityManager.find(Book.class, 6l);
Book book7 = entityManager.find(Book.class, 7l);
List<Book> borrowList1 = new ArrayList<Book>();
borrowList1.add(book1);
borrowList1.add(book2);
List<Book> borrowAnotherBooks = new ArrayList<Book>();
borrowAnotherBooks.add(book3);
borrowAnotherBooks.add(book4);
borrowAnotherBooks.add(book5);
borrowAnotherBooks.add(book6);
borrowAnotherBooks.add(book7);
Borrow borrow = new Borrow(entityManager.find(Reader.class, 1l),borrowList1,
new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis()));
entityManager.getTransaction().begin();
entityManager.persist(borrow);
entityManager.getTransaction().commit();
borrow.getTitles().get(0).getId();
entityManager.close();
try {
borrowDAO.borrowBooks(borrow, borrowAnotherBooks);
fail("Cannot borrow more books than allowed count");
} catch (IllegalArgumentException ex) {
// OK
}
}
public void testDeleteBorrow() {
EntityManager entityManager = entityManagerFactory.createEntityManager();
Book book1 = entityManager.find(Book.class, 1l);
Book book2 = entityManager.find(Book.class, 2l);
List<Book> borrowList1 = new ArrayList<Book>();
borrowList1.add(book1);
borrowList1.add(book2);
Borrow borrow = new Borrow(entityManager.find(Reader.class, 1l),borrowList1,
new Date(System.currentTimeMillis()), new Date(System.currentTimeMillis() + 3432423423l));
entityManager.close();
entityManager = entityManagerFactory.createEntityManager();
entityManager.getTransaction().begin();
entityManager.persist(borrow);
entityManager.getTransaction().commit();
borrow.getTitles().get(0).getId();
entityManager.close();
entityManager = entityManagerFactory.createEntityManager();
em.getTransaction().begin();
borrowDAO.deleteBorrow(borrowDAO.findBorrowById(new Long(1)));
em.getTransaction().commit();
assert(entityManager.find(Borrow.class, 1l) == null);
entityManager.close();
}
public void testDeleteBorrowWithNullArgument() {
try {
borrowDAO.deleteBorrow((Borrow) null);
fail("cannot delete null borrow");
} catch (IllegalArgumentException ex) {
// OK
}
}
}