Source code: org/objectweb/jtests/jms/conform/session/TopicSessionTest.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.session;
26
27 import org.objectweb.jtests.jms.framework.*;
28 import junit.framework.*;
29
30 import javax.jms.*;
31 import java.util.Hashtable;
32
33 /**
34 * Test topic sessions
35 * <br />
36 * See JMS specifications, §4.4 Session
37 *
38 * @author Jeff Mesnil (jmesnil@inrialpes.fr)
39 * @version $Id: TopicSessionTest.java,v 1.1.4.1 2003/01/09 16:41:41 ejort Exp $
40 */
41 public class TopicSessionTest extends PubSubTestCase {
42
43 /**
44 * Test that if we rollback a transaction which has consumed a message,
45 * the message is effectively redelivered.
46 */
47 public void testRollbackReceivedMessage() {
48 try {
49 publisherConnection.stop();
50 // publisherSession has been declared has non transacted
51 // we recreate it as a transacted session
52 publisherSession = publisherConnection.createTopicSession(true,0);
53 assertEquals(true, publisherSession.getTransacted());
54 // we also recreate the publisher
55 publisher = publisherSession.createPublisher(publisherTopic);
56 publisherConnection.start();
57
58 subscriberConnection.stop();
59 // subscriberSession has been declared has non transacted
60 // we recreate it as a transacted session
61 subscriberSession = subscriberConnection.createTopicSession(true,0);
62 assertEquals(true, subscriberSession.getTransacted());
63 // we also recreate the subscriber
64 subscriber = subscriberSession.createSubscriber(subscriberTopic);
65 subscriberConnection.start();
66
67 // we create a message...
68 TextMessage message = publisherSession.createTextMessage();
69 message.setText("testRollbackReceivedMessage");
70 // ... publish it ...
71 publisher.publish(message);
72 // ... and commit the transaction
73 publisherSession.commit();
74
75 // we receive it
76 Message msg1 = subscriber.receive(1000);
77 assertTrue("no message received", msg1 != null);
78 assertTrue(msg1 instanceof TextMessage);
79 assertEquals("testRollbackReceivedMessage", ((TextMessage)msg1).getText());
80
81 // we rollback the transaction of subscriberSession
82 subscriberSession.rollback();
83
84 // we expect to receive a second time the message
85 Message msg2 = subscriber.receive(1000);
86 assertTrue("no message received after rollbacking subscriber session.", msg2 != null);
87 assertTrue(msg2 instanceof TextMessage);
88 assertEquals("testRollbackReceivedMessage", ((TextMessage)msg2).getText());
89
90
91 // finally we commit the subscriberSession transaction
92 subscriberSession.commit();
93 } catch (Exception e) {
94 fail(e);
95 }
96 }
97
98 /**
99 * Test that a durable subscriber effectively receives the messages sent to its
100 * topic while it was inactive.
101 */
102 public void testDurableSubscriber() {
103 try {
104 subscriber = subscriberSession.createDurableSubscriber(subscriberTopic,"testTopic");
105 subscriberConnection.close();
106 subscriberConnection = null;
107
108 TextMessage message = publisherSession.createTextMessage();
109 message.setText("test");
110 publisher.publish(message);
111
112 subscriberConnection = subscriberTCF.createTopicConnection();
113 if (subscriberConnection.getClientID() == null) {
114 subscriberConnection.setClientID("subscriberConnection");
115 }
116 subscriberSession = subscriberConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);
117 subscriber = subscriberSession.createDurableSubscriber(subscriberTopic, "testTopic");
118 subscriberConnection.start();
119
120 TextMessage m = (TextMessage)subscriber.receiveNoWait();
121 assertTrue(m != null);
122 assertEquals("test", m.getText());
123 } catch (JMSException e) {
124 fail(e);
125 }
126 finally
127 {
128 try
129 {
130 subscriberSession.unsubscribe("testTopic");
131 }
132 catch (JMSException e)
133 {
134 fail(e);
135 }
136 }
137 }
138
139 /**
140 * Test the unsubscription of a durable subscriber.
141 */
142 public void testUnsubscribe() {
143 try {
144 subscriber = subscriberSession.createDurableSubscriber(subscriberTopic,"topic");
145 subscriber.close();
146 // nothing should happen when unsubscribing the durable subscriber
147 subscriberSession.unsubscribe("topic");
148 } catch (JMSException e) {
149 fail(e);
150 }
151 }
152
153 /**
154 * Test that a call to the <code>createDurableSubscriber()</code> method with an invalid
155 * message selector throws a <code>javax.jms.InvalidSelectorException</code>.
156 */
157 public void testCreateDurableSubscriber_2() {
158 try {
159 subscriberSession.createDurableSubscriber(subscriberTopic,
160 "topic",
161 "definitely not a message selector!",
162 true);
163 fail("Should throw a javax.jms.InvalidSelectorException.\n");
164 } catch (InvalidSelectorException e) {
165 } catch (JMSException e) {
166 fail("Should throw a javax.jms.InvalidSelectorException, not a "+ e);
167 }
168 }
169
170 /**
171 * Test that a call to the <code>createDurableSubscriber()</code> method with an invalid
172 * <code>Topic</code> throws a <code>javax.jms.InvalidDestinationException</code>.
173 */
174 public void testCreateDurableSubscriber_1() {
175 try {
176 subscriberSession.createDurableSubscriber((Topic)null, "topic");
177 fail("Should throw a javax.jms.InvalidDestinationException.\n");
178 } catch (InvalidDestinationException e) {
179 } catch (JMSException e) {
180 fail("Should throw a javax.jms.InvalidDestinationException, not a "+ e);
181 }
182 }
183
184 /**
185 * Test that a call to the <code>createSubscriber()</code> method with an invalid
186 * message selector throws a <code>javax.jms.InvalidSelectorException</code>.
187 */
188 public void testCreateSubscriber_2() {
189 try {
190 subscriberSession.createSubscriber(subscriberTopic,
191 "definitely not a message selector!",
192 true);
193 fail("Should throw a javax.jms.InvalidSelectorException.\n");
194 } catch (InvalidSelectorException e) {
195 } catch (JMSException e) {
196 fail("Should throw a javax.jms.InvalidSelectorException, not a "+ e);
197 }
198 }
199
200 /**
201 * Test that a call to the <code>createSubscriber()</code> method with an invalid
202 * <code>Topic</code> throws a <code>javax.jms.InvalidDestinationException</code>.
203 */
204 public void testCreateSubscriber_1() {
205 try {
206 subscriberSession.createSubscriber((Topic)null);
207 fail("Should throw a javax.jms.InvalidDestinationException.\n");
208 } catch (InvalidDestinationException e) {
209 } catch (JMSException e) {
210 fail("Should throw a javax.jms.InvalidDestinationException, not a "+ e);
211 }
212 }
213
214 /**
215 * Method to use this class in a Test suite
216 */
217 public static Test suite() {
218 return new TestSuite(TopicSessionTest.class);
219 }
220
221 public TopicSessionTest(String name) {
222 super(name);
223 }
224 }