1 18 package org.apache.activemq; 19 20 import java.net.URI ; 21 import java.net.URISyntaxException ; 22 23 import javax.jms.*; 24 25 import org.apache.activemq.ActiveMQXAConnectionFactory; 26 import org.apache.activemq.broker.BrokerRegistry; 27 import org.apache.activemq.broker.BrokerService; 28 import org.apache.activemq.broker.TransportConnector; 29 30 public class ActiveMQXAConnectionFactoryTest extends CombinationTestSupport { 31 32 public void testCopy() throws URISyntaxException , JMSException { 33 ActiveMQXAConnectionFactory cf = new ActiveMQXAConnectionFactory("vm://localhost?"); 34 ActiveMQConnectionFactory copy = cf.copy(); 35 assertTrue("Should be an ActiveMQXAConnectionFactory", copy instanceof ActiveMQXAConnectionFactory); 36 } 37 38 39 public void testUseURIToSetOptionsOnConnectionFactory() throws URISyntaxException , JMSException { 40 ActiveMQXAConnectionFactory cf = new ActiveMQXAConnectionFactory("vm://localhost?jms.useAsyncSend=true"); 41 assertTrue(cf.isUseAsyncSend()); 42 assertEquals("vm://localhost", cf.getBrokerURL()); 44 45 cf = new ActiveMQXAConnectionFactory("vm://localhost?jms.useAsyncSend=false"); 46 assertFalse(cf.isUseAsyncSend()); 47 assertEquals("vm://localhost", cf.getBrokerURL()); 49 50 cf = new ActiveMQXAConnectionFactory("vm:(broker:()/localhost)?jms.useAsyncSend=true"); 51 assertTrue(cf.isUseAsyncSend()); 52 assertEquals("vm:(broker:()/localhost)", cf.getBrokerURL()); 54 } 55 56 public void testCreateVMConnectionWithEmbdeddBroker() throws URISyntaxException , JMSException { 57 ActiveMQXAConnectionFactory cf = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false"); 58 assertNull( BrokerRegistry.getInstance().lookup("localhost") ); 60 Connection connection = cf.createConnection(); 61 assertNotNull(connection); 63 assertNotNull( BrokerRegistry.getInstance().lookup("localhost") ); 65 connection.close(); 66 assertNull( BrokerRegistry.getInstance().lookup("localhost") ); 68 } 69 70 public void testGetBrokerName() throws URISyntaxException , JMSException { 71 ActiveMQXAConnectionFactory cf = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false"); 72 ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection(); 73 connection.start(); 74 75 String brokerName = connection.getBrokerName(); 76 log.info("Got broker name: " + brokerName); 77 78 assertNotNull("No broker name available!", brokerName); 79 connection.close(); 80 } 81 82 public void testCreateTcpConnectionUsingAllocatedPort() throws Exception { 83 assertCreateConnection("tcp://localhost:0?wireFormat.tcpNoDelayEnabled=true"); 84 } 85 public void testCreateTcpConnectionUsingKnownPort() throws Exception { 86 assertCreateConnection("tcp://localhost:61610?wireFormat.tcpNoDelayEnabled=true"); 87 } 88 89 protected void assertCreateConnection(String uri) throws Exception { 90 BrokerService broker = new BrokerService(); 92 broker.setPersistent(false); 93 TransportConnector connector = broker.addConnector(uri); 94 broker.start(); 95 96 URI temp = new URI (uri); 97 URI currentURI = connector.getServer().getConnectURI(); 100 101 URI connectURI = new URI (temp.getScheme(), temp.getUserInfo(), temp.getHost(), currentURI.getPort(), temp.getPath(), temp.getQuery(), temp.getFragment()); 104 105 106 log.info("connection URI is: " + connectURI); 107 108 ActiveMQXAConnectionFactory cf = new ActiveMQXAConnectionFactory(connectURI); 110 Connection connection = cf.createConnection(); 111 112 assertXAConnection(connection); 113 114 assertNotNull(connection); 115 connection.close(); 116 117 connection = cf.createXAConnection(); 118 119 assertXAConnection(connection); 120 121 assertNotNull(connection); 122 connection.close(); 123 124 broker.stop(); 125 } 126 127 private void assertXAConnection(Connection connection) { 128 assertTrue("Should be an XAConnection", connection instanceof XAConnection); 129 assertTrue("Should be an XATopicConnection", connection instanceof XATopicConnection); 130 assertTrue("Should be an XAQueueConnection", connection instanceof XAQueueConnection); 131 } 132 133 } 134 | Popular Tags |