KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > jtests > jms > conform > connection > ConnectionTest


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): Andreas Mueller <am@iit.de>.
23  */

24
25 package org.objectweb.jtests.jms.conform.connection;
26
27 import org.objectweb.jtests.jms.framework.PTPTestCase;
28 import org.objectweb.jtests.jms.framework.TestConfig;
29 import javax.jms.*;
30 import junit.framework.*;
31
32 /**
33  * Test connections.
34  *
35  * See JMS specifications, §4.3.5 Closing a Connection
36  *
37  * @author Jeff Mesnil (jmesnil@inrialpes.fr)
38  * @version $Id: ConnectionTest.java,v 1.5 2002/05/16 10:36:19 jmesnil Exp $
39  */

40 public class ConnectionTest extends PTPTestCase {
41   
42     /**
43      * Test that invoking the <code>acknowledge()</code> method of a received message
44      * from a closed connection's session must throw an <code>IllegalStateException</code>.
45      */

46     public void testAcknowledge() {
47         try {
48             receiverConnection.stop();
49             receiverSession = receiverConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
50             receiver = receiverSession.createReceiver(receiverQueue);
51             receiverConnection.start();
52
53             Message message = senderSession.createMessage();
54             sender.send(message);
55
56             Message m = receiver.receive(TestConfig.TIMEOUT);
57             receiverConnection.close();
58             m.acknowledge();
59             fail("§4.3.5 Invoking the acknowledge method of a received message from a closed "+
60                  "connection's session must throw a [javax.jms.]IllegalStateException.\n");
61         } catch (javax.jms.IllegalStateException JavaDoc e) {
62         } catch (JMSException e) {
63             fail("§4.3.5 Invoking the acknowledge method of a received message from a closed "+
64                  "connection's session must throw a [javax.jms.]IllegalStateException, not a "+ e);
65         } catch (java.lang.IllegalStateException JavaDoc e) {
66             fail("§4.3.5 Invoking the acknowledge method of a received message from a closed "+
67                  "connection's session must throw an [javax.jms.]IllegalStateException "+
68                  "[not a java.lang.IllegalStateException]");
69         }
70     }
71     
72     /**
73      * Test that an attempt to use a <code>Connection</code> which has been closed
74      * throws a <code>javax.jms.IllegalStateException</code>.
75      */

76     public void testUseClosedConnection() {
77         try {
78             senderConnection.close();
79             senderConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
80             fail("Should raise a javax.jms.IllegalStateException");
81         } catch (javax.jms.IllegalStateException JavaDoc e) {
82         } catch (JMSException e) {
83             fail("Should raise a javax.jms.IllegalStateException, not a "+ e);
84         } catch (java.lang.IllegalStateException JavaDoc e) {
85             fail("Should raise a javax.jms.IllegalStateException, not a java.lang.IllegalStateException");
86         }
87     }
88
89     /**
90      * Test that a <code>MessageProducer</code> can send messages while a
91      * <code>Connection</code> is stopped.
92      */

93     public void testMessageSentWhenConnectionClosed() {
94         try {
95             senderConnection.stop();
96             Message message = senderSession.createTextMessage();
97             sender.send(message);
98       
99             Message m = receiver.receive(TestConfig.TIMEOUT);
100         } catch (JMSException e) {
101             fail(e);
102         }
103     }
104   
105
106     /**
107      * Test that closing a closed connectiondoes not throw an exception.
108      */

109     public void testCloseClosedConnection() {
110         try {
111             // senderConnection is already started
112
// we close it once
113
senderConnection.close();
114             // we close it a second time
115
senderConnection.close();
116         } catch (Exception JavaDoc e) {
117             fail("§4.3.5 Closing a closed connection must not throw an exception.\n");
118         }
119     }
120   
121     /**
122      * Test that starting a started connection is ignored
123      */

124     public void testStartStartedConnection() {
125         try {
126             // senderConnection is already started
127
// start it again should be ignored
128
senderConnection.start();
129         } catch (JMSException e) {
130             fail(e);
131         }
132     }
133
134     /**
135      * Test that stopping a stopped connection is ignored
136      */

137     public void testStopStoppedConnection() {
138         try {
139             // senderConnection is started
140
// we stop it once
141
senderConnection.stop();
142             // stopping it a second time is ignored
143
senderConnection.stop();
144         } catch (JMSException e) {
145             fail(e);
146         }
147     }
148
149     /**
150      * Test that delivery of message is stopped if the message consumer connection is stopped
151      */

152     public void testStopConsumerConnection() {
153         try {
154             receiverConnection.stop();
155       
156             receiver.setMessageListener(new MessageListener() {
157                     public void onMessage(Message m) {
158                         try {
159                             fail("The message must not be received, the consumer connection is stopped");
160                             assertEquals("test", ((TextMessage)m).getText());
161                         } catch (JMSException e) {
162                             fail(e);
163                         }
164                     }
165                 });
166       
167             TextMessage message = senderSession.createTextMessage();
168             message.setText("test");
169             sender.send(message);
170             synchronized(this) {
171                 try {
172                     wait(1000);
173                 } catch (Exception JavaDoc e) {
174                     fail (e);
175                 }
176             }
177         } catch (JMSException e) {
178             fail(e);
179         }
180     }
181   
182     /**
183      * Method to use this class in a Test suite
184      */

185     public static Test suite() {
186         return new TestSuite(ConnectionTest.class);
187     }
188   
189     public ConnectionTest(String JavaDoc name) {
190         super(name);
191     }
192 }
193
Popular Tags