Skip to content
Permalink
Browse files
add some agreed upon test cases for GERONIMO-4448, merge from branch 2.1
  • Loading branch information
Lin Sun committed Dec 10, 2008
1 parent 10d371c commit 3e51acd6967f3d01afe79e1f6d3dfb58f7b70962
Showing 3 changed files with 143 additions and 23 deletions.
@@ -18,6 +18,7 @@
package org.apache.geronimo.transaction.manager;

import javax.transaction.Status;
import javax.transaction.InvalidTransactionException;
import javax.transaction.Transaction;
import javax.transaction.TransactionManager;
import javax.transaction.xa.XAResource;
@@ -19,6 +19,7 @@

import java.util.Map;

import javax.transaction.InvalidTransactionException;
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.Transaction;
@@ -290,28 +291,125 @@ public void testImportedXidRecovery() throws Exception {
assertEquals(xid, recovered.keySet().iterator().next());
}

public void testTimeout() throws Exception
{
long timeout = tm.getTransactionTimeoutMilliseconds(0L);
tm.setTransactionTimeout((int)timeout/4000);
tm.begin();
System.out.println("Test to sleep for " + timeout + " millisecs");
Thread.sleep(timeout);
try
{
tm.commit();
fail("Tx Should get Rollback exception");
}catch(RollbackException rex)
{
// Caught expected exception
}

// Now test if the default timeout is active
tm.begin();
System.out.println("Test to sleep for " + (timeout/2) + " millisecs");
Thread.sleep((timeout/2));
tm.commit();
// Its a failure if exception occurs.
}
public void testTimeout() throws Exception
{
long timeout = tm.getTransactionTimeoutMilliseconds(0L);
tm.setTransactionTimeout((int)timeout/4000);
tm.begin();
System.out.println("Test to sleep for " + timeout + " millisecs");
Thread.sleep(timeout);
try
{
tm.commit();
fail("Tx Should get Rollback exception");
}catch(RollbackException rex)
{
// Caught expected exception
}

// Now test if the default timeout is active
tm.begin();
System.out.println("Test to sleep for " + (timeout/2) + " millisecs");
Thread.sleep((timeout/2));
tm.commit();
// Its a failure if exception occurs.
}

// resume throws InvalidTransactionException on completed tx (via commit)
public void testResume1() throws Exception {
Transaction tx;
assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
tm.begin();
assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
tx = tm.getTransaction();
assertNotNull(tx);
assertEquals(Status.STATUS_ACTIVE, tx.getStatus());

tm.commit();
assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
assertNull(tm.getTransaction());

try {
tm.resume(tx);
fail();
} catch (InvalidTransactionException e) {
// expected
}
}

// resume throws InvalidTransactionException on completed tx (via rollback)
public void testResume2() throws Exception {
Transaction tx;
assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
tm.begin();
assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
tx = tm.getTransaction();
assertNotNull(tx);
assertEquals(Status.STATUS_ACTIVE, tx.getStatus());

tx = tm.suspend();
assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
assertNull(tm.getTransaction());

tm.resume(tx);
assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
assertEquals(tx, tm.getTransaction());

tm.rollback();
assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
assertNull(tm.getTransaction());

try {
tm.resume(tx);
fail();
} catch (InvalidTransactionException e) {
// expected
}
}

// resume work on null tx
public void testResume3() throws Exception {
Transaction tx;
assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
tm.begin();
assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
tx = tm.getTransaction();
assertNotNull(tx);
assertEquals(Status.STATUS_ACTIVE, tx.getStatus());

tm.commit();
assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
assertNull(tm.getTransaction());

// tx should be null
tx = tm.suspend();
assertNull(tx);

try {
tm.resume(tx);
} catch (InvalidTransactionException e) {
// null is considered valid so we don't expect InvalidTransactionException here
e.printStackTrace();
fail();
}
}

// resume works on any valid tx
public void testResume4() throws Exception {
Transaction tx;
assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
tm.begin();
assertEquals(Status.STATUS_ACTIVE, tm.getStatus());
tx = tm.getTransaction();
assertNotNull(tx);
assertEquals(Status.STATUS_ACTIVE, tx.getStatus());

tm.resume(tx);
assertNotNull(tx);
assertEquals(Status.STATUS_ACTIVE, tx.getStatus());

tm.commit();
assertEquals(Status.STATUS_NO_TRANSACTION, tm.getStatus());
assertNull(tm.getTransaction());
}
}
@@ -83,6 +83,15 @@ public void testInterposedSynchIsCalledOnRollback() throws Exception {
tm.rollback();
checkInterposedSyncCalled();
}

/*public void testNormalSynchIsNotCalledOnRollback() throws Exception {
normalSync = new CountingSync();
tm.begin();
tm.getTransaction().registerSynchronization(normalSync);
tm.rollback();
assertFalse(normalSync.beforeCompletionCalled());
assertTrue(normalSync.afterCompletionCalled());
}*/

public void testInterposedSynchIsCalledOnMarkRollback() throws Exception {
setUpInterposedSync();
@@ -150,12 +159,16 @@ private class CountingSync implements Synchronization {

private int beforeCount = -1;
private int afterCount = -1;
private boolean beforeCalled = false;
private boolean afterCalled = false;

public void beforeCompletion() {
beforeCalled = true;
beforeCount = beforeCounter++;
}

public void afterCompletion(int i) {
afterCalled = true;
afterCount = afterCounter++;
}

@@ -166,6 +179,14 @@ public int getBeforeCount() {
public int getAfterCount() {
return afterCount;
}

public boolean beforeCompletionCalled() {
return beforeCalled;
}

public boolean afterCompletionCalled() {
return afterCalled;
}
}

}

0 comments on commit 3e51acd

Please sign in to comment.