Source code: org/objectweb/jtests/jms/conform/connection/ConnectionTest.java
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.*;
28 import javax.jms.*;
29 import junit.framework.*;
30
31 /**
32 * Test connections.
33 *
34 * See JMS specifications, §4.3.5 Closing a Connection
35 *
36 * @author Jeff Mesnil (jmesnil@inrialpes.fr)
37 * @version $Id: ConnectionTest.java,v 1.1 2002/04/21 21:15:18 chirino Exp $
38 */
39 public class ConnectionTest extends PTPTestCase {
40
41 /**
42 * Test that invoking the <code>acknowledge()</code> method of a received message
43 * from a closed connection's session must throw an <code>IllegalStateException</code>.
44 */
45 public void testAcknowledge() {
46 try {
47 receiverConnection.stop();
48 receiverSession = receiverConnection.createQueueSession(false, Session.CLIENT_ACKNOWLEDGE);
49 receiver = receiverSession.createReceiver(receiverQueue);
50 receiverConnection.start();
51
52 Message message = senderSession.createMessage();
53 sender.send(message);
54
55 Message m = receiver.receive();
56 receiverConnection.close();
57 m.acknowledge();
58 fail("§4.3.5 Invoking the acknowledge method of a received message from a closed "+
59 "connection's session must throw a [javax.jms.]IllegalStateException.\n");
60 } catch (javax.jms.IllegalStateException e) {
61 } catch (JMSException e) {
62 fail("§4.3.5 Invoking the acknowledge method of a received message from a closed "+
63 "connection's session must throw a [javax.jms.]IllegalStateException, not a "+ e);
64 } catch (java.lang.IllegalStateException e) {
65 fail("§4.3.5 Invoking the acknowledge method of a received message from a closed "+
66 "connection's session must throw an [javax.jms.]IllegalStateException "+
67 "[not a java.lang.IllegalStateException]");
68 }
69 }
70
71 /**
72 * Test that an attempt to use a <code>Connection</code> which has been closed
73 * throws a <code>javax.jms.IllegalStateException</code>.
74 */
75 public void testUseClosedConnection() {
76 try {
77 senderConnection.close();
78 senderConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
79 fail("Should raise a javax.jms.IllegalStateException");
80 } catch (javax.jms.IllegalStateException e) {
81 } catch (JMSException e) {
82 fail("Should raise a javax.jms.IllegalStateException, not a "+ e);
83 } catch (java.lang.IllegalStateException e) {
84 fail("Should raise a javax.jms.IllegalStateException, not a java.lang.IllegalStateException");
85 }
86 }
87
88 /**
89 * Test that a <code>MessageProducer</code> can send messages while a
90 * <code>Connection</code> is stopped.
91 */
92 public void testMessageSentWhenConnectionClosed() {
93 try {
94 senderConnection.stop();
95 Message message = senderSession.createTextMessage();
96 sender.send(message);
97
98 Message m = receiver.receive();
99 } catch (JMSException e) {
100 fail(e);
101 }
102 }
103
104
105 /**
106 * Test that closing a closed connectiondoes not throw an exception.
107 */
108 public void testCloseClosedConnection() {
109 try {
110 // senderConnection is already started
111 // we close it once
112 senderConnection.close();
113 // we close it a second time
114 senderConnection.close();
115 } catch (Exception e) {
116 fail("§4.3.5 Closing a closed connection must not throw an exception.\n");
117 }
118 }
119
120 /**
121 * Test that starting a started connection is ignored
122 */
123 public void testStartStartedConnection() {
124 try {
125 // senderConnection is already started
126 // start it again should be ignored
127 senderConnection.start();
128 } catch (JMSException e) {
129 fail(e);
130 }
131 }
132
133 /**
134 * Test that stopping a stopped connection is ignored
135 */
136 public void testStopStoppedConnection() {
137 try {
138 // senderConnection is started
139 // we stop it once
140 senderConnection.stop();
141 // stopping it a second time is ignored
142 senderConnection.stop();
143 } catch (JMSException e) {
144 fail(e);
145 }
146 }
147
148 /**
149 * Test that delivery of message is stopped if the message consumer connection is stopped
150 */
151 public void testStopConsumerConnection() {
152 try {
153 receiverConnection.stop();
154
155 receiver.setMessageListener(new MessageListener() {
156 public void onMessage(Message m) {
157 try {
158 fail("The message must not be received, the consumer connection is stopped");
159 assertEquals("test", ((TextMessage)m).getText());
160 } catch (JMSException e) {
161 fail(e);
162 }
163 }
164 });
165
166 TextMessage message = senderSession.createTextMessage();
167 message.setText("test");
168 sender.send(message);
169 synchronized(this) {
170 try {
171 wait(1000);
172 } catch (Exception e) {
173 fail (e);
174 }
175 }
176 } catch (JMSException e) {
177 fail(e);
178 }
179 }
180
181 /**
182 * Method to use this class in a Test suite
183 */
184 public static Test suite() {
185 return new TestSuite(ConnectionTest.class);
186 }
187
188 public ConnectionTest(String name) {
189 super(name);
190 }
191 }