KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmq > test > RecoveryQueueSendUnitTestCase


1 /*
2 * JBoss, Home of Professional Open Source
3 * Copyright 2006, JBoss Inc., and individual contributors as indicated
4 * by the @authors tag. See the copyright.txt in the distribution for a
5 * full listing of individual contributors.
6 *
7 * This is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU Lesser General Public License as
9 * published by the Free Software Foundation; either version 2.1 of
10 * the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this software; if not, write to the Free
19 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21 */

22 package org.jboss.test.jbossmq.test;
23
24 import java.util.List JavaDoc;
25
26 import javax.jms.Queue JavaDoc;
27 import javax.naming.InitialContext JavaDoc;
28 import javax.transaction.xa.XAResource JavaDoc;
29 import javax.transaction.xa.Xid JavaDoc;
30
31 /**
32  * Tests for recovery
33  *
34  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
35  * @version <tt>$Revision: 45399 $</tt>
36  */

37 public class RecoveryQueueSendUnitTestCase extends RecoveryTest
38 {
39    static String JavaDoc RECOVERY_QUEUE = "queue/recoveryQueue";
40
41    Queue JavaDoc recoveryQueue;
42    
43    public RecoveryQueueSendUnitTestCase(String JavaDoc name) throws Exception JavaDoc
44    {
45       super(name);
46    }
47
48    /**
49     * Send a message and prepare the transaction.
50     *
51     * Assert the xid appears in the recovery list
52     * Assert the message cannot be received
53     */

54    public void testSendPrepareShowsInRecovery() throws Exception JavaDoc
55    {
56       deployRecoveryService();
57       try
58       {
59          reset(recoveryQueue);
60          JMSClient client = new JMSClient(recoveryQueue);
61          try
62          {
63             Xid JavaDoc xid = new MyXid();
64             client.enlist(xid);
65             client.sendMessage("Hello");
66             client.delist(xid);
67             int result = client.prepare(xid);
68             try
69             {
70                assertEquals(XAResource.XA_OK, result);
71                List JavaDoc xids = client.recover();
72                assertEquals(1, xids.size());
73                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
74             }
75             finally
76             {
77                client.rollback(xid);
78             }
79          }
80          finally
81          {
82             client.close();
83          }
84          assertEquals(0, clearDestination(recoveryQueue));
85       }
86       finally
87       {
88          undeployRecoveryService();
89       }
90    }
91    
92    /**
93     * Send a message and prepare the transaction, but
94     * then roll it back
95     *
96     * Assert the xid doesn't appears in the recovery list
97     * Assert the message cannot be received
98     */

99    public void testSendPrepareRollbackDoesntShowInRecovery() throws Exception JavaDoc
100    {
101       deployRecoveryService();
102       try
103       {
104          reset(recoveryQueue);
105          JMSClient client = new JMSClient(recoveryQueue);
106          try
107          {
108             Xid JavaDoc xid = new MyXid();
109             client.enlist(xid);
110             client.sendMessage("Hello");
111             client.delist(xid);
112             int result = client.prepare(xid);
113             try
114             {
115                assertEquals(XAResource.XA_OK, result);
116                List JavaDoc xids = client.recover();
117                assertEquals(1, xids.size());
118                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
119                client.rollback(xid);
120                xids = client.recover();
121                assertEquals(0, xids.size());
122             }
123             finally
124             {
125                try
126                {
127                   client.rollback(xid);
128                }
129                catch (Exception JavaDoc ignored)
130                {
131                }
132             }
133          }
134          finally
135          {
136             client.close();
137          }
138          assertEquals(0, clearDestination(recoveryQueue));
139       }
140       finally
141       {
142          undeployRecoveryService();
143       }
144    }
145    
146    /**
147     * Send a message and prepare the transaction, but
148     * then forget it
149     *
150     * Assert the xid doesn't appears in the recovery list
151     * Assert the message cannot be received
152     */

153    public void testSendPrepareForgetDoesntShowInRecovery() throws Exception JavaDoc
154    {
155       deployRecoveryService();
156       try
157       {
158          reset(recoveryQueue);
159          JMSClient client = new JMSClient(recoveryQueue);
160          try
161          {
162             Xid JavaDoc xid = new MyXid();
163             client.enlist(xid);
164             client.sendMessage("Hello");
165             client.delist(xid);
166             int result = client.prepare(xid);
167             try
168             {
169                assertEquals(XAResource.XA_OK, result);
170                List JavaDoc xids = client.recover();
171                assertEquals(1, xids.size());
172                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
173                client.forget(xid);
174                xids = client.recover();
175                assertEquals(0, xids.size());
176             }
177             finally
178             {
179                try
180                {
181                   client.rollback(xid);
182                }
183                catch (Exception JavaDoc ignored)
184                {
185                }
186             }
187          }
188          finally
189          {
190             client.close();
191          }
192          assertEquals(0, clearDestination(recoveryQueue));
193       }
194       finally
195       {
196          undeployRecoveryService();
197       }
198    }
199    
200    /**
201     * Send a message and prepare the transaction, then commit it.
202     *
203     * Assert the xid doesn't appears in the recovery list
204     * Assert the message can be received
205     */

206    public void testSendPrepareCommitDoesntShowInRecovery() throws Exception JavaDoc
207    {
208       deployRecoveryService();
209       try
210       {
211          reset(recoveryQueue);
212          JMSClient client = new JMSClient(recoveryQueue);
213          try
214          {
215             Xid JavaDoc xid = new MyXid();
216             client.enlist(xid);
217             client.sendMessage("Hello");
218             client.delist(xid);
219             int result = client.prepare(xid);
220             try
221             {
222                assertEquals(XAResource.XA_OK, result);
223                List JavaDoc xids = client.recover();
224                assertEquals(1, xids.size());
225                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
226                client.commit(xid);
227                xids = client.recover();
228                assertEquals(0, xids.size());
229             }
230             finally
231             {
232                try
233                {
234                   client.rollback(xid);
235                }
236                catch (Exception JavaDoc ignored)
237                {
238                }
239             }
240          }
241          finally
242          {
243             client.close();
244          }
245          assertEquals(1, clearDestination(recoveryQueue));
246       }
247       finally
248       {
249          undeployRecoveryService();
250       }
251    }
252    
253    /**
254     * Send a message and prepare the transaction on one connection
255     * then commit it on another connection
256     *
257     * Assert the xid appears in the recovery list before the commit
258     * Assert the message cannot be received before the commit
259     * Assert the xid doesn't appear in the recovery list after the commit
260     * Assert the message can be received after the commit
261     */

262    public void testSendPrepareCommitDifferentConnection() throws Exception JavaDoc
263    {
264       deployRecoveryService();
265       try
266       {
267          reset(recoveryQueue);
268          Xid JavaDoc xid = new MyXid();
269          JMSClient client = new JMSClient(recoveryQueue);
270          try
271          {
272             client.enlist(xid);
273             client.sendMessage("Hello");
274             client.delist(xid);
275             int result = client.prepare(xid);
276             assertEquals(XAResource.XA_OK, result);
277             List JavaDoc xids = client.recover();
278             assertEquals(1, xids.size());
279             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
280          }
281          finally
282          {
283             client.close();
284          }
285          assertEquals(0, clearDestination(recoveryQueue));
286
287          client = new JMSClient(recoveryQueue);
288          try
289          {
290             List JavaDoc xids = client.recover();
291             assertEquals(1, xids.size());
292             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
293             client.commit(xid);
294             xids = client.recover();
295             assertEquals(0, xids.size());
296          }
297          finally
298          {
299             client.close();
300          }
301          assertEquals(1, clearDestination(recoveryQueue));
302       }
303       finally
304       {
305          undeployRecoveryService();
306       }
307    }
308    
309    /**
310     * Send a message and prepare the transaction on one connection
311     * then roll it back on another connection
312     *
313     * Assert the xid appears in the recovery list before the rollback
314     * Assert the message cannot be received before the rollback
315     * Assert the xid doesn't appear in the recovery list after the rollback
316     * Assert the message cannot be received after the rollback
317     */

318    public void testSendPrepareRollbackDifferentConnection() throws Exception JavaDoc
319    {
320       deployRecoveryService();
321       try
322       {
323          reset(recoveryQueue);
324          Xid JavaDoc xid = new MyXid();
325          JMSClient client = new JMSClient(recoveryQueue);
326          try
327          {
328             client.enlist(xid);
329             client.sendMessage("Hello");
330             client.delist(xid);
331             int result = client.prepare(xid);
332             assertEquals(XAResource.XA_OK, result);
333             List JavaDoc xids = client.recover();
334             assertEquals(1, xids.size());
335             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
336          }
337          finally
338          {
339             client.close();
340          }
341          assertEquals(0, clearDestination(recoveryQueue));
342
343          client = new JMSClient(recoveryQueue);
344          try
345          {
346             List JavaDoc xids = client.recover();
347             assertEquals(1, xids.size());
348             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
349             client.rollback(xid);
350             xids = client.recover();
351             assertEquals(0, xids.size());
352          }
353          finally
354          {
355             client.close();
356          }
357          assertEquals(0, clearDestination(recoveryQueue));
358       }
359       finally
360       {
361          undeployRecoveryService();
362       }
363    }
364    
365    /**
366     * Send a message and prepare the transaction on one connection
367     * then forget it on another connection
368     *
369     * Assert the xid appears in the recovery list before the forget
370     * Assert the message cannot be received before the forget
371     * Assert the xid doesn't appear in the recovery list after the forget
372     * Assert the message cannot be received after the forget
373     */

374    public void testSendPrepareForgetDifferentConnection() throws Exception JavaDoc
375    {
376       deployRecoveryService();
377       try
378       {
379          reset(recoveryQueue);
380          Xid JavaDoc xid = new MyXid();
381          JMSClient client = new JMSClient(recoveryQueue);
382          try
383          {
384             client.enlist(xid);
385             client.sendMessage("Hello");
386             client.delist(xid);
387             int result = client.prepare(xid);
388             assertEquals(XAResource.XA_OK, result);
389             List JavaDoc xids = client.recover();
390             assertEquals(1, xids.size());
391             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
392          }
393          finally
394          {
395             client.close();
396          }
397          assertEquals(0, clearDestination(recoveryQueue));
398
399          client = new JMSClient(recoveryQueue);
400          try
401          {
402             List JavaDoc xids = client.recover();
403             assertEquals(1, xids.size());
404             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
405             client.forget(xid);
406             xids = client.recover();
407             assertEquals(0, xids.size());
408          }
409          finally
410          {
411             client.close();
412          }
413          assertEquals(0, clearDestination(recoveryQueue));
414       }
415       finally
416       {
417          undeployRecoveryService();
418       }
419    }
420    
421    /**
422     * Send a message and prepare the transaction on one connection
423     * then commit it on another connection with a restart.
424     *
425     * Assert the xid appears in the recovery list before the commit
426     * Assert the message cannot be received before the commit
427     * Assert the xid doesn't appear in the recovery list after the commit
428     * Assert the message can be received after the commit
429     */

430    public void testSendPrepareCommitDifferentConnectionRestart() throws Exception JavaDoc
431    {
432       deployRecoveryService();
433       try
434       {
435          reset(recoveryQueue);
436          Xid JavaDoc xid = new MyXid();
437          JMSClient client = new JMSClient(recoveryQueue);
438          try
439          {
440             client.enlist(xid);
441             client.sendMessage("Hello");
442             client.delist(xid);
443             int result = client.prepare(xid);
444             assertEquals(XAResource.XA_OK, result);
445             List JavaDoc xids = client.recover();
446             assertEquals(1, xids.size());
447             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
448          }
449          finally
450          {
451             client.close();
452          }
453          assertEquals(0, clearDestination(recoveryQueue));
454
455          restartRecoveryService();
456
457          assertEquals(0, clearDestination(recoveryQueue));
458          
459          client = new JMSClient(recoveryQueue);
460          try
461          {
462             List JavaDoc xids = client.recover();
463             assertEquals(1, xids.size());
464             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
465             client.commit(xid);
466             xids = client.recover();
467             assertEquals(0, xids.size());
468          }
469          finally
470          {
471             client.close();
472          }
473          assertEquals(1, clearDestination(recoveryQueue));
474       }
475       finally
476       {
477          undeployRecoveryService();
478       }
479    }
480    
481    /**
482     * Send a message and prepare the transaction on one connection
483     * then roll it back on another connection with a restart
484     *
485     * Assert the xid appears in the recovery list before the rollback
486     * Assert the message cannot be received before the rollback
487     * Assert the xid doesn't appear in the recovery list after the rollback
488     * Assert the message cannot be received after the rollback
489     */

490    public void testSendPrepareRollbackDifferentConnectionRestart() throws Exception JavaDoc
491    {
492       deployRecoveryService();
493       try
494       {
495          reset(recoveryQueue);
496          Xid JavaDoc xid = new MyXid();
497          JMSClient client = new JMSClient(recoveryQueue);
498          try
499          {
500             client.enlist(xid);
501             client.sendMessage("Hello");
502             client.delist(xid);
503             int result = client.prepare(xid);
504             assertEquals(XAResource.XA_OK, result);
505             List JavaDoc xids = client.recover();
506             assertEquals(1, xids.size());
507             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
508          }
509          finally
510          {
511             client.close();
512          }
513          assertEquals(0, clearDestination(recoveryQueue));
514
515          restartRecoveryService();
516          assertEquals(0, clearDestination(recoveryQueue));
517          
518          client = new JMSClient(recoveryQueue);
519          try
520          {
521             List JavaDoc xids = client.recover();
522             assertEquals(1, xids.size());
523             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
524             client.rollback(xid);
525             xids = client.recover();
526             assertEquals(0, xids.size());
527          }
528          finally
529          {
530             client.close();
531          }
532          assertEquals(0, clearDestination(recoveryQueue));
533       }
534       finally
535       {
536          undeployRecoveryService();
537       }
538    }
539    
540    /**
541     * Send a message and prepare the transaction on one connection
542     * then forget it on another connection with a restart
543     *
544     * Assert the xid appears in the recovery list before the forget
545     * Assert the message cannot be received before the forget
546     * Assert the xid doesn't appear in the recovery list after the forget
547     * Assert the message cannot be received after the forget
548     */

549    public void testSendPrepareForgetDifferentConnectionRestart() throws Exception JavaDoc
550    {
551       deployRecoveryService();
552       try
553       {
554          reset(recoveryQueue);
555          Xid JavaDoc xid = new MyXid();
556          JMSClient client = new JMSClient(recoveryQueue);
557          try
558          {
559             client.enlist(xid);
560             client.sendMessage("Hello");
561             client.delist(xid);
562             int result = client.prepare(xid);
563             assertEquals(XAResource.XA_OK, result);
564             List JavaDoc xids = client.recover();
565             assertEquals(1, xids.size());
566             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
567          }
568          finally
569          {
570             client.close();
571          }
572          assertEquals(0, clearDestination(recoveryQueue));
573
574          restartRecoveryService();
575          assertEquals(0, clearDestination(recoveryQueue));
576          
577          client = new JMSClient(recoveryQueue);
578          try
579          {
580             List JavaDoc xids = client.recover();
581             assertEquals(1, xids.size());
582             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
583             client.forget(xid);
584             xids = client.recover();
585             assertEquals(0, xids.size());
586          }
587          finally
588          {
589             client.close();
590          }
591          assertEquals(0, clearDestination(recoveryQueue));
592       }
593       finally
594       {
595          undeployRecoveryService();
596       }
597    }
598    
599    protected void deployRecoveryService() throws Exception JavaDoc
600    {
601       super.deployRecoveryService();
602       InitialContext JavaDoc ctx = getInitialContext();
603       recoveryQueue = (Queue JavaDoc) ctx.lookup(RECOVERY_QUEUE);
604    }
605 }
606
Popular Tags