Skip to content
Permalink
Browse files
Fix unit tests after GERONIMO-6372
  • Loading branch information
gnodet committed Jul 24, 2012
1 parent 8564bb3 commit 87593cd3899e9aad8ac01afce68580d7ab01ba3c
Showing 6 changed files with 70 additions and 48 deletions.
@@ -47,7 +47,7 @@ protected void closeTransactionLog(TransactionLog transactionLog) throws Excepti


protected TransactionLog createTransactionLog() throws Exception {
XidFactory xidFactory = new XidFactoryImpl();
XidFactory xidFactory = new XidFactoryImpl("hi".getBytes());
HOWLLog howlLog = new HOWLLog(
"org.objectweb.howl.log.BlockLogBuffer", // "bufferClassName",
4, // "bufferSizeKBytes",
@@ -35,9 +35,6 @@
* */
public abstract class AbstractRecoveryTest extends TestCase {

protected TransactionLog txLog;

protected final XidFactory xidFactory = new XidFactoryImpl();
protected TransactionManagerImpl txManager;
private static final String RM1 = "rm1";
private static final String RM2 = "rm2";
@@ -47,7 +44,11 @@ public abstract class AbstractRecoveryTest extends TestCase {

@Override
protected void setUp() throws Exception {
txManager = new TransactionManagerImpl(1, xidFactory, txLog);
txManager = createTransactionManager();
}

protected TransactionManagerImpl createTransactionManager() throws Exception {
return new TransactionManagerImpl(1, new XidFactoryImpl("hi".getBytes()), null);
}

public void test2ResOnlineAfterRecoveryStart() throws Exception {
@@ -57,7 +58,7 @@ public void test2ResOnlineAfterRecoveryStart() throws Exception {
MockTransactionInfo[] txInfos = makeTxInfos(xids);
addBranch(txInfos, xares1);
addBranch(txInfos, xares2);
prepareLog(txLog, txInfos);
prepareLog(txManager.getTransactionLog(), txInfos);
prepareForReplay();
Recovery recovery = txManager.recovery;
assertTrue(!recovery.hasRecoveryErrors());
@@ -97,15 +98,15 @@ public void test3ResOnlineAfterRecoveryStart() throws Exception {
MockTransactionInfo[] txInfos12 = makeTxInfos(xids12);
addBranch(txInfos12, xares1);
addBranch(txInfos12, xares2);
prepareLog(txLog, txInfos12);
prepareLog(txManager.getTransactionLog(), txInfos12);
MockTransactionInfo[] txInfos13 = makeTxInfos(xids13);
addBranch(txInfos13, xares1);
addBranch(txInfos13, xares3);
prepareLog(txLog, txInfos13);
prepareLog(txManager.getTransactionLog(), txInfos13);
MockTransactionInfo[] txInfos23 = makeTxInfos(xids23);
addBranch(txInfos23, xares2);
addBranch(txInfos23, xares3);
prepareLog(txLog, txInfos23);
prepareLog(txManager.getTransactionLog(), txInfos23);
prepareForReplay();
Recovery recovery = txManager.recovery;
assertTrue(!recovery.hasRecoveryErrors());
@@ -136,7 +137,7 @@ private void prepareLog(TransactionLog txLog, MockTransactionInfo[] txInfos) thr
private Xid[] getXidArray(int i) {
Xid[] xids = new Xid[i];
for (int j = 0; j < xids.length; j++) {
xids[j] = xidFactory.createXid();
xids[j] = txManager.getXidFactory().createXid();
}
return xids;
}
@@ -145,7 +146,7 @@ private void addBranch(MockTransactionInfo[] txInfos, MockXAResource xaRes) thro
for (int i = 0; i < txInfos.length; i++) {
MockTransactionInfo txInfo = txInfos[i];
Xid globalXid = txInfo.globalXid;
Xid branchXid = xidFactory.createBranch(globalXid, branchCounter++);
Xid branchXid = txManager.getXidFactory().createBranch(globalXid, branchCounter++);
xaRes.start(branchXid, 0);
txInfo.branches.add(new TransactionBranchInfoImpl(branchXid, xaRes.getName()));
}
@@ -19,10 +19,10 @@

import java.io.File;

import org.apache.geronimo.transaction.log.HOWLLog;
import junit.extensions.TestSetup;
import junit.framework.Test;
import junit.framework.TestSuite;
import junit.extensions.TestSetup;
import org.apache.geronimo.transaction.log.HOWLLog;

/**
*
@@ -37,27 +37,29 @@ public class HOWLLogRecoveryTest extends AbstractRecoveryTest {
private static final String targetDir = new File(basedir, "target").getAbsolutePath();
private static final File txlogDir = new File(basedir, "target/" + logFileDir);

public void test2Again() throws Exception {
test2ResOnlineAfterRecoveryStart();
}

public void test3Again() throws Exception {
test3ResOnlineAfterRecoveryStart();
}

protected void setUp() throws Exception {
@Override
public void setUp() throws Exception {
// Deletes the previous transaction log files.
File[] files = txlogDir.listFiles();
if ( null != files ) {
for (int i = 0; i < files.length; i++) {
files[i].delete();
}
}
setUpHowlLog();
super.setUp();
}

private void setUpHowlLog() throws Exception {
public void test2Again() throws Exception {
test2ResOnlineAfterRecoveryStart();
}

public void test3Again() throws Exception {
test3ResOnlineAfterRecoveryStart();
}

@Override
protected TransactionManagerImpl createTransactionManager() throws Exception {
XidFactory xidFactory = new XidFactoryImpl("hi".getBytes());
HOWLLog howlLog = new HOWLLog(
"org.objectweb.howl.log.BlockLogBuffer", // "bufferClassName",
4, // "bufferSizeKBytes",
@@ -76,18 +78,17 @@ private void setUpHowlLog() throws Exception {
new File(targetDir)
);
howlLog.doStart();
txLog = howlLog;
return new TransactionManagerImpl(1, xidFactory, howlLog);
}

protected void tearDown() throws Exception {
((HOWLLog)txLog).doStop();
txLog = null;
((HOWLLog) txManager.getTransactionLog()).doStop();
}

protected void prepareForReplay() throws Exception {
Thread.sleep(100);
tearDown();
setUpHowlLog();
super.setUp();
txManager = createTransactionManager();
}

public static Test suite() {
@@ -25,16 +25,21 @@
* */
public class MockLogRecoveryTest extends AbstractRecoveryTest {

protected void setUp() throws Exception {
txLog = new MockLog();
private TransactionLog log;

@Override
public void setUp() throws Exception {
log = new MockLog();
super.setUp();
}

protected void tearDown() throws Exception {
txLog = null;
@Override
protected TransactionManagerImpl createTransactionManager() throws Exception {
return new TransactionManagerImpl(1, new XidFactoryImpl("hi".getBytes()), log);
}

protected void prepareForReplay() throws Exception {
txManager.recovery.recoverLog();
Thread.sleep(100);
txManager = createTransactionManager();
}
}
@@ -35,7 +35,6 @@
* */
public class RecoveryTest extends TestCase {

XidFactory xidFactory = new XidFactoryImpl();
MockLog mockLog = new MockLog();
protected TransactionManagerImpl txManager;
private final String RM1 = "rm1";
@@ -45,7 +44,16 @@ public class RecoveryTest extends TestCase {

@Override
protected void setUp() throws Exception {
txManager = new TransactionManagerImpl(1, xidFactory, mockLog);
txManager = createTransactionManager();
}

protected TransactionManagerImpl createTransactionManager() throws XAException, InterruptedException {
return new TransactionManagerImpl(1, new XidFactoryImpl("hi".getBytes()), mockLog);
}

protected void prepareForReplay() throws Exception {
Thread.sleep(100);
txManager = createTransactionManager();
}

public void testCommittedRMToBeRecovered() throws Exception {
@@ -58,8 +66,8 @@ public void testCommittedRMToBeRecovered() throws Exception {
MockTransactionInfo[] txInfos = makeTxInfos(xids);
addBranch(txInfos, xares1);
prepareLog(mockLog, txInfos);
Recovery recovery = new RecoveryImpl(txManager);
recovery.recoverLog();
prepareForReplay();
Recovery recovery = txManager.recovery;
assertTrue(!recovery.hasRecoveryErrors());
assertTrue(recovery.getExternalXids().isEmpty());
assertTrue(!recovery.localRecoveryComplete());
@@ -77,8 +85,8 @@ public void test2ResOnlineAfterRecoveryStart() throws Exception {
addBranch(txInfos, xares1);
addBranch(txInfos, xares2);
prepareLog(mockLog, txInfos);
Recovery recovery = new RecoveryImpl(txManager);
recovery.recoverLog();
prepareForReplay();
Recovery recovery = txManager.recovery;
assertTrue(!recovery.hasRecoveryErrors());
assertTrue(recovery.getExternalXids().isEmpty());
assertTrue(!recovery.localRecoveryComplete());
@@ -87,14 +95,14 @@ public void test2ResOnlineAfterRecoveryStart() throws Exception {
assertEquals(3, xares1.committed.size());
recovery.recoverResourceManager(xares2);
assertEquals(3, xares2.committed.size());
assertTrue(recovery.localRecoveryComplete());
assertTrue(txManager.recovery.localRecoveryComplete());

}

private void addBranch(MockTransactionInfo[] txInfos, MockXAResource xaRes) throws XAException {
for (int i = 0; i < txInfos.length; i++) {
MockTransactionInfo txInfo = txInfos[i];
Xid xid = xidFactory.createBranch(txInfo.globalXid, count++);
Xid xid = txManager.getXidFactory().createBranch(txInfo.globalXid, count++);
xaRes.start(xid, 0);
txInfo.branches.add(new TransactionBranchInfoImpl(xid, xaRes.getName()));
}
@@ -144,8 +152,8 @@ public void test3ResOnlineAfterRecoveryStart() throws Exception {
addBranch(txInfos23, xares2);
addBranch(txInfos23, xares3);
prepareLog(mockLog, txInfos23);
Recovery recovery = new RecoveryImpl(txManager);
recovery.recoverLog();
prepareForReplay();
Recovery recovery = txManager.recovery;
assertTrue(!recovery.hasRecoveryErrors());
assertTrue(recovery.getExternalXids().isEmpty());
assertEquals(9, recovery.localUnrecoveredCount());
@@ -172,7 +180,7 @@ private void prepareLog(TransactionLog txLog, MockTransactionInfo[] txInfos) thr
private Xid[] getXidArray(int i) {
Xid[] xids = new Xid[i];
for (int j = 0; j < xids.length; j++) {
xids[j] = xidFactory.createXid();
xids[j] = txManager.getXidFactory().createXid();
}
return xids;
}
@@ -23,6 +23,7 @@
import javax.transaction.RollbackException;
import javax.transaction.Status;
import javax.transaction.Transaction;
import javax.transaction.xa.XAException;
import javax.transaction.xa.XAResource;
import javax.transaction.xa.Xid;

@@ -45,7 +46,11 @@ public class TransactionManagerImplTest extends TestCase {
TransactionManagerImpl tm;

protected void setUp() throws Exception {
tm = new TransactionManagerImpl(10,
tm = createTransactionManager();
}

private TransactionManagerImpl createTransactionManager() throws XAException {
return new TransactionManagerImpl(10,
new XidFactoryImpl("WHAT DO WE CALL IT?".getBytes()), transactionLog);
}

@@ -255,7 +260,8 @@ public void testSimpleRecovery() throws Exception {
tm.suspend();
tm.prepare(tx);
//recover
tm.recovery.recoverLog();
Thread.sleep(100); // Wait a bit for new XidFactoryImpl
tm = createTransactionManager();
recover(r1_1);
recover(r1_2);
assertTrue(r1_2.isCommitted());
@@ -285,7 +291,8 @@ public void testImportedXidRecovery() throws Exception {
tm.suspend();
tm.prepare(tx);
//recover
tm.recovery.recoverLog();
Thread.sleep(100); // Wait a bit for new XidFactoryImpl
tm = createTransactionManager();
recover(r1_1);
recover(r1_2);
assertTrue(!r1_2.isCommitted());

0 comments on commit 87593cd

Please sign in to comment.