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
This commit is contained in:
Timothy A. Bish 2013-01-03 15:57:05 +00:00
parent 6335b7e331
commit 60d7cdc1dd
1 changed files with 54 additions and 54 deletions

View File

@ -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;
}
}