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: RollbackTest.java,v 1.9 2004/02/03 21:52:12 tanderson Exp $
44 */
45 package org.exolab.jmscts.test.session.transacted;
46
47 import java.util.Iterator;
48 import java.util.List;
49
50 import javax.jms.IllegalStateException;
51 import javax.jms.Message;
52 import javax.jms.Session;
53
54 import org.apache.log4j.Category;
55
56 import junit.framework.Test;
57
58 import org.exolab.jmscts.core.AckTypes;
59 import org.exolab.jmscts.core.AbstractSendReceiveTestCase;
60 import org.exolab.jmscts.core.ConnectionHelper;
61 import org.exolab.jmscts.core.MessageReceiver;
62 import org.exolab.jmscts.core.MessageSender;
63 import org.exolab.jmscts.core.MessagingBehaviour;
64 import org.exolab.jmscts.core.ReceiptType;
65 import org.exolab.jmscts.core.TestContext;
66 import org.exolab.jmscts.core.TestCreator;
67
68
69 /***
70 * This class tests session rollback functionality.
71 *
72 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
73 * @version $Revision: 1.9 $
74 * @jmscts.session TRANSACTED
75 * @jmscts.message MapMessage
76 */
77 public class RollbackTest extends AbstractSendReceiveTestCase {
78
79 /***
80 * The destinations to create prior to running the test
81 */
82 private static final String[] DESTINATIONS = {"rollback1", "rollback2",
83 "rollback3"};
84
85 /***
86 * The logger
87 */
88 private static final Category log =
89 Category.getInstance(RollbackTest.class);
90
91 /***
92 * Construct a new <code>RollbackTest</code>
93 *
94 * @param name the name of test case
95 */
96 public RollbackTest(String name) {
97 super(name);
98 }
99
100 /***
101 * Sets up the test suite.
102 *
103 * @return an instance of this class that may be run by
104 * {@link org.exolab.jmscts.core.JMSTestRunner}
105 */
106 public static Test suite() {
107 return TestCreator.createSendReceiveTest(RollbackTest.class);
108 }
109
110 /***
111 * Returns the list of destination names used by this test case. These
112 * are used to pre-administer destinations prior to running the test case.
113 *
114 * @return the list of destinations used by this test case
115 */
116 public String[] getDestinations() {
117 return DESTINATIONS;
118 }
119
120 /***
121 * Verifies session rollback behaviour
122 *
123 * @jmscts.requirement session.rollback
124 * @throws Exception for any error
125 */
126 public void testRollback() throws Exception {
127 final int count = 10; // send count messages to each destination
128
129 TestContext context = getContext();
130 Session session = context.getSession();
131 Message message = context.getMessage();
132 MessagingBehaviour behaviour = context.getMessagingBehaviour();
133 boolean isBrowser = false;
134 if (behaviour.getReceiptType() == ReceiptType.BROWSER) {
135 isBrowser = true;
136 }
137
138 // create the senders and receivers
139 MessageSender[] senders = createSenders();
140 MessageReceiver[] receivers = createReceivers();
141
142
143 try {
144 // send the messages
145 send(senders, message, count);
146
147 // roll back the session. All sent messages should be discarded
148 session.rollback();
149
150 // verify that the receivers cannot receive any messages
151 receive(receivers, 0);
152
153 // send the messages and commit the session. The receivers should
154 // be able to receive all messages
155 send(senders, message, count);
156 session.commit();
157
158 for (int i = 0; i < receivers.length; ++i) {
159 List messages = receive(receivers[i], count);
160 if (!isBrowser) {
161 // verify that the redelivered flag isn't set.
162 // The state of the flag is not defined for QueueBrowsers
163 checkRedelivered(messages, false);
164 }
165 }
166
167 // rollback the session, and verify that the receivers can
168 // re-receive all messages
169 session.rollback();
170
171 for (int i = 0; i < receivers.length; ++i) {
172 List messages = receive(receivers[i], count);
173 if (!isBrowser) {
174 // verify that the redelivered flag is set
175 // The state of the flag is not defined for QueueBrowsers
176 checkRedelivered(messages, true);
177 }
178 }
179 } finally {
180 close(senders);
181 close(receivers);
182 }
183 }
184
185 /***
186 * Verifies that calling <code>Session.rollback()</code> for a closed
187 * session throws IllegalStateException
188 *
189 * @jmscts.requirement session.close
190 * @throws Exception for any error
191 */
192 public void testClose() throws Exception {
193 TestContext context = getContext();
194 Session session = ConnectionHelper.createSession(
195 context, AckTypes.TRANSACTED);
196 session.close();
197 try {
198 session.rollback();
199 String msg = "Session.rollback() for a closed session should "
200 + " throw " + IllegalStateException.class.getName();
201 log.debug(msg);
202 fail(msg);
203 } catch (IllegalStateException expected) {
204 // the expected behaviour
205 } catch (Exception exception) {
206 String msg = "Session.rollback() for a closed session should "
207 + " throw " + IllegalStateException.class.getName()
208 + ". Thrown exception=" + exception.getClass().getName();
209 log.debug(msg, exception);
210 fail(msg);
211 }
212 }
213
214 /***
215 * Helper to compare the JMSRedelivered property of a list of messages
216 * against that expected
217 *
218 * @param messages the list of messages to check
219 * @param redelivered if true, expect JMSRedelivered to be true
220 * @throws Exception if JMSRedivered is not equal to redelivered
221 */
222 private void checkRedelivered(List messages, boolean redelivered)
223 throws Exception {
224
225 TestContext context = getContext();
226 Iterator iter = messages.iterator();
227 while (iter.hasNext()) {
228 Message received = (Message) iter.next();
229 if (received.getJMSRedelivered() && !redelivered) {
230 String msg = "The JMSRedelivered property has been set for "
231 + "a message that has not been re-delivered";
232 log.debug(msg);
233 fail(msg);
234 } else if (!received.getJMSRedelivered() && redelivered) {
235 String msg = "The JMSRedelivered property must be set for a "
236 + "message that has been re-delivered";
237 log.debug(msg);
238 fail(msg);
239 }
240 }
241 }
242
243 }
This page was automatically generated by Maven