From 60d7cdc1dd0ee77c52e0335cb1b30152980f758d Mon Sep 17 00:00:00 2001 From: "Timothy A. Bish" Date: Thu, 3 Jan 2013 15:57:05 +0000 Subject: [PATCH] fix test case after changes in https://issues.apache.org/jira/browse/AMQ-4237 broke the test's MBean lookup git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1428420 13f79535-47bb-0310-9956-ffa450edef68 --- .../activemq/RemoveDestinationTest.java | 108 +++++++++--------- 1 file changed, 54 insertions(+), 54 deletions(-) diff --git a/activemq-core/src/test/java/org/apache/activemq/RemoveDestinationTest.java b/activemq-core/src/test/java/org/apache/activemq/RemoveDestinationTest.java index 2c0f688890..009221b53f 100644 --- a/activemq-core/src/test/java/org/apache/activemq/RemoveDestinationTest.java +++ b/activemq-core/src/test/java/org/apache/activemq/RemoveDestinationTest.java @@ -16,6 +16,11 @@ */ package org.apache.activemq; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; + import java.net.URI; import javax.jms.Connection; @@ -24,40 +29,39 @@ import javax.jms.JMSException; import javax.jms.MessageConsumer; import javax.jms.MessageProducer; import javax.jms.Session; -import javax.jms.Topic; import javax.jms.TextMessage; +import javax.jms.Topic; import javax.management.ObjectName; import org.apache.activemq.advisory.DestinationSource; import org.apache.activemq.broker.BrokerFactory; import org.apache.activemq.broker.BrokerService; +import org.apache.activemq.broker.jmx.DestinationViewMBean; import org.apache.activemq.command.ActiveMQDestination; import org.apache.activemq.command.ActiveMQTopic; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; -import junit.framework.TestCase; - -public class RemoveDestinationTest extends TestCase { +public class RemoveDestinationTest { private static final String VM_BROKER_URL = "vm://localhost?create=false"; private static final String BROKER_URL = "broker:vm://localhost?broker.persistent=false&broker.useJmx=true"; - - BrokerService broker; - - public RemoveDestinationTest(String name) { - super(name); - } - protected void setUp() throws Exception { - super.setUp(); - + BrokerService broker; + + @Before + public void setUp() throws Exception { broker = BrokerFactory.createBroker(new URI(BROKER_URL)); broker.start(); - + broker.waitUntilStarted(); } - protected void tearDown() throws Exception { - super.tearDown(); + @After + public void tearDown() throws Exception { broker.stop(); + broker.waitUntilStopped(); + broker = null; } private Connection createConnection(final boolean start) throws JMSException { @@ -69,6 +73,7 @@ public class RemoveDestinationTest extends TestCase { return conn; } + @Test public void testRemoveDestinationWithoutSubscriber() throws Exception { ActiveMQConnection amqConnection = (ActiveMQConnection) createConnection(true); @@ -80,29 +85,27 @@ public class RemoveDestinationTest extends TestCase { TextMessage msg = session.createTextMessage("Hellow World"); producer.send(msg); - assertNotNull( consumer.receive( 5000 ) ); - Thread.sleep( 1000 ); + assertNotNull(consumer.receive(5000)); + Thread.sleep(1000); - ActiveMQTopic amqTopic = (ActiveMQTopic)topic; - assertTrue( destinationSource.getTopics().contains(amqTopic) ); + ActiveMQTopic amqTopic = (ActiveMQTopic) topic; + assertTrue(destinationSource.getTopics().contains(amqTopic)); consumer.close(); producer.close(); session.close(); - Thread.sleep( 3000 ); - - amqConnection.destroyDestination( (ActiveMQDestination)topic ); - - Thread.sleep( 3000 ); - - assertFalse( destinationSource.getTopics().contains(amqTopic) ); + Thread.sleep(3000); + amqConnection.destroyDestination((ActiveMQDestination) topic); + Thread.sleep(3000); + assertFalse(destinationSource.getTopics().contains(amqTopic)); } + @Test public void testRemoveDestinationWithSubscriber() throws Exception { ActiveMQConnection amqConnection = (ActiveMQConnection) createConnection(true); DestinationSource destinationSource = amqConnection.getDestinationSource(); - + Session session = amqConnection.createSession(false, Session.AUTO_ACKNOWLEDGE); Topic topic = session.createTopic("TEST.FOO"); MessageProducer producer = session.createProducer(topic); @@ -110,56 +113,53 @@ public class RemoveDestinationTest extends TestCase { TextMessage msg = session.createTextMessage("Hellow World"); producer.send(msg); - assertNotNull( consumer.receive( 5000 ) ); - Thread.sleep( 1000 ); + assertNotNull(consumer.receive(5000)); + Thread.sleep(1000); - ActiveMQTopic amqTopic = (ActiveMQTopic)topic; + ActiveMQTopic amqTopic = (ActiveMQTopic) topic; assertTrue(destinationPresentInAdminView(broker, amqTopic)); - - assertTrue( destinationSource.getTopics().contains(amqTopic) ); + assertTrue(destinationSource.getTopics().contains(amqTopic)); // This line generates a broker error since the consumer is still active. - try{ - amqConnection.destroyDestination( (ActiveMQDestination)topic ); + try { + amqConnection.destroyDestination((ActiveMQDestination) topic); fail("expect exception on destroy if comsumer present"); - } catch( JMSException expected ) { + } catch (JMSException expected) { assertTrue(expected.getMessage().indexOf(amqTopic.getTopicName()) != -1); } - Thread.sleep( 3000 ); + Thread.sleep(3000); - assertTrue( destinationSource.getTopics().contains(amqTopic) ); + assertTrue(destinationSource.getTopics().contains(amqTopic)); assertTrue(destinationPresentInAdminView(broker, amqTopic)); - + consumer.close(); producer.close(); session.close(); - Thread.sleep( 3000 ); + Thread.sleep(3000); - // The destination will not be removed with this call, but if you remove the call - // above that generates the error it will. - amqConnection.destroyDestination( amqTopic ); - - Thread.sleep( 3000 ); - - assertFalse( destinationSource.getTopics().contains(amqTopic) ); + // The destination will not be removed with this call, but if you remove + // the call above that generates the error it will. + amqConnection.destroyDestination(amqTopic); + Thread.sleep(3000); + assertFalse(destinationSource.getTopics().contains(amqTopic)); assertFalse(destinationPresentInAdminView(broker, amqTopic)); - } - private boolean destinationPresentInAdminView(BrokerService broker2, - ActiveMQTopic amqTopic) throws Exception { + private boolean destinationPresentInAdminView(BrokerService broker2, ActiveMQTopic amqTopic) throws Exception { boolean found = false; for (ObjectName name : broker.getAdminView().getTopics()) { - if (name.getKeyProperty("Destination") != null && - name.getKeyProperty("Destination").equalsIgnoreCase(amqTopic.getTopicName())) { + + DestinationViewMBean proxy = (DestinationViewMBean) + broker.getManagementContext().newProxyInstance(name, DestinationViewMBean.class, true); + + if (proxy.getName().equals(amqTopic.getPhysicalName())) { found = true; break; - } + } } return found; } } -