package org.jboss.test.cts.interfaces;
import javax.ejb.*;
import javax.transaction.Status;
import javax.transaction.UserTransaction;
import javax.transaction.RollbackException;
import org.jboss.test.cts.keys.AccountPK;
public class UserTransactionTester
{
static org.apache.log4j.Category log =
org.apache.log4j.Category.getInstance(UserTransactionTester.class);
private CtsBmpHome home;
private UserTransaction ut;
private CtsBmp bean1;
private CtsBmp bean2;
public UserTransactionTester(CtsBmpHome home,
UserTransaction userTransaction)
{
this.home = home;
this.ut = userTransaction;
}
public boolean runAllTests()
{
if (!testBeginRollback())
return false;
if (!testBeginCommit())
return false;
if (!testBeginSetrollbackonlyRollback())
return false;
if (!testBeginSetrollbackonlyCommit())
return false;
try {
bean1 = home.create(new AccountPK("UT_TestBean1"), "Ole1");
} catch (Exception ex) {
log.debug("failed", ex);
return false;
}
if (!testSingleRollback())
return false;
if (!testSingleCommit())
return false;
if (!testSingleSetrollbackonlyCommit())
return false;
try {
ut.begin();
bean2 = home.create(new AccountPK("UT_TestBean2"), "Ole2");
ut.rollback();
boolean gotException = false;
try {
bean2.setPersonsName("Ole");
} catch (Exception e) {
log.info("IGNORE PREVIOUS NoSuchEntityException - it is intentional");
gotException = true;
}
if (!gotException)
throw new RuntimeException("Rollback didn't rollback create.");
} catch (Exception ex) {
log.debug("failed", ex);
return false;
}
try {
bean2 = home.create(new AccountPK("UT_TestBean2"), "Ole2");
} catch (Exception ex) {
log.debug("failed", ex);
return false;
}
return true;
}
private boolean testBeginRollback()
{
try {
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
ut.begin();
if (ut.getStatus() != Status.STATUS_ACTIVE)
throw new RuntimeException("New tx not active.");
ut.rollback();
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
} catch (Exception ex) {
log.debug("failed", ex);
return false;
}
return true;
}
private boolean testBeginCommit()
{
try {
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
ut.begin();
if (ut.getStatus() != Status.STATUS_ACTIVE)
throw new RuntimeException("New tx not active.");
ut.commit();
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
} catch (Exception ex) {
log.debug("failed", ex);
return false;
}
return true;
}
private boolean testBeginSetrollbackonlyRollback()
{
try {
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
ut.begin();
if (ut.getStatus() != Status.STATUS_ACTIVE)
throw new RuntimeException("New tx not active.");
ut.setRollbackOnly();
if (ut.getStatus() != Status.STATUS_MARKED_ROLLBACK)
throw new RuntimeException("Tx not marked for rollback.");
ut.rollback();
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
} catch (Exception ex) {
log.debug("failed", ex);
return false;
}
return true;
}
private boolean testBeginSetrollbackonlyCommit()
{
try {
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
ut.begin();
if (ut.getStatus() != Status.STATUS_ACTIVE)
throw new RuntimeException("New tx not active.");
ut.setRollbackOnly();
if (ut.getStatus() != Status.STATUS_MARKED_ROLLBACK)
throw new RuntimeException("Tx not marked for rollback.");
boolean gotException = false;
try {
ut.commit();
} catch (RollbackException rbe) {
gotException = true;
}
if (!gotException)
throw new RuntimeException("Didn't get expected RollbackException.");
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
} catch (Exception ex) {
log.debug("failed", ex);
return false;
}
return true;
}
private boolean testSingleRollback()
{
try {
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
bean1.setPersonsName("Ole");
if (!bean1.getPersonsName().equals("Ole"))
throw new RuntimeException("Unable to set property.");
ut.begin();
if (ut.getStatus() != Status.STATUS_ACTIVE)
throw new RuntimeException("New tx not active.");
if (!bean1.getPersonsName().equals("Ole"))
throw new RuntimeException("Property changes after begin.");
bean1.setPersonsName("Peter");
if (!bean1.getPersonsName().equals("Peter"))
throw new RuntimeException("Unable to set property.");
ut.rollback();
if (!bean1.getPersonsName().equals("Ole"))
throw new RuntimeException("Rollback doesn't work.");
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
} catch (Exception ex) {
log.debug("failed", ex);
return false;
}
return true;
}
private boolean testSingleCommit()
{
try {
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
bean1.setPersonsName("Ole");
if (!bean1.getPersonsName().equals("Ole"))
throw new RuntimeException("Unable to set property.");
ut.begin();
if (ut.getStatus() != Status.STATUS_ACTIVE)
throw new RuntimeException("New tx not active.");
if (!bean1.getPersonsName().equals("Ole"))
throw new RuntimeException("Property changes after begin.");
bean1.setPersonsName("Peter");
if (!bean1.getPersonsName().equals("Peter"))
throw new RuntimeException("Unable to set property.");
ut.commit();
if (!bean1.getPersonsName().equals("Peter"))
throw new RuntimeException("Property not set after commit.");
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
} catch (Exception ex) {
log.debug("failed", ex);
return false;
}
return true;
}
private boolean testSingleSetrollbackonlyCommit()
{
try {
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
bean1.setPersonsName("Ole");
if (!bean1.getPersonsName().equals("Ole"))
throw new RuntimeException("Unable to set property.");
ut.begin();
if (ut.getStatus() != Status.STATUS_ACTIVE)
throw new RuntimeException("New tx not active.");
if (!bean1.getPersonsName().equals("Ole"))
throw new RuntimeException("Property changes after begin.");
bean1.setPersonsName("Peter");
if (!bean1.getPersonsName().equals("Peter"))
throw new RuntimeException("Unable to set property.");
ut.setRollbackOnly();
if (ut.getStatus() != Status.STATUS_MARKED_ROLLBACK)
throw new RuntimeException("Tx not marked for rollback.");
boolean gotException = false;
try {
ut.commit();
} catch (RollbackException rbe) {
gotException = true;
}
if (!gotException)
throw new RuntimeException("Didn't get expected RollbackException.");
if (!bean1.getPersonsName().equals("Ole"))
throw new RuntimeException("Didn't roll back.");
if (ut.getStatus() != Status.STATUS_NO_TRANSACTION)
throw new RuntimeException("No tx should be active.");
} catch (Exception ex) {
log.debug("failed", ex);
return false;
}
return true;
}
}