diff --git a/activemq-core/src/test/java/org/apache/activemq/ActiveMQConnectionFactoryTest.java b/activemq-core/src/test/java/org/apache/activemq/ActiveMQConnectionFactoryTest.java index 4bdba49dba..71d483f2fb 100755 --- a/activemq-core/src/test/java/org/apache/activemq/ActiveMQConnectionFactoryTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/ActiveMQConnectionFactoryTest.java @@ -34,23 +34,34 @@ import org.apache.activemq.broker.TransportConnector; public class ActiveMQConnectionFactoryTest extends CombinationTestSupport { - public void testUseURIToSetUseClientIDPrefixOnConnectionFactory() throws URISyntaxException, JMSException { + private ActiveMQConnection connection; + private BrokerService broker; + + public void testUseURIToSetUseClientIDPrefixOnConnectionFactory() throws URISyntaxException, JMSException { ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?jms.clientIDPrefix=Cheese"); assertEquals("Cheese", cf.getClientIDPrefix()); - ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection(); - try { - connection.start(); + connection = (ActiveMQConnection) cf.createConnection(); + connection.start(); - String clientID = connection.getClientID(); - log.info("Got client ID: " + clientID); + String clientID = connection.getClientID(); + log.info("Got client ID: " + clientID); - assertTrue("should start with Cheese! but was: " + clientID, clientID.startsWith("Cheese")); - } - finally { - connection.close(); - } + assertTrue("should start with Cheese! but was: " + clientID, clientID.startsWith("Cheese")); } + + protected void tearDown() throws Exception { + // Try our best to close any previously opend connection. + try { + connection.close(); + } catch (Throwable ignore) { + } + // Try our best to stop any previously started broker. + try { + broker.stop(); + } catch (Throwable ignore) { + } + } public void testUseURIToSetOptionsOnConnectionFactory() throws URISyntaxException, JMSException { ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?jms.useAsyncSend=true"); @@ -73,26 +84,27 @@ public class ActiveMQConnectionFactoryTest extends CombinationTestSupport { ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); // Make sure the broker is not created until the connection is instantiated. assertNull( BrokerRegistry.getInstance().lookup("localhost") ); - Connection connection = cf.createConnection(); + connection = (ActiveMQConnection) cf.createConnection(); // This should create the connection. assertNotNull(connection); // Verify the broker was created. assertNotNull( BrokerRegistry.getInstance().lookup("localhost") ); + connection.close(); + // Verify the broker was destroyed. assertNull( BrokerRegistry.getInstance().lookup("localhost") ); } public void testGetBrokerName() throws URISyntaxException, JMSException { ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); - ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection(); + connection = (ActiveMQConnection) cf.createConnection(); connection.start(); String brokerName = connection.getBrokerName(); log.info("Got broker name: " + brokerName); assertNotNull("No broker name available!", brokerName); - connection.close(); } public void testCreateTcpConnectionUsingAllocatedPort() throws Exception { @@ -128,7 +140,7 @@ public class ActiveMQConnectionFactoryTest extends CombinationTestSupport { protected void assertCreateConnection(String uri) throws Exception { // Start up a broker with a tcp connector. - BrokerService broker = new BrokerService(); + broker = new BrokerService(); broker.setPersistent(false); TransportConnector connector = broker.addConnector(uri); broker.start(); @@ -147,9 +159,8 @@ public class ActiveMQConnectionFactoryTest extends CombinationTestSupport { // This should create the connection. ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(connectURI); - Connection connection = cf.createConnection(); + connection = (ActiveMQConnection) cf.createConnection(); assertNotNull(connection); - connection.close(); broker.stop(); } diff --git a/activemq-core/src/test/java/org/apache/activemq/broker/BrokerTest.java b/activemq-core/src/test/java/org/apache/activemq/broker/BrokerTest.java index 41a2ac905f..2bf97c2287 100755 --- a/activemq-core/src/test/java/org/apache/activemq/broker/BrokerTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/broker/BrokerTest.java @@ -1309,6 +1309,9 @@ public class BrokerTest extends BrokerTestSupport { connection2.send(sessionInfo2); connection2.send(consumerInfo2); + // Wait a little for messages to get delivered. + Thread.sleep(1000); + for( int i=0; i < 4; i++ ) { Message m1 = receiveMessage(connection1); Message m2 = receiveMessage(connection2); diff --git a/activemq-core/src/test/java/org/apache/activemq/broker/RecoveryBrokerTest.java b/activemq-core/src/test/java/org/apache/activemq/broker/RecoveryBrokerTest.java index 0ada1ff605..717751a8ce 100755 --- a/activemq-core/src/test/java/org/apache/activemq/broker/RecoveryBrokerTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/broker/RecoveryBrokerTest.java @@ -173,7 +173,7 @@ public class RecoveryBrokerTest extends BrokerRestartTestSupport { // The we should get the messages. for( int i=0; i < 4 ; i++ ) { Message m2 = receiveMessage(connection2); - assertNotNull(m2); + assertNotNull("Message "+(i+1)+" is missing.", m2); } assertNoMessagesLeft(connection2); } diff --git a/activemq-core/src/test/java/org/apache/activemq/network/DemandForwardingBridgeTest.java b/activemq-core/src/test/java/org/apache/activemq/network/DemandForwardingBridgeTest.java index ed610fc50a..b8ab947492 100755 --- a/activemq-core/src/test/java/org/apache/activemq/network/DemandForwardingBridgeTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/network/DemandForwardingBridgeTest.java @@ -117,6 +117,14 @@ public class DemandForwardingBridgeTest extends NetworkTestSupport { ConsumerInfo consumerInfo = createConsumerInfo(sessionInfo2, destination); connection2.send(consumerInfo); + // Give demand forwarding bridge a chance to finish forwarding the subscriptions. + try { + Thread.sleep(1000); + } catch (InterruptedException ie) { + ie.printStackTrace(); + } + + // Send the message to the local boker. connection1.send(createMessage(producerInfo, destination, deliveryMode)); @@ -130,14 +138,7 @@ public class DemandForwardingBridgeTest extends NetworkTestSupport { bridge = new DemandForwardingBridge(createTransport(), createRemoteTransport()); bridge.setLocalBrokerName("local"); bridge.setDispatchAsync(false); - bridge.start(); - - // PATCH: Give demand forwarding bridge a chance to finish setting up - try { - Thread.sleep(1000); - } catch (InterruptedException ie) { - ie.printStackTrace(); - } + bridge.start(); } protected void tearDown() throws Exception { diff --git a/activemq-core/src/test/java/org/apache/activemq/network/ForwardingBridgeTest.java b/activemq-core/src/test/java/org/apache/activemq/network/ForwardingBridgeTest.java index 49bcc81088..7ad56e4384 100755 --- a/activemq-core/src/test/java/org/apache/activemq/network/ForwardingBridgeTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/network/ForwardingBridgeTest.java @@ -68,6 +68,13 @@ public class ForwardingBridgeTest extends NetworkTestSupport { ConsumerInfo consumerInfo = createConsumerInfo(sessionInfo2, destination); connection2.send(consumerInfo); Thread.sleep(1000); + // Give forwarding bridge a chance to finish setting up + try { + Thread.sleep(1000); + } catch (InterruptedException ie) { + ie.printStackTrace(); + } + // Send the message to the local boker. connection1.send(createMessage(producerInfo, destination, deliveryMode)); @@ -82,14 +89,7 @@ public class ForwardingBridgeTest extends NetworkTestSupport { bridge = new ForwardingBridge(createTransport(), createRemoteTransport()); bridge.setClientId("local-remote-bridge"); bridge.setDispatchAsync(false); - bridge.start(); - - // PATCH: Give forwarding bridge a chance to finish setting up - try { - Thread.sleep(1000); - } catch (InterruptedException ie) { - ie.printStackTrace(); - } + bridge.start(); } protected void tearDown() throws Exception {