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.resource.ResourceException;
28  import javax.resource.spi.BootstrapContext;
29  import javax.resource.spi.ConnectionEvent;
30  import javax.resource.spi.UnavailableException;
31  import javax.resource.spi.XATerminator;
32  import javax.resource.spi.work.WorkManager;
33  
34  import junit.framework.TestCase;
35  
36  import org.codehaus.activemq.ActiveMQConnection;
37  
38  
39  /***
40   * @version $Revision: 1.4 $
41   */
42  public class ManagedConnectionTest extends TestCase {
43      
44      
45      private static final String DEFAULT_HOST = "vm://localhost";
46      
47      private ConnectionManagerAdapter connectionManager = new ConnectionManagerAdapter();
48      private ActiveMQManagedConnectionFactory managedConnectionFactory;
49      private ConnectionFactory connectionFactory;
50      private JMSConnectionProxy connection;
51      private ActiveMQManagedConnection managedConnection;
52      
53      /***
54       * @see junit.framework.TestCase#setUp()
55       */
56      protected void setUp() throws Exception {
57          
58      	ActiveMQResourceAdapter adapter = new ActiveMQResourceAdapter(); 
59      	adapter.setServerUrl(DEFAULT_HOST);
60      	adapter.setUserName(ActiveMQConnection.DEFAULT_USER);
61      	adapter.setPassword(ActiveMQConnection.DEFAULT_PASSWORD);
62      	adapter.start(new BootstrapContext(){
63  			public WorkManager getWorkManager() {
64  				return null;
65  			}
66  			public XATerminator getXATerminator() {
67  				return null;
68  			}
69  
70  			public Timer createTimer() throws UnavailableException {
71  				return null;
72  			}
73  		});
74      	
75          managedConnectionFactory = new ActiveMQManagedConnectionFactory();
76          managedConnectionFactory.setResourceAdapter(adapter);
77      	    	
78          connectionFactory = (ConnectionFactory) managedConnectionFactory.createConnectionFactory(connectionManager);
79          connection = (JMSConnectionProxy) connectionFactory.createConnection();
80          managedConnection = connection.getManagedConnection();
81          
82      }
83      
84      public void testConnectionCloseEvent() throws ResourceException, JMSException {
85          
86          final boolean test[] = new boolean[]{false};
87          connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() {
88                  public void connectionClosed(ConnectionEvent arg0) {
89                      test[0]=true;
90                  }
91              });
92          connection.close();
93          assertTrue( test[0] );
94      }
95      
96      public void testLocalTransactionCommittedEvent() throws ResourceException, JMSException {
97  
98          final boolean test[] = new boolean[]{false};
99          connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() {
100                 public void localTransactionCommitted(ConnectionEvent arg0) {
101                     test[0]=true;
102                 }
103             });
104         Session session = connection.createSession(true,0);
105         doWork(session);
106         session.commit();
107         
108         assertTrue( test[0] );
109         
110     }
111     
112     public void testLocalTransactionRollbackEvent() throws ResourceException, JMSException {
113         
114         final boolean test[] = new boolean[]{false};
115         connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() {
116 	            public void localTransactionRolledback(ConnectionEvent arg0) {
117 	                test[0]=true;
118 	            }
119             });
120         Session session = connection.createSession(true,0);
121         doWork(session);
122         session.rollback();
123         
124         assertTrue( test[0] );
125     }    
126 
127     public void testLocalTransactionStartedEvent() throws ResourceException, JMSException {
128         
129         final boolean test[] = new boolean[]{false};
130         connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() {
131                 public void localTransactionStarted(ConnectionEvent arg0) {
132                     test[0]=true;
133                 }
134             });
135         
136         // Send a message...  that should start a local transaction...
137 	    Session session = connection.createSession(true,0);
138         doWork(session);
139         
140         assertTrue( test[0] );
141     }
142 
143     /***
144      * A managed connection that has been clean up should throw exceptions
145      * when it used.
146      */
147     public void testCleanup() throws ResourceException, JMSException {
148         
149         // Do some work and close it...
150 	    Session session = connection.createSession(true,0);
151         doWork(session);
152         connection.close();
153         try {
154 	        // This should throw expection        
155 	        doWork(session);
156 	        fail("Using a session after the connection is closed should throw exception.");
157         } catch ( JMSException e) {
158         }
159     }
160 
161     public void testSessionCloseIndependance() throws ResourceException, JMSException {
162         
163 	    Session session1 = connection.createSession(true,0);
164 	    Session session2 = connection.createSession(true,0);
165         assertTrue( session1!=session2 );
166 	    
167 	    doWork(session1);
168         session1.close();
169         try {        	
170 	        // This should throw expection
171 	        doWork(session1);
172 	        fail("Using a session after the connection is closed should throw exception.");
173         } catch ( JMSException e) {
174         }
175         
176         // Make sure that closing session 1 does not close session 2
177         doWork(session2);
178         session2.close();
179         try {        	
180 	        // This should throw expection
181 	        doWork(session2);
182 	        fail("Using a session after the connection is closed should throw exception.");
183         } catch ( JMSException e) {
184         }
185     }
186 
187     /***
188      * Does some work so that we can test commit/rollback etc.
189      * @throws JMSException
190      */
191     public void doWork(Session session) throws JMSException {
192 	    Queue t = session.createQueue("TEST");
193 	    MessageProducer producer = session.createProducer(t);
194 	    producer.send(session.createTextMessage("test message."));
195     }
196 
197     
198 }