KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ActiveMQConnectionFactoryTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq;
19
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.io.ObjectInputStream JavaDoc;
23 import java.io.ObjectOutputStream JavaDoc;
24 import java.net.URI JavaDoc;
25 import java.net.URISyntaxException JavaDoc;
26
27 import javax.jms.Connection JavaDoc;
28 import javax.jms.JMSException JavaDoc;
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 JavaDoc, JMSException JavaDoc {
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 JavaDoc 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 JavaDoc, JMSException JavaDoc {
56         ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?jms.useAsyncSend=true");
57         assertTrue(cf.isUseAsyncSend());
58         // the broker url have been adjusted.
59
assertEquals("vm://localhost", cf.getBrokerURL());
60         
61         cf = new ActiveMQConnectionFactory("vm://localhost?jms.useAsyncSend=false");
62         assertFalse(cf.isUseAsyncSend());
63         // the broker url have been adjusted.
64
assertEquals("vm://localhost", cf.getBrokerURL());
65
66         cf = new ActiveMQConnectionFactory("vm:(broker:()/localhost)?jms.useAsyncSend=true");
67         assertTrue(cf.isUseAsyncSend());
68         // the broker url have been adjusted.
69
assertEquals("vm:(broker:()/localhost)", cf.getBrokerURL());
70     }
71
72     public void testCreateVMConnectionWithEmbdeddBroker() throws URISyntaxException JavaDoc, JMSException JavaDoc {
73         ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
74         // Make sure the broker is not created until the connection is instantiated.
75
assertNull( BrokerRegistry.getInstance().lookup("localhost") );
76         Connection JavaDoc connection = cf.createConnection();
77         // This should create the connection.
78
assertNotNull(connection);
79         // Verify the broker was created.
80
assertNotNull( BrokerRegistry.getInstance().lookup("localhost") );
81         connection.close();
82         // Verify the broker was destroyed.
83
assertNull( BrokerRegistry.getInstance().lookup("localhost") );
84     }
85     
86     public void testGetBrokerName() throws URISyntaxException JavaDoc, JMSException JavaDoc {
87         ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory("vm://localhost?broker.persistent=false");
88         ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection();
89         connection.start();
90         
91         String JavaDoc 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 JavaDoc {
99         assertCreateConnection("tcp://localhost:0?wireFormat.tcpNoDelayEnabled=true");
100     }
101     public void testCreateTcpConnectionUsingKnownPort() throws Exception JavaDoc {
102         assertCreateConnection("tcp://localhost:61610?wireFormat.tcpNoDelayEnabled=true");
103     }
104
105     public void testConnectionFailsToConnectToVMBrokerThatIsNotRunning() throws Exception JavaDoc {
106         ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory("vm://localhost?create=false");
107         try {
108             factory.createConnection();
109             fail("Expected connection failure.");
110         } catch (JMSException JavaDoc e) {
111         }
112     }
113     
114     public void testFactorySerializable() throws Exception JavaDoc{
115         String JavaDoc clientID="TestClientID";
116         ActiveMQConnectionFactory cf=new ActiveMQConnectionFactory();
117         cf.setClientID(clientID);
118         ByteArrayOutputStream JavaDoc bytesOut=new ByteArrayOutputStream JavaDoc();
119         ObjectOutputStream JavaDoc objectsOut=new ObjectOutputStream JavaDoc(bytesOut);
120         objectsOut.writeObject(cf);
121         objectsOut.flush();
122         byte[] data=bytesOut.toByteArray();
123         ByteArrayInputStream JavaDoc bytesIn=new ByteArrayInputStream JavaDoc(data);
124         ObjectInputStream JavaDoc objectsIn=new ObjectInputStream JavaDoc(bytesIn);
125         cf=(ActiveMQConnectionFactory)objectsIn.readObject();
126         assertEquals(cf.getClientID(),clientID);
127     }
128
129     protected void assertCreateConnection(String JavaDoc uri) throws Exception JavaDoc {
130         // Start up a broker with a tcp connector.
131
BrokerService broker = new BrokerService();
132         broker.setPersistent(false);
133         TransportConnector connector = broker.addConnector(uri);
134         broker.start();
135         
136         URI JavaDoc temp = new URI JavaDoc(uri);
137         //URI connectURI = connector.getServer().getConnectURI();
138
// TODO this sometimes fails when using the actual local host name
139
URI JavaDoc currentURI = connector.getServer().getConnectURI();
140
141         // sometimes the actual host name doesn't work in this test case
142
// e.g. on OS X so lets use the original details but just use the actual port
143
URI JavaDoc connectURI = new URI JavaDoc(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         // This should create the connection.
149
ActiveMQConnectionFactory cf = new ActiveMQConnectionFactory(connectURI);
150         Connection JavaDoc connection = cf.createConnection();
151         assertNotNull(connection);
152         connection.close();
153         
154         broker.stop();
155     }
156     
157 }
158
Popular Tags