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 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: DurableSubscriberTest.java,v 1.9 2004/02/03 21:52:12 tanderson Exp $
44   */
45  package org.exolab.jmscts.test.topic;
46  
47  import javax.jms.Destination;
48  import javax.jms.Session;
49  import javax.jms.TopicSession;
50  
51  import junit.framework.Test;
52  
53  import org.exolab.jmscts.core.AbstractSendReceiveTestCase;
54  import org.exolab.jmscts.core.MessageReceiver;
55  import org.exolab.jmscts.core.MessagingHelper;
56  import org.exolab.jmscts.core.SessionHelper;
57  import org.exolab.jmscts.core.TestCreator;
58  import org.exolab.jmscts.core.TestContext;
59  
60  
61  /***
62   * This class tests the behaviour of durable TopicSubscribers
63   * @todo - needs to be modified when OpenJMS supports clientId
64   *
65   * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
66   * @version $Revision: 1.9 $
67   * @jmscts.factory TopicConnectionFactory
68   * @jmscts.factory XATopicConnectionFactory
69   * @jmscts.message TextMessage
70   * @jmscts.delivery PERSISTENT, administered, synchronous
71   * @jmscts.delivery PERSISTENT, administered, asynchronous
72   * @jmscts.durableonly true
73   */
74  public class DurableSubscriberTest extends AbstractSendReceiveTestCase {
75  
76      /***
77       * The destinations to create prior to running the test
78       */
79      private static final String[] DESTINATIONS = {
80          "DurableSubscriberTest1", "DurableSubscriberTest2"};
81  
82      /***
83       * Construct a new <code>DurableSubscriberTest</code>
84       *
85       * @param name the name of test case
86       */
87      public DurableSubscriberTest(String name) {
88          super(name);
89      }
90  
91      /***
92       * Sets up the test suite
93       *
94       * @return an instance of this class that may be run by
95       * {@link org.exolab.jmscts.core.JMSTestRunner}
96       */
97      public static Test suite() {
98          return TestCreator.createSendReceiveTest(DurableSubscriberTest.class);
99      }
100 
101     /***
102      * Returns the list of destination names used by this test case. These
103      * are used to pre-create destinations prior to running the test case.
104      *
105      * @return the list of destinations used by this test case
106      */
107     public String[] getDestinations() {
108         return DESTINATIONS;
109     }
110 
111     /***
112      * Verifies that a durable subscriber can be created for a session,
113      * closed, and recreated.
114      *
115      * @jmscts.requirement subscriber.durable
116      * @throws Exception for any error
117      */
118     public void testDurableSubscriber() throws Exception {
119         final String name = "durableSubscriber";
120         final int count = 10;
121 
122         TestContext context = getContext();
123         Destination destination = getDestination(DESTINATIONS[0]);
124         Session session = context.getSession();
125         MessageReceiver subscriber = null;
126 
127         subscriber = SessionHelper.createReceiver(context, destination, name,
128                                                   null, false);
129         MessagingHelper.send(context, destination, count);
130 
131         subscriber.close();
132 
133         subscriber = SessionHelper.createReceiver(context, destination, name,
134                                                   null, false);
135         MessagingHelper.receive(context, subscriber, count);
136 
137         if (session.getTransacted()) {
138             // commit the session, as it is shared across test cases
139             session.commit();
140         }
141 
142         subscriber.remove();
143     }
144 
145     /***
146      * Verifies that a client can change an existing durable subscription by
147      * creating a durable subscriber with the same name and a new topic.
148      *
149      * @jmscts.requirement subscriber.durable.changing
150      * @throws Exception for any error
151      */
152     public void testChangeSubscription() throws Exception {
153         final String name = "changeSubscription";
154         final int count = 10;
155 
156         TestContext context = getContext();
157         Destination destination1 = getDestination(DESTINATIONS[0]);
158         Destination destination2 = getDestination(DESTINATIONS[1]);
159         Session session = context.getSession();
160         MessageReceiver subscriber = null;
161 
162         subscriber = SessionHelper.createReceiver(context, destination1, name,
163                                                   null, false);
164         MessagingHelper.send(context, destination1, count);
165 
166         subscriber.close();
167 
168         subscriber = SessionHelper.createReceiver(context, destination2, name,
169                                                   null, false);
170         // verify that the subscription has changed by ensuring that no
171         // messages are received.
172         MessagingHelper.receive(context, subscriber, 0);
173         MessagingHelper.send(context, destination2, count);
174         MessagingHelper.receive(context, subscriber, count);
175 
176         if (session.getTransacted()) {
177             // commit the session, as it is shared across test cases
178             session.commit();
179         }
180 
181         subscriber.remove();
182     }
183 
184     /***
185      * Verifies that a durable subscriber can be created for a session,
186      * unsubscribed, and recreated
187      *
188      * @jmscts.requirement subscriber.durable.unsubscribe
189      * @throws Exception for any error
190      */
191     public void testUnsubscribe() throws Exception {
192         final String name = "unsubscribe";
193         final int count = 10;
194 
195         TestContext context = getContext();
196         TopicSession session = (TopicSession) context.getSession();
197         Destination destination = getDestination(DESTINATIONS[0]);
198         MessageReceiver subscriber = null;
199 
200         subscriber = SessionHelper.createReceiver(context, destination, name,
201                                                   null, false);
202         MessagingHelper.send(context, destination, count);
203 
204         subscriber.close();
205         try {
206             session.unsubscribe(name);
207         } catch (Exception exception) {
208             fail("Failed to unsubscribe durable subscriber");
209         }
210 
211         // resubscribe. The specification does not provide reliability
212         // requirements, so no guarantee can be made if the subscriber will
213         // receive prior messages.
214         subscriber = SessionHelper.createReceiver(context, destination, name,
215                                                   null, false);
216         subscriber.remove();
217     }
218 
219 }
This page was automatically generated by Maven