KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jms > connection > JmsTransactionManagerTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.springframework.jms.connection;
17
18 import javax.jms.Connection JavaDoc;
19 import javax.jms.ConnectionFactory JavaDoc;
20 import javax.jms.JMSException JavaDoc;
21 import javax.jms.QueueConnection JavaDoc;
22 import javax.jms.QueueConnectionFactory JavaDoc;
23 import javax.jms.QueueSession JavaDoc;
24 import javax.jms.Session JavaDoc;
25 import javax.jms.TopicConnection JavaDoc;
26 import javax.jms.TopicConnectionFactory JavaDoc;
27 import javax.jms.TopicSession JavaDoc;
28
29 import junit.framework.TestCase;
30 import org.easymock.MockControl;
31
32 import org.springframework.jms.core.JmsTemplate;
33 import org.springframework.jms.core.JmsTemplate102;
34 import org.springframework.jms.core.SessionCallback;
35 import org.springframework.transaction.TransactionDefinition;
36 import org.springframework.transaction.TransactionStatus;
37 import org.springframework.transaction.UnexpectedRollbackException;
38 import org.springframework.transaction.support.DefaultTransactionDefinition;
39 import org.springframework.transaction.support.TransactionCallbackWithoutResult;
40 import org.springframework.transaction.support.TransactionSynchronizationManager;
41 import org.springframework.transaction.support.TransactionTemplate;
42
43 /**
44  * @author Juergen Hoeller
45  * @since 26.07.2004
46  */

