Update JMS and RA Tests to ensure fresh TX usage

Some of the OutgoingConnection and RA tests were failing due to a
previous transaction being present when using the Arjuna TM.  This patch
adds methods to allow tests which TM to use and ensures that Dummy TM is
cleaned up each after each test.
This commit is contained in:
Martyn Taylor 2015-01-15 14:25:33 +00:00
parent 475f2322e2
commit 2e9aad9ef4
5 changed files with 100 additions and 81 deletions

View File

@ -75,6 +75,12 @@ public class ServiceUtils
return transactionManager;
}
public static void setTransactionManager(TransactionManager tm)
{
transactionManager = tm;
transactionManagerLoaded = (transactionManager != null);
}
private static void setActiveMQXAResourceWrapperFactory(Iterable<ActiveMQXAResourceWrapperFactory> iterable)
{
if (iterable.iterator().hasNext())

View File

@ -30,6 +30,7 @@ import org.apache.activemq.ra.ActiveMQRAConnectionManager;
import org.apache.activemq.ra.ActiveMQRAManagedConnectionFactory;
import org.apache.activemq.ra.ActiveMQResourceAdapter;
import org.apache.activemq.spi.core.security.ActiveMQSecurityManagerImpl;
import org.apache.activemq.service.extensions.ServiceUtils;
import org.apache.activemq.tests.integration.jms.bridge.TransactionManagerLocatorImpl;
import org.junit.After;
import org.junit.Before;
@ -51,6 +52,7 @@ public class JMSContextTest extends ActiveMQRATestBase
@Before
public void setUp() throws Exception
{
useDummyTransactionManager();
super.setUp();
ActiveMQSecurityManagerImpl securityManager = (ActiveMQSecurityManagerImpl) server.getSecurityManager();
securityManager.getConfiguration().addUser("testuser", "testpassword");
@ -142,7 +144,7 @@ public class JMSContextTest extends ActiveMQRATestBase
@Test
public void sessionTransactedTestNoActiveJTATx() throws Exception
{
DummyTransactionManager.tm.tx = new DummyTransaction();
((DummyTransactionManager) ServiceUtils.getTransactionManager()).tx = new DummyTransaction();
JMSContext context = qraConnectionFactory.createContext(JMSContext.SESSION_TRANSACTED);
assertEquals(context.getSessionMode(), JMSContext.AUTO_ACKNOWLEDGE);
}
@ -164,7 +166,7 @@ public class JMSContextTest extends ActiveMQRATestBase
@Test
public void clientAckTestNoActiveJTATx() throws Exception
{
DummyTransactionManager.tm.tx = new DummyTransaction();
((DummyTransactionManager) ServiceUtils.getTransactionManager()).tx = new DummyTransaction();
JMSContext context = qraConnectionFactory.createContext(JMSContext.CLIENT_ACKNOWLEDGE);
assertEquals(context.getSessionMode(), JMSContext.AUTO_ACKNOWLEDGE);
}

View File

@ -19,7 +19,6 @@ package org.apache.activemq.tests.integration.ra;
import javax.jms.Connection;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.JMSProducer;
import javax.jms.JMSSecurityException;
import javax.jms.Message;
import javax.jms.MessageConsumer;
@ -39,10 +38,6 @@ import javax.transaction.xa.Xid;
import java.util.HashSet;
import java.util.Set;
import org.apache.activemq.api.core.client.ClientConsumer;
import org.apache.activemq.api.core.client.ClientMessage;
import org.apache.activemq.api.core.client.ClientSession;
import org.apache.activemq.api.core.client.ClientSessionFactory;
import org.apache.activemq.api.jms.ActiveMQJMSClient;
import org.apache.activemq.core.remoting.impl.invm.InVMConnectorFactory;
import org.apache.activemq.core.security.Role;
@ -112,7 +107,6 @@ public class OutgoingConnectionTest extends ActiveMQRATestBase
@After
public void tearDown() throws Exception
{
DummyTransactionManager.tm.tx = null;
if (resourceAdapter != null)
{
resourceAdapter.stop();
@ -122,70 +116,6 @@ public class OutgoingConnectionTest extends ActiveMQRATestBase
super.tearDown();
}
@Test
public void testSimpleMessageSendAndReceive() throws Exception
{
QueueConnection queueConnection = qraConnectionFactory.createQueueConnection();
Session s = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
MessageProducer mp = s.createProducer(q);
MessageConsumer consumer = s.createConsumer(q);
Message message = s.createTextMessage("test");
mp.send(message);
queueConnection.start();
TextMessage textMessage = (TextMessage) consumer.receive(1000);
assertNotNull(textMessage);
assertEquals(textMessage.getText(), "test");
}
@Test
public void testSimpleSendNoXAJMSContext() throws Exception
{
Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
try (ClientSessionFactory sf = locator.createSessionFactory();
ClientSession session = sf.createSession();
ClientConsumer consVerify = session.createConsumer("jms.queue." + MDBQUEUE);
JMSContext jmsctx = qraConnectionFactory.createContext();
)
{
session.start();
// These next 4 lines could be written in a single line however it makes difficult for debugging
JMSProducer producer = jmsctx.createProducer();
producer.setProperty("strvalue", "hello");
TextMessage msgsend = jmsctx.createTextMessage("hello");
producer.send(q, msgsend);
ClientMessage msg = consVerify.receive(1000);
assertNotNull(msg);
assertEquals("hello", msg.getStringProperty("strvalue"));
}
}
@Test
public void testSimpleSendNoXAJMS1() throws Exception
{
Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
try (ClientSessionFactory sf = locator.createSessionFactory();
ClientSession session = sf.createSession();
ClientConsumer consVerify = session.createConsumer("jms.queue." + MDBQUEUE);
Connection conn = qraConnectionFactory.createConnection();
)
{
Session jmsSess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
session.start();
MessageProducer producer = jmsSess.createProducer(q);
// These next 4 lines could be written in a single line however it makes difficult for debugging
TextMessage msgsend = jmsSess.createTextMessage("hello");
msgsend.setStringProperty("strvalue", "hello");
producer.send(msgsend);
ClientMessage msg = consVerify.receive(1000);
assertNotNull(msg);
assertEquals("hello", msg.getStringProperty("strvalue"));
}
}
@Test
public void testSimpleMessageSendAndReceiveXA() throws Exception
{

View File

@ -17,7 +17,10 @@
package org.apache.activemq.tests.integration.ra;
import javax.jms.Connection;
import javax.jms.JMSContext;
import javax.jms.JMSException;
import javax.jms.JMSProducer;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.MessageProducer;
@ -29,6 +32,10 @@ import javax.jms.TextMessage;
import java.util.HashSet;
import java.util.Set;
import org.apache.activemq.api.core.client.ClientConsumer;
import org.apache.activemq.api.core.client.ClientMessage;
import org.apache.activemq.api.core.client.ClientSession;
import org.apache.activemq.api.core.client.ClientSessionFactory;
import org.apache.activemq.api.jms.ActiveMQJMSClient;
import org.apache.activemq.core.remoting.impl.invm.InVMConnectorFactory;
import org.apache.activemq.core.security.Role;
@ -38,7 +45,7 @@ import org.apache.activemq.ra.ActiveMQRAConnectionManager;
import org.apache.activemq.ra.ActiveMQRAManagedConnectionFactory;
import org.apache.activemq.ra.ActiveMQResourceAdapter;
import org.apache.activemq.spi.core.security.ActiveMQSecurityManagerImpl;
import org.apache.activemq.tests.integration.jms.bridge.TransactionManagerLocatorImpl;
import org.apache.activemq.service.extensions.ServiceUtils;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
@ -54,12 +61,6 @@ public class OutgoingConnectionTestJTA extends ActiveMQRATestBase
protected ActiveMQRAManagedConnectionFactory mcf;
ActiveMQRAConnectionManager qraConnectionManager = new ActiveMQRAConnectionManager();
static
{
DummyTransactionManager dummyTransactionManager = new DummyTransactionManager();
TransactionManagerLocatorImpl.tm = dummyTransactionManager;
}
@Override
public boolean useSecurity()
{
@ -70,6 +71,7 @@ public class OutgoingConnectionTestJTA extends ActiveMQRATestBase
@Before
public void setUp() throws Exception
{
useDummyTransactionManager();
super.setUp();
((ActiveMQSecurityManagerImpl)server.getSecurityManager()).getConfiguration().addUser("testuser", "testpassword");
((ActiveMQSecurityManagerImpl)server.getSecurityManager()).getConfiguration().addUser("guest", "guest");
@ -96,7 +98,7 @@ public class OutgoingConnectionTestJTA extends ActiveMQRATestBase
@After
public void tearDown() throws Exception
{
((DummyTransactionManager) TransactionManagerLocatorImpl.tm).tx = null;
((DummyTransactionManager) ServiceUtils.getTransactionManager()).tx = null;
if (resourceAdapter != null)
{
resourceAdapter.stop();
@ -211,6 +213,30 @@ public class OutgoingConnectionTestJTA extends ActiveMQRATestBase
}
@Test
public void testSimpleSendNoXAJMSContext() throws Exception
{
Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
try (ClientSessionFactory sf = locator.createSessionFactory();
ClientSession session = sf.createSession();
ClientConsumer consVerify = session.createConsumer("jms.queue." + MDBQUEUE);
JMSContext jmsctx = qraConnectionFactory.createContext();
)
{
session.start();
// These next 4 lines could be written in a single line however it makes difficult for debugging
JMSProducer producer = jmsctx.createProducer();
producer.setProperty("strvalue", "hello");
TextMessage msgsend = jmsctx.createTextMessage("hello");
producer.send(q, msgsend);
ClientMessage msg = consVerify.receive(1000);
assertNotNull(msg);
assertEquals("hello", msg.getStringProperty("strvalue"));
}
}
@Test
public void testQueueSessionAckModeJTA() throws Exception
{
@ -223,8 +249,47 @@ public class OutgoingConnectionTestJTA extends ActiveMQRATestBase
testQueuSessionAckMode(false);
}
@Test
public void testSimpleMessageSendAndReceive() throws Exception
{
QueueConnection queueConnection = qraConnectionFactory.createQueueConnection();
Session s = queueConnection.createSession(false, Session.AUTO_ACKNOWLEDGE);
Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
MessageProducer mp = s.createProducer(q);
MessageConsumer consumer = s.createConsumer(q);
Message message = s.createTextMessage("test");
mp.send(message);
queueConnection.start();
TextMessage textMessage = (TextMessage) consumer.receive(1000);
assertNotNull(textMessage);
assertEquals(textMessage.getText(), "test");
}
@Test
public void testSimpleSendNoXAJMS1() throws Exception
{
Queue q = ActiveMQJMSClient.createQueue(MDBQUEUE);
try (ClientSessionFactory sf = locator.createSessionFactory();
ClientSession session = sf.createSession();
ClientConsumer consVerify = session.createConsumer("jms.queue." + MDBQUEUE);
Connection conn = qraConnectionFactory.createConnection();
)
{
Session jmsSess = conn.createSession(false, Session.AUTO_ACKNOWLEDGE);
session.start();
MessageProducer producer = jmsSess.createProducer(q);
// These next 4 lines could be written in a single line however it makes difficult for debugging
TextMessage msgsend = jmsSess.createTextMessage("hello");
msgsend.setStringProperty("strvalue", "hello");
producer.send(msgsend);
ClientMessage msg = consVerify.receive(1000);
assertNotNull(msg);
assertEquals("hello", msg.getStringProperty("strvalue"));
}
}
private void setDummyTX()
{
((DummyTransactionManager) TransactionManagerLocatorImpl.tm).tx = new DummyTransaction();
((DummyTransactionManager) ServiceUtils.getTransactionManager()).tx = new DummyTransaction();
}
}

View File

@ -34,6 +34,7 @@ import java.util.List;
import java.util.Random;
import java.util.Set;
import com.arjuna.ats.internal.jta.transaction.arjunacore.TransactionManagerImple;
import org.apache.activemq.api.core.TransportConfiguration;
import org.apache.activemq.api.core.management.QueueControl;
import org.apache.activemq.api.jms.management.JMSQueueControl;
@ -44,6 +45,8 @@ import org.apache.activemq.core.server.ActiveMQServers;
import org.apache.activemq.jms.server.config.ConnectionFactoryConfiguration;
import org.apache.activemq.jms.server.config.impl.ConnectionFactoryConfigurationImpl;
import org.apache.activemq.jms.server.impl.JMSServerManagerImpl;
import org.apache.activemq.service.extensions.ServiceUtils;
import org.apache.activemq.tests.integration.ra.DummyTransactionManager;
import org.apache.activemq.tests.unit.util.InVMNamingContext;
import org.junit.After;
import org.junit.Assert;
@ -150,6 +153,9 @@ public class JMSTestBase extends ServiceTestBase
{
super.setUp();
// Load Arjuna TM if one is not already set.
if (ServiceUtils.getTransactionManager() == null) useRealTransactionManager();
mbeanServer = MBeanServerFactory.createMBeanServer();
Configuration conf = createDefaultConfig(true)
@ -295,6 +301,16 @@ public class JMSTestBase extends ServiceTestBase
}
}
protected void useRealTransactionManager()
{
ServiceUtils.setTransactionManager((javax.transaction.TransactionManager) new TransactionManagerImple());
}
protected void useDummyTransactionManager()
{
ServiceUtils.setTransactionManager(new DummyTransactionManager());
}
protected final void receiveMessages(JMSConsumer consumer, final int start, final int msgCount, final boolean ack)
{