KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jtests > jms > conform > session > QueueSessionTest


1 /*
2  * JORAM: Java(TM) Open Reliable Asynchronous Messaging
3  * Copyright (C) 2002 INRIA
4  * Contact: joram-team@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * Initial developer(s): Jeff Mesnil (jmesnil@inrialpes.fr)
22  * Contributor(s): ______________________________________.
23  */

24
25 package org.objectweb.jtests.jms.conform.session;
26
27 import org.objectweb.jtests.jms.framework.*;
28 import junit.framework.*;
29
30 import javax.jms.*;
31
32 /**
33  * Test queue sessions
34  * <br />
35  * See JMS specifications, §4.4 Session
36  *
37  * @author Jeff Mesnil (jmesnil@inrialpes.fr)
38  * @version $Id: QueueSessionTest.java,v 1.2 2002/04/23 08:36:49 jmesnil Exp $
39  */

40 public class QueueSessionTest extends PTPTestCase {
41
42     /**
43      * Test that if we rollback a transaction which has consumed a message,
44      * the message is effectively redelivered.
45      */

46     public void testRollbackRececeivedMessage() {
47     try {
48         senderConnection.stop();
49         // senderSession has been created as non transacted
50
// we create it again but as a transacted session
51
senderSession = senderConnection.createQueueSession(true, 0);
52         assertEquals(true, senderSession.getTransacted());
53         // we create again the sender
54
sender = senderSession.createSender(senderQueue);
55         senderConnection.start();
56
57         receiverConnection.stop();
58         // receiverSession has been created as non transacted
59
// we create it again but as a transacted session
60
receiverSession = receiverConnection.createQueueSession(true,0);
61         assertEquals(true, receiverSession.getTransacted());
62         // we create again the receiver
63
receiver = receiverSession.createReceiver(receiverQueue);
64         receiverConnection.start();
65
66         // we send a message...
67
TextMessage message = senderSession.createTextMessage();
68         message.setText("testRollbackRececeivedMessage");
69         sender.send(message);
70         // ... and commit the *producer* transaction
71
senderSession.commit();
72
73         // we receive a message...
74
Message m = receiver.receive(TestConfig.TIMEOUT);
75         assertTrue(m != null);
76         assertTrue(m instanceof TextMessage);
77         TextMessage msg = (TextMessage)m;
78         // ... which is the one which was sent...
79
assertEquals("testRollbackRececeivedMessage", msg.getText());
80         // ...and has not been redelivered
81
assertEquals(false, msg.getJMSRedelivered());
82
83         // we rollback the *consumer* transaction
84
receiverSession.rollback();
85
86         // we receive again a message
87
m = receiver.receive(TestConfig.TIMEOUT);
88         assertTrue(m != null);
89         assertTrue(m instanceof TextMessage);
90         msg = (TextMessage)m;
91         // ... which is still the one which was sent...
92
assertEquals("testRollbackRececeivedMessage", msg.getText());
93         // .. but this time, it has been redelivered
94
assertEquals(true, msg.getJMSRedelivered());
95       
96     } catch (Exception JavaDoc e) {
97         fail(e);
98     }
99     }
100
101     /**
102      * Test that a call to the <code>createBrowser()</code> method with an invalid
103      * messaeg session throws a <code>javax.jms.InvalidSelectorException</code>.
104      */

105     public void testCreateBrowser_2() {
106     try {
107         QueueBrowser browser = senderSession.createBrowser(senderQueue,
108                                    "definitely not a message selector!");
109         fail("Should throw a javax.jms.InvalidSelectorException.\n");
110     } catch (InvalidSelectorException e) {
111     } catch (JMSException e) {
112         fail("Should throw a javax.jms.InvalidSelectorException, not a "+ e);
113     }
114     }
115
116     /**
117      * Test that a call to the <code>createBrowser()</code> method with an invalid
118      * <code>Queue</code> throws a <code>javax.jms.InvalidDestinationException</code>.
119      */

120     public void testCreateBrowser_1() {
121     try {
122         QueueBrowser browser = senderSession.createBrowser((Queue)null);
123         fail("Should throw a javax.jms.InvalidDestinationException.\n");
124     } catch (InvalidDestinationException e) {
125     } catch (JMSException e) {
126         fail("Should throw a javax.jms.InvalidDestinationException, not a "+ e);
127     }
128     }
129
130     /**
131      * Test that a call to the <code>createReceiver()</code> method with an invalid
132      * message selector throws a <code>javax.jms.InvalidSelectorException</code>.
133      */

134     public void testCreateReceiver_2() {
135     try {
136         receiver = senderSession.createReceiver(senderQueue,
137                             "definitely not a message selector!");
138         fail("Should throw a javax.jms.InvalidSelectorException.\n");
139     } catch (InvalidSelectorException e) {
140     } catch (JMSException e) {
141         fail("Should throw a javax.jms.InvalidSelectorException, not a "+ e);
142     }
143     }
144
145     /**
146      * Test that a call to the <code>createReceiver()</code> method with an invalid
147      * <code>Queue</code> throws a <code>javax.jms.InvalidDestinationException</code>>
148      */

149     public void testCreateReceiver_1() {
150     try {
151         receiver = senderSession.createReceiver((Queue)null);
152         fail("Should throw a javax.jms.InvalidDestinationException.\n");
153     } catch (InvalidDestinationException e) {
154     } catch (JMSException e) {
155         fail("Should throw a javax.jms.InvalidDestinationException, not a "+ e);
156     }
157     }
158
159     /**
160      * Method to use this class in a Test suite
161      */

162     public static Test suite() {
163     return new TestSuite(QueueSessionTest.class);
164     }
165   
166     public QueueSessionTest(String JavaDoc name) {
167     super(name);
168     }
169 }
170
Popular Tags