Created
January 18, 2011 16:02
-
-
Save maeste/784638 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| * 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 file in the | |
| * distribution for a full listing of individual contributors. | |
| * | |
| * This is free software; you can redistribute it and/or modify it | |
| * under the terms of the GNU Lesser General Public License as | |
| * published by the Free Software Foundation; either version 2.1 of | |
| * the License, or (at your option) any later version. | |
| * | |
| * This software is distributed in the hope that it will be useful, | |
| * but WITHOUT ANY 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 along with this software; if not, write to the Free | |
| * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
| * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
| */ | |
| package org.jboss.jca.core.connectionmanager.listener; | |
| import org.jboss.jca.core.api.connectionmanager.pool.PoolConfiguration; | |
| import org.jboss.jca.core.connectionmanager.ConnectionManagerFactory; | |
| import org.jboss.jca.core.connectionmanager.TxConnectionManager; | |
| import org.jboss.jca.core.connectionmanager.common.MockManagedConnectionFactory; | |
| import org.jboss.jca.core.connectionmanager.listener.TxConnectionListener.TransactionSynchronization; | |
| import org.jboss.jca.core.connectionmanager.pool.api.Pool; | |
| import org.jboss.jca.core.connectionmanager.pool.api.PoolFactory; | |
| import org.jboss.jca.core.connectionmanager.pool.api.PoolStrategy; | |
| import org.jboss.jca.core.connectionmanager.tx.TxConnectionManagerTestCase; | |
| import org.jboss.jca.embedded.Embedded; | |
| import org.jboss.jca.embedded.EmbeddedFactory; | |
| import java.net.URL; | |
| import javax.resource.ResourceException; | |
| import javax.resource.spi.ConnectionManager; | |
| import javax.resource.spi.ManagedConnectionFactory; | |
| import javax.resource.spi.TransactionSupport.TransactionSupportLevel; | |
| import javax.transaction.Status; | |
| import javax.transaction.Synchronization; | |
| import javax.transaction.SystemException; | |
| import javax.transaction.Transaction; | |
| import javax.transaction.TransactionManager; | |
| import javax.transaction.xa.XAResource; | |
| import org.junit.AfterClass; | |
| import org.junit.BeforeClass; | |
| import org.junit.Test; | |
| import static org.hamcrest.core.Is.is; | |
| import static org.hamcrest.core.IsNot.not; | |
| import static org.junit.Assert.assertThat; | |
| import static org.mockito.Matchers.anyInt; | |
| import static org.mockito.Matchers.anyObject; | |
| import static org.mockito.Mockito.doThrow; | |
| import static org.mockito.Mockito.mock; | |
| import static org.mockito.Mockito.verifyZeroInteractions; | |
| import static org.mockito.Mockito.when; | |
| /** | |
| * | |
| * A TxConnectionListenerTestCase. | |
| * | |
| * @author <a href="[email protected]">Stefano Maestri</a> | |
| * | |
| */ | |
| public class TxConnectionListenerTestCase | |
| { | |
| /**Embedded JCA*/ | |
| private static Embedded embedded = null; | |
| private static TxConnectionManager txConnectionManager = null; | |
| private static ManagedConnectionFactory mcf = null; | |
| /** | |
| * | |
| * delistShouldSetTransactionSynchronizationToNullIfEverythingCommitted | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test | |
| public void delistShouldSetTransactionSynchronizationToNullIfEverythingCommitted() throws Exception | |
| { | |
| //given | |
| TxConnectionListener listener = new TxConnectionListener(txConnectionManager, null, null, null, null); | |
| listener.setTrackByTx(false); | |
| Transaction tx = mock(Transaction.class); | |
| when(tx.getStatus()).thenReturn(Status.STATUS_COMMITTED); | |
| TransactionSynchronization transactionSynchronization = listener.new TransactionSynchronization(tx, false); | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| //when | |
| listener.delist(); | |
| //then | |
| assertThat(listener.getTransactionSynchronization(), is((TransactionSynchronization) null)); | |
| } | |
| /** | |
| * | |
| * delistShouldNotSetTransactionSynchronizationToNullIfTrackByTx | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test | |
| public void delistShouldNotSetTransactionSynchronizationToNullIfTrackByTx() throws Exception | |
| { | |
| //given | |
| TxConnectionListener listener = new TxConnectionListener(txConnectionManager, null, null, null, null); | |
| listener.setTrackByTx(true); | |
| Transaction tx = mock(Transaction.class); | |
| when(tx.getStatus()).thenReturn(Status.STATUS_COMMITTED); | |
| TransactionSynchronization transactionSynchronization = listener.new TransactionSynchronization(tx, false); | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| //when | |
| listener.delist(); | |
| //then | |
| assertThat(listener.getTransactionSynchronization(), not(is((TransactionSynchronization) null))); | |
| } | |
| /** | |
| * | |
| * enlistShouldThrowSystemExceptionIfEnlistResourceFail | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test | |
| public void deListShouldCompleteCorrectlyAfterARightEnlist() throws Exception | |
| { | |
| //given | |
| TxConnectionManager cm = mock(TxConnectionManager.class); | |
| TransactionManager tm = mock(TransactionManager.class); | |
| com.arjuna.ats.jta.transaction.Transaction threadTx = mock(com.arjuna.ats.jta.transaction.Transaction.class); | |
| when(threadTx.isAlive()).thenReturn(true); | |
| when(threadTx.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(threadTx.enlistResource((XAResource) anyObject())).thenReturn(true); | |
| when(threadTx.delistResource((XAResource) anyObject(), anyInt())).thenReturn(true); | |
| when(tm.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(tm.getTransaction()).thenReturn(threadTx); | |
| when(cm.getTransactionManager()).thenReturn(tm); | |
| TxConnectionListener listener = new TxConnectionListener(cm, null, null, null, null); | |
| listener.setTrackByTx(false); | |
| TransactionSynchronization transactionSynchronization = null; | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| listener.enlist(); | |
| //when | |
| listener.delist(); | |
| //then complete without exception | |
| } | |
| /** | |
| * | |
| * enlistShouldThrowSystemExceptionIfEnlistResourceFail | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test(expected = ResourceException.class) | |
| public void deListShouldThrowResourceExceptionIfDelistResourceFailOnTx() throws Exception | |
| { | |
| //given | |
| TxConnectionManager cm = mock(TxConnectionManager.class); | |
| TransactionManager tm = mock(TransactionManager.class); | |
| com.arjuna.ats.jta.transaction.Transaction threadTx = mock(com.arjuna.ats.jta.transaction.Transaction.class); | |
| when(threadTx.isAlive()).thenReturn(true); | |
| when(threadTx.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(threadTx.enlistResource((XAResource) anyObject())).thenReturn(true); | |
| /*******this is the important GIVEN *******/ | |
| when(threadTx.delistResource((XAResource) anyObject(), anyInt())).thenReturn(false); | |
| when(tm.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(tm.getTransaction()).thenReturn(threadTx); | |
| when(cm.getTransactionManager()).thenReturn(tm); | |
| TxConnectionListener listener = new TxConnectionListener(cm, null, null, null, null); | |
| listener.setTrackByTx(false); | |
| TransactionSynchronization transactionSynchronization = null; | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| listener.enlist(); | |
| //when | |
| listener.delist(); | |
| //then complete without exception | |
| } | |
| /** | |
| * | |
| * enlistShouldThrowIllegalStateExceptionWithCurrentTxAndStatusNoTransaction | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test(expected = IllegalStateException.class) | |
| public void enlistShouldThrowIllegalStateExceptionWithCurrentTxAndStatusNoTransaction() throws Exception | |
| { | |
| //given | |
| TxConnectionManager cm = mock(TxConnectionManager.class); | |
| TransactionManager tm = mock(TransactionManager.class); | |
| when(tm.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION); | |
| when(cm.getTransactionManager()).thenReturn(tm); | |
| TxConnectionListener listener = new TxConnectionListener(cm, null, null, null, null); | |
| listener.setTrackByTx(true); | |
| Transaction tx = mock(Transaction.class); | |
| TransactionSynchronization transactionSynchronization = listener.new TransactionSynchronization(tx, false); | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| //when | |
| listener.enlist(); | |
| //then iLLEGALsTATEeXCEPTION | |
| } | |
| /** | |
| * | |
| * enlistShouldDoNothingForNoStransaction | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test | |
| public void enlistShouldDoNothingForNoStransaction() throws Exception | |
| { | |
| //given | |
| TxConnectionManager cm = mock(TxConnectionManager.class); | |
| TransactionManager tm = mock(TransactionManager.class); | |
| when(tm.getStatus()).thenReturn(Status.STATUS_NO_TRANSACTION); | |
| when(cm.getTransactionManager()).thenReturn(tm); | |
| TxConnectionListener listener = new TxConnectionListener(cm, null, null, null, null); | |
| listener.setTrackByTx(true); | |
| Transaction tx = mock(Transaction.class); | |
| TransactionSynchronization transactionSynchronization = mock(TxConnectionListener.TransactionSynchronization.class); | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| //when | |
| listener.enlist(); | |
| //then | |
| verifyZeroInteractions(transactionSynchronization); | |
| } | |
| /** | |
| * | |
| * enlistShouldThrowIllegalStateExceptionWithCurrentTxAndStatusNoTransaction | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test(expected = IllegalStateException.class) | |
| public void enlistShouldThrowIllegalStateExceptionWithNoActiveTransaction() throws Exception | |
| { | |
| //given | |
| TxConnectionManager cm = mock(TxConnectionManager.class); | |
| TransactionManager tm = mock(TransactionManager.class); | |
| when(tm.getStatus()).thenReturn(Status.STATUS_COMMITTING); | |
| when(cm.getTransactionManager()).thenReturn(tm); | |
| TxConnectionListener listener = new TxConnectionListener(cm, null, null, null, null); | |
| listener.setTrackByTx(true); | |
| Transaction tx = mock(Transaction.class); | |
| TransactionSynchronization transactionSynchronization = listener.new TransactionSynchronization(tx, false); | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| //when | |
| listener.enlist(); | |
| //then iLLEGALsTATEeXCEPTION | |
| } | |
| /** | |
| * | |
| * enlistShouldThrowIllegalStateExceptionWithCurrentTxAndStatusNoTransaction | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test(expected = IllegalStateException.class) | |
| public void enlistShouldThrowIllegalStateExceptionTryingToChangeTransaction() throws Exception | |
| { | |
| //given | |
| TxConnectionManager cm = mock(TxConnectionManager.class); | |
| TransactionManager tm = mock(TransactionManager.class); | |
| com.arjuna.ats.jta.transaction.Transaction threadTx = mock(com.arjuna.ats.jta.transaction.Transaction.class); | |
| when(threadTx.isAlive()).thenReturn(true); | |
| when(threadTx.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(tm.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(tm.getTransaction()).thenReturn(threadTx); | |
| when(cm.getTransactionManager()).thenReturn(tm); | |
| TxConnectionListener listener = new TxConnectionListener(cm, null, null, null, null); | |
| listener.setTrackByTx(true); | |
| Transaction tx = mock(Transaction.class); | |
| TransactionSynchronization transactionSynchronization = listener.new TransactionSynchronization(tx, false); | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| //when | |
| listener.enlist(); | |
| //then iLLEGALsTATEeXCEPTION | |
| } | |
| /** | |
| * | |
| * enlistShouldThrowIllegalStateExceptionForCantEnlistAlreadyTx | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test(expected = IllegalStateException.class) | |
| public void enlistShouldThrowIllegalStateExceptionForCantEnlistAlreadyTx() throws Exception | |
| { | |
| //given | |
| TxConnectionManager cm = mock(TxConnectionManager.class); | |
| TransactionManager tm = mock(TransactionManager.class); | |
| com.arjuna.ats.jta.transaction.Transaction threadTx = mock(com.arjuna.ats.jta.transaction.Transaction.class); | |
| when(threadTx.isAlive()).thenReturn(true); | |
| when(threadTx.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(tm.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(tm.getTransaction()).thenReturn(threadTx); | |
| when(cm.getTransactionManager()).thenReturn(tm); | |
| TxConnectionListener listener = new TxConnectionListener(cm, null, null, null, null); | |
| listener.setTrackByTx(false); | |
| TransactionSynchronization transactionSynchronization = listener.new TransactionSynchronization(threadTx, false); | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| //when | |
| listener.enlist(); | |
| //then iLLEGALsTATEeXCEPTION | |
| } | |
| /** | |
| * | |
| * enlistShouldThrowIllegalStateExceptionForCantEnlistAlreadyTx | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test(expected = SystemException.class) | |
| public void enlistShouldThrowIllegalStateExceptionForCantEnlistAlreadyTx2() throws Exception | |
| { | |
| //given | |
| TxConnectionManager cm = mock(TxConnectionManager.class); | |
| TransactionManager tm = mock(TransactionManager.class); | |
| com.arjuna.ats.jta.transaction.Transaction threadTx = mock(com.arjuna.ats.jta.transaction.Transaction.class); | |
| when(threadTx.isAlive()).thenReturn(true); | |
| when(threadTx.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| doThrow(new SystemException("ThreadTx Exception")).when(threadTx).registerSynchronization( | |
| (Synchronization) anyObject()); | |
| when(tm.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(tm.getTransaction()).thenReturn(threadTx); | |
| when(cm.getTransactionManager()).thenReturn(tm); | |
| TxConnectionListener listener = new TxConnectionListener(cm, null, null, null, null); | |
| listener.setTrackByTx(true); | |
| TransactionSynchronization transactionSynchronization = listener.new TransactionSynchronization(threadTx, false); | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| //when | |
| listener.enlist(); | |
| //then iLLEGALsTATEeXCEPTION | |
| } | |
| /** | |
| * | |
| * enlistShouldThrowIllegalStateExceptionWithCurrentTxAndStatusNoTransaction | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test(expected = IllegalStateException.class) | |
| public void enlistShouldThrowIllegalStateExceptionForNotEnlistedResources() throws Exception | |
| { | |
| //given | |
| TxConnectionManager cm = mock(TxConnectionManager.class); | |
| TransactionManager tm = mock(TransactionManager.class); | |
| com.arjuna.ats.jta.transaction.Transaction threadTx = mock(com.arjuna.ats.jta.transaction.Transaction.class); | |
| when(threadTx.isAlive()).thenReturn(true); | |
| when(threadTx.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(tm.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(tm.getTransaction()).thenReturn(threadTx); | |
| when(cm.getTransactionManager()).thenReturn(tm); | |
| TxConnectionListener listener = new TxConnectionListener(cm, null, null, null, null); | |
| listener.setTrackByTx(true); | |
| TransactionSynchronization transactionSynchronization = listener.new TransactionSynchronization(threadTx, false); | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| //when | |
| listener.enlist(); | |
| //then iLLEGALsTATEeXCEPTION | |
| } | |
| /** | |
| * | |
| * enlistShouldThrowSystemExceptionIfEnlistResourceFail | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test(expected = SystemException.class) | |
| public void enlistShouldThrowSystemExceptionIfEnlistResourceFail() throws Exception | |
| { | |
| //given | |
| TxConnectionManager cm = mock(TxConnectionManager.class); | |
| TransactionManager tm = mock(TransactionManager.class); | |
| com.arjuna.ats.jta.transaction.Transaction threadTx = mock(com.arjuna.ats.jta.transaction.Transaction.class); | |
| when(threadTx.isAlive()).thenReturn(true); | |
| when(threadTx.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| /******* THIS IS THE IMPORTANT GIVEN ****/ | |
| when(threadTx.enlistResource((XAResource) anyObject())).thenReturn(false); | |
| when(tm.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(tm.getTransaction()).thenReturn(threadTx); | |
| when(cm.getTransactionManager()).thenReturn(tm); | |
| TxConnectionListener listener = new TxConnectionListener(cm, null, null, null, null); | |
| listener.setTrackByTx(true); | |
| TransactionSynchronization transactionSynchronization = null; | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| //when | |
| listener.enlist(); | |
| //then SystemException | |
| } | |
| /** | |
| * | |
| * enlistShouldThrowSystemExceptionIfEnlistResourceFail | |
| * | |
| * @throws Exception in case of error | |
| */ | |
| @Test | |
| public void enlistShouldCompleteWithoutExceptions() throws Exception | |
| { | |
| //given | |
| TxConnectionManager cm = mock(TxConnectionManager.class); | |
| TransactionManager tm = mock(TransactionManager.class); | |
| com.arjuna.ats.jta.transaction.Transaction threadTx = mock(com.arjuna.ats.jta.transaction.Transaction.class); | |
| when(threadTx.isAlive()).thenReturn(true); | |
| when(threadTx.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(threadTx.enlistResource((XAResource) anyObject())).thenReturn(true); | |
| when(tm.getStatus()).thenReturn(Status.STATUS_ACTIVE); | |
| when(tm.getTransaction()).thenReturn(threadTx); | |
| when(cm.getTransactionManager()).thenReturn(tm); | |
| TxConnectionListener listener = new TxConnectionListener(cm, null, null, null, null); | |
| listener.setTrackByTx(true); | |
| TransactionSynchronization transactionSynchronization = null; | |
| listener.setTransactionSynchronization(transactionSynchronization); | |
| //when | |
| listener.enlist(); | |
| //then complete without exception | |
| } | |
| // @Test | |
| // public void testEnlist() | |
| // { | |
| // fail("Not yet implemented"); | |
| // } | |
| // @Test | |
| // public void testIsManagedConnectionFree() | |
| // { | |
| // fail("Not yet implemented"); | |
| // } | |
| // | |
| // @Test | |
| // public void testTidyup() | |
| // { | |
| // fail("Not yet implemented"); | |
| // } | |
| // | |
| // @Test | |
| // public void testConnectionClosed() | |
| // { | |
| // fail("Not yet implemented"); | |
| // } | |
| // | |
| // @Test | |
| // public void testConnectionErrorOccurred() | |
| // { | |
| // fail("Not yet implemented"); | |
| // } | |
| // | |
| // @Test | |
| // public void testLocalTransactionCommitted() | |
| // { | |
| // fail("Not yet implemented"); | |
| // } | |
| // | |
| // @Test | |
| // public void testLocalTransactionRolledback() | |
| // { | |
| // fail("Not yet implemented"); | |
| // } | |
| // | |
| // @Test | |
| // public void testLocalTransactionStarted() | |
| // { | |
| // fail("Not yet implemented"); | |
| // } | |
| // | |
| // @Test | |
| // public void testToStringStringBuffer() | |
| // { | |
| // fail("Not yet implemented"); | |
| // } | |
| // | |
| // @Test | |
| // public void testTxConnectionListener() | |
| // { | |
| // fail("Not yet implemented"); | |
| // } | |
| // | |
| // @Test | |
| // public void testGetXAResource() | |
| // { | |
| // fail("Not yet implemented"); | |
| // } | |
| // | |
| // @Test | |
| // public void testWasFreed() | |
| // { | |
| // fail("Not yet implemented"); | |
| // } | |
| /** | |
| * Lifecycle start, before the suite is executed | |
| * @throws Throwable throwable exception | |
| */ | |
| @BeforeClass | |
| public static void beforeClass() throws Throwable | |
| { | |
| // Create and set an embedded JCA instance | |
| embedded = EmbeddedFactory.create(false); | |
| // Startup | |
| embedded.startup(); | |
| // Deploy Naming and Transaction | |
| URL naming = TxConnectionManagerTestCase.class.getClassLoader().getResource("naming.xml"); | |
| URL transaction = TxConnectionManagerTestCase.class.getClassLoader().getResource("transaction.xml"); | |
| embedded.deploy(naming); | |
| embedded.deploy(transaction); | |
| TransactionManager tm = embedded.lookup("RealTransactionManager", TransactionManager.class); | |
| mcf = new MockManagedConnectionFactory(); | |
| PoolConfiguration pc = new PoolConfiguration(); | |
| PoolFactory pf = new PoolFactory(); | |
| Pool pool = pf.create(PoolStrategy.ONE_POOL, mcf, pc, true); | |
| ConnectionManagerFactory cmf = new ConnectionManagerFactory(); | |
| ConnectionManager connectionManager = cmf.createTransactional(TransactionSupportLevel.LocalTransaction, pool, | |
| null, null, tm, null, null, null, null, null); | |
| txConnectionManager = (TxConnectionManager) connectionManager; | |
| } | |
| /** | |
| * Lifecycle stop, after the suite is executed | |
| * @throws Throwable throwable exception | |
| */ | |
| @AfterClass | |
| public static void afterClass() throws Throwable | |
| { | |
| // Undeploy Transaction and Naming | |
| URL naming = TxConnectionManagerTestCase.class.getClassLoader().getResource("naming.xml"); | |
| URL transaction = TxConnectionManagerTestCase.class.getClassLoader().getResource("transaction.xml"); | |
| embedded.undeploy(transaction); | |
| embedded.undeploy(naming); | |
| // Shutdown embedded | |
| embedded.shutdown(); | |
| // Set embedded to null | |
| embedded = null; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment