KickJava   Java API By Example, From Geeks To Geeks.

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


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 sessions
34  * <br />
35  * See JMS specifications, §4.4 Session
36  *
37  * @author Jeff Mesnil (jmesnil@inrialpes.fr)
38  * @version $Id: SessionTest.java,v 1.2 2002/04/23 08:36:54 jmesnil Exp $
39  */

40 public class SessionTest extends PTPTestCase {
41         
42   /**
43    * Test that an attempt to call the <code>recover()</code> method on a
44    * <strong>transacted </strong> <code>Session</code> throws a
45    * <code>javax.jms.IllegalStateException</code>.
46    */

47   public void testRecoverTransactedSession() {
48     try {
49       // senderSession has been created as non transacted
50
assertEquals(false, senderSession.getTransacted());
51       // we create it again but as a transacted session
52
senderSession = senderConnection.createQueueSession(true, 0);
53       assertEquals(true, senderSession.getTransacted());
54       senderSession.recover();
55       fail("Should raise an IllegalStateException, the session is not transacted.\n");
56     } catch (javax.jms.IllegalStateException JavaDoc e) {
57     } catch (java.lang.IllegalStateException JavaDoc e) {
58       fail ("Should raise a javax.jms.IllegalStateException, not a java.lang.IllegalStateException.\n");
59     } catch (Exception JavaDoc e) {
60       fail("Should raise a javax.jms.IllegalStateException, not a "+ e);
61     }
62   }
63
64   /**
65    * Test that a call to the <code>rollback()</code> method on a
66    * <strong>transacted</strong> <code>Session</code> rollbacks all
67    * the messages sent in the transaction.
68    */

69  public void testRollbackTransactedSession() {
70     try {
71       // re-create senderSession as a transacted session
72
senderSession = senderConnection.createQueueSession(true, 0);
73       sender = senderSession.createSender(senderQueue);
74       assertEquals(true, senderSession.getTransacted());
75       
76       TextMessage message = senderSession.createTextMessage();
77       message.setText("testRollbackTransactedSession");
78       // send a message within a transacted session
79
sender.send (message);
80       
81       // rollback the transaction -> the sent message shouldn't be received
82
senderSession.rollback();
83       
84       TextMessage m = (TextMessage) receiver.receiveNoWait();
85       // test that no message has been received
86
assertEquals(null, m);
87     } catch (Exception JavaDoc e) {
88       fail(e);
89     }
90   }
91
92   /**
93    * Test that a call to the <code>rollback()</code> method on a
94    * <strong>transacted</strong> <code>Session</code> rollbacks all
95    * the messages sent in the transaction.
96    */

97   public void testCommitTransactedSession() {
98     try {
99       // re-create senderSession as a transacted session
100
senderSession = senderConnection.createQueueSession(true, 0);
101       sender = senderSession.createSender(senderQueue);
102       assertEquals(true, senderSession.getTransacted());
103       
104       TextMessage message = senderSession.createTextMessage();
105       message.setText("testCommitTransactedSession");
106       // send a message within a transacted session
107
sender.send (message);
108       
109       TextMessage m = (TextMessage) receiver.receiveNoWait();
110       // test that no message has been received (the transaction has not been committed yet)
111
assertEquals(null,m);
112       
113       // commit the transaction -> the sent message should be received
114
senderSession.commit();
115       
116       m = (TextMessage) receiver.receive(TestConfig.TIMEOUT);
117       assertTrue(m != null);
118       assertEquals("testCommitTransactedSession", m.getText());
119     } catch (Exception JavaDoc e) {
120       fail(e);
121     }
122   }
123
124   /**
125    * Test that an attempt to call the <code>roolback()</code> method on a
126    * <strong>non transacted</strong> <code>Session</code> throws a
127    * <code>javax.jms.IllegalStateException</code>.
128    */

129   public void testRollbackNonTransactedSession() {
130     try {
131       // senderSession has been created as non transacted in the setUp() method
132
assertEquals(false, senderSession.getTransacted());
133       senderSession.rollback();
134       fail("Should raise an IllegalStateException, the session is not transacted.\n");
135     } catch (javax.jms.IllegalStateException JavaDoc e) {
136     } catch (java.lang.IllegalStateException JavaDoc e) {
137       fail ("Should raise a javax.jms.IllegalStateException, not a java.lang.IllegalStateException.\n");
138     } catch (Exception JavaDoc e) {
139       fail("Should raise a javax.jms.IllegalStateException, not a "+ e);
140     }
141   }
142   
143   /**
144    * Test that an attempt to call the <code>commit()</code> method on a
145    * <strong>non transacted</strong> <code>Session</code> throws a
146    * <code>javax.jms.IllegalStateException</code>.
147    */

148   public void testCommitNonTransactedSession() {
149     try {
150       // senderSession has been created as non transacted in the setUp() method
151
assertEquals(false, senderSession.getTransacted());
152       senderSession.commit();
153       fail("Should raise an IllegalStateException, the session is not transacted.\n");
154     } catch (javax.jms.IllegalStateException JavaDoc e) {
155     } catch (java.lang.IllegalStateException JavaDoc e) {
156       fail ("Should raise a javax.jms.IllegalStateException, not a java.lang.IllegalStateException.\n");
157     } catch (Exception JavaDoc e) {
158       fail("Should raise a javax.jms.IllegalStateException, not a "+ e);
159     }
160   }
161
162   /**
163    * Test that the <code>getTransacted()</code> method of a <code>Session</code> returns <code>true</code>
164    * if the session is transacted, <code>false</code> else.
165    */

166   public void testGetTransacted() {
167     try {
168       // senderSession has been created as non transacted
169
assertEquals(false, senderSession.getTransacted());
170       // we re-create senderSession as a transacted session
171
senderSession = senderConnection.createQueueSession(true, Session.AUTO_ACKNOWLEDGE);
172       assertEquals(true, senderSession.getTransacted());
173     } catch (Exception JavaDoc e) {
174       fail(e);
175     }
176   }
177
178
179   /**
180    * Test that invoking the <code>acknowledge()</code> method of a received message
181    * from a closed session must throw an <code>IllegalStateException</code>.
182    */

183   public void testAcknowledge() {
184     try {
185       receiverSession = receiverConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
186       receiver = receiverSession.createReceiver(receiverQueue);
187       
188       Message message = senderSession.createMessage();
189       sender.send(message);
190
191       Message m = receiver.receive(TestConfig.TIMEOUT);
192       receiverSession.close();
193       m.acknowledge();
194       fail("§4.4.1 Invoking the acknowledge method of a received message from a closed "+
195        " session must throw an [javax.jms.]IllegalStateException.\n");
196     } catch (javax.jms.IllegalStateException JavaDoc e) {
197     } catch (JMSException e) {
198       fail("Should raise a javax.jms.IllegalStateException, not a "+ e);
199     } catch (java.lang.IllegalStateException JavaDoc e) {
200       fail("§4.4.1 Invoking the acknowledge method of a received message from a closed "+
201        "session must throw an [javax.jms.]IllegalStateException, "+
202        "[not a java.lang.IllegalStateException]");
203     }
204   }
205
206   /**
207    * Test that it is valid to use message objects created or received via the [closed] session with the
208    * exception of a received message <code>acknowledge()</code> method.
209    */

210   public void testUseMessage() {
211     try {
212       TextMessage message = senderSession.createTextMessage();
213       message.setText("testUseMessage");
214       sender.send(message);
215       
216       TextMessage m = (TextMessage)receiver.receive(TestConfig.TIMEOUT);
217       receiverSession.close();
218       assertEquals("testUseMessage", m.getText());
219     } catch (Exception JavaDoc e) {
220       fail("§4.4.1 It is valid to continue to use message objects created or received via "+
221        "the [closed] session.\n");
222     }
223   }
224
225   /**
226    * Test that an attempt to use a <code>Session</code> which has been closed
227    * throws a <code>javax.jms.IllegalStateException</code>.
228    */

229   public void testUsedClosedSession() {
230     try {
231       senderSession.close();
232       senderSession.createMessage();
233       fail ("§4.4.1 An attempt to use [a closed session] must throw a [javax.jms.]IllegalStateException.\n");
234     } catch (javax.jms.IllegalStateException JavaDoc e) {
235     } catch (JMSException e) {
236       fail("Should raise a javax.jms.IllegalStateException, not a "+ e);
237     } catch (java.lang.IllegalStateException JavaDoc e) {
238       fail("Should raise a javax.jms.IllegalStateException, not a java.lang.IllegalStateException");
239     }
240   }
241
242   /**
243    * Test that closing a closed session does <strong>not</strong> throw
244    * an exception.
245    */

246   public void testCloseClosedSession() {
247     try {
248       // senderSession is already started
249
// we close it once
250
senderSession.close();
251       // we close it a second time
252
senderSession.close();
253     } catch (Exception JavaDoc e) {
254       fail("§4.4.1 Closing a closed session must NOT throw an exception.\n");
255     }
256   }
257
258   /**
259    * Method to use this class in a Test suite
260    */

261   public static Test suite() {
262      return new TestSuite(SessionTest.class);
263    }
264   
265   public SessionTest(String JavaDoc name) {
266     super(name);
267   }
268 }
269
Popular Tags