KickJava   Java API By Example, From Geeks To Geeks.

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


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.Topic 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 RecoveryDurableTopicReceiveUnitTestCase extends RecoveryTest
38 {
39    static String JavaDoc RECOVERY_TOPIC = "topic/recoveryDurableTopic";
40
41    Topic JavaDoc recoveryTopic;
42    
43    public RecoveryDurableTopicReceiveUnitTestCase(String JavaDoc name) throws Exception JavaDoc
44    {
45       super(name);
46    }
47
48    /**
49     * Receive a message and prepare the transaction.
50     *
51     * Assert the xid appears in the recovery list
52     * Assert the message is received after the rollback
53     */

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

108    public void testReceivePrepareRollbackDoesntShowInRecovery() throws Exception JavaDoc
109    {
110       deployRecoveryService();
111       try
112       {
113          makeSubscription(recoveryTopic);
114          try
115          {
116             reset(recoveryTopic);
117             setup(recoveryTopic, 1);
118             JMSClient client = new JMSClient(recoveryTopic);
119             try
120             {
121                Xid JavaDoc xid = new MyXid();
122                client.enlist(xid);
123                assertNotNull(client.receiveNoWait());
124                client.delist(xid);
125                int result = client.prepare(xid);
126                try
127                {
128                   assertEquals(XAResource.XA_OK, result);
129                   List JavaDoc xids = client.recover();
130                   assertEquals(1, xids.size());
131                   assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
132                   client.rollback(xid);
133                   xids = client.recover();
134                   assertEquals(0, xids.size());
135                }
136                finally
137                {
138                   try
139                   {
140                      client.rollback(xid);
141                   }
142                   catch (Exception JavaDoc ignored)
143                   {
144                   }
145                }
146             }
147             finally
148             {
149                client.close();
150             }
151             assertEquals(1, clearDestination(recoveryTopic));
152          }
153          finally
154          {
155             removeSubscription(recoveryTopic);
156          }
157       }
158       finally
159       {
160          undeployRecoveryService();
161       }
162    }
163    
164    /**
165     * Receive a message and prepare the transaction, but
166     * then forget it
167     *
168     * Assert the xid doesn't appears in the recovery list
169     * Assert the message is received after the forget
170     */

171    public void testReceivePrepareForgetDoesntShowInRecovery() throws Exception JavaDoc
172    {
173       deployRecoveryService();
174       try
175       {
176          makeSubscription(recoveryTopic);
177          try
178          {
179             reset(recoveryTopic);
180             setup(recoveryTopic, 1);
181             JMSClient client = new JMSClient(recoveryTopic);
182             try
183             {
184                Xid JavaDoc xid = new MyXid();
185                client.enlist(xid);
186                assertNotNull(client.receiveNoWait());
187                client.delist(xid);
188                int result = client.prepare(xid);
189                try
190                {
191                   assertEquals(XAResource.XA_OK, result);
192                   List JavaDoc xids = client.recover();
193                   assertEquals(1, xids.size());
194                   assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
195                   client.forget(xid);
196                   xids = client.recover();
197                   assertEquals(0, xids.size());
198                }
199                finally
200                {
201                   try
202                   {
203                      client.rollback(xid);
204                   }
205                   catch (Exception JavaDoc ignored)
206                   {
207                   }
208                }
209             }
210             finally
211             {
212                client.close();
213             }
214             assertEquals(1, clearDestination(recoveryTopic));
215          }
216          finally
217          {
218             removeSubscription(recoveryTopic);
219          }
220       }
221       finally
222       {
223          undeployRecoveryService();
224       }
225    }
226    
227    /**
228     * Receive a message and prepare the transaction, then commit it.
229     *
230     * Assert the xid doesn't appears in the recovery list
231     * Assert the message cannot be received after the commit
232     */

233    public void testReceivePrepareCommitDoesntShowInRecovery() throws Exception JavaDoc
234    {
235       deployRecoveryService();
236       try
237       {
238          makeSubscription(recoveryTopic);
239          try
240          {
241             reset(recoveryTopic);
242             setup(recoveryTopic, 1);
243             JMSClient client = new JMSClient(recoveryTopic);
244             try
245             {
246                Xid JavaDoc xid = new MyXid();
247                client.enlist(xid);
248                assertNotNull(client.receiveNoWait());
249                client.delist(xid);
250                int result = client.prepare(xid);
251                try
252                {
253                   assertEquals(XAResource.XA_OK, result);
254                   List JavaDoc xids = client.recover();
255                   assertEquals(1, xids.size());
256                   assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
257                   client.commit(xid);
258                   xids = client.recover();
259                   assertEquals(0, xids.size());
260                }
261                finally
262                {
263                   try
264                   {
265                      client.rollback(xid);
266                   }
267                   catch (Exception JavaDoc ignored)
268                   {
269                   }
270                }
271             }
272             finally
273             {
274                client.close();
275             }
276             assertEquals(0, clearDestination(recoveryTopic));
277          }
278          finally
279          {
280             removeSubscription(recoveryTopic);
281          }
282       }
283       finally
284       {
285          undeployRecoveryService();
286       }
287    }
288    
289    /**
290     * Receive a message and prepare the transaction on one connection
291     * then commit it on another connection
292     *
293     * Assert the xid appears in the recovery list before the commit
294     * Assert the message cannot be received before the commit
295     * Assert the xid doesn't appear in the recovery list after the commit
296     * Assert the message cannot be received after the commit
297     */

298    public void testReceivePrepareCommitDifferentConnection() throws Exception JavaDoc
299    {
300       deployRecoveryService();
301       try
302       {
303          makeSubscription(recoveryTopic);
304          try
305          {
306             reset(recoveryTopic);
307             setup(recoveryTopic, 1);
308             Xid JavaDoc xid = new MyXid();
309             JMSClient client = new JMSClient(recoveryTopic);
310             try
311             {
312                client.enlist(xid);
313                assertNotNull(client.receiveNoWait());
314                client.delist(xid);
315                int result = client.prepare(xid);
316                assertEquals(XAResource.XA_OK, result);
317                List JavaDoc xids = client.recover();
318                assertEquals(1, xids.size());
319                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
320             }
321             finally
322             {
323                client.close();
324             }
325
326             assertEquals(0, clearDestination(recoveryTopic));
327             
328             client = new JMSClient(recoveryTopic);
329             try
330             {
331                List JavaDoc xids = client.recover();
332                assertEquals(1, xids.size());
333                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
334                client.commit(xid);
335                xids = client.recover();
336                assertEquals(0, xids.size());
337             }
338             finally
339             {
340                client.close();
341             }
342             assertEquals(0, clearDestination(recoveryTopic));
343          }
344          finally
345          {
346             removeSubscription(recoveryTopic);
347          }
348       }
349       finally
350       {
351          undeployRecoveryService();
352       }
353    }
354    
355    /**
356     * Receive a message and prepare the transaction on one connection
357     * then roll it back on another connection
358     *
359     * Assert the xid appears in the recovery list before the rollback
360     * Assert the message cannot be received before the rollback
361     * Assert the xid doesn't appear in the recovery list after the rollback
362     * Assert the message can be received after the rollback
363     */

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

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

494    public void testReceivePrepareCommitDifferentConnectionRestart() throws Exception JavaDoc
495    {
496       deployRecoveryService();
497       try
498       {
499          makeSubscription(recoveryTopic);
500          try
501          {
502             reset(recoveryTopic);
503             setup(recoveryTopic, 1);
504             Xid JavaDoc xid = new MyXid();
505             JMSClient client = new JMSClient(recoveryTopic);
506             try
507             {
508                client.enlist(xid);
509                assertNotNull(client.receiveNoWait());
510                client.delist(xid);
511                int result = client.prepare(xid);
512                assertEquals(XAResource.XA_OK, result);
513                List JavaDoc xids = client.recover();
514                assertEquals(1, xids.size());
515                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
516             }
517             finally
518             {
519                client.close();
520             }
521
522             assertEquals(0, clearDestination(recoveryTopic));
523             
524             restartRecoveryService();
525             assertEquals(0, clearDestination(recoveryTopic));
526             
527             client = new JMSClient(recoveryTopic);
528             try
529             {
530                List JavaDoc xids = client.recover();
531                assertEquals(1, xids.size());
532                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
533                client.commit(xid);
534                xids = client.recover();
535                assertEquals(0, xids.size());
536             }
537             finally
538             {
539                client.close();
540             }
541             assertEquals(0, clearDestination(recoveryTopic));
542          }
543          finally
544          {
545             removeSubscription(recoveryTopic);
546          }
547       }
548       finally
549       {
550          undeployRecoveryService();
551       }
552    }
553    
554    /**
555     * Receive a message and prepare the transaction on one connection
556     * then roll it back on another connection with a restart
557     *
558     * Assert the xid appears in the recovery list before the rollback
559     * Assert the message cannot be received before the rollback
560     * Assert the xid doesn't appear in the recovery list after the rollback
561     * Assert the message can be received after the rollback
562     */

563    public void testReceivePrepareRollbackDifferentConnectionRestart() throws Exception JavaDoc
564    {
565       deployRecoveryService();
566       try
567       {
568          makeSubscription(recoveryTopic);
569          try
570          {
571             reset(recoveryTopic);
572             setup(recoveryTopic, 1);
573             Xid JavaDoc xid = new MyXid();
574             JMSClient client = new JMSClient(recoveryTopic);
575             try
576             {
577                client.enlist(xid);
578                assertNotNull(client.receiveNoWait());
579                client.delist(xid);
580                int result = client.prepare(xid);
581                assertEquals(XAResource.XA_OK, result);
582                List JavaDoc xids = client.recover();
583                assertEquals(1, xids.size());
584                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
585             }
586             finally
587             {
588                client.close();
589             }
590             assertEquals(0, clearDestination(recoveryTopic));
591             
592             restartRecoveryService();
593             assertEquals(0, clearDestination(recoveryTopic));
594
595             client = new JMSClient(recoveryTopic);
596             try
597             {
598                List JavaDoc xids = client.recover();
599                assertEquals(1, xids.size());
600                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
601                client.rollback(xid);
602                xids = client.recover();
603                assertEquals(0, xids.size());
604             }
605             finally
606             {
607                client.close();
608             }
609             assertEquals(1, clearDestination(recoveryTopic));
610          }
611          finally
612          {
613             removeSubscription(recoveryTopic);
614          }
615       }
616       finally
617       {
618          undeployRecoveryService();
619       }
620    }
621    
622    /**
623     * Receive a message and prepare the transaction on one connection
624     * then forget it on another connection with a restart
625     *
626     * Assert the xid appears in the recovery list before the forget
627     * Assert the message cannot be received before the forget
628     * Assert the xid doesn't appear in the recovery list after the forget
629     * Assert the message can be received after the forget
630     */

631    public void testReceivePrepareForgetDifferentConnectionRestart() throws Exception JavaDoc
632    {
633       deployRecoveryService();
634       try
635       {
636          makeSubscription(recoveryTopic);
637          try
638          {
639             reset(recoveryTopic);
640             setup(recoveryTopic, 1);
641             Xid JavaDoc xid = new MyXid();
642             JMSClient client = new JMSClient(recoveryTopic);
643             try
644             {
645                client.enlist(xid);
646                assertNotNull(client.receiveNoWait());
647                client.delist(xid);
648                int result = client.prepare(xid);
649                assertEquals(XAResource.XA_OK, result);
650                List JavaDoc xids = client.recover();
651                assertEquals(1, xids.size());
652                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
653             }
654             finally
655             {
656                client.close();
657             }
658             assertEquals(0, clearDestination(recoveryTopic));
659             
660             restartRecoveryService();
661             assertEquals(0, clearDestination(recoveryTopic));
662
663             client = new JMSClient(recoveryTopic);
664             try
665             {
666                List JavaDoc xids = client.recover();
667                assertEquals(1, xids.size());
668                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
669                client.forget(xid);
670                xids = client.recover();
671                assertEquals(0, xids.size());
672             }
673             finally
674             {
675                client.close();
676             }
677             assertEquals(1, clearDestination(recoveryTopic));
678          }
679          finally
680          {
681             removeSubscription(recoveryTopic);
682          }
683       }
684       finally
685       {
686          undeployRecoveryService();
687       }
688    }
689    
690    protected void deployRecoveryService() throws Exception JavaDoc
691    {
692       super.deployRecoveryService();
693       InitialContext JavaDoc ctx = getInitialContext();
694       recoveryTopic = (Topic JavaDoc) ctx.lookup(RECOVERY_TOPIC);
695    }
696 }
697
Popular Tags