47 public class JmsTransactionManagerTests extends TestCase {
48
49     public void testTransactionCommit() throws JMSException JavaDoc {
50         MockControl cfControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
51         ConnectionFactory JavaDoc cf = (ConnectionFactory JavaDoc) cfControl.getMock();
52         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
53         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
54         MockControl sessionControl = MockControl.createControl(Session JavaDoc.class);
55         final Session JavaDoc session = (Session JavaDoc) sessionControl.getMock();
56
57         cf.createConnection();
58         cfControl.setReturnValue(con, 1);
59         con.createSession(true, Session.AUTO_ACKNOWLEDGE);
60         conControl.setReturnValue(session, 1);
61         session.commit();
62         sessionControl.setVoidCallable(1);
63         session.close();
64         sessionControl.setVoidCallable(1);
65         con.close();
66         conControl.setVoidCallable(1);
67
68         sessionControl.replay();
69         conControl.replay();
70         cfControl.replay();
71
72         JmsTransactionManager tm = new JmsTransactionManager(cf);
73         TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
74         JmsTemplate jt = new JmsTemplate(cf);
75         jt.execute(new SessionCallback() {
76             public Object JavaDoc doInJms(Session JavaDoc sess) {
77                 assertTrue(sess == session);
78                 return null;
79             }
80         });
81         tm.commit(ts);
82
83         sessionControl.verify();
84         conControl.verify();
85         cfControl.verify();
86     }
87
88     public void testTransactionRollback() throws JMSException JavaDoc {
89         MockControl cfControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
90         ConnectionFactory JavaDoc cf = (ConnectionFactory JavaDoc) cfControl.getMock();
91         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
92         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
93         MockControl sessionControl = MockControl.createControl(Session JavaDoc.class);
94         final Session JavaDoc session = (Session JavaDoc) sessionControl.getMock();
95
96         cf.createConnection();
97         cfControl.setReturnValue(con, 1);
98         con.createSession(true, Session.AUTO_ACKNOWLEDGE);
99         conControl.setReturnValue(session, 1);
100         session.rollback();
101         sessionControl.setVoidCallable(1);
102         session.close();
103         sessionControl.setVoidCallable(1);
104         con.close();
105         conControl.setVoidCallable(1);
106
107         sessionControl.replay();
108         conControl.replay();
109         cfControl.replay();
110
111         JmsTransactionManager tm = new JmsTransactionManager(cf);
112         TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
113         JmsTemplate jt = new JmsTemplate(cf);
114         jt.execute(new SessionCallback() {
115             public Object JavaDoc doInJms(Session JavaDoc sess) {
116                 assertTrue(sess == session);
117                 return null;
118             }
119         });
120         tm.rollback(ts);
121
122         sessionControl.verify();
123         conControl.verify();
124         cfControl.verify();
125     }
126
127     public void testParticipatingTransactionWithCommit() throws JMSException JavaDoc {
128         MockControl cfControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
129         final ConnectionFactory JavaDoc cf = (ConnectionFactory JavaDoc) cfControl.getMock();
130         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
131         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
132         MockControl sessionControl = MockControl.createControl(Session JavaDoc.class);
133         final Session JavaDoc session = (Session JavaDoc) sessionControl.getMock();
134
135         cf.createConnection();
136         cfControl.setReturnValue(con, 1);
137         con.createSession(true, Session.AUTO_ACKNOWLEDGE);
138         conControl.setReturnValue(session, 1);
139         session.commit();
140         sessionControl.setVoidCallable(1);
141         session.close();
142         sessionControl.setVoidCallable(1);
143         con.close();
144         conControl.setVoidCallable(1);
145
146         sessionControl.replay();
147         conControl.replay();
148         cfControl.replay();
149
150         JmsTransactionManager tm = new JmsTransactionManager(cf);
151         TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
152         final JmsTemplate jt = new JmsTemplate(cf);
153         jt.execute(new SessionCallback() {
154             public Object JavaDoc doInJms(Session JavaDoc sess) {
155                 assertTrue(sess == session);
156                 return null;
157             }
158         });
159         TransactionTemplate tt = new TransactionTemplate(tm);
160         tt.execute(new TransactionCallbackWithoutResult() {
161             protected void doInTransactionWithoutResult(TransactionStatus status) {
162                 jt.execute(new SessionCallback() {
163                     public Object JavaDoc doInJms(Session JavaDoc sess) {
164                         assertTrue(sess == session);
165                         return null;
166                     }
167                 });
168             }
169         });
170         tm.commit(ts);
171
172         sessionControl.verify();
173         conControl.verify();
174         cfControl.verify();
175     }
176
177     public void testParticipatingTransactionWithRollbackOnly() throws JMSException JavaDoc {
178         MockControl cfControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
179         final ConnectionFactory JavaDoc cf = (ConnectionFactory JavaDoc) cfControl.getMock();
180         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
181         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
182         MockControl sessionControl = MockControl.createControl(Session JavaDoc.class);
183         final Session JavaDoc session = (Session JavaDoc) sessionControl.getMock();
184
185         cf.createConnection();
186         cfControl.setReturnValue(con, 1);
187         con.createSession(true, Session.AUTO_ACKNOWLEDGE);
188         conControl.setReturnValue(session, 1);
189         session.rollback();
190         sessionControl.setVoidCallable(1);
191         session.close();
192         sessionControl.setVoidCallable(1);
193         con.close();
194         conControl.setVoidCallable(1);
195
196         sessionControl.replay();
197         conControl.replay();
198         cfControl.replay();
199
200         JmsTransactionManager tm = new JmsTransactionManager(cf);
201         TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
202         final JmsTemplate jt = new JmsTemplate(cf);
203         jt.execute(new SessionCallback() {
204             public Object JavaDoc doInJms(Session JavaDoc sess) {
205                 assertTrue(sess == session);
206                 return null;
207             }
208         });
209         TransactionTemplate tt = new TransactionTemplate(tm);
210         tt.execute(new TransactionCallbackWithoutResult() {
211             protected void doInTransactionWithoutResult(TransactionStatus status) {
212                 jt.execute(new SessionCallback() {
213                     public Object JavaDoc doInJms(Session JavaDoc sess) {
214                         assertTrue(sess == session);
215                         return null;
216                     }
217                 });
218                 status.setRollbackOnly();
219             }
220         });
221         try {
222             tm.commit(ts);
223             fail("Should have thrown UnexpectedRollbackException");
224         }
225         catch (UnexpectedRollbackException ex) {
226             // expected
227
}
228
229         sessionControl.verify();
230         conControl.verify();
231         cfControl.verify();
232     }
233
234     public void testSuspendedTransaction() throws JMSException JavaDoc {
235         MockControl cfControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
236         final ConnectionFactory JavaDoc cf = (ConnectionFactory JavaDoc) cfControl.getMock();
237         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
238         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
239         MockControl sessionControl = MockControl.createControl(Session JavaDoc.class);
240         final Session JavaDoc session = (Session JavaDoc) sessionControl.getMock();
241         MockControl session2Control = MockControl.createControl(Session JavaDoc.class);
242         final Session JavaDoc session2 = (Session JavaDoc) session2Control.getMock();
243
244         cf.createConnection();
245         cfControl.setReturnValue(con, 2);
246         con.createSession(true, Session.AUTO_ACKNOWLEDGE);
247         conControl.setReturnValue(session, 1);
248         con.createSession(false, Session.AUTO_ACKNOWLEDGE);
249         conControl.setReturnValue(session2, 1);
250         session.commit();
251         sessionControl.setVoidCallable(1);
252         session.close();
253         sessionControl.setVoidCallable(1);
254         session2.close();
255         session2Control.setVoidCallable(1);
256         con.close();
257         conControl.setVoidCallable(2);
258
259         sessionControl.replay();
260         conControl.replay();
261         cfControl.replay();
262
263         JmsTransactionManager tm = new JmsTransactionManager(cf);
264         TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
265         final JmsTemplate jt = new JmsTemplate(cf);
266         jt.execute(new SessionCallback() {
267             public Object JavaDoc doInJms(Session JavaDoc sess) {
268                 assertTrue(sess == session);
269                 return null;
270             }
271         });
272         TransactionTemplate tt = new TransactionTemplate(tm);
273         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_NOT_SUPPORTED);
274         tt.execute(new TransactionCallbackWithoutResult() {
275             protected void doInTransactionWithoutResult(TransactionStatus status) {
276                 jt.execute(new SessionCallback() {
277                     public Object JavaDoc doInJms(Session JavaDoc sess) {
278                         assertTrue(sess != session);
279                         return null;
280                     }
281                 });
282             }
283         });
284         jt.execute(new SessionCallback() {
285             public Object JavaDoc doInJms(Session JavaDoc sess) {
286                 assertTrue(sess == session);
287                 return null;
288             }
289         });
290         tm.commit(ts);
291
292         sessionControl.verify();
293         conControl.verify();
294         cfControl.verify();
295     }
296
297     public void testNestedTransaction() throws JMSException JavaDoc {
298         MockControl cfControl = MockControl.createControl(ConnectionFactory JavaDoc.class);
299         final ConnectionFactory JavaDoc cf = (ConnectionFactory JavaDoc) cfControl.getMock();
300         MockControl conControl = MockControl.createControl(Connection JavaDoc.class);
301         Connection JavaDoc con = (Connection JavaDoc) conControl.getMock();
302         MockControl sessionControl = MockControl.createControl(Session JavaDoc.class);
303         final Session JavaDoc session = (Session JavaDoc) sessionControl.getMock();
304         MockControl session2Control = MockControl.createControl(Session JavaDoc.class);
305         final Session JavaDoc session2 = (Session JavaDoc) session2Control.getMock();
306
307         cf.createConnection();
308         cfControl.setReturnValue(con, 2);
309         con.createSession(true, Session.AUTO_ACKNOWLEDGE);
310         conControl.setReturnValue(session, 1);
311         con.createSession(true, Session.AUTO_ACKNOWLEDGE);
312         conControl.setReturnValue(session2, 1);
313         session.commit();
314         sessionControl.setVoidCallable(1);
315         session2.commit();
316         session2Control.setVoidCallable(1);
317         session.close();
318         sessionControl.setVoidCallable(1);
319         session2.close();
320         session2Control.setVoidCallable(1);
321         con.close();
322         conControl.setVoidCallable(2);
323
324         sessionControl.replay();
325         conControl.replay();
326         cfControl.replay();
327
328         JmsTransactionManager tm = new JmsTransactionManager(cf);
329         TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
330         final JmsTemplate jt = new JmsTemplate(cf);
331         jt.execute(new SessionCallback() {
332             public Object JavaDoc doInJms(Session JavaDoc sess) {
333                 assertTrue(sess == session);
334                 return null;
335             }
336         });
337         TransactionTemplate tt = new TransactionTemplate(tm);
338         tt.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
339         tt.execute(new TransactionCallbackWithoutResult() {
340             protected void doInTransactionWithoutResult(TransactionStatus status) {
341                 jt.execute(new SessionCallback() {
342                     public Object JavaDoc doInJms(Session JavaDoc sess) {
343                         assertTrue(sess != session);
344                         return null;
345                     }
346                 });
347             }
348         });
349         jt.execute(new SessionCallback() {
350             public Object JavaDoc doInJms(Session JavaDoc sess) {
351                 assertTrue(sess == session);
352                 return null;
353             }
354         });
355         tm.commit(ts);
356
357         sessionControl.verify();
358         conControl.verify();
359         cfControl.verify();
360     }
361
362     public void testTransactionCommit102WithQueue() throws JMSException JavaDoc {
363         MockControl cfControl = MockControl.createControl(QueueConnectionFactory JavaDoc.class);
364         QueueConnectionFactory JavaDoc cf = (QueueConnectionFactory JavaDoc) cfControl.getMock();
365         MockControl conControl = MockControl.createControl(QueueConnection JavaDoc.class);
366         QueueConnection JavaDoc con = (QueueConnection JavaDoc) conControl.getMock();
367         MockControl sessionControl = MockControl.createControl(QueueSession JavaDoc.class);
368         final QueueSession JavaDoc session = (QueueSession JavaDoc) sessionControl.getMock();
369
370         cf.createQueueConnection();
371         cfControl.setReturnValue(con, 1);
372         con.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
373         conControl.setReturnValue(session, 1);
374         session.commit();
375         sessionControl.setVoidCallable(1);
376         session.close();
377         sessionControl.setVoidCallable(1);
378         con.close();
379         conControl.setVoidCallable(1);
380
381         sessionControl.replay();
382         conControl.replay();
383         cfControl.replay();
384
385         JmsTransactionManager tm = new JmsTransactionManager102(cf, false);
386         TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
387         JmsTemplate jt = new JmsTemplate102(cf, false);
388         jt.execute(new SessionCallback() {
389             public Object JavaDoc doInJms(Session JavaDoc sess) {
390                 assertTrue(sess == session);
391                 return null;
392             }
393         });
394         tm.commit(ts);
395
396         sessionControl.verify();
397         conControl.verify();
398         cfControl.verify();
399     }
400
401     public void testTransactionCommit102WithTopic() throws JMSException JavaDoc {
402         MockControl cfControl = MockControl.createControl(TopicConnectionFactory JavaDoc.class);
403         TopicConnectionFactory JavaDoc cf = (TopicConnectionFactory JavaDoc) cfControl.getMock();
404         MockControl conControl = MockControl.createControl(TopicConnection JavaDoc.class);
405         TopicConnection JavaDoc con = (TopicConnection JavaDoc) conControl.getMock();
406         MockControl sessionControl = MockControl.createControl(TopicSession JavaDoc.class);
407         final TopicSession JavaDoc session = (TopicSession JavaDoc) sessionControl.getMock();
408
409         cf.createTopicConnection();
410         cfControl.setReturnValue(con, 1);
411         con.createTopicSession(true, Session.AUTO_ACKNOWLEDGE);
412         conControl.setReturnValue(session, 1);
413         session.commit();
414         sessionControl.setVoidCallable(1);
415         session.close();
416         sessionControl.setVoidCallable(1);
417         con.close();
418         conControl.setVoidCallable(1);
419
420         sessionControl.replay();
421         conControl.replay();
422         cfControl.replay();
423
424         JmsTransactionManager tm = new JmsTransactionManager102(cf, true);
425         TransactionStatus ts = tm.getTransaction(new DefaultTransactionDefinition());
426         JmsTemplate jt = new JmsTemplate102(cf, true);
427         jt.execute(new SessionCallback() {
428             public Object JavaDoc doInJms(Session JavaDoc sess) {
429                 assertTrue(sess == session);
430                 return null;
431             }
432         });
433         tm.commit(ts);
434
435         sessionControl.verify();
436         conControl.verify();
437         cfControl.verify();
438     }
439
440     protected void tearDown() {
441         assertTrue(TransactionSynchronizationManager.getResourceMap().isEmpty());
442         assertFalse(TransactionSynchronizationManager.isSynchronizationActive());
443     }
444
445 }
446
Popular Tags