[jboss-svn-commits] JBL Code SVN: r23856 - labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate.

jboss-svn-commits at lists.jboss.org jboss-svn-commits at lists.jboss.org
Thu Nov 13 09:14:55 EST 2008


Author: jhalliday
Date: 2008-11-13 09:14:55 -0500 (Thu, 13 Nov 2008)
New Revision: 23856

Added:
   labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate/TestSynchronization.java
   labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate/TestXAResource.java
Modified:
   labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate/SubordinateTestCase.java
Log:
Improved unit test coverage for subordinate tx handling. JBTM-430


Modified: labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate/SubordinateTestCase.java
===================================================================
--- labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate/SubordinateTestCase.java	2008-11-13 14:09:15 UTC (rev 23855)
+++ labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate/SubordinateTestCase.java	2008-11-13 14:14:55 UTC (rev 23856)
@@ -1,33 +1,41 @@
 /*
  * JBoss, Home of Professional Open Source
- * Copyright 2006, Red Hat Middleware LLC, and individual contributors 
- * as indicated by the @author tags. 
+ * Copyright 2006, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags.
  * See the copyright.txt in the distribution for a
- * full listing of individual contributors. 
+ * full listing of individual contributors.
  * This copyrighted material is made available to anyone wishing to use,
  * modify, copy, or redistribute it subject to the terms and conditions
  * of the GNU Lesser General Public License, v. 2.1.
- * This program is distributed in the hope that it will be useful, but WITHOUT A 
- * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A 
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
  * You should have received a copy of the GNU Lesser General Public License,
  * v.2.1 along with this distribution; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
  * MA  02110-1301, USA.
- * 
+ *
  * (C) 2005-2006,
  * @author JBoss Inc.
  */
 package com.hp.mwtests.ts.jta.subordinate;
 
-import java.io.File;
-import java.io.PrintWriter;
-
 import junit.framework.TestCase;
 
 import com.arjuna.ats.arjuna.coordinator.ActionManager;
+import com.arjuna.ats.arjuna.coordinator.TwoPhaseOutcome;
+import com.arjuna.ats.arjuna.common.Uid;
 import com.arjuna.ats.internal.jta.transaction.arjunacore.subordinate.TransactionImple;
+import com.arjuna.ats.internal.jta.transaction.arjunacore.jca.TxImporter;
+import com.arjuna.ats.internal.jta.transaction.arjunacore.jca.XATerminatorImple;
+import com.arjuna.ats.jta.xa.XidImple;
 
+import javax.transaction.RollbackException;
+import javax.transaction.xa.XAResource;
+import javax.transaction.xa.Xid;
+import javax.transaction.xa.XAException;
+import javax.resource.spi.XATerminator;
+
 public class SubordinateTestCase extends TestCase
 {
 	public void testCleanupCommit () throws Exception
@@ -39,10 +47,10 @@
 			tm.doPrepare();
 			tm.doCommit();
 		}
-		
+
 		assertEquals(ActionManager.manager().inflightTransactions().size(), 0);
 	}
-	
+
 	public void testCleanupRollback () throws Exception
 	{
 		for (int i = 0; i < 1000; i++)
@@ -51,12 +59,12 @@
 
 			tm.doRollback();
 		}
-		
+
 		assertEquals(ActionManager.manager().inflightTransactions().size(), 0);
 	}
-	
+
 	public void testCleanupSecondPhaseRollback () throws Exception
-	{	
+	{
 		for (int i = 0; i < 1000; i++)
 		{
 			final TransactionImple tm = new TransactionImple(0); // implicit begin
@@ -64,10 +72,10 @@
 			tm.doPrepare();
 			tm.doRollback();
 		}
-		
+
 		assertEquals(ActionManager.manager().inflightTransactions().size(), 0);
 	}
-	
+
 	public void testCleanupOnePhaseCommit () throws Exception
 	{
 		for (int i = 0; i < 1000; i++)
@@ -76,7 +84,188 @@
 
 			tm.doOnePhaseCommit();
 		}
-		
+
 		assertEquals(ActionManager.manager().inflightTransactions().size(), 0);
 	}
