    1   /***
    2    *
    3    * Copyright 2004 Protique Ltd
    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.transport.tcp;
    19
    20  import org.codehaus.activemq.ActiveMQConnection;
    21  import org.codehaus.activemq.ActiveMQConnectionFactory;
    22  import org.codehaus.activemq.TestSupport;
    23
    24  import javax.jms.Connection;
    25  import javax.jms.ExceptionListener;
    26  import javax.jms.JMSException;
    27  import java.io.IOException;
    28  import java.net.Socket;
    29
    30  /***
    31   * @version $Revision: 1.3 $
    32   */
    33  public class ExceptionListenerTest extends TestSupport implements ExceptionListener {
    34      protected Connection connection;
    35      protected JMSException exception;
    36
    37      public void testFailingConnection() throws Exception {
    38          breakTransport();
    39
    40          synchronized (this) {
    41              if (exception == null) {
    42                  wait(3000L);
    43              }
    44          }
    45          assertTrue("Should have been notified of an exception", exception != null);
    46
    47      }
    48
    49      public synchronized void onException(JMSException e) {
    50          System.out.println("Caught exception: " + e);
    51
    52          exception = e;
    53          notifyAll();
    54      }
    55
    56      protected void breakTransport() throws IOException, JMSException {
    57          // now lets make the socket fail
    58          TcpTransportChannel transport = (TcpTransportChannel) ((ActiveMQConnection) connection).getTransportChannel();
    59          Socket socket = transport.getSocket();
    60          socket.close();
    61      }
    62
    63
    64      protected ActiveMQConnectionFactory createConnectionFactory() throws Exception {
    65          ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
    66          factory.setUseEmbeddedBroker(true);
    67          factory.setBrokerURL("tcp://localhost:62345");
    68          return factory;
    69      }
    70
    71      protected void setUp() throws Exception {
    72          super.setUp();
    73          connection = createConnection();
    74          connection.setExceptionListener(this);
    75          System.out.println("Created connection: " + connection);
    76
    77      }
    78
    79      protected void tearDown() throws Exception {
    80          if (connection != null) {
    81              try {
    82                  connection.close();
    83              }
    84              catch (JMSException e) {
    85                  System.out.println("Failed to close connection: " + e);
    86                  e.printStackTrace();
    87              }
    88
    89          }
    90          super.tearDown();
    91      }
    92  }