【Java开源代码栏目提醒】:网学会员--在 Java开源代码编辑为广大网友搜集整理了:BatchTest.java绩等信息,祝愿广大网友取得需要的信息,参考学习。
/* $Id$
*
* Copyright (c) DAKOSY AG and Riege Software. All rights reserved.
* Use is subject to license terms.
*/
package com.h3book.penguin.examples.dao.c10;
import static com.h3book.penguin.examples.dao.InitTest.getDateByString;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.getSession;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.initHibernate;
import static com.h3book.penguin.examples.util.HibernateServiceProvider.openStatelessSession;
import static junit.framework.Assert.assertEquals;
import java.io.FileInputStream;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Date;
import junit.framework.JUnit4TestAdapter;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;
import org.hibernate.StatelessSession;
import org.hibernate.Transaction;
import org.junit.After;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import com.h3book.penguin.examples.domain.c10.Address;
import com.h3book.penguin.examples.domain.c10.Author;
import com.h3book.penguin.examples.domain.c10.Book;
import com.h3book.penguin.examples.domain.c10.Reader;
/**
* Ge: Add docs
*
* @author <a href="mailto:3710167@qq.com">Jing Ge</a>
* @version $Revision$
*/
public class BatchTest {
private static final String imageName = "oneAndMany.png";
/**
* needed when running this test on the Junit 3.8 or earlier version.
*/
public static junit.framework.Test suite() {
return new JUnit4TestAdapter(BatchTest.class);
}
@BeforeClass
public static void setUp()
{
initHibernate(Book.class, Author.class, Reader.class, Address.class);
}
@Before
public void initBooks()
{
StatelessSession ssession = openStatelessSession();
Transaction stx = ssession.beginTransaction();
for (int i = 0; i < 100; i++) {
Book book = createC10Book("The Matrix" + i,
"the book of the matrix", getDateByString("12051996"));
ssession.insert(book);
}
stx.commit();
}
@After
public void emptyBooks()
{
Session session = getSession();
Transaction t = session.beginTransaction();
session.createQuery("delete Book").executeUpdate();
t.commit();
}
/**
* @throws Exception
*/
@Test
public void insertUseSession() throws Exception
{
String dir = System.getProperty("user.dir");
Session session = getSession();
Transaction tx = session.beginTransaction();
for (int i = 0; i < 6; i++) {
Book book = createC10Book("The Matrix" + i,
"the book of the matrix", getDateByString("12051996"));
book.setCover(read(dir + "/images/" + imageName));
session.save(book);
// remove the if and try to load 1000 images, this test will throw
// expected=OutOfMemoryError
if (i % 2 == 0) {
session.flush();
session.clear();
}
}
tx.commit();
}
@Test
public void updateUseHQL() throws Exception
{
Session session = getSession();
Transaction tx = session.beginTransaction();
session.createQuery("update Book b set b.borrowed = true")
.executeUpdate();
tx.commit();
session = getSession();
tx = session.beginTransaction();
int size = session.createQuery("from Book b where b.borrowed = false")
.list().size();
assertEquals(size, 0);
tx.commit();
}
@Test
public void updateUseHQLAndSession() throws Exception
{
Session session = getSession();
Transaction tx = session.beginTransaction();
ScrollableResults result = session.createQuery("from Book").scroll();
int i = 0;
while (result.next()) {
((Book) result.get(0))
.setDescription("the book of the matrix (2. version)");
if (i % 10 == 0) {
session.flush();
session.clear();
}
}
tx.commit();
session = getSession();
tx = session.beginTransaction();
int size = session.createQuery(
"from Book b where b.description like '%2. version%'").list()
.size();
assertEquals(size, 100);
tx.commit();
}
@Test
public void insertUseStatelessSession() throws Exception
{
StatelessSession session = openStatelessSession();
Transaction tx = session.beginTransaction();
for (int i = 0; i < 6; i++) {
Book book = createC10Book("The Matrix" + i,
"the book of the matrix", getDateByString("12051996"));
book.setCover(read(System.getProperty("user.dir") + "/images/"
+ imageName));
session.insert(book);
}
tx.commit();
}
private byte[] read(String file) throws Exception // lots of exceptions
{
FileInputStream fis = new FileInputStrea