+
+    /////////////
+
+    public void testOnePhaseCommitSync() throws Exception
+    {
+        final TransactionImple tm = new TransactionImple(0);
+        final TestSynchronization sync = new TestSynchronization();
+        tm.registerSynchronization(sync);
+        tm.doOnePhaseCommit();
+        assertTrue(sync.isBeforeCompletionDone());
+        assertTrue(sync.isAfterCompletionDone());
+        assertEquals(javax.transaction.Status.STATUS_COMMITTED, tm.getStatus());
+    }
+
+    public void testOnePhaseCommitSyncViaXATerminator() throws Exception
+    {
+        final Xid xid = new XidImple(new Uid());
+        final TransactionImple tm = TxImporter.importTransaction(xid);
+        final TestSynchronization sync = new TestSynchronization();
+        tm.registerSynchronization(sync);
+        final XATerminator xaTerminator = new XATerminatorImple();
+        xaTerminator.commit(xid, true);
+        assertTrue(sync.isBeforeCompletionDone());
+        assertTrue(sync.isAfterCompletionDone());
+        assertEquals(javax.transaction.Status.STATUS_COMMITTED, tm.getStatus());
+    }
+
+    public void testOnePhaseCommitSyncWithRollbackOnly() throws Exception
+    {
+        final TransactionImple tm = new TransactionImple(0);
+        final TestSynchronization sync = new TestSynchronization();
+        tm.registerSynchronization(sync);
+        tm.setRollbackOnly();
+        try {
+            tm.doOnePhaseCommit();
+            fail("did not get expected rollback exception");
+        } catch(RollbackException e) {
+            // expected - we tried to commit a rollbackonly tx.
+        }
+        assertFalse(sync.isBeforeCompletionDone());
+        assertTrue(sync.isAfterCompletionDone());
+        assertEquals(javax.transaction.Status.STATUS_ROLLEDBACK, tm.getStatus());
+    }
+
+    public void testOnePhaseCommitSyncWithRollbackOnlyViaXATerminator() throws Exception
+    {
+        final Xid xid = new XidImple(new Uid());
+        final TransactionImple tm = TxImporter.importTransaction(xid);
+        final TestSynchronization sync = new TestSynchronization();
+        tm.registerSynchronization(sync);
+        tm.setRollbackOnly();
+        final XATerminator xaTerminator = new XATerminatorImple();
+        try {
+            xaTerminator.commit(xid, true);
+            tm.doOnePhaseCommit();
+            fail("did not get expected rollback exception");
+        } catch(XAException e) {
+            assertEquals("javax.transaction.RollbackException", e.getCause().getClass().getName());
+            assertEquals(XAException.XA_RBROLLBACK, e.errorCode);
+            // expected - we tried to commit a rollbackonly tx.
+        }
+        assertFalse(sync.isBeforeCompletionDone());
+        assertTrue(sync.isAfterCompletionDone());
+        assertEquals(javax.transaction.Status.STATUS_ROLLEDBACK, tm.getStatus());
+    }
+
+    public void testRollbackSync() throws Exception
+    {
+        final TransactionImple tm = new TransactionImple(0);
+        final TestSynchronization sync = new TestSynchronization();
+        tm.registerSynchronization(sync);
+        tm.doRollback();
+        assertFalse(sync.isBeforeCompletionDone());
+        assertTrue(sync.isAfterCompletionDone());
+        assertEquals(javax.transaction.Status.STATUS_ROLLEDBACK, tm.getStatus());
+    }
+
+    public void testRollbackSyncViaXATerminator() throws Exception
+    {
+        final Xid xid = new XidImple(new Uid());
+        final TransactionImple tm = TxImporter.importTransaction(xid);
+        final TestSynchronization sync = new TestSynchronization();
+        tm.registerSynchronization(sync);
+        final XATerminator xaTerminator = new XATerminatorImple();
+        xaTerminator.rollback(xid);
+        assertFalse(sync.isBeforeCompletionDone());
+        assertTrue(sync.isAfterCompletionDone());
+        assertEquals(javax.transaction.Status.STATUS_ROLLEDBACK, tm.getStatus());
+    }
+
+    public void testTwoPhaseCommitSync() throws Exception
+    {
+        final TransactionImple tm = new TransactionImple(0);
+        final TestSynchronization sync = new TestSynchronization();
+        tm.registerSynchronization(sync);
+        assertEquals(TwoPhaseOutcome.PREPARE_READONLY, tm.doPrepare());
+        tm.doCommit();
+        assertTrue(sync.isBeforeCompletionDone());
+        assertTrue(sync.isAfterCompletionDone());
+        assertEquals(javax.transaction.Status.STATUS_COMMITTED, tm.getStatus());
+    }
+
+    public void testTwoPhaseCommitSyncViaXATerminator() throws Exception
+    {
+        final Xid xid = new XidImple(new Uid());
+        final TransactionImple tm = TxImporter.importTransaction(xid);
+        final TestSynchronization sync = new TestSynchronization();
+        tm.registerSynchronization(sync);
+        final XATerminator xaTerminator = new XATerminatorImple();
+        assertEquals(XAResource.XA_RDONLY, xaTerminator.prepare(xid));
+        // note that unlike the above test we don't call commit - the XA_RDONLY means its finished, per XA semantics.
+        assertTrue(sync.isBeforeCompletionDone());
+        assertTrue(sync.isAfterCompletionDone());
+        assertEquals(javax.transaction.Status.STATUS_COMMITTED, tm.getStatus());
+    }
+
+    public void testTwoPhaseCommitSyncWithXAOK() throws Exception
+    {
+        final TransactionImple tm = new TransactionImple(0);
+        final TestSynchronization sync = new TestSynchronization();
+        tm.registerSynchronization(sync);
+        final TestXAResource xaResource = new TestXAResource();
+        xaResource.setPrepareReturnValue(XAResource.XA_OK);
+        tm.enlistResource(xaResource);
+        assertEquals(TwoPhaseOutcome.PREPARE_OK, tm.doPrepare());
+        tm.doCommit();
+        assertTrue(sync.isBeforeCompletionDone());
+        assertTrue(sync.isAfterCompletionDone());
+        assertEquals(javax.transaction.Status.STATUS_COMMITTED, tm.getStatus());
+    }
+
+    public void testTwoPhaseCommitSyncWithXAOKViaXATerminator() throws Exception
+    {
+        final Xid xid = new XidImple(new Uid());
+        final TransactionImple tm = TxImporter.importTransaction(xid);
+        final TestSynchronization sync = new TestSynchronization();
+        tm.registerSynchronization(sync);
+        final TestXAResource xaResource = new TestXAResource();
+        xaResource.setPrepareReturnValue(XAResource.XA_OK);
+        tm.enlistResource(xaResource);
+        final XATerminator xaTerminator = new XATerminatorImple();
+        assertEquals(XAResource.XA_OK, xaTerminator.prepare(xid));
+        xaTerminator.commit(xid, false);
+        assertTrue(sync.isBeforeCompletionDone());
+        assertTrue(sync.isAfterCompletionDone());
+        assertEquals(javax.transaction.Status.STATUS_COMMITTED, tm.getStatus());
+    }
+
+    public void testTwoPhaseCommitSyncWithRollbackOnly() throws Exception
+    {
+        final TransactionImple tm = new TransactionImple(0);
+        final TestSynchronization sync = new TestSynchronization();
+        tm.registerSynchronization(sync);
+        tm.setRollbackOnly();
+        assertEquals(TwoPhaseOutcome.PREPARE_NOTOK, tm.doPrepare());
+        tm.doRollback();
+        assertFalse(sync.isBeforeCompletionDone());
+        assertTrue(sync.isAfterCompletionDone());
+        assertEquals(javax.transaction.Status.STATUS_ROLLEDBACK, tm.getStatus());
+    }
+
+    public void testTwoPhaseCommitSyncWithRollbackOnlyViaXATerminator() throws Exception
+    {
+        final Xid xid = new XidImple(new Uid());
+        final TransactionImple tm = TxImporter.importTransaction(xid);
+        final TestSynchronization sync = new TestSynchronization();
+        tm.registerSynchronization(sync);
+        tm.setRollbackOnly();
+        final XATerminator xaTerminator = new XATerminatorImple();
+
+        try {
+            xaTerminator.prepare(xid);
+        } catch(XAException e) {
+            assertEquals(XAException.XA_RBROLLBACK, e.errorCode);
+            // expected - we tried to prepare a rollbackonly tx.
+        }
+        // no need to call rollback - the XA_RBROLLBACK code indicates it's been done.
+        assertFalse(sync.isBeforeCompletionDone());
+        assertTrue(sync.isAfterCompletionDone());
+        assertEquals(javax.transaction.Status.STATUS_ROLLEDBACK, tm.getStatus());
+    }
 }

