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.codehaus.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.resource.Referenceable;
28  import javax.resource.ResourceException;
29  import javax.resource.spi.BootstrapContext;
30  import javax.resource.spi.ConnectionRequestInfo;
31  import javax.resource.spi.ManagedConnection;
32  import javax.resource.spi.ManagedConnectionFactory;
33  import javax.resource.spi.UnavailableException;
34  import javax.resource.spi.XATerminator;
35  import javax.resource.spi.work.WorkManager;
36  
37  import org.codehaus.activemq.ActiveMQConnection;
38  
39  import junit.framework.TestCase;
40  
41  
42  /***
43   * @version $Revision: 1.4 $
44   */
45  public class ManagedConnectionFactoryTest extends TestCase {
46      
47      private static final String DEFAULT_HOST = "vm://localhost";
48      private ActiveMQManagedConnectionFactory managedConnectionFactory;
49      
50      /***
51       * @see junit.framework.TestCase#setUp()
52       */
53      protected void setUp() throws Exception {
54          
55      	ActiveMQResourceAdapter adapter = new ActiveMQResourceAdapter(); 
56      	adapter.setServerUrl(DEFAULT_HOST);
57      	adapter.setUserName(ActiveMQConnection.DEFAULT_USER);
58      	adapter.setPassword(ActiveMQConnection.DEFAULT_PASSWORD);
59      	adapter.start(new BootstrapContext(){
60  			public WorkManager getWorkManager() {
61  				return null;
62  			}
63  			public XATerminator getXATerminator() {
64  				return null;
65  			}
66  
67  			public Timer createTimer() throws UnavailableException {
68  				return null;
69  			}
70  		});
71      	
72          managedConnectionFactory = new ActiveMQManagedConnectionFactory();
73          managedConnectionFactory.setResourceAdapter(adapter);
74          
75      }
76      
77      public void testConnectionFactoryAllocation() throws ResourceException, JMSException {
78          
79          // Make sure that the ConnectionFactory is asking the connection manager to
80          // allocate the connection.
81          final boolean allocateRequested[] = new boolean[]{false};
82          Object cf = managedConnectionFactory.createConnectionFactory(
83              new ConnectionManagerAdapter() {
84                  public Object allocateConnection(ManagedConnectionFactory connectionFactory, ConnectionRequestInfo info)
85                          throws ResourceException {
86                      allocateRequested[0]=true;
87                      return super.allocateConnection(connectionFactory, info);
88                  }
89              }    
90          );
91          
92          // We should be getting a JMS Connection Factory.
93          assertTrue( cf instanceof ConnectionFactory );
94          ConnectionFactory connectionFactory = (ConnectionFactory)cf;
95          
96          // Make sure that the connection factory is using the ConnectionManager..
97          Connection connection = connectionFactory.createConnection();        
98          assertTrue(allocateRequested[0]);
99          
100         // Make sure that the returned connection is of the expected type.
101         assertTrue( connection!=null );
102         assertTrue( connection instanceof JMSConnectionProxy );
103         
104     }
105 
106     
107     public void testConnectionFactoryConnectionMatching() throws ResourceException, JMSException {
108         
109         ActiveMQConnectionRequestInfo ri1 = new ActiveMQConnectionRequestInfo();
110         ri1.setServerUrl(DEFAULT_HOST);
111         ri1.setUserName(ActiveMQConnection.DEFAULT_USER);
112         ri1.setPassword(ActiveMQConnection.DEFAULT_PASSWORD);
113 
114         ActiveMQConnectionRequestInfo ri2 = new ActiveMQConnectionRequestInfo();
115         ri2.setServerUrl(DEFAULT_HOST);
116         ri2.setUserName("test");
117         ri2.setPassword(null);
118         assertNotSame(ri1, ri2);
119         
120         ManagedConnection connection1 = managedConnectionFactory.createManagedConnection(null, ri1);
121         ManagedConnection connection2 = managedConnectionFactory.createManagedConnection(null, ri2);        
122         assertTrue(connection1!=connection2);
123         
124         HashSet set = new HashSet();
125         set.add(connection1);
126         set.add(connection2);
127         
128         // Can we match for the first connection?
129         ActiveMQConnectionRequestInfo ri3 = ri1.copy();
130         assertTrue( ri1!=ri3 && ri1.equals(ri3) );
131         ManagedConnection test = managedConnectionFactory.matchManagedConnections(set,null, ri3);
132         assertTrue( connection1==test );
133 
134         // Can we match for the second connection?
135         ri3 = ri2.copy();
136         assertTrue( ri2!=ri3 && ri2.equals(ri3) );
137         test = managedConnectionFactory.matchManagedConnections(set,null, ri2);
138         assertTrue( connection2==test );
139         
140     }
141     
142     public void testConnectionFactoryIsSerializableAndReferenceable() throws ResourceException, JMSException {
143         Object cf = managedConnectionFactory.createConnectionFactory(new ConnectionManagerAdapter());
144         assertTrue( cf!=null );
145         assertTrue( cf instanceof Serializable );
146         assertTrue( cf instanceof Referenceable );
147     }
148     
149 }