KickJava   Java API By Example, From Geeks To Geeks.

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


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 RecoveryQueueReceiveUnitTestCase extends RecoveryTest
38 {
39    static String JavaDoc RECOVERY_QUEUE = "queue/recoveryQueue";
40
41    Queue JavaDoc recoveryQueue;
42    
43    public RecoveryQueueReceiveUnitTestCase(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          reset(recoveryQueue);
60          setup(recoveryQueue, 1);
61          JMSClient client = new JMSClient(recoveryQueue);
62          try
63          {
64             Xid JavaDoc xid = new MyXid();
65             client.enlist(xid);
66             assertNotNull(client.receiveNoWait());
67             client.delist(xid);
68             int result = client.prepare(xid);
69             try
70             {
71                assertEquals(XAResource.XA_OK, result);
72                List JavaDoc xids = client.recover();
73                assertEquals(1, xids.size());
74                assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
75             }
76             finally
77             {
78                client.rollback(xid);
79             }
80          }
81          finally
82          {
83             client.close();
84          }
85          assertEquals(1, clearDestination(recoveryQueue));
86       }
87       finally
88       {
89          undeployRecoveryService();
90       }
91    }
92    
93    /**
94     * Receive a message and prepare the transaction, but
95     * then roll it back
96     *
97     * Assert the xid doesn't appears in the recovery list
98     * Assert the message is received after the rollback
99     */

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

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

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

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

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

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

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

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

559    public void testReceivePrepareForgetDifferentConnectionRestart() throws Exception JavaDoc
560    {
561       deployRecoveryService();
562       try
563       {
564          reset(recoveryQueue);
565          setup(recoveryQueue, 1);
566          Xid JavaDoc xid = new MyXid();
567          JMSClient client = new JMSClient(recoveryQueue);
568          try
569          {
570             client.enlist(xid);
571             assertNotNull(client.receiveNoWait());
572             client.delist(xid);
573             int result = client.prepare(xid);
574             assertEquals(XAResource.XA_OK, result);
575             List JavaDoc xids = client.recover();
576             assertEquals(1, xids.size());
577             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
578          }
579          finally
580          {
581             client.close();
582          }
583          assertEquals(0, clearDestination(recoveryQueue));
584          
585          restartRecoveryService();
586          assertEquals(0, clearDestination(recoveryQueue));
587
588          client = new JMSClient(recoveryQueue);
589          try
590          {
591             List JavaDoc xids = client.recover();
592             assertEquals(1, xids.size());
593             assertXidEquals(xid, (Xid JavaDoc) xids.get(0));
594             client.forget(xid);
595             xids = client.recover();
596             assertEquals(0, xids.size());
597          }
598          finally
599          {
600             client.close();
601          }
602          assertEquals(1, clearDestination(recoveryQueue));
603       }
604       finally
605       {
606          undeployRecoveryService();
607       }
608    }
609    
610    protected void deployRecoveryService() throws Exception JavaDoc
611    {
612       super.deployRecoveryService();
613       InitialContext JavaDoc ctx = getInitialContext();
614       recoveryQueue = (Queue JavaDoc) ctx.lookup(RECOVERY_QUEUE);
615    }
616 }
617
Popular Tags