KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > ra > ManagedConnectionTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq.ra;
19
20 import java.util.Timer JavaDoc;
21
22 import javax.jms.ConnectionFactory JavaDoc;
23 import javax.jms.JMSException JavaDoc;
24 import javax.jms.MessageProducer JavaDoc;
25 import javax.jms.Queue JavaDoc;
26 import javax.jms.QueueConnection JavaDoc;
27 import javax.jms.QueueConnectionFactory JavaDoc;
28 import javax.jms.Session JavaDoc;
29 import javax.jms.TopicConnection JavaDoc;
30 import javax.jms.TopicConnectionFactory JavaDoc;
31 import javax.resource.ResourceException JavaDoc;
32 import javax.resource.spi.BootstrapContext JavaDoc;
33 import javax.resource.spi.ConnectionEvent JavaDoc;
34 import javax.resource.spi.UnavailableException JavaDoc;
35 import javax.resource.spi.XATerminator JavaDoc;
36 import javax.resource.spi.work.WorkManager JavaDoc;
37
38 import junit.framework.TestCase;
39
40 import org.apache.activemq.ActiveMQConnectionFactory;
41 import org.apache.activemq.ra.ActiveMQManagedConnection;
42 import org.apache.activemq.ra.ActiveMQManagedConnectionFactory;
43 import org.apache.activemq.ra.ActiveMQResourceAdapter;
44 import org.apache.activemq.ra.ManagedConnectionProxy;
45
46
47 /**
48  * @version $Revision$
49  */

50 public class ManagedConnectionTest extends TestCase {
51     
52     
53     private static final String JavaDoc DEFAULT_HOST = "vm://localhost";
54     
55     private ConnectionManagerAdapter connectionManager = new ConnectionManagerAdapter();
56     private ActiveMQManagedConnectionFactory managedConnectionFactory;
57     private ConnectionFactory JavaDoc connectionFactory;
58     private ManagedConnectionProxy connection;
59     private ActiveMQManagedConnection managedConnection;
60     
61     /**
62      * @see junit.framework.TestCase#setUp()
63      */

64     protected void setUp() throws Exception JavaDoc {
65         
66         ActiveMQResourceAdapter adapter = new ActiveMQResourceAdapter();
67         adapter.setServerUrl(DEFAULT_HOST);
68         adapter.setUserName(ActiveMQConnectionFactory.DEFAULT_USER);
69         adapter.setPassword(ActiveMQConnectionFactory.DEFAULT_PASSWORD);
70         adapter.start(new BootstrapContext JavaDoc(){
71             public WorkManager JavaDoc getWorkManager() {
72                 return null;
73             }
74             public XATerminator JavaDoc getXATerminator() {
75                 return null;
76             }
77
78             public Timer JavaDoc createTimer() throws UnavailableException JavaDoc {
79                 return null;
80             }
81         });
82         
83         managedConnectionFactory = new ActiveMQManagedConnectionFactory();
84         managedConnectionFactory.setResourceAdapter(adapter);
85                 
86         connectionFactory = (ConnectionFactory JavaDoc) managedConnectionFactory.createConnectionFactory(connectionManager);
87         connection = (ManagedConnectionProxy) connectionFactory.createConnection();
88         managedConnection = connection.getManagedConnection();
89         
90     }
91     
92     public void testConnectionCloseEvent() throws ResourceException JavaDoc, JMSException JavaDoc {
93         
94         final boolean test[] = new boolean[]{false};
95         connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() {
96                 public void connectionClosed(ConnectionEvent JavaDoc arg0) {
97                     test[0]=true;
98                 }
99             });
100         connection.close();
101         assertTrue( test[0] );
102     }
103     
104     public void testLocalTransactionCommittedEvent() throws ResourceException JavaDoc, JMSException JavaDoc {
105
106         final boolean test[] = new boolean[]{false};
107         connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() {
108                 public void localTransactionCommitted(ConnectionEvent JavaDoc arg0) {
109                     test[0]=true;
110                 }
111             });
112         
113         managedConnection.getLocalTransaction().begin();
114         Session JavaDoc session = connection.createSession(true,0);
115         
116         doWork(session);
117         session.commit();
118         
119         assertTrue( test[0] );
120         
121     }
122     
123     public void testLocalTransactionRollbackEvent() throws ResourceException JavaDoc, JMSException JavaDoc {
124         
125         final boolean test[] = new boolean[]{false};
126         connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() {
127                 public void localTransactionRolledback(ConnectionEvent JavaDoc arg0) {
128                     test[0]=true;
129                 }
130             });
131         managedConnection.getLocalTransaction().begin();
132         Session JavaDoc session = connection.createSession(true,0);
133         doWork(session);
134         session.rollback();
135         
136         assertTrue( test[0] );
137     }
138
139     public void testLocalTransactionStartedEvent() throws ResourceException JavaDoc, JMSException JavaDoc {
140         
141         final boolean test[] = new boolean[]{false};
142         connectionManager.addConnectionEventListener(new ConnectionEventListenerAdapter() {
143                 public void localTransactionStarted(ConnectionEvent JavaDoc arg0) {
144                     test[0]=true;
145                 }
146             });
147         
148         // Begin the transaction... that should kick off the event.
149
managedConnection.getLocalTransaction().begin();
150         Session JavaDoc session = connection.createSession(true,0);
151         doWork(session);
152         
153         assertTrue( test[0] );
154     }
155
156     /**
157      * A managed connection that has been clean up should throw exceptions
158      * when it used.
159      */

