KickJava   Java API By Example, From Geeks To Geeks.

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


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.net.URI JavaDoc;
21 import java.net.URISyntaxException JavaDoc;
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 JavaDoc, 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 JavaDoc, JMSException {
40         ActiveMQXAConnectionFactory cf = new ActiveMQXAConnectionFactory("vm://localhost?jms.useAsyncSend=true");
41         assertTrue(cf.isUseAsyncSend());
42         // the broker url have been adjusted.
43
assertEquals("vm://localhost", cf.getBrokerURL());
44         
45         cf = new ActiveMQXAConnectionFactory("vm://localhost?jms.useAsyncSend=false");
46         assertFalse(cf.isUseAsyncSend());
47         // the broker url have been adjusted.
48
assertEquals("vm://localhost", cf.getBrokerURL());
49
50         cf = new ActiveMQXAConnectionFactory("vm:(broker:()/localhost)?jms.useAsyncSend=true");
51         assertTrue(cf.isUseAsyncSend());
52         // the broker url have been adjusted.
53
assertEquals("vm:(broker:()/localhost)", cf.getBrokerURL());
54     }
55
56     public void testCreateVMConnectionWithEmbdeddBroker() throws URISyntaxException JavaDoc, JMSException {
57         ActiveMQXAConnectionFactory cf = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false");
58         // Make sure the broker is not created until the connection is instantiated.
59
assertNull( BrokerRegistry.getInstance().lookup("localhost") );
60         Connection connection = cf.createConnection();
61         // This should create the connection.
62
assertNotNull(connection);
63         // Verify the broker was created.
64
assertNotNull( BrokerRegistry.getInstance().lookup("localhost") );
65         connection.close();
66         // Verify the broker was destroyed.
67
assertNull( BrokerRegistry.getInstance().lookup("localhost") );
68     }
69     
70     public void testGetBrokerName() throws URISyntaxException JavaDoc, JMSException {
71         ActiveMQXAConnectionFactory cf = new ActiveMQXAConnectionFactory("vm://localhost?broker.persistent=false");
72         ActiveMQConnection connection = (ActiveMQConnection) cf.createConnection();
73         connection.start();
74         
75         String JavaDoc 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 JavaDoc {
83         assertCreateConnection("tcp://localhost:0?wireFormat.tcpNoDelayEnabled=true");
84     }
85     public void testCreateTcpConnectionUsingKnownPort() throws Exception JavaDoc {
86         assertCreateConnection("tcp://localhost:61610?wireFormat.tcpNoDelayEnabled=true");
87     }
88     
89     protected void assertCreateConnection(String JavaDoc uri) throws Exception JavaDoc {
90         // Start up a broker with a tcp connector.
91
BrokerService broker = new BrokerService();
92         broker.setPersistent(false);
93         TransportConnector connector = broker.addConnector(uri);
94         broker.start();
95         
96         URI JavaDoc temp = new URI JavaDoc(uri);
97         //URI connectURI = connector.getServer().getConnectURI();
98
// TODO this sometimes fails when using the actual local host name
99
URI JavaDoc currentURI = connector.getServer().getConnectURI();
100
101         // sometimes the actual host name doesn't work in this test case
102
// e.g. on OS X so lets use the original details but just use the actual port
103
URI JavaDoc connectURI = new URI JavaDoc(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         // This should create the connection.
109
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