【Java开源代码栏目提醒】:本文主要为网学会员提供BookDAOTest.java,希望对需要BookDAOTest.java网友有所帮助,学习一下!
/* $Id$
*
* Copyright (c) DAKOSY AG and Riege Software. All rights reserved.
* Use is subject to license terms.
*/
package com.h3book.penguin.examples.dao;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.ShutDown;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.createNewSession;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.getSession;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.initHibernate;
import static org.junit.Assert.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import junit.framework.JUnit4TestAdapter;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.*;
import com.h3book.penguin.examples.domain.Author;
import com.h3book.penguin.examples.domain.Book;
/**
* Ge: Add docs
*
* @author <a href="mailto:3710167@qq.com">Jing Ge</a>
* @version $Revision$
*/
public class BookDAOTest {
private static BookDAO bookDAO;
private Book book1;
/**
* needed when running this test on the Junit 3.8 or earlier version.
*/
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(BookDAOTest.class);
}
@BeforeClass
public static void setUp()
{
initHibernate(Book.class, Author.class);
bookDAO = new BookDAO();
}
@AfterClass
public static void tearDown()
{
bookDAO = null;
ShutDown();
}
@Before
public void initBooks()
{
book1 = createBook("The Matrix", "the book of the matrix",
getDateByString("12051996"));
Book book2 = createBook("Thinking in
Java",
"one of the best Java book", getDateByString("20032006"));
Transaction t = null;
try {
Session session = getSession();
t = session.beginTransaction();
int o = bookDAO.save(book1);
bookDAO.save(book2);
t.commit();
} catch (HibernateException e) {
if (t != null) {
t.rollback();
e.printStackTrace();
}
}
}
@After
public void emptyBooks()
{
Session session = getSession();
Transaction t = session.beginTransaction();
// session.createQuery("delete Book").executeUpdate();
for (Book book : (List<Book>)session.createCriteria(Book.class).list()) {
session.delete(book);
}
t.commit();
}
@Test(timeout= 2 * 1000)
public void getByOid()
{
Book book2 = bookDAO.getByOid(book1.getOid());
assertNotNull(book2);
assertEquals(book2.getName(), "The Matrix");
assertEquals(book2.getDescription(), "the book of the matrix");
assertEquals(dateToString(book2.getPublish()), "1996.5.12");
}
@Test
public void save()
{
Date publish = new Date();
Book book = createBook("Hibernate3", "hibernate 3 with annotation",
publish);
Author a1 = createAuthor("Jing", "Ge", getDateByString("06011975"),
true);
a1.addBook(book);
Author a2 = createAuthor("Neo", "Anderson",
getDateByString("01011970"), true);
a2.addBook(book);
// book.addAuthor(a1);
// book.addAuthor(a2);
Session session = getSession();
Transaction t = session.beginTransaction();
int oid = bookDAO.save(book);
t.commit();
Book book2 = bookDAO.getByOid(oid);
assertEquals(book2.getName(), "Hibernate3");
assertEquals(book2.getDescription(), "hibernate 3 with annotation");
assertEquals(book2.getPublish().getTime(), publish.getTime());
session = createNewSession();
t = session.beginTransaction();
book2 = (Book) session.get(Book.class, oid);
book2.setName("Hibernate3.2.1");
book.setName("Hibernate3.2");
session.merge(book);
session.update(book2);
t.commit();
//
// Author author = book2.getAuthors().get(0);
// assertEquals(author.getFirstName(), "Jing");
// assertEquals(author.getLastName(), "Ge");
// assertEquals(dateToString(author.getBirthday()), "1975.1.6");
// assertEquals(author.isMale(), true);
// author = book2.getAuthors().get(1);
// assertEquals(author.getFirstName(), "Neo");
// assertEquals(author.getLastName(), "Anderson");
// assertEquals(dateToString(author.getBirthday()), "1970.1.1");
// assertEquals(author.isMale(), true);
}
@Test
public void update()
{
book1.setName("The Matrix Book");
Session session = getSession();
Transaction t = session.beginTransaction();
bookDAO.update(book1);
t.commit();
Book book2 = bookDAO.getByOid(book1.getOid());
assertEquals(book2