Source code: com/presumo/jms/client/ClientTests.java
1 /**
2 * This file is part of Presumo.
3 *
4 * Presumo is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * Presumo is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with Presumo; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 *
18 *
19 * Copyright 2001 Dan Greff
20 */
21 package com.presumo.jms.client;
22
23 import javax.jms.*;
24 import junit.framework.Test;
25 import junit.framework.TestCase;
26 import junit.framework.TestSuite;
27
28 /**
29 * Unit tests for the client. Should test IntraJVM traffic, so consequently
30 * the Router will get instantiate to route the messages between the publisher
31 * and subscriber in the same JVM.
32 *
33 * @author Dan Greff
34 */
35 public class ClientTests extends TestCase
36 {
37 private static final boolean VERBOSE = true;
38
39 public ClientTests(String name)
40 {
41 super(name);
42 }
43
44 public static Test suite()
45 {
46 TestSuite suite = new TestSuite(ClientTests.class);
47 return suite;
48 }
49
50
51 ///////////////////////////////////////////////////////////////////////////
52 // Unit Tests //
53 ///////////////////////////////////////////////////////////////////////////
54
55 public void testAutoAcknowledge() throws JMSException
56 {
57 verbose("start testAutoAcknowleged");
58
59 testSimplePubSub(Session.AUTO_ACKNOWLEDGE);
60
61 verbose("finish testAutoAcknowledge");
62 }
63
64 public void testDupsOkAcknowledge() throws JMSException
65 {
66 verbose("start testDupsOkAcknowleged");
67
68 testSimplePubSub(Session.DUPS_OK_ACKNOWLEDGE);
69
70 verbose("finish testDupsOkAcknowledge");
71 }
72
73
74 public void testClientAcknowledge() throws JMSException
75 {
76 verbose("start testClientAcknowleged");
77
78 testSimplePubSub(Session.CLIENT_ACKNOWLEDGE);
79
80 verbose("finish testClientAcknowledge");
81 }
82
83
84 public void testSimplePubSubMultiple() throws JMSException
85 {
86 for (int i=0; i < 10; ++i) {
87 testAutoAcknowledge();
88 testDupsOkAcknowledge();
89 testClientAcknowledge();
90 }
91 }
92
93 ///////////////////////////////////////////////////////////////////////////
94 // Unit Test Helpers //
95 ///////////////////////////////////////////////////////////////////////////
96
97 private void testSimplePubSub(int ackMode) throws JMSException
98 {
99 verbose("start simpleSubscriberTest()");
100
101 verbose("Creating a session");
102 JmsTopicConnectionFactory factory = new JmsTopicConnectionFactory();
103 //factory.setHost("localhost");
104 //factory.setPort(2323);
105 TopicConnection conn = factory.createTopicConnection();
106 TopicSession session = conn.createTopicSession(false, ackMode);
107
108 Topic topic = session.createTopic("dan");
109
110 TopicSubscriber sub1 = session.createSubscriber(topic);
111 verbose("Created subscriber 1");
112
113 TopicSubscriber sub2 = session.createSubscriber(topic);
114 verbose("Created subscriber 2");
115
116 TopicSubscriber sub3 = session.createSubscriber(topic);
117 verbose("Created subscriber 3");
118
119 TopicPublisher pub = session.createPublisher(topic);
120 verbose("created publisher");
121
122 TextMessage message = session.createTextMessage();
123 message.setText("Hello World");
124
125
126 verbose("starting connection");
127 conn.start();
128 sleepRandom();
129
130 pub.setDeliveryMode(javax.jms.DeliveryMode.PERSISTENT);
131 pub.publish(message);
132
133
134 TextMessage msg1 = (TextMessage) sub1.receive(5000);
135 assertNotNull("message not received from sub 1", msg1);
136 verbose(msg1.getText());
137 if (ackMode == Session.CLIENT_ACKNOWLEDGE) {
138 msg1.acknowledge();
139 }
140
141 TextMessage msg2 = (TextMessage) sub2.receive(5000);
142 assertNotNull("message not received from sub 2", msg2);
143 verbose(msg2.getText());
144 TextMessage msg3 = (TextMessage) sub3.receive(5000);
145 assertNotNull("message not received from sub 3", msg3);
146 verbose(msg3.getText());
147
148 if (ackMode == Session.CLIENT_ACKNOWLEDGE) {
149 msg3.acknowledge(); // should cause msg2 to be acknowledged also
150 }
151
152 sleepRandom();
153 conn.close();
154 }
155
156
157 private static final void verbose(String s)
158 {
159 if (VERBOSE)
160 System.out.println(s);
161 }
162
163 private static final void sleepRandom()
164 {
165 int sleep = (int) (200.0 * Math.random());
166 try {
167 Thread.sleep(sleep);
168 } catch (InterruptedException ie) {}
169 }
170
171 ///////////////////////////////////////////////////////////////////////////
172 // main() //
173 ///////////////////////////////////////////////////////////////////////////
174 public static void main(String [] args) throws Exception
175 {
176 junit.textui.TestRunner.run(ClientTests.class);
177 }
178
179 }
180