Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: org/activemq/transport/activeio/ExceptionListenerTest.java


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.activemq.transport.activeio;
19  
20  import java.io.IOException;
21  
22  import javax.jms.Connection;
23  import javax.jms.ExceptionListener;
24  import javax.jms.JMSException;
25  
26  import junit.framework.Test;
27  import junit.framework.TestSuite;
28  
29  import org.activemq.ActiveMQConnection;
30  import org.activemq.ActiveMQConnectionFactory;
31  import org.activemq.test.TestSupport;
32  
33  /**
34   * @version $Revision: 1.1.1.1 $
35   */
36  public class ExceptionListenerTest extends TestSupport implements ExceptionListener {
37      protected Connection connection;
38      protected JMSException exception;
39  
40      public void testFailingConnection() throws Exception {
41          breakTransport();
42  
43          synchronized (this) {
44              if (exception == null) {
45                  wait(3000L);
46              }
47          }
48          assertTrue("Should have been notified of an exception", exception != null);
49  
50      }
51  
52      public synchronized void onException(JMSException e) {
53          System.out.println("Caught exception: " + e);
54  
55          exception = e;
56          notifyAll();
57      }
58  
59      protected void breakTransport() throws IOException, JMSException {
60          // now lets make the socket fail
61          ActiveIOTransportChannel transport = (ActiveIOTransportChannel) ((ActiveMQConnection) connection).getTransportChannel();
62          transport.getAsynchChannel().dispose();
63      }
64  
65  
66      protected ActiveMQConnectionFactory createConnectionFactory() throws Exception {
67          ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory();
68          factory.setUseEmbeddedBroker(true);
69          factory.setBrokerURL(getURL());
70          return factory;
71      }
72  
73      protected void setUp() throws Exception {
74          super.setUp();
75          connection = createConnection();
76          connection.setExceptionListener(this);
77          System.out.println("Created connection: " + connection);
78  
79      }
80  
81      protected void tearDown() throws Exception {
82          if (connection != null) {
83              try {
84                  connection.close();
85              }
86              catch (JMSException e) {
87                  System.out.println("Failed to close connection: " + e);
88                  e.printStackTrace();
89              }
90  
91          }
92          super.tearDown();
93      }
94      
95      
96      protected String getURL() {
97          return "activeio:socket://localhost:62345";
98      }
99      
100     public static class VMPipeExceptionListenerTest extends ExceptionListenerTest {
101         protected String getURL() {
102             return "activeio:vmpipe://localhost";
103         }       
104     }
105     public static class NIOExceptionListenerTest extends ExceptionListenerTest {
106         protected String getURL() {
107             return "activeio:nio://localhost:62345";
108         }       
109     }
110     
111     public static Test suite() { 
112         TestSuite suite= new TestSuite(); 
113         suite.addTestSuite(ExceptionListenerTest.class); 
114         suite.addTestSuite(VMPipeExceptionListenerTest.class); 
115         suite.addTestSuite(NIOExceptionListenerTest.class); 
116         return suite;
117     }
118 
119 }