KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  * JBoss, Home of Professional Open Source.
3  * Copyright 2006, Red Hat Middleware LLC, and individual contributors
4  * as indicated by the @author tags. See the copyright.txt file in the
5  * distribution for a 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 javax.jms.Connection JavaDoc;
25 import javax.jms.JMSException JavaDoc;
26 import javax.jms.Message JavaDoc;
27 import javax.jms.MessageConsumer JavaDoc;
28 import javax.jms.MessageProducer JavaDoc;
29 import javax.jms.Session JavaDoc;
30
31 import junit.framework.Test;
32
33 import org.jboss.mq.SpyDestination;
34 import org.jboss.test.jbossmq.JBossMQMicrocontainerTest;
35
36 /**
37  * A test to make sure an error during receive doesn't lead to corrupt "receiving" state
38  *
39  * @author <a HREF="mailto:adrian@jboss.org>Adrian Brock</a>
40  * @version <tt>$Revision: 57211 $</tt>
41  */

42 public class ReceiveAfterErrorUnitTestCase extends JBossMQMicrocontainerTest
43 {
44    public ReceiveAfterErrorUnitTestCase(String JavaDoc name) throws Exception JavaDoc
45    {
46       super(name);
47    }
48
49    public static Test suite()
50    {
51       return suite(ReceiveAfterErrorUnitTestCase.class);
52    }
53    
54    public interface ReceiveOperation
55    {
56       Message JavaDoc receive(MessageConsumer JavaDoc consumer) throws JMSException JavaDoc;
57    }
58    
59    public void testReceiveAfterError() throws Exception JavaDoc
60    {
61       receiveAfterError(new ReceiveOperation()
62       {
63          public Message JavaDoc receive(MessageConsumer JavaDoc consumer) throws JMSException JavaDoc
64          {
65             return consumer.receive();
66          }
67       });
68    }
69    
70    public void testReceiveWithWaitAfterError() throws Exception JavaDoc
71    {
72       receiveAfterError(new ReceiveOperation()
73       {
74          public Message JavaDoc receive(MessageConsumer JavaDoc consumer) throws JMSException JavaDoc
75          {
76             return consumer.receive(1000);
77          }
78       });
79    }
80    
81    public void testReceiveNoWaitAfterError() throws Exception JavaDoc
82    {
83       receiveAfterError(new ReceiveOperation()
84       {
85          public Message JavaDoc receive(MessageConsumer JavaDoc consumer) throws JMSException JavaDoc
86          {
87             return consumer.receiveNoWait();
88          }
89       });
90    }
91    
92    protected void receiveAfterError(ReceiveOperation operation) throws Exception JavaDoc
93    {
94       SpyDestination destination = createQueue("testQueue");
95       try
96       {
97          Connection JavaDoc connection = createConnection();
98          try
99          {
100             Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
101             connection.start();
102
103             MessageProducer JavaDoc producer = session.createProducer(destination);
104             Message JavaDoc message = session.createMessage();
105             producer.send(message);
106             
107             MessageConsumer JavaDoc consumer = session.createConsumer(destination);
108             
109             // Receive should now throw an error
110
raiseReceiveError(true);
111             try
112             {
113                operation.receive(consumer);
114                fail("Should not be here!");
115             }
116             catch (Throwable JavaDoc t)
117             {
118                checkThrowable(JMSException JavaDoc.class, t);
119             }
120             raiseReceiveError(false);
121             
122             message = operation.receive(consumer);
123             assertNotNull(message);
124          }
125          finally
126          {
127             connection.close();
128          }
129       }
130       finally
131       {
132          removeDestination(destination);
133       }
134    }
135 }
136
137
Popular Tags