KickJava   Java API By Example, From Geeks To Geeks.

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


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 RecoveryDurableTopicSendUnitTestCase extends RecoveryTest
38 {
39    static String JavaDoc RECOVERY_TOPIC = "topic/recoveryDurableTopic";
40
41    Topic JavaDoc recoveryTopic;
42    
43    public RecoveryDurableTopicSendUnitTestCase(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          makeSubscription(recoveryTopic);
60          try
61          {
62             reset(recoveryTopic);
63             JMSClient client = new JMSClient(recoveryTopic);
64             try
65             {
66                Xid JavaDoc xid = new MyXid();
67                client.enlist(xid);
68                client.sendMessage("Hello");
69                client.delist(xid);
70                int result = client.prepare(xid);
71                try
72                {
73                   assertEquals(XAResource.XA_OK, result);
74                   List JavaDoc xids = client.recover();
75                   assertEquals(1, xids.size());
76                   assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
77                }
78                finally
79                {
80                   client.rollback(xid);
81                }
82             }
83             finally
84             {
85                client.close();
86             }
87             assertEquals(0, clearDestination(recoveryTopic));
88          }
89          finally
90          {
91             removeSubscription(recoveryTopic);
92          }
93       }
94       finally
95       {
96          undeployRecoveryService();
97       }
98    }
99    
100    /**
101     * Send a message and prepare the transaction, but
102     * then roll it back
103     *
104     * Assert the xid doesn't appears in the recovery list
105     * Assert the message cannot be received
106     */

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

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

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

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

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

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

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

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

621    public void testSendPrepareForgetDifferentConnectionRestart() throws Exception JavaDoc
622    {
623       deployRecoveryService();
624       try
625       {
626          makeSubscription(recoveryTopic);
627          try
628          {
629             reset(recoveryTopic);
630             Xid JavaDoc xid = new MyXid();
631             JMSClient client = new JMSClient(recoveryTopic);
632             try
633             {
634                client.enlist(xid);
635                client.sendMessage("Hello");
636                client.delist(xid);
637                int result = client.prepare(xid);
638                assertEquals(XAResource.XA_OK, result);
639                List JavaDoc xids = client.recover();
640                assertEquals(1, xids.size());
641                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
642             }
643             finally
644             {
645                client.close();
646             }
647             assertEquals(0, clearDestination(recoveryTopic));
648
649             restartRecoveryService();
650             assertEquals(0, clearDestination(recoveryTopic));
651             
652             client = new JMSClient(recoveryTopic);
653             try
654             {
655                List JavaDoc xids = client.recover();
656                assertEquals(1, xids.size());
657                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
658                client.forget(xid);
659                xids = client.recover();
660                assertEquals(0, xids.size());
661             }
662             finally
663             {
664                client.close();
665             }
666             assertEquals(0, clearDestination(recoveryTopic));
667          }
668          finally
669          {
670             removeSubscription(recoveryTopic);
671          }
672       }
673       finally
674       {
675          undeployRecoveryService();
676       }
677    }
678    
679    protected void deployRecoveryService() throws Exception JavaDoc
680    {
681       super.deployRecoveryService();
682       InitialContext JavaDoc ctx = getInitialContext();
683       recoveryTopic = (Topic JavaDoc) ctx.lookup(RECOVERY_TOPIC);
684    }
685 }
686
Popular Tags