KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ra > ManagedConnectionFactoryTest


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.ra;
19
20 import java.io.Serializable JavaDoc;
21 import java.util.HashSet JavaDoc;
22 import java.util.Timer JavaDoc;
23
24 import javax.jms.Connection JavaDoc;
25 import javax.jms.ConnectionFactory JavaDoc;
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.QueueConnectionFactory JavaDoc;
28 import javax.jms.TopicConnectionFactory JavaDoc;
29 import javax.resource.Referenceable JavaDoc;
30 import javax.resource.ResourceException JavaDoc;
31 import javax.resource.spi.BootstrapContext JavaDoc;
32 import javax.resource.spi.ConnectionRequestInfo JavaDoc;
33 import javax.resource.spi.ManagedConnection JavaDoc;
34 import javax.resource.spi.ManagedConnectionFactory JavaDoc;
35 import javax.resource.spi.UnavailableException JavaDoc;
36 import javax.resource.spi.XATerminator JavaDoc;
37 import javax.resource.spi.work.WorkManager JavaDoc;
38
39 import junit.framework.TestCase;
40
41 import org.apache.activemq.ActiveMQConnectionFactory;
42 import org.apache.activemq.ra.ActiveMQConnectionRequestInfo;
43 import org.apache.activemq.ra.ActiveMQManagedConnectionFactory;
44 import org.apache.activemq.ra.ActiveMQResourceAdapter;
45 import org.apache.activemq.ra.ManagedConnectionProxy;
46
47
48 /**
49  * @version $Revision$
50  */

51 public class ManagedConnectionFactoryTest extends TestCase {
52     
53     private static final String JavaDoc DEFAULT_HOST = "vm://localhost?broker.persistent=false";
54     private static final String JavaDoc REMOTE_HOST = "vm://remotehost?broker.persistent=false";
55     private ActiveMQManagedConnectionFactory managedConnectionFactory;
56     
57     /**
58      * @see junit.framework.TestCase#setUp()
59      */

60     protected void setUp() throws Exception JavaDoc {
61         
62         ActiveMQResourceAdapter adapter = new ActiveMQResourceAdapter();
63         adapter.setServerUrl(DEFAULT_HOST);
64         adapter.setUserName(ActiveMQConnectionFactory.DEFAULT_USER);
65         adapter.setPassword(ActiveMQConnectionFactory.DEFAULT_PASSWORD);
66         adapter.start(new BootstrapContext JavaDoc(){
67             public WorkManager JavaDoc getWorkManager() {
68                 return null;
69             }
70             public XATerminator JavaDoc getXATerminator() {
71                 return null;
72             }
73
74             public Timer JavaDoc createTimer() throws UnavailableException JavaDoc {
75                 return null;
76             }
77         });
78         
79         managedConnectionFactory = new ActiveMQManagedConnectionFactory();
80         managedConnectionFactory.setResourceAdapter(adapter);
81         
82     }
83     
84     public void testConnectionFactoryAllocation() throws ResourceException JavaDoc, JMSException JavaDoc {
85         
86         // Make sure that the ConnectionFactory is asking the connection manager to
87
// allocate the connection.
88
final boolean allocateRequested[] = new boolean[]{false};
89         Object JavaDoc cf = managedConnectionFactory.createConnectionFactory(
90             new ConnectionManagerAdapter() {
91                 private static final long serialVersionUID = 1699499816530099939L;
92
93                 public Object JavaDoc allocateConnection(ManagedConnectionFactory JavaDoc connectionFactory, ConnectionRequestInfo JavaDoc info)
94                         throws ResourceException JavaDoc {
95                     allocateRequested[0]=true;
96                     return super.allocateConnection(connectionFactory, info);
97                 }
98             }
99         );
100         
101         // We should be getting a JMS Connection Factory.
102
assertTrue( cf instanceof ConnectionFactory JavaDoc );
103         ConnectionFactory JavaDoc connectionFactory = (ConnectionFactory JavaDoc)cf;
104         
105         // Make sure that the connection factory is using the ConnectionManager..
106
Connection JavaDoc connection = connectionFactory.createConnection();
107         assertTrue(allocateRequested[0]);
108         
109         // Make sure that the returned connection is of the expected type.
110
assertTrue( connection!=null );
111         assertTrue( connection instanceof ManagedConnectionProxy );
112         
113     }
114
115     
116     public void testConnectionFactoryConnectionMatching() throws ResourceException JavaDoc, JMSException JavaDoc {
117         
118         ActiveMQConnectionRequestInfo ri1 = new ActiveMQConnectionRequestInfo();
119         ri1.setServerUrl(DEFAULT_HOST);
120         ri1.setUserName(ActiveMQConnectionFactory.DEFAULT_USER);
121         ri1.setPassword(ActiveMQConnectionFactory.DEFAULT_PASSWORD);
122
123         ActiveMQConnectionRequestInfo ri2 = new ActiveMQConnectionRequestInfo();
124         ri2.setServerUrl(REMOTE_HOST);
125         ri2.setUserName(ActiveMQConnectionFactory.DEFAULT_USER);
126         ri2.setPassword(ActiveMQConnectionFactory.DEFAULT_PASSWORD);
127         assertNotSame(ri1, ri2);
128         
129         ManagedConnection JavaDoc connection1 = managedConnectionFactory.createManagedConnection(null, ri1);
130         ManagedConnection JavaDoc connection2 = managedConnectionFactory.createManagedConnection(null, ri2);
131         assertTrue(connection1!=connection2);
132         
133         HashSet JavaDoc set = new HashSet JavaDoc();
134         set.add(connection1);
135         set.add(connection2);
136         
137         // Can we match for the first connection?
138
ActiveMQConnectionRequestInfo ri3 = ri1.copy();
139         assertTrue( ri1!=ri3 && ri1.equals(ri3) );
140         ManagedConnection JavaDoc test = managedConnectionFactory.matchManagedConnections(set,null, ri3);
141         assertTrue( connection1==test );
142
143         // Can we match for the second connection?
144
ri3 = ri2.copy();
145         assertTrue( ri2!=ri3 && ri2.equals(ri3) );
146         test = managedConnectionFactory.matchManagedConnections(set,null, ri2);
147         assertTrue( connection2==test );
148         
149     }
150     
151     public void testConnectionFactoryIsSerializableAndReferenceable() throws ResourceException JavaDoc, JMSException JavaDoc {
152         Object JavaDoc cf = managedConnectionFactory.createConnectionFactory(new ConnectionManagerAdapter());
153         assertTrue( cf!=null );
154         assertTrue( cf instanceof Serializable JavaDoc );
155         assertTrue( cf instanceof Referenceable JavaDoc );
156     }
157
158     public void testImplementsQueueAndTopicConnectionFactory() throws Exception JavaDoc {
159         Object JavaDoc cf = managedConnectionFactory.createConnectionFactory(new ConnectionManagerAdapter());
160         assertTrue( cf instanceof QueueConnectionFactory JavaDoc );
161         assertTrue( cf instanceof TopicConnectionFactory JavaDoc );
162     }
163
164 }
165
Popular Tags