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