【Java开源代码栏目提醒】:网学会员为需要Java开源代码的朋友们搜集整理了ArrayMappingTest.java相关资料,希望对各位网友有所帮助!
/* $Id$
*
* Copyright (c) DAKOSY AG and Riege Software. All rights reserved.
* Use is subject to license terms.
*/
package com.h3book.penguin.examples.dao.c8.array;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.getSession;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.initHibernate;
import static org.junit.Assert.assertEquals;
import java.util.Collection;
import java.util.Date;
import junit.framework.JUnit4TestAdapter;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.BeforeClass;
import org.junit.Test;
import com.h3book.penguin.examples.dao.InitTest;
import com.h3book.penguin.examples.dao.c8.array.*;
import com.h3book.penguin.examples.domain.c8.array.Author;
import com.h3book.penguin.examples.domain.c8.array.Book;
/**
* Ge: Add docs
*
* @author <a href="mailto:3710167@qq.com">Jing Ge</a>
* @version $Revision$
*/
public class ArrayMappingTest extends InitTest {
/**
* needed when running this test on the Junit 3.8 or earlier version.
*/
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(ArrayMappingTest.class);
}
@BeforeClass
public static void setUp() {
initHibernate(Book.class, Author.class);
}
@Override
@After
public void emptyBooks() {
Session session = getSession();
Transaction t = session.beginTransaction();
for (Book book : (Collection<Book>) session.createCriteria(Book.class).list()) {
session.delete(book);
}
t.commit();
}
@Test
public void listAuthors() {
Book book1 = createC8Book("Thinking in Matrix",
"one of the best Matrix book", getDateByString("20032006"));
Book book2 = createC8Book("Hibernate3", "new book about Hibernate 3",
getDateByString("06062007"));
Author author1 = createC8Author("Jing", "Ge",
getDateByString("01011975"), true);
Author author2 = createC8Author("Michael", "Peter",
getDateByString("01011970"), true);
Author author3 = createC8Author("Anderson", "Neo",
getDateByString("01011966"), true);
book1.setAuthors(author3, author2);
author3.addBook(book1);
author2.addBook(book1);
book2.setAuthors(author1);
author1.addBook(book2);
Transaction t = null;
try {
Session session = getSession();
t = session.beginTransaction();
session.saveOrUpdate(book1);
session.saveOrUpdate(book2);
t.commit();
} catch (HibernateException e) {
if (t != null) {
t.rollback();
e.printStackTrace();
throw e;
}
}
try {
Session session = getSession();
t = session.beginTransaction();
Book book = (Book) session.load(Book.class, book1.getOid());
assertEquals(2, book.getAuthors().length);
assertEquals(author3.getOid(), book.getAuthors()[0].getOid());
assertEquals(author2.getOid(), book.getAuthors()[1].getOid());
t.commit();
} catch (HibernateException e) {
if (t != null) {
t.rollback();
e.printStackTrace();
throw e;
}
}
}
// help
private Book createC8Book(String name, String description, Date publish) {
Book book = new Book();
book.setName(name);
book.setDescription(description);
book.setPublish(publish);
return book;
}
private Author createC8Author(String firstName, String lastName,
Date birthday, boolean isMale) {
Author author = new Author();
author.setFirstName(firstName);
author.setLastName(lastName);
author.setBirthday(birthday);
author.setMale(isMale);
return author;
}
}