1 18 package org.apache.activemq; 19 20 import java.io.ByteArrayInputStream ; 21 import java.io.ByteArrayOutputStream ; 22 import java.io.ObjectInputStream ; 23 import java.io.ObjectOutputStream ; 24 import java.net.URI ; 25 import java.net.URISyntaxException ; 26 27 import javax.jms.Connection ; 28 import javax.jms.JMSException ; 29 30 import org.apache.activemq.ActiveMQConnectionFactory; 31 import org.apache.activemq.broker.BrokerRegistry; 32 import org.apache.activemq.broker.BrokerService; 33 import org.apache.activemq.broker.TransportConnector; 34 35 public class ActiveMQConnectionFactoryTest extends CombinationTestSupport { 36 37 public void testUseURIToSetUseClientIDPrefixOnConnectionFactory() throws URISyntaxException , JMSException { 38 ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?jms.clientIDPrefix=Cheese"); 39 assertEquals("Cheese", cf.getClientIDPrefix()); 40 41 ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection(); 42 try { 43 connection.start(); 44 45 String clientID = connection.getClientID(); 46 log.info("Got client ID: " + clientID); 47 48 assertTrue("should start with Cheese! but was: " + clientID, clientID.startsWith("Cheese")); 49 } 50 finally { 51 connection.close(); 52 } 53 } 54 55 public void testUseURIToSetOptionsOnConnectionFactory() throws URISyntaxException , JMSException { 56 ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?jms.useAsyncSend=true"); 57 assertTrue(cf.isUseAsyncSend()); 58 assertEquals("vm://localhost", cf.getBrokerURL()); 60 61 cf = new ActiveMQConnectionFactory("vm://localhost?jms.useAsyncSend=false"); 62 assertFalse(cf.isUseAsyncSend()); 63 assertEquals("vm://localhost", cf.getBrokerURL()); 65 66 cf = new ActiveMQConnectionFactory("vm:(broker:()/localhost)?jms.useAsyncSend=true"); 67 assertTrue(cf.isUseAsyncSend()); 68 assertEquals("vm:(broker:()/localhost)", cf.getBrokerURL()); 70 } 71 72 public void testCreateVMConnectionWithEmbdeddBroker() throws URISyntaxException , JMSException { 73 ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); 74 assertNull( BrokerRegistry.getInstance().lookup("localhost") ); 76 Connection connection = cf.createConnection(); 77 assertNotNull(connection); 79 assertNotNull( BrokerRegistry.getInstance().lookup("localhost") ); 81 connection.close(); 82 assertNull( BrokerRegistry.getInstance().lookup("localhost") ); 84 } 85 86 public void testGetBrokerName() throws URISyntaxException , JMSException { 87 ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false"); 88 ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection(); 89 connection.start(); 90 91 String brokerName = connection.getBrokerName(); 92 log.info("Got broker name: " + brokerName); 93 94 assertNotNull("No broker name available!", brokerName); 95 connection.close(); 96 } 97 98 public void testCreateTcpConnectionUsingAllocatedPort() throws Exception { 99 assertCreateConnection("tcp://localhost:0?wireFormat.tcpNoDelayEnabled=true"); 100 } 101 public void testCreateTcpConnectionUsingKnownPort() throws Exception { 102 assertCreateConnection("tcp://localhost:61610?wireFormat.tcpNoDelayEnabled=true"); 103 } 104 105 public void testConnectionFailsToConnectToVMBrokerThatIsNotRunning() throws Exception { 106 ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost?create=false"); 107 try { 108 factory.createConnection(); 109 fail("Expected connection failure."); 110 } catch (JMSException e) { 111 } 112 } 113 114 public void testFactorySerializable() throws Exception { 115 String clientID="TestClientID"; 116 ActiveMQConnectionFactory cf=new ActiveMQConnectionFactory(); 117 cf.setClientID(clientID); 118 ByteArrayOutputStream bytesOut=new ByteArrayOutputStream (); 119 ObjectOutputStream objectsOut=new ObjectOutputStream (bytesOut); 120 objectsOut.writeObject(cf); 121 objectsOut.flush(); 122 byte[] data=bytesOut.toByteArray(); 123 ByteArrayInputStream bytesIn=new ByteArrayInputStream (data); 124 ObjectInputStream objectsIn=new ObjectInputStream (bytesIn); 125 cf=(ActiveMQConnectionFactory)objectsIn.readObject(); 126 assertEquals(cf.getClientID(),clientID); 127 } 128 129 protected void assertCreateConnection(String uri) throws Exception { 130 BrokerService broker = new BrokerService(); 132 broker.setPersistent(false); 133 TransportConnector connector = broker.addConnector(uri); 134 broker.start(); 135 136 URI temp = new URI (uri); 137 URI currentURI = connector.getServer().getConnectURI(); 140 141 URI connectURI = new URI (temp.getScheme(), temp.getUserInfo(), temp.getHost(), currentURI.getPort(), temp.getPath(), temp.getQuery(), temp.getFragment()); 144 145 146 log.info("connection URI is: " + connectURI); 147 148 ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(connectURI); 150 Connection connection = cf.createConnection(); 151 assertNotNull(connection); 152 connection.close(); 153 154 broker.stop(); 155 } 156 157 } 158 | Popular Tags |