Source code: org/activemq/ra/ManagedConnectionFactoryTest.java
1 /**
2 *
3 * Copyright 2004 Hiram Chirino
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 *
17 **/
18 package org.activemq.ra;
19
20 import java.io.Serializable;
21 import java.util.HashSet;
22 import java.util.Timer;
23
24 import javax.jms.Connection;
25 import javax.jms.ConnectionFactory;
26 import javax.jms.JMSException;
27 import javax.jms.QueueConnectionFactory;
28 import javax.jms.TopicConnectionFactory;
29 import javax.resource.Referenceable;
30 import javax.resource.ResourceException;
31 import javax.resource.spi.BootstrapContext;
32 import javax.resource.spi.ConnectionRequestInfo;
33 import javax.resource.spi.ManagedConnection;
34 import javax.resource.spi.ManagedConnectionFactory;
35 import javax.resource.spi.UnavailableException;
36 import javax.resource.spi.XATerminator;
37 import javax.resource.spi.work.WorkManager;
38
39 import org.activemq.ActiveMQConnection;
40 import org.activemq.broker.impl.BrokerContainerImpl;
41 import org.activemq.broker.BrokerContainer;
42
43 import junit.framework.TestCase;
44
45
46 /**
47 * @version $Revision: 1.1.1.1 $
48 */
49 public class ManagedConnectionFactoryTest extends TestCase {
50
51 protected static final String DEFAULT_HOST = "vm://localhost";
52 protected static final String REMOTE_HOST = "tcp://localhost:61616";
53
54 protected ActiveMQManagedConnectionFactory managedConnectionFactory;
55 protected BrokerContainer broker;
56
57 /**
58 * @see junit.framework.TestCase#setUp()
59 */
60 protected void setUp() throws Exception {
61
62 ActiveMQResourceAdapter adapter = new ActiveMQResourceAdapter();
63 adapter.setServerUrl(DEFAULT_HOST);
64 adapter.setUserName(ActiveMQConnection.DEFAULT_USER);
65 adapter.setPassword(ActiveMQConnection.DEFAULT_PASSWORD);
66 adapter.start(new BootstrapContext(){
67 public WorkManager getWorkManager() {
68 return null;
69 }
70 public XATerminator getXATerminator() {
71 return null;
72 }
73
74 public Timer createTimer() throws UnavailableException {
75 return null;
76 }
77 });
78
79 managedConnectionFactory = new ActiveMQManagedConnectionFactory();
80 managedConnectionFactory.setResourceAdapter(adapter);
81
82 }
83
84 protected void startBroker() throws JMSException {
85 broker = new BrokerContainerImpl("localhost");
86 broker.addConnector("tcp://localhost:61616");
87 broker.start();
88 }
89
90 protected void tearDown() throws Exception {
91 if (broker != null) {
92 broker.stop();
93 }
94 super.tearDown();
95 }
96
97 public void testConnectionFactoryAllocation() throws ResourceException, JMSException {
98
99 // Make sure that the ConnectionFactory is asking the connection manager to
100 // allocate the connection.
101 final boolean allocateRequested[] = new boolean[]{false};
102 Object cf = managedConnectionFactory.createConnectionFactory(
103 new ConnectionManagerAdapter() {
104 public Object allocateConnection(ManagedConnectionFactory connectionFactory, ConnectionRequestInfo info)
105 throws ResourceException {
106 allocateRequested[0]=true;
107 return super.allocateConnection(connectionFactory, info);
108 }
109 }
110 );
111
112 // We should be getting a JMS Connection Factory.
113 assertTrue( cf instanceof ConnectionFactory );
114 ConnectionFactory connectionFactory = (ConnectionFactory)cf;
115
116 // Make sure that the connection factory is using the ConnectionManager..
117 Connection connection = connectionFactory.createConnection();
118 assertTrue(allocateRequested[0]);
119
120 // Make sure that the returned connection is of the expected type.
121 assertTrue( connection!=null );
122 assertTrue( connection instanceof JMSConnectionProxy );
123
124 }
125
126
127 public void testConnectionFactoryConnectionMatching() throws ResourceException, JMSException {
128 startBroker();
129
130 ActiveMQConnectionRequestInfo ri1 = new ActiveMQConnectionRequestInfo();
131 ri1.setServerUrl(DEFAULT_HOST);
132 ri1.setUserName(ActiveMQConnection.DEFAULT_USER);
133 ri1.setPassword(ActiveMQConnection.DEFAULT_PASSWORD);
134
135 ActiveMQConnectionRequestInfo ri2 = new ActiveMQConnectionRequestInfo();
136 ri2.setServerUrl(REMOTE_HOST);
137 ri2.setUserName(ActiveMQConnection.DEFAULT_USER);
138 ri2.setPassword(ActiveMQConnection.DEFAULT_PASSWORD);
139 assertNotSame(ri1, ri2);
140
141 ManagedConnection connection1 = managedConnectionFactory.createManagedConnection(null, ri1);
142 ManagedConnection connection2 = managedConnectionFactory.createManagedConnection(null, ri2);
143 assertTrue(connection1!=connection2);
144
145 HashSet set = new HashSet();
146 set.add(connection1);
147 set.add(connection2);
148
149 // Can we match for the first connection?
150 ActiveMQConnectionRequestInfo ri3 = ri1.copy();
151 assertTrue( ri1!=ri3 && ri1.equals(ri3) );
152 ManagedConnection test = managedConnectionFactory.matchManagedConnections(set,null, ri3);
153 assertTrue( connection1==test );
154
155 // Can we match for the second connection?
156 ri3 = ri2.copy();
157 assertTrue( ri2!=ri3 && ri2.equals(ri3) );
158 test = managedConnectionFactory.matchManagedConnections(set,null, ri2);
159 assertTrue( connection2==test );
160
161 }
162
163 public void testConnectionFactoryIsSerializableAndReferenceable() throws ResourceException, JMSException {
164 Object cf = managedConnectionFactory.createConnectionFactory(new ConnectionManagerAdapter());
165 assertTrue( cf!=null );
166 assertTrue( cf instanceof Serializable );
167 assertTrue( cf instanceof Referenceable );
168 }
169
170 public void testImplementsQueueAndTopicConnectionFactory() throws Exception {
171 Object cf = managedConnectionFactory.createConnectionFactory(new ConnectionManagerAdapter());
172 assertTrue( cf instanceof QueueConnectionFactory );
173 assertTrue( cf instanceof TopicConnectionFactory );
174 }
175
176 }