View Javadoc
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: MessagingHelper.java,v 1.4 2004/01/31 13:44:24 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.core; 46 47 import java.util.List; 48 49 import javax.jms.Destination; 50 import javax.jms.Message; 51 import javax.jms.Session; 52 import javax.jms.XASession; 53 54 55 /*** 56 * Helper class for performing messaging operations 57 * 58 * @version $Revision: 1.4 $ $Date: 2004/01/31 13:44:24 $ 59 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 60 * @see SessionHelper 61 * @see TestContext 62 */ 63 public final class MessagingHelper { 64 65 /*** 66 * Prevent construction of utility class 67 */ 68 private MessagingHelper() { 69 } 70 71 /*** 72 * Helper to send messages to a destination, using the message supplied by 73 * the test context 74 * <p> 75 * If the session is transactional, it will be committed. 76 * 77 * @param context the test context 78 * @param destination the destination 79 * @param count the number of times to send the message 80 * @throws Exception for any error 81 */ 82 public static void send(TestContext context, Destination destination, 83 int count) throws Exception { 84 if (context == null) { 85 throw new IllegalArgumentException("Argument 'context' is null"); 86 } 87 send(context, context.getMessage(), destination, count); 88 } 89 90 /*** 91 * Helper to send messages to a destination. 92 * <p> 93 * If the session is transactional, it will be committed. 94 * 95 * @param context the test context 96 * @param message the message to send 97 * @param destination the destination 98 * @param count the number of times to send the message 99 * @throws Exception for any error 100 */ 101 public static void send(TestContext context, Message message, 102 Destination destination, int count) 103 throws Exception { 104 105 if (context == null) { 106 throw new IllegalArgumentException("Argument 'context' is null"); 107 } 108 if (message == null) { 109 throw new IllegalArgumentException("Argument 'message' is null"); 110 } 111 if (destination == null) { 112 throw new IllegalArgumentException( 113 "Argument 'destination' is null"); 114 } 115 116 MessageSender sender = SessionHelper.createSender( 117 context, destination); 118 try { 119 sender.send(message, count); 120 Session session = context.getSession(); 121 if (session.getTransacted() && !(session instanceof XASession)) { 122 session.commit(); 123 } 124 } finally { 125 sender.close(); 126 } 127 } 128 129 /*** 130 * Helper to send a message to a destination, and return a message from 131 * the same destination. The message is obtained from the test context. 132 * <p> 133 * If the session is transactional, it will be committed. 134 * <br> 135 * Note: the persistent state of the receiver is removed on completion, 136 * so tests for durable topic subcribers should not use this method if 137 * more than one message is expected. 138 * 139 * @param context the test context 140 * @param destination the destination 141 * @return the message received from the destination 142 * @throws Exception if the message cannot be sent, no message is received, 143 * or more than one message is received. 144 */ 145 public static Message sendReceive(TestContext context, 146 Destination destination) 147 throws Exception { 148 if (context == null) { 149 throw new IllegalArgumentException("Argument 'context' is null"); 150 } 151 152 return sendReceive(context, context.getMessage(), destination); 153 } 154 155 /*** 156 * Helper to send a message to a destination, and return a message from 157 * the same destination. 158 * <p> 159 * If the session is transactional, it will be committed. 160 * <br> 161 * Note: the persistent state of the receiver is removed on completion, 162 * so tests for durable topic subcribers should not use this method if 163 * more than one message is expected. 164 * 165 * @param context the test context 166 * @param message the message to send 167 * @param destination the destination 168 * @return Message the message received from the destination 169 * @throws Exception if the message cannot be sent, no message is received, 170 * or more than one message is received. 171 */ 172 public static Message sendReceive(TestContext context, Message message, 173 Destination destination) 174 throws Exception { 175 if (context == null) { 176 throw new IllegalArgumentException("Argument 'context' is null"); 177 } 178 if (message == null) { 179 throw new IllegalArgumentException("Argument 'message' is null"); 180 } 181 if (destination == null) { 182 throw new IllegalArgumentException( 183 "Argument 'destination' is null"); 184 } 185 186 MessageReceiver receiver = null; 187 MessageSender sender = null; 188 List messages = null; 189 try { 190 receiver = SessionHelper.createReceiver(context, destination); 191 sender = SessionHelper.createSender(context, destination); 192 sender.send(message, 1); 193 Session session = context.getSession(); 194 if (session.getTransacted()) { 195 session.commit(); 196 } 197 long timeout = context.getMessagingBehaviour().getTimeout(); 198 messages = receiver.receive(1, timeout); 199 if (messages == null) { 200 throw new Exception( 201 "Failed to receive a message from destination=" 202 + DestinationHelper.getName(destination)); 203 } 204 if (messages.size() != 1) { 205 throw new Exception("Expected one message from destination=" 206 + DestinationHelper.getName(destination) 207 + " but got " + messages.size()); 208 } 209 if (session.getTransacted()) { 210 session.commit(); 211 } 212 } finally { 213 if (receiver != null) { 214 receiver.remove(); 215 } 216 if (sender != null) { 217 sender.close(); 218 } 219 } 220 return (Message) messages.get(0); 221 } 222 223 /*** 224 * Helper to receive messages 225 * 226 * @param context the test context 227 * @param receiver the message receiver 228 * @param count the expected number of messages 229 * @return the list of messages, or null, if no messages were expected 230 * @throws Exception if the receive fails, or the wrong number of 231 * messages were returned 232 */ 233 public static List receive(TestContext context, MessageReceiver receiver, 234 int count) throws Exception { 235 if (context == null) { 236 throw new IllegalArgumentException("Argument 'context' is null"); 237 } 238 MessagingBehaviour behaviour = context.getMessagingBehaviour(); 239 if (behaviour == null) { 240 throw new IllegalArgumentException( 241 "Argument context has no MessagingBehaviour instance"); 242 } 243 244 return receive(receiver, count, behaviour.getTimeout()); 245 } 246 247 /*** 248 * Helper to receive messages 249 * 250 * @param receiver the message receiver 251 * @param count the expected number of messages 252 * @param timeout the maximum time to wait for each message 253 * @return the list of messages, or null, if no messages were expected 254 * @throws Exception if the receive fails, or the wrong number of 255 * messages were returned 256 */ 257 public static List receive(MessageReceiver receiver, int count, 258 long timeout) throws Exception { 259 List result = receiver.receive(count, timeout); 260 if (result == null) { 261 if (count != 0) { 262 throw new Exception( 263 "Failed to receive any messages from destination=" 264 + DestinationHelper.getName(receiver.getDestination())); 265 } 266 } else if (result.size() != count) { 267 throw new Exception( 268 "Expected " + count + " messages from destination=" 269 + DestinationHelper.getName(receiver.getDestination()) 270 + " but got " + result.size()); 271 } 272 return result; 273 } 274 275 }

This page was automatically generated by Maven