Anthony, Christian,
I make test case (one test success and 2 test fail - test name talk
Code:
package org.springframework.richclient.test.hibernate;
import org.springframework.richclient.model.hibernate.AdmSnpeMenus;
import net.sf.hibernate.HibernateException;
import net.sf.hibernate.LockMode;
import net.sf.hibernate.Session;
import net.sf.hibernate.Transaction;
/**
* @author snpe
*
*/
public class HibernateUpgradeTests extends BaseHibernateWithouSpringTests {
private Session session;
private Session transactionSession;
private Transaction transaction;
protected void setUp() throws Exception {
AdmSnpeMenus menu = getMenu();
menu.setImg("neki.gif");
openTransaction();
transactionSession.update(menu);
commitTransaction();
}
protected void tearDown() throws Exception {
}
public HibernateUpgradeTests(String name) {
super(name);
}
public void testLockUpgradeSuccess() throws HibernateException {
AdmSnpeMenus menu = getMenu();
assertTrue("id=neki",menu.getId().equals("neki"));
assertTrue("img = neki.gif",menu.getImg().equals("neki.gif"));
openTransaction();
transactionSession.lock(menu,LockMode.UPGRADE_NOWAIT);
menu.setImg("another.gif");
transactionSession.update(menu);
commitTransaction();
menu = getMenu();
assertTrue("neki=another.gif",menu.getImg().equals("another.gif"));
}
public void testLockUpgradeFail1() throws HibernateException {
AdmSnpeMenus menu = getMenu();
assertTrue("id=neki",menu.getId().equals("neki"));
assertTrue("img = neki.gif",menu.getImg().equals("neki.gif"));
menu.setImg("another.gif");
openTransaction();
transactionSession.lock(menu,LockMode.UPGRADE_NOWAIT);
transactionSession.update(menu);
commitTransaction();
menu = getMenu();
assertTrue("neki=another.gif",menu.getImg().equals("another.gif"));
}
public void testLockUpgradeFail2() throws HibernateException {
AdmSnpeMenus menu = getMenu();
assertTrue("id=neki",menu.getId().equals("neki"));
assertTrue("img = neki.gif",menu.getImg().equals("neki.gif"));
openTransaction();
menu.setImg("another.gif");
transactionSession.lock(menu,LockMode.UPGRADE_NOWAIT);
transactionSession.update(menu);
commitTransaction();
menu = getMenu();
assertTrue("neki=another.gif",menu.getImg().equals("another.gif"));
}
private AdmSnpeMenus getMenu() throws HibernateException {
session = getSessionFactory().openSession();
String query = "from " + AdmSnpeMenus.class.getName()
+ " as menu where menu.id = 'neki' ";
AdmSnpeMenus menu = (AdmSnpeMenus) session.find(query).get(0);
session.close();
return menu;
}
private void openTransaction () throws HibernateException {
if (transactionSession == null) {
transactionSession = getSessionFactory().openSession();
transaction = transactionSession.beginTransaction();
}
}
private void commitTransaction() throws HibernateException {
if (transaction != null) {
//transactionSession.flush();
transaction.commit();
transactionSession.close();
}
transactionSession = null;
transaction=null;
}
private void rollbackTransaction() throws HibernateException {
if (transaction != null) {
transaction.rollback();
transactionSession.close();
}
transactionSession = null;
transaction = null;
}
}
regards