From b29eb384b8e6b24f2b11242f1ddb464413301e5a Mon Sep 17 00:00:00 2001 From: gtully Date: Thu, 9 Apr 2015 16:49:44 +0100 Subject: [PATCH] https://issues.apache.org/jira/browse/AMQ-5710 - rework fix to resolve some unit test regressions, change now confined to rar usecase --- .../apache/activemq/ActiveMQConnection.java | 23 ++++++++--- .../activemq/ConnectionCleanupTest.java | 38 +++++++++++++++++-- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/activemq-client/src/main/java/org/apache/activemq/ActiveMQConnection.java b/activemq-client/src/main/java/org/apache/activemq/ActiveMQConnection.java index d87118dae4..87e4c91150 100755 --- a/activemq-client/src/main/java/org/apache/activemq/ActiveMQConnection.java +++ b/activemq-client/src/main/java/org/apache/activemq/ActiveMQConnection.java @@ -1572,7 +1572,10 @@ public class ActiveMQConnection implements Connection, TopicConnection, QueueCon * connection. */ public void cleanup() throws JMSException { + doCleanup(false); + } + public void doCleanup(boolean removeConnection) throws JMSException { if (advisoryConsumer != null && !isTransportFailed()) { advisoryConsumer.dispose(); advisoryConsumer = null; @@ -1587,13 +1590,21 @@ public class ActiveMQConnection implements Connection, TopicConnection, QueueCon c.dispose(); } - if (userSpecifiedClientID) { - info.setClientId(null); - userSpecifiedClientID = false; + if (removeConnection) { + if (isConnectionInfoSentToBroker) { + if (!transportFailed.get() && !closing.get()) { + syncSendPacket(info.createRemoveCommand()); + } + isConnectionInfoSentToBroker = false; + } + if (userSpecifiedClientID) { + info.setClientId(null); + userSpecifiedClientID = false; + } + clientIDSet = false; } - clientIDSet = false; - stop(); + started.set(false); } /** @@ -1983,7 +1994,7 @@ public class ActiveMQConnection implements Connection, TopicConnection, QueueCon ServiceSupport.dispose(ActiveMQConnection.this.transport); brokerInfoReceived.countDown(); try { - cleanup(); + doCleanup(true); } catch (JMSException e) { LOG.warn("Exception during connection cleanup, " + e, e); } diff --git a/activemq-unit-tests/src/test/java/org/apache/activemq/ConnectionCleanupTest.java b/activemq-unit-tests/src/test/java/org/apache/activemq/ConnectionCleanupTest.java index f7a64acdc8..39be83b685 100755 --- a/activemq-unit-tests/src/test/java/org/apache/activemq/ConnectionCleanupTest.java +++ b/activemq-unit-tests/src/test/java/org/apache/activemq/ConnectionCleanupTest.java @@ -29,7 +29,7 @@ public class ConnectionCleanupTest extends TestCase { private ActiveMQConnection connection; protected void setUp() throws Exception { - ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost"); + ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); connection = (ActiveMQConnection)factory.createConnection(); } @@ -50,18 +50,48 @@ public class ConnectionCleanupTest extends TestCase { try { connection.setClientID("test"); - // fail("Should have received JMSException"); + fail("Should have received JMSException"); } catch (JMSException e) { } - connection.cleanup(); + connection.doCleanup(true); + connection.setClientID("test"); connection.createSession(false, Session.AUTO_ACKNOWLEDGE); try { connection.setClientID("test"); - // fail("Should have received JMSException"); + fail("Should have received JMSException"); + } catch (JMSException e) { + } + + } + + public void testChangeClientIDDenied() throws JMSException { + + connection.setClientID("test"); + connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + try { + connection.setClientID("test"); + fail("Should have received JMSException"); + } catch (JMSException e) { + } + + connection.cleanup(); + + try { + connection.setClientID("test"); + fail("Should have received JMSException"); + } catch (JMSException e) { + } + + connection.createSession(false, Session.AUTO_ACKNOWLEDGE); + + try { + connection.setClientID("test"); + fail("Should have received JMSException"); } catch (JMSException e) { } }