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: TopicPublisherTest.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.Topic;
49  import javax.jms.TopicPublisher;
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>TopicPublisher</code>s
62   *
63   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
64   * @version $Revision: 1.3 $
65   * @jmscts.factory TopicConnectionFactory
66   * @jmscts.factory XATopicConnectionFactory
67   */
68  public class TopicPublisherTest extends TimeToLiveTestCase {
69  
70      /***
71       * The default destination
72       */
73      private static final String DESTINATION = "TopicPublisherTest";
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>TopicPublisherTest</code>
83       *
84       * @param name the name of test case
85       */
86      public TopicPublisherTest(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(TopicPublisherTest.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 publisher 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 publisher
128         TopicPublisher publisher = (TopicPublisher)
129             SessionHelper.createProducer(context.getSession(),
130                                          getDestination(DESTINATION));
131 
132         assertEquals("Default time-to-live incorrect for TopicPublisher",
133                      timeToLive, publisher.getTimeToLive());
134 
135         verifyPublish(publisher, context.getMessage(), timeToLive);
136         publisher.close();
137     }
138 
139     /***
140      * Verifies that the default time-to-live for a publisher 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 testDefaultTTLForAnonTopic() throws Exception {
153         final long timeToLive = 0;
154         TestContext context = getContext();
155         Topic topic = (Topic) getDestination(DESTINATION);
156         TopicPublisher publisher = (TopicPublisher)
157             SessionHelper.createProducer(context.getSession(), null);
158 
159         assertEquals("Default time-to-live incorrect for TopicPublisher "
160                      + "created with an anonymous topic",
161                      timeToLive, publisher.getTimeToLive());
162 
163         verifyPublishWithTopic(publisher, topic, context.getMessage(),
164                                timeToLive);
165         publisher.close();
166     }
167 
168     /***
169      * Verifies that the time-to-live can be set on a publisher,
170      * that the value is used when no time-to-live is provided when a
171      * message is sent, and that the JMSExpiration property on the received
172      * message equals that sent.
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 publisher, and set its ttl
185         TopicPublisher publisher = (TopicPublisher)
186             SessionHelper.createProducer(context.getSession(),
187                                          getDestination(DESTINATION));
188         publisher.setTimeToLive(timeToLive);
189         assertEquals("Failed to set the time-to-live on TopicPublisher",
190                      timeToLive, publisher.getTimeToLive());
191 
192         verifyPublish(publisher, context.getMessage(), timeToLive);
193         publisher.close();
194     }
195 
196     /***
197      * Verifies that the time-to-live can be set on a publisher created
198      * with no default topic, 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 testSetTTLWithAnonTopic() throws Exception {
209         final long timeToLive = TTL;
210         TestContext context = getContext();
211         Topic topic = (Topic) getDestination(DESTINATION);
212 
213         // construct a publisher, and set its ttl
214         TopicPublisher publisher = (TopicPublisher)
215             SessionHelper.createProducer(context.getSession(), null);
216         publisher.setTimeToLive(timeToLive);
217 
218         assertEquals("Failed to set the time-to-live on TopicPublisher "
219                      + "with an anonymous destination",
220                      timeToLive, publisher.getTimeToLive());
221 
222         verifyPublishWithTopic(publisher, topic, context.getMessage(),
223                                timeToLive);
224         publisher.close();
225     }
226 
227     /***
228      * Verifies that the time-to-live set at publication is used when a
229      * message is sent, and that the JMSExpiration property on the received
230      * message equals that sent.
231      *
232      * @jmscts.requirement message.expiration.send
233      * @jmscts.requirement message.expiration.receive
234      * @jmscts.message StreamMessage
235      * @throws Exception for any error
236      */
237     public void testPublishWithTTL() throws Exception {
238         final long timeToLive = TTL;
239         TestContext context = getContext();
240         Message message = context.getMessage();
241         int deliveryMode = context.getMessagingBehaviour().getDeliveryMode();
242 
243         // construct a publisher
244         TopicPublisher publisher = (TopicPublisher)
245             SessionHelper.createProducer(context.getSession(),
246                                          getDestination(DESTINATION));
247 
248         // construct a receiver to consume the message
249         MessageReceiver receiver = createReceiver(DESTINATION);
250 
251         try {
252             // now verify that a message sent via the publisher has its
253             // JMSExpiration set correctly
254             long start = System.currentTimeMillis();
255             publisher.publish(message, deliveryMode, 1, timeToLive);
256             long end = System.currentTimeMillis();
257             checkExpiration(message, start, end, timeToLive);
258 
259             // now receive the message and verify it has the same JMSExpiration
260             checkSameExpiration(message, receiver);
261         } finally {
262             close(receiver);
263             publisher.close();
264         }
265     }
266 
267     /***
268      * Verifies that the time-to-live set at publication is used when a
269      * message is sent, using a TopicPublisher created with no default topic,
270      * and that the JMSExpiration property on the received message equals
271      * that sent.
272      *
273      * @jmscts.requirement message.expiration.send
274      * @jmscts.requirement message.expiration.receive
275      * @jmscts.message TextMessage
276      * @throws Exception for any error
277      */
278     public void testPublishWithTTLAndAnonTopic() throws Exception {
279         final long timeToLive = TTL;
280         TestContext context = getContext();
281         Message message = context.getMessage();
282         int deliveryMode = context.getMessagingBehaviour().getDeliveryMode();
283         Topic topic = (Topic) getDestination(DESTINATION);
284 
285         // construct a publisher
286         TopicPublisher publisher = (TopicPublisher)
287             SessionHelper.createProducer(context.getSession(), null);
288 
289         // construct a receiver to consume the message
290         MessageReceiver receiver = createReceiver(DESTINATION);
291 
292         try {
293             // now verify that a message sent via the publisher has its
294             // JMSExpiration set correctly
295             long start = System.currentTimeMillis();
296             publisher.publish(topic, message, deliveryMode, 1, timeToLive);
297             long end = System.currentTimeMillis();
298             checkExpiration(message, start, end, timeToLive);
299 
300             // now receive the message and verify it has the same JMSExpiration
301             checkSameExpiration(message, receiver);
302         } finally {
303             close(receiver);
304             publisher.close();
305         }
306     }
307 
308     /***
309      * Publishes a message using the TopicPublisher.publish(Message) method
310      * and verifies that the JMSExpiration property is set correctly on
311      * publish, and is the same on receipt
312      *
313      * @param publisher the publisher
314      * @param message the message to publish
315      * @param timeToLive the expected time-to-live
316      * @throws Exception for any error
317      */
318     protected void verifyPublish(TopicPublisher publisher, Message message,
319                                  long timeToLive)
320         throws Exception {
321 
322         MessageReceiver receiver = createReceiver(DESTINATION);
323 
324         try {
325             long start = System.currentTimeMillis();
326             publisher.publish(message);
327             long end = System.currentTimeMillis();
328             checkExpiration(message, start, end, timeToLive);
329 
330             // now receive the message and verify it has the same JMSExpiration
331             checkSameExpiration(message, receiver);
332         } finally {
333             close(receiver);
334         }
335     }
336 
337     /***
338      * Publishes a message using the TopicPublisher.publish(Topic, Message)
339      * method and verifies that the JMSExpiration property is set correctly
340      *
341      * @param publisher the publisher
342      * @param topic the topic to publish to
343      * @param message the message to publish
344      * @param timeToLive the expected time-to-live
345      * @throws Exception for any error
346      */
347     protected void verifyPublishWithTopic(TopicPublisher publisher,
348                                           Topic topic, Message message,
349                                           long timeToLive)
350         throws Exception {
351 
352         MessageReceiver receiver = createReceiver(DESTINATION);
353 
354         try {
355             long start = System.currentTimeMillis();
356             publisher.publish(topic, message);
357             long end = System.currentTimeMillis();
358             checkExpiration(message, start, end, timeToLive);
359 
360             // now receive the message and verify it has the same JMSExpiration
361             checkSameExpiration(message, receiver);
362         } finally {
363             close(receiver);
364         }
365     }
366 
367 }
This page was automatically generated by Maven