Don't use fixed ports for broker instances, let the connector find an open port.

git-svn-id: https://svn.apache.org/repos/asf/activemq/trunk@1352103 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Timothy A. Bish 2012-06-20 13:56:31 +00:00
parent e2e71eb3ca
commit f3d84aeb1a
1 changed files with 15 additions and 14 deletions

View File

@ -41,40 +41,41 @@ import javax.jms.TopicSubscriber;
import javax.naming.InitialContext; import javax.naming.InitialContext;
import org.apache.activemq.broker.BrokerService; import org.apache.activemq.broker.BrokerService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class AMQ2084Test { public class AMQ2084Test {
private static final Logger LOG = LoggerFactory.getLogger(AMQ2084Test.class); private static final Logger LOG = LoggerFactory.getLogger(AMQ2084Test.class);
BrokerService broker; BrokerService broker;
CountDownLatch qreceived; CountDownLatch qreceived;
String connectionUri;
@Before @Before
public void startBroker() throws Exception { public void startBroker() throws Exception {
broker = new BrokerService(); broker = new BrokerService();
broker.setPersistent(false); broker.setPersistent(false);
broker.addConnector("tcp://localhost:61616"); connectionUri = broker.addConnector("tcp://localhost:0").getPublishableConnectString();
broker.start(); broker.start();
qreceived = new CountDownLatch(1); qreceived = new CountDownLatch(1);
} }
@After @After
public void stopBroker() throws Exception { public void stopBroker() throws Exception {
if (broker != null) { if (broker != null) {
broker.stop(); broker.stop();
} }
} }
public void listenQueue(final String queueName, final String selectors) { public void listenQueue(final String queueName, final String selectors) {
try { try {
Properties props = new Properties(); Properties props = new Properties();
props.put("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory"); props.put("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.put("java.naming.provider.url", "tcp://localhost:61616"); props.put("java.naming.provider.url", connectionUri);
props.put("queue.queueName", queueName); props.put("queue.queueName", queueName);
javax.naming.Context ctx = new InitialContext(props); javax.naming.Context ctx = new InitialContext(props);
@ -92,7 +93,7 @@ public class AMQ2084Test {
String msg = txtMsg.getText(); String msg = txtMsg.getText();
LOG.info("Queue Message Received: " + queueName + " - " + msg); LOG.info("Queue Message Received: " + queueName + " - " + msg);
qreceived.countDown(); qreceived.countDown();
} }
message.acknowledge(); message.acknowledge();
} catch (Throwable e) { } catch (Throwable e) {
@ -110,7 +111,7 @@ public class AMQ2084Test {
try { try {
Properties props = new Properties(); Properties props = new Properties();
props.put("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory"); props.put("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.put("java.naming.provider.url", "tcp://localhost:61616"); props.put("java.naming.provider.url", connectionUri);
props.put("topic.topicName", topicName); props.put("topic.topicName", topicName);
javax.naming.Context ctx = new InitialContext(props); javax.naming.Context ctx = new InitialContext(props);
@ -144,7 +145,7 @@ public class AMQ2084Test {
try { try {
Properties props = new Properties(); Properties props = new Properties();
props.put("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory"); props.put("java.naming.factory.initial", "org.apache.activemq.jndi.ActiveMQInitialContextFactory");
props.put("java.naming.provider.url", "tcp://localhost:61616"); props.put("java.naming.provider.url", connectionUri);
props.put("topic.topicName", topicName); props.put("topic.topicName", topicName);
javax.naming.Context ctx = new InitialContext(props); javax.naming.Context ctx = new InitialContext(props);
TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("ConnectionFactory"); TopicConnectionFactory factory = (TopicConnectionFactory) ctx.lookup("ConnectionFactory");
@ -166,15 +167,15 @@ public class AMQ2084Test {
String xPath = "XPATH '//books//book[@lang=''en'']'"; String xPath = "XPATH '//books//book[@lang=''en'']'";
listenQueue("Consumer.Sample.VirtualTopic.TestXpath", xPath); listenQueue("Consumer.Sample.VirtualTopic.TestXpath", xPath);
publish("VirtualTopic.TestXpath", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><books><book lang=\"en\">ABC</book></books>"); publish("VirtualTopic.TestXpath", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><books><book lang=\"en\">ABC</book></books>");
assertTrue("topic received: ", qreceived.await(20, TimeUnit.SECONDS)); assertTrue("topic received: ", qreceived.await(20, TimeUnit.SECONDS));
} }
@Test @Test
public void tryXpathSelectorNoMatch() throws Exception { public void tryXpathSelectorNoMatch() throws Exception {
String xPath = "XPATH '//books//book[@lang=''es'']'"; String xPath = "XPATH '//books//book[@lang=''es'']'";
listenQueue("Consumer.Sample.VirtualTopic.TestXpath", xPath); listenQueue("Consumer.Sample.VirtualTopic.TestXpath", xPath);
publish("VirtualTopic.TestXpath", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><books><book lang=\"en\">ABC</book></books>"); publish("VirtualTopic.TestXpath", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><books><book lang=\"en\">ABC</book></books>");
assertFalse("topic did not receive unmatched", qreceived.await(5, TimeUnit.SECONDS)); assertFalse("topic did not receive unmatched", qreceived.await(5, TimeUnit.SECONDS));
} }
} }