160     public void testCleanup() throws ResourceException JavaDoc, JMSException JavaDoc {
161         
162         // Do some work and close it...
163
Session JavaDoc session = connection.createSession(true,0);
164         doWork(session);
165         connection.close();
166         try {
167             // This should throw expection
168
doWork(session);
169             fail("Using a session after the connection is closed should throw exception.");
170         } catch ( JMSException JavaDoc e) {
171         }
172     }
173
174     public void testSessionCloseIndependance() throws ResourceException JavaDoc, JMSException JavaDoc {
175         
176         Session JavaDoc session1 = connection.createSession(true,0);
177         Session JavaDoc session2 = connection.createSession(true,0);
178         assertTrue( session1!=session2 );
179         
180         doWork(session1);
181         session1.close();
182         try {
183             // This should throw expection
184
doWork(session1);
185             fail("Using a session after the connection is closed should throw exception.");
186         } catch ( JMSException JavaDoc e) {
187         }
188         
189         // Make sure that closing session 1 does not close session 2
190
doWork(session2);
191         session2.close();
192         try {
193             // This should throw expection
194
doWork(session2);
195             fail("Using a session after the connection is closed should throw exception.");
196         } catch ( JMSException JavaDoc e) {
197         }
198     }
199
200     /**
201      * Does some work so that we can test commit/rollback etc.
202      * @throws JMSException
203      */

204     public void doWork(Session JavaDoc session) throws JMSException JavaDoc {
205         Queue JavaDoc t = session.createQueue("TEST");
206         MessageProducer JavaDoc producer = session.createProducer(t);
207         producer.send(session.createTextMessage("test message."));
208     }
209
210     public void testImplementsQueueAndTopicConnection() throws Exception JavaDoc {
211         QueueConnection JavaDoc qc = ((QueueConnectionFactory JavaDoc)connectionFactory).createQueueConnection();
212         assertNotNull(qc);
213         TopicConnection JavaDoc tc = ((TopicConnectionFactory JavaDoc)connectionFactory).createTopicConnection();
214         assertNotNull(tc);
215     }
216
217     public void testSelfEquality() {
218         assertEquality(managedConnection, managedConnection);
219     }
220
221     public void testSamePropertiesButNotEqual() throws Exception JavaDoc {
222         ManagedConnectionProxy newConnection = (ManagedConnectionProxy) connectionFactory.createConnection();
223         assertNonEquality(managedConnection, newConnection.getManagedConnection());
224     }
225
226     private void assertEquality(ActiveMQManagedConnection leftCon, ActiveMQManagedConnection rightCon) {
227         assertTrue("ActiveMQManagedConnection are not equal", leftCon.equals(rightCon));
228         assertTrue("ActiveMQManagedConnection are not equal", rightCon.equals(leftCon));
229         assertTrue("HashCodes are not equal", leftCon.hashCode() == rightCon.hashCode());
230     }
231
232     private void assertNonEquality(ActiveMQManagedConnection leftCon, ActiveMQManagedConnection rightCon) {
233         assertFalse("ActiveMQManagedConnection are equal", leftCon.equals(rightCon));
234         assertFalse("ActiveMQManagedConnection are equal", rightCon.equals(leftCon));
235         assertFalse("HashCodes are equal", leftCon.hashCode() == rightCon.hashCode());
236     }
237
238 }
239
Popular Tags