ManagedConnectionTest xref

    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.util.Timer;
    21
    22  import javax.jms.ConnectionFactory;
    23  import javax.jms.JMSException;
    24  import javax.jms.MessageProducer;
    25  import javax.jms.Queue;
    26  import javax.jms.Session;
    27  import javax.jms.QueueConnection;
    28  import javax.jms.QueueConnectionFactory;
    29  import javax.jms.TopicConnection;
    30  import javax.jms.TopicConnectionFactory;
    31  import javax.resource.ResourceException;
    32  import javax.resource.spi.BootstrapContext;
    33  import javax.resource.spi.ConnectionEvent;
    34  import javax.resource.spi.UnavailableException;
    35  import javax.resource.spi.XATerminator;
    36  import javax.resource.spi.work.WorkManager;
    37
    38  import junit.framework.TestCase;
    39
    40  import org.codehaus.activemq.ActiveMQConnection;
    41
    42
    43  /***
    44   * @version $Revision: 1.5 $
    45   */
    46  public class ManagedConnectionTest extends TestCase {
    47
    48
    49      private static final String DEFAULT_HOST = "vm://localhost";
    50
    51      private ConnectionManagerAdapter connectionManager = new ConnectionManagerAdapter();
    52      private ActiveMQManagedConnectionFactory managedConnectionFactory;
    53      private ConnectionFactory connectionFactory;
    54      private JMSConnectionProxy connection;
    55      private ActiveMQManagedConnection managedConnection;
    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          connectionFactory = (ConnectionFactory) managedConnectionFactory.createConnectionFactory(connectionManager);
    83          connection = (JMSConnectionProxy) connectionFactory.createConnection();
    84          managedConnection = connection.getManagedConnection();
    85
    86      }
    87
    88      public void testConnectionCloseEvent() throws ResourceException, JMSException {
    89
    90          final boolean test[] = new boolean[]{false};
    91          connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() {
    92                  public void connectionClosed(ConnectionEvent arg0) {
    93                      test[0]=true;
    94                  }
    95              });
    96          connection.close();
    97          assertTrue( test[0] );
    98      }
    99
    100     public void testLocalTransactionCommittedEvent() throws ResourceException, JMSException {
    101
    102         final boolean test[] = new boolean[]{false};
    103         connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() {
    104                 public void localTransactionCommitted(ConnectionEvent arg0) {
    105                     test[0]=true;
    106                 }
    107             });
    108         Session session = connection.createSession(true,0);
    109         doWork(session);
    110         session.commit();
    111
    112         assertTrue( test[0] );
    113
    114     }
    115
    116     public void testLocalTransactionRollbackEvent() throws ResourceException, JMSException {
    117
    118         final boolean test[] = new boolean[]{false};
    119         connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() {
    120 	            public void localTransactionRolledback(ConnectionEvent arg0) {
    121 	                test[0]=true;
    122 	            }
    123             });
    124         Session session = connection.createSession(true,0);
    125         doWork(session);
    126         session.rollback();
    127
    128         assertTrue( test[0] );
    129     }
    130
    131     public void testLocalTransactionStartedEvent() throws ResourceException, JMSException {
    132
    133         final boolean test[] = new boolean[]{false};
    134         connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() {
    135                 public void localTransactionStarted(ConnectionEvent arg0) {
    136                     test[0]=true;
    137                 }
    138             });
    139
    140         // Send a message...  that should start a local transaction...
    141 	    Session session = connection.createSession(true,0);
    142         doWork(session);
    143
    144         assertTrue( test[0] );
    145     }
    146
    147     /***
    148      * A managed connection that has been clean up should throw exceptions
    149      * when it used.
    150      */
    151     public void testCleanup() throws ResourceException, JMSException {
    152
    153         // Do some work and close it...
    154 	    Session session = connection.createSession(true,0);
    155         doWork(session);
    156         connection.close();
    157         try {
    158 	        // This should throw expection
    159 	        doWork(session);
    160 	        fail("Using a session after the connection is closed should throw exception.");
    161         } catch ( JMSException e) {
    162         }
    163     }
    164
    165     public void testSessionCloseIndependance() throws ResourceException, JMSException {
    166
    167 	    Session session1 = connection.createSession(true,0);
    168 	    Session session2 = connection.createSession(true,0);
    169         assertTrue( session1!=session2 );
    170
    171 	    doWork(session1);
    172         session1.close();
    173         try {
    174 	        // This should throw expection
    175 	        doWork(session1);
    176 	        fail("Using a session after the connection is closed should throw exception.");
    177         } catch ( JMSException e) {
    178         }
    179
    180         // Make sure that closing session 1 does not close session 2
    181         doWork(session2);
    182         session2.close();
    183         try {
    184 	        // This should throw expection
    185 	        doWork(session2);
    186 	        fail("Using a session after the connection is closed should throw exception.");
    187         } catch ( JMSException e) {
    188         }
    189     }
    190
    191     /***
    192      * Does some work so that we can test commit/rollback etc.
    193      * @throws JMSException
    194      */
    195     public void doWork(Session session) throws JMSException {
    196 	    Queue t = session.createQueue("TEST");
    197 	    MessageProducer producer = session.createProducer(t);
    198 	    producer.send(session.createTextMessage("test message."));
    199     }
    200
    201     public void testImplementsQueueAndTopicConnection() throws Exception {
    202         QueueConnection qc = ((QueueConnectionFactory)connectionFactory).createQueueConnection();
    203         assertNotNull(qc);
    204         TopicConnection tc = ((TopicConnectionFactory)connectionFactory).createTopicConnection();
    205         assertNotNull(tc);
    206     }
    207
    208
    209 }