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: ClientAcknowledgeTestCase.java,v 1.7 2004/02/03 21:52:12 tanderson Exp $
44 */
45 package org.exolab.jmscts.test.session.clientack;
46
47 import java.util.Iterator;
48 import java.util.List;
49
50 import javax.jms.Message;
51
52 import org.apache.log4j.Category;
53
54 import org.exolab.jmscts.core.AbstractSendReceiveTestCase;
55 import org.exolab.jmscts.core.MessagePopulator;
56 import org.exolab.jmscts.core.MessageReceiver;
57 import org.exolab.jmscts.core.MessageSender;
58 import org.exolab.jmscts.core.SequencePropertyPopulator;
59 import org.exolab.jmscts.core.TestContext;
60
61
62 /***
63 * This class provides helper methods for CLIENT_ACKNOWLEDGE test cases
64 *
65 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
66 * @version $Revision: 1.7 $
67 */
68 abstract class ClientAcknowledgeTestCase extends AbstractSendReceiveTestCase {
69
70 /***
71 * The logger
72 */
73 private static final Category log =
74 Category.getInstance(ClientAcknowledgeTestCase.class);
75
76
77 /***
78 * Construct an instance of this class for a specific test case
79 *
80 * @param name the name of test case
81 */
82 public ClientAcknowledgeTestCase(String name) {
83 super(name);
84 }
85
86 /***
87 * Helper to send messages to each destination, setting the JMSXGroupID
88 * and JMSXGroupSeq properties for each message
89 *
90 * @param count the number of messages to send
91 * @throws Exception for any error
92 */
93 protected void send(int count) throws Exception {
94 send(getContext(), count);
95 }
96
97 /***
98 * Helper to send messages to each destination, setting the JMSXGroupID
99 * and JMSXGroupSeq properties for each message
100 *
101 * @param context the test context to use
102 * @param count the number of messages to send
103 * @throws Exception for any error
104 */
105 protected void send(TestContext context, int count) throws Exception {
106 Message message = context.getMessage();
107
108 String[] destinations = getDestinations();
109 for (int i = 0; i < destinations.length; ++i) {
110 String name = destinations[i];
111 MessageSender sender = createSender(name);
112
113 // use the destination name as the group name
114
115 String group = name;
116 MessagePopulator populator = new SequencePropertyPopulator(group);
117
118 // send count messages, setting the JMSXGroupID and
119 // JMSXGroupSeq properties for each message
120 sender.send(message, count, populator);
121 sender.close();
122 }
123 }
124
125 /***
126 * Helper to receive messages from a list of receivers and verify they
127 * are in the correct sequence.
128 *
129 * @param receivers the list of receivers to receive messages with.
130 * @param count the number of expected messages to receive
131 * @param sequence the starting sequence
132 * @param redelivered the expected value of JMSRedelivered
133 * @return the last message returned by the session
134 * @throws Exception for any error
135 */
136 protected Message receive(MessageReceiver[] receivers, int count,
137 int sequence, boolean redelivered)
138 throws Exception {
139 return receive(getContext(), receivers, count, sequence, redelivered);
140 }
141
142 /***
143 * Helper to receive messages from a list of receivers and verify they
144 * are in the correct sequence.
145 *
146 * @param context the test context to use
147 * @param receivers the list of receivers to receive messages with.
148 * @param count the number of expected messages to receive
149 * @param sequence the starting sequence
150 * @param redelivered the expected value of JMSRedelivered
151 * @return the last message returned by the session
152 * @throws Exception for any error
153 */
154 protected Message receive(TestContext context, MessageReceiver[] receivers,
155 int count, int sequence, boolean redelivered)
156 throws Exception {
157
158 Message last = null;
159 for (int i = 0; i < receivers.length; ++i) {
160 List messages = receive(context, receivers[i], count);
161
162 // verify that each message has the correct properties
163 String name = getDestinations()[i];
164 checkProperties(messages, name, sequence, redelivered);
165
166 last = (Message) messages.get(count - 1);
167 }
168 return last;
169 }
170
171 /***
172 * Verifies that the JMSXGroupID and JMXSGroupSeq properties are set
173 * and that the messages are in sequence
174 *
175 * @param messages the list of messages to verify
176 * @param group the expected value of JMSXGroupID
177 * @param sequence the starting sequence number
178 * @param redelivered the expected value of JMSRedelivered
179 * @throws Exception for any error
180 */
181 protected void checkProperties(List messages, String group, int sequence,
182 boolean redelivered) throws Exception {
183 String groupProperty = SequencePropertyPopulator.GROUP_ID;
184 String seqProperty = SequencePropertyPopulator.GROUP_SEQ;
185 Iterator iter = messages.iterator();
186 while (iter.hasNext()) {
187 Message message = (Message) iter.next();
188 String groupValue = message.getStringProperty(groupProperty);
189 if (!group.equals(groupValue)) {
190 String msg = "Expected " + groupProperty + "=" + group
191 + " but got " + groupValue;
192 fail(msg);
193 }
194 int seqValue = message.getIntProperty(seqProperty);
195 if (sequence != seqValue) {
196 String msg = "Expected " + seqProperty + "=" + sequence
197 + " but got " + seqValue;
198 fail(msg);
199 }
200 ++sequence;
201 if (message.getJMSRedelivered() != redelivered) {
202 fail("Expected message to have JMSRedelivered="
203 + redelivered + ", but got JMSRedelivered="
204 + message.getJMSRedelivered());
205 }
206 }
207 }
208
209 }
This page was automatically generated by Maven