From 2e9aad9ef4c08854b7b68804ac3cdcd7637a2ca6 Mon Sep 17 00:00:00 2001 From: Martyn Taylor Date: Thu, 15 Jan 2015 14:25:33 +0000 Subject: [PATCH] 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. --- .../service/extensions/ServiceUtils.java | 6 ++ .../tests/integration/ra/JMSContextTest.java | 6 +- .../ra/OutgoingConnectionTest.java | 70 ---------------- .../ra/OutgoingConnectionTestJTA.java | 83 +++++++++++++++++-- .../activemq/tests/util/JMSTestBase.java | 16 ++++ 5 files changed, 100 insertions(+), 81 deletions(-) diff --git a/activemq-service-extensions/src/main/java/org/apache/activemq/service/extensions/ServiceUtils.java b/activemq-service-extensions/src/main/java/org/apache/activemq/service/extensions/ServiceUtils.java index 635163aa0b..79b7aaa105 100644 --- a/activemq-service-extensions/src/main/java/org/apache/activemq/service/extensions/ServiceUtils.java +++ b/activemq-service-extensions/src/main/java/org/apache/activemq/service/extensions/ServiceUtils.java @@ -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 iterable) { if (iterable.iterator().hasNext()) diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/tests/integration/ra/JMSContextTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/tests/integration/ra/JMSContextTest.java index 86d4ed0d32..e4ef1bd249 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/tests/integration/ra/JMSContextTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/tests/integration/ra/JMSContextTest.java @@ -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); } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/tests/integration/ra/OutgoingConnectionTest.java b/tests/integration-tests/src/test/java/org/apache/activemq/tests/integration/ra/OutgoingConnectionTest.java index 8fb25bd93c..bd0ec20b0f 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/tests/integration/ra/OutgoingConnectionTest.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/tests/integration/ra/OutgoingConnectionTest.java @@ -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 { diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/tests/integration/ra/OutgoingConnectionTestJTA.java b/tests/integration-tests/src/test/java/org/apache/activemq/tests/integration/ra/OutgoingConnectionTestJTA.java index 2d11968c8c..51c911b168 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/tests/integration/ra/OutgoingConnectionTestJTA.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/tests/integration/ra/OutgoingConnectionTestJTA.java @@ -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(); } } diff --git a/tests/integration-tests/src/test/java/org/apache/activemq/tests/util/JMSTestBase.java b/tests/integration-tests/src/test/java/org/apache/activemq/tests/util/JMSTestBase.java index b568b9aebd..4ad86c4bcc 100644 --- a/tests/integration-tests/src/test/java/org/apache/activemq/tests/util/JMSTestBase.java +++ b/tests/integration-tests/src/test/java/org/apache/activemq/tests/util/JMSTestBase.java @@ -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) {