1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2003-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: QueueSenderTest.java,v 1.3 2004/02/03 07:33:32 tanderson Exp $
44 */
45 package org.exolab.jmscts.test.producer.ttl;
46
47 import javax.jms.Message;
48 import javax.jms.Queue;
49 import javax.jms.QueueSender;
50
51 import junit.framework.Test;
52
53 import org.exolab.jmscts.core.MessageReceiver;
54 import org.exolab.jmscts.core.SessionHelper;
55 import org.exolab.jmscts.core.TestContext;
56 import org.exolab.jmscts.core.TestCreator;
57
58
59 /***
60 * This class tests the behaviour of the time-to-live methods and their
61 * affect on the JMSExpiration, for <code>QueueSender</code>s
62 *
63 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
64 * @version $Revision: 1.3 $
65 * @jmscts.factory QueueConnectionFactory
66 * @jmscts.factory XAQueueConnectionFactory
67 */
68 public class QueueSenderTest extends TimeToLiveTestCase {
69
70 /***
71 * The default destination
72 */
73 private static final String DESTINATION = "QueueSenderTest";
74
75 /***
76 * Time to live, in milliseconds (ie. 50 secs)
77 */
78 private static final long TTL = 50000;
79
80
81 /***
82 * Construct a new <code>QueueSenderTest</code>
83 *
84 * @param name the name of test case
85 */
86 public QueueSenderTest(String name) {
87 super(name);
88 }
89
90 /***
91 * Sets up the test suite
92 *
93 * @return an instance of this class that may be run by
94 * {@link org.exolab.jmscts.core.JMSTestRunner}
95 */
96 public static Test suite() {
97 return TestCreator.createSendReceiveTest(QueueSenderTest.class);
98 }
99
100 /***
101 * Returns the list of destination names used by this test case. These
102 * are used to pre-administer destinations prior to running the test case.
103 *
104 * @return the list of destinations used by this test case
105 */
106 public String[] getDestinations() {
107 return new String[] {DESTINATION};
108 }
109
110 /***
111 * Verifies that the default time-to-live for a sender equals
112 * <code>0</code>, and that the JMSExpiration property is set to
113 * <code>0</code> on send and receive when the default time-to-live is
114 * used.
115 *
116 * @jmscts.requirement message.expiration.send
117 * @jmscts.requirement message.expiration.receive
118 * @jmscts.requirement message.expiration.zero
119 * @jmscts.requirement producer.ttl.default
120 * @jmscts.message Message
121 * @throws Exception for any error
122 */
123 public void testDefaultTTL() throws Exception {
124 final long timeToLive = 0;
125 TestContext context = getContext();
126
127 // construct a sender
128 QueueSender sender = (QueueSender)
129 SessionHelper.createProducer(context.getSession(),
130 getDestination(DESTINATION));
131
132 assertEquals("Default time-to-live incorrect for QueueSender",
133 timeToLive, sender.getTimeToLive());
134
135 verifySend(sender, context.getMessage(), timeToLive);
136 sender.close();
137 }
138
139 /***
140 * Verifies that the default time-to-live for a sender constructed
141 * with no destination equals <code>0</code>, and that the JMSExpiration
142 * property is set to <code>0</code> on send and receive when the default
143 * time-to-live is used.
144 *
145 * @jmscts.requirement message.expiration.send
146 * @jmscts.requirement message.expiration.receive
147 * @jmscts.requirement message.expiration.zero
148 * @jmscts.requirement producer.ttl.default
149 * @jmscts.message BytesMessage
150 * @throws Exception for any error
151 */
152 public void testDefaultTTLForAnonQueue() throws Exception {
153 final long timeToLive = 0;
154 TestContext context = getContext();
155 Queue queue = (Queue) getDestination(DESTINATION);
156 QueueSender sender = (QueueSender)
157 SessionHelper.createProducer(context.getSession(), null);
158
159 assertEquals("Default time-to-live incorrect for QueueSender "
160 + "created with an anonymous queue",
161 timeToLive, sender.getTimeToLive());
162
163 verifySendWithQueue(sender, queue, context.getMessage(),
164 timeToLive);
165 sender.close();
166 }
167
168 /***
169 * Verifies that the time-to-live can be set on a sender, that the value is
170 * used when no time-to-live is provided when a message is sent,
171 * and that the JMSExpiration message property is set correctly on send and
172 * receive
173 *
174 * @jmscts.requirement message.expiration.send
175 * @jmscts.requirement message.expiration.receive
176 * @jmscts.requirement producer.ttl.set
177 * @jmscts.message MapMessage
178 * @throws Exception for any error
179 */
180 public void testSetTTL() throws Exception {
181 final long timeToLive = TTL;
182 TestContext context = getContext();
183
184 // construct a sender, and set its ttl
185 QueueSender sender = (QueueSender)
186 SessionHelper.createProducer(context.getSession(),
187 getDestination(DESTINATION));
188 sender.setTimeToLive(timeToLive);
189 assertEquals("Failed to set the time-to-live on QueueSender",
190 timeToLive, sender.getTimeToLive());
191
192 verifySend(sender, context.getMessage(), timeToLive);
193 sender.close();
194 }
195
196 /***
197 * Verifies that the time-to-live can be set on a sender created
198 * with no default queue, that the value is used when no time-to-live
199 * is provided when a message is sent, and that the JMSExpiration property
200 * on the received message equals that sent
201 *
202 * @jmscts.requirement message.expiration.send
203 * @jmscts.requirement message.expiration.receive
204 * @jmscts.requirement producer.ttl.set
205 * @jmscts.message ObjectMessage
206 * @throws Exception for any error
207 */
208 public void testSetTTLWithAnonQueue() throws Exception {
209 final long timeToLive = TTL;
210 TestContext context = getContext();
211 Queue queue = (Queue) getDestination(DESTINATION);
212
213 // construct a sender, and set its ttl
214 QueueSender sender = (QueueSender)
215 SessionHelper.createProducer(context.getSession(), null);
216 sender.setTimeToLive(timeToLive);
217
218 assertEquals("Failed to set the time-to-live on QueueSender "
219 + "with an anonymous destination",
220 timeToLive, sender.getTimeToLive());
221
222 verifySendWithQueue(sender, queue, context.getMessage(), timeToLive);
223 sender.close();
224 }
225
226 /***
227 * Verifies that the time-to-live set at send is used when a
228 * message is sent, and that the JMSExpiration property on the received
229 * message equals that sent
230 *
231 * @jmscts.requirement message.expiration.send
232 * @jmscts.requirement message.expiration.receive
233 * @jmscts.message StreamMessage
234 * @throws Exception for any error
235 */
236 public void testSendWithTTL() throws Exception {
237 final long timeToLive = TTL;
238 TestContext context = getContext();
239 Message message = context.getMessage();
240 int deliveryMode = context.getMessagingBehaviour().getDeliveryMode();
241
242 // construct a sender
243 QueueSender sender = (QueueSender)
244 SessionHelper.createProducer(context.getSession(),
245 getDestination(DESTINATION));
246
247 // construct a receiver to consume the message
248 MessageReceiver receiver = createReceiver(DESTINATION);
249
250 try {
251 // now verify that a message sent via the sender has its
252 // JMSExpiration set correctly
253 long start = System.currentTimeMillis();
254 sender.send(message, deliveryMode, 1, timeToLive);
255 long end = System.currentTimeMillis();
256 checkExpiration(message, start, end, timeToLive);
257
258 // now receive the message and verify it has the same JMSExpiration
259 checkSameExpiration(message, receiver);
260 } finally {
261 close(receiver);
262 sender.close();
263 }
264 }
265
266 /***
267 * Verifies that the time-to-live set at send is used when a message is
268 * sent, using a QueueSender created with no default queue, and that the
269 * JMSExpiration property on the received message equals that sent
270 *
271 * @jmscts.requirement message.expiration.send
272 * @jmscts.requirement message.expiration.receive
273 * @jmscts.message TextMessage
274 * @throws Exception for any error
275 */
276 public void testSendWithTTLAndAnonQueue() throws Exception {
277 final long timeToLive = TTL;
278 TestContext context = getContext();
279 Message message = context.getMessage();
280 int deliveryMode = context.getMessagingBehaviour().getDeliveryMode();
281 Queue queue = (Queue) getDestination(DESTINATION);
282
283 // construct a sender
284 QueueSender sender = (QueueSender)
285 SessionHelper.createProducer(context.getSession(), null);
286
287 // construct a receiver to consume the message
288 MessageReceiver receiver = createReceiver(DESTINATION);
289
290 try {
291 // now verify that a message sent via the sender has its
292 // JMSExpiration set correctly
293 long start = System.currentTimeMillis();
294 sender.send(queue, message, deliveryMode, 1, timeToLive);
295 long end = System.currentTimeMillis();
296 checkExpiration(message, start, end, timeToLive);
297
298 // now receive the message and verify it has the same JMSExpiration
299 checkSameExpiration(message, receiver);
300 } finally {
301 close(receiver);
302 sender.close();
303 }
304 }
305
306 /***
307 * Sendes a message using the QueueSender.send(Message) method
308 * and verifies that the JMSExpiration property is set correctly on
309 * send, and is the same on receipt
310 *
311 * @param sender the sender
312 * @param message the message to send
313 * @param timeToLive the expected time-to-live
314 * @throws Exception for any error
315 */
316 protected void verifySend(QueueSender sender, Message message,
317 long timeToLive)
318 throws Exception {
319
320 MessageReceiver receiver = createReceiver(DESTINATION);
321
322 try {
323 long start = System.currentTimeMillis();
324 sender.send(message);
325 long end = System.currentTimeMillis();
326 checkExpiration(message, start, end, timeToLive);
327
328 // now receive the message and verify it has the same JMSExpiration
329 checkSameExpiration(message, receiver);
330 } finally {
331 close(receiver);
332 }
333 }
334
335 /***
336 * Sendes a message using the QueueSender.send(Queue, Message)
337 * method and verifies that the JMSExpiration property is set correctly
338 *
339 * @param sender the sender
340 * @param queue the queue to send to
341 * @param message the message to send
342 * @param timeToLive the expected time-to-live
343 * @throws Exception for any error
344 */
345 protected void verifySendWithQueue(QueueSender sender, Queue queue,
346 Message message, long timeToLive)
347 throws Exception {
348
349 MessageReceiver receiver = createReceiver(DESTINATION);
350
351 try {
352 long start = System.currentTimeMillis();
353 sender.send(queue, message);
354 long end = System.currentTimeMillis();
355 checkExpiration(message, start, end, timeToLive);
356
357 // now receive the message and verify it has the same JMSExpiration
358 checkSameExpiration(message, receiver);
359 } finally {
360 close(receiver);
361 }
362 }
363
364 }
This page was automatically generated by Maven