Added: labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate/TestSynchronization.java
===================================================================
--- labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate/TestSynchronization.java	                        (rev 0)
+++ labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate/TestSynchronization.java	2008-11-13 14:14:55 UTC (rev 23856)
@@ -0,0 +1,52 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA  02110-1301, USA.
+ *
+ * (C) 2008,
+ * @author JBoss Inc.
+ */
+package com.hp.mwtests.ts.jta.subordinate;
+
+import javax.transaction.Synchronization;
+
+/**
+ * Implementation of Synchronization for use in tx test cases.
+ */
+public class TestSynchronization implements Synchronization
+{
+    private boolean beforeCompletionDone = false;
+    private boolean afterCompletionDone = false;
+
+    public boolean isBeforeCompletionDone()
+    {
+        return beforeCompletionDone;
+    }
+
+    public boolean isAfterCompletionDone()
+    {
+        return afterCompletionDone;
+    }
+
+    public void beforeCompletion() {
+        beforeCompletionDone = true;
+        System.out.println("TestSynchronization.beforeCompletion()");
+    }
+
+    public void afterCompletion(int i) {
+        afterCompletionDone = true;
+        System.out.println("TestSynchronization.afterCompletion("+i+")");
+    }
+}
\ No newline at end of file

Added: labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate/TestXAResource.java
===================================================================
--- labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate/TestXAResource.java	                        (rev 0)
+++ labs/jbosstm/trunk/ArjunaJTA/jta/tests/classes/com/hp/mwtests/ts/jta/subordinate/TestXAResource.java	2008-11-13 14:14:55 UTC (rev 23856)
@@ -0,0 +1,119 @@
+/*
+ * JBoss, Home of Professional Open Source
+ * Copyright 2008, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags.
+ * See the copyright.txt in the distribution for a
+ * full listing of individual contributors.
+ * This copyrighted material is made available to anyone wishing to use,
+ * modify, copy, or redistribute it subject to the terms and conditions
+ * of the GNU Lesser General Public License, v. 2.1.
+ * This program is distributed in the hope that it will be useful, but WITHOUT A
+ * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
+ * PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more details.
+ * You should have received a copy of the GNU Lesser General Public License,
+ * v.2.1 along with this distribution; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+ * MA  02110-1301, USA.
+ *
+ * (C) 2008,
+ * @author JBoss Inc.
+ */
+package com.hp.mwtests.ts.jta.subordinate;
+
+import javax.transaction.xa.XAResource;
+import javax.transaction.xa.Xid;
+import javax.transaction.xa.XAException;
+
+/**
+ * Created by IntelliJ IDEA.
+ * User: jhalli
+ * Date: Apr 4, 2008
+ * Time: 3:45:55 PM
+ * To change this template use File | Settings | File Templates.
+ */
+public class TestXAResource implements XAResource
+{
+    private int txTimeout;
+
+    private Xid currentXid;
+
+    private int prepareReturnValue = XAResource.XA_OK;
+
+    public int getPrepareReturnValue()
+    {
+        return prepareReturnValue;
+    }
+
+    public void setPrepareReturnValue(int prepareReturnValue)
+    {
+        this.prepareReturnValue = prepareReturnValue;
+    }
+
+    public void commit(Xid xid, boolean b) throws XAException
+    {
+        System.out.println("XAResourceImpl.commit(Xid="+xid+", b="+b+")");
+        if(!xid.equals(currentXid)) {
+            System.out.println("XAResourceImpl.commit - wrong Xid!");
+        }
+
+        currentXid = null;
+    }
+
+    public void end(Xid xid, int i) throws XAException {
+        System.out.println("XAResourceImpl.end(Xid="+xid+", b="+i+")");
+    }
+
+    public void forget(Xid xid) throws XAException {
+        System.out.println("XAResourceImpl.forget(Xid="+xid+")");
+        if(!xid.equals(currentXid)) {
+            System.out.println("XAResourceImpl.forget - wrong Xid!");
+        }
+        currentXid = null;
+    }
+
+    public int getTransactionTimeout() throws XAException {
+        System.out.println("XAResourceImpl.getTransactionTimeout() [returning "+txTimeout+"]");
+        return txTimeout;
+    }
+
+    public boolean isSameRM(XAResource xaResource) throws XAException {
+        System.out.println("XAResourceImpl.isSameRM(xaResource="+xaResource+")");
+        return false;
+    }
+
+    public int prepare(Xid xid) throws XAException {
+        System.out.println("XAResourceImpl.prepare(Xid="+xid+") returning "+prepareReturnValue);
+        return prepareReturnValue;
+    }
+
+    public Xid[] recover(int i) throws XAException {
+        System.out.println("XAResourceImpl.recover(i="+i+")");
+        return new Xid[0];
+    }
+
+    public void rollback(Xid xid) throws XAException {
+        System.out.println("XAResourceImpl.rollback(Xid="+xid+")");
+        if(!xid.equals(currentXid)) {
+            System.out.println("XAResourceImpl.rollback - wrong Xid!");
+        }
+        currentXid = null;
+    }
+
+    public boolean setTransactionTimeout(int i) throws XAException {
+        System.out.println("XAResourceImpl.setTransactionTimeout(i="+i+")");
+        txTimeout= i;
+        return true;
+    }
+
+    public void start(Xid xid, int i) throws XAException {
+        System.out.println("XAResourceImpl.start(Xid="+xid+", i="+i+")");
+        if(currentXid != null) {
+            System.out.println("XAResourceImpl.start - wrong Xid!");
+        }
+        currentXid = xid;
+    }
+
+    public String toString() {
+        return new String("XAResourceImple("+txTimeout+", "+currentXid+")");
+    }
+}




More information about the jboss-svn-commits mailing list