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: AbstractSendReceiveTestCase.java,v 1.9 2004/02/03 01:00:33 tanderson Exp $ 44 */ 45 package org.exolab.jmscts.core; 46 47 import java.util.List; 48 import java.util.Map; 49 50 import javax.jms.Destination; 51 import javax.jms.Message; 52 53 import org.apache.log4j.Category; 54 55 56 /*** 57 * This class provides a default implementation of the 58 * {@link SendReceiveTestCase} interface. 59 * 60 * @version $Revision: 1.9 $ $Date: 2004/02/03 01:00:33 $ 61 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a> 62 * @see SendReceiveTestCase 63 * @see SendReceiveTestRunner 64 */ 65 public abstract class AbstractSendReceiveTestCase 66 extends AbstractMessageTestCase implements SendReceiveTestCase { 67 68 /*** 69 * The destinations to test against. This is a map of Destination 70 * instances keyed by name. 71 */ 72 private Map _destinations = null; 73 74 /*** 75 * The logger 76 */ 77 private static final Category log = 78 Category.getInstance(AbstractSendReceiveTestCase.class); 79 80 81 /*** 82 * Construct an instance of this class for a specific test case 83 * 84 * @param name the name of test case 85 */ 86 public AbstractSendReceiveTestCase(String name) { 87 super(name); 88 } 89 90 /*** 91 * Returns the list of destination names used by this test case. These 92 * are used to pre-create destinations prior to running the test case. 93 * 94 * @return this implementation returns <code>null</code>. 95 */ 96 public String[] getDestinations() { 97 return null; 98 } 99 100 /*** 101 * Sets the pre-created destinations corresponding to those returned by 102 * {@link #getDestinations}. 103 * 104 * @param destinations a map of Destination instances keyed on their name 105 */ 106 public void setDestinations(Map destinations) { 107 _destinations = destinations; 108 } 109 110 /*** 111 * Returns the named destination 112 * 113 * @param name the destination name 114 * @return the destination, or null if none exists with the specified name 115 */ 116 public Destination getDestination(String name) { 117 return (Destination) _destinations.get(name); 118 } 119 120 /*** 121 * Return the delivery types to run this test case against 122 * 123 * This implementation returns the values of any 124 * <code>jmscts.delivery</code> javadoc tags associated with the test 125 * case. 126 * The none are specified, returns {@link DeliveryTypes#ALL} 127 * 128 * @return the delivery types to run this test case against 129 */ 130 public DeliveryTypes getDeliveryTypes() { 131 DeliveryTypes result = null; 132 String test = getName(); 133 String[] types = types = AttributeHelper.getAttributes( 134 getClass(), test, "jmscts.delivery", false); 135 136 if (types.length == 0) { 137 result = DeliveryTypes.ALL; 138 } else { 139 result = DeliveryTypes.fromString(types); 140 } 141 return result; 142 } 143 144 /*** 145 * Determines if this test case only supports durable topic subscribers. 146 * If false, it supports both durable and non-durable topic subscribers. 147 * Note that this does not prevent the test case from supporting queue 148 * consumers or browsers. 149 * 150 * This implementation returns the values of any 151 * <code>jmscts.durableonly</code> javadoc tags associated with the test 152 * case. 153 * The none are specified, returns <code>false</code> 154 * 155 * @return <code>true</code> if this test only supports durable topic 156 * subcribers; otherwise <code>false</code> 157 */ 158 public boolean getDurableOnly() { 159 String test = getName(); 160 return AttributeHelper.getBoolean(getClass(), test, 161 "jmscts.durableonly", false); 162 } 163 164 /*** 165 * Helper to send a message 166 * 167 * @param message the message to send 168 * @param destination the destination 169 * @throws Exception if the message cannot be sent. 170 */ 171 protected void send(Message message, Destination destination) 172 throws Exception { 173 send(getContext(), message, destination, 1); 174 } 175 176 /*** 177 * Helper to send a message 178 * 179 * @param message the message to send 180 * @param name the name of the destination 181 * @throws Exception if the message cannot be sent. 182 */ 183 protected void send(Message message, String name) throws Exception { 184 Destination destination = getDestination(name); 185 if (destination == null) { 186 throw new Exception("No destination associated with name=" 187 + name + " exists"); 188 } 189 send(message, destination); 190 } 191 192 /*** 193 * Helper to send messages to a destination 194 * 195 * @param destination the destination 196 * @param count the no. of messages to send 197 * @throws Exception if messages cannot be sent. 198 */ 199 protected void send(Destination destination, int count) throws Exception { 200 send(getContext(), destination, count); 201 } 202 203 /*** 204 * Helper to send messages to a destination 205 * 206 * @param name the destination name. This must correspond to a destination 207 * returned by {@link #getDestinations}. 208 * @param count the no. of messages to send 209 * @throws Exception if messages cannot be sent. 210 */ 211 protected void send(String name, int count) throws Exception { 212 Destination destination = getDestination(name); 213 if (destination == null) { 214 throw new Exception("No destination associated with name=" 215 + name + " exists"); 216 } 217 send(destination, count); 218 } 219 220 /*** 221 * Helper to send messages to a destination 222 * 223 * @param context the context to use 224 * @param name the destination name. This must correspond to a destination 225 * returned by {@link #getDestinations}. 226 * @param count the no. of messages to send 227 * @throws Exception if messages cannot be sent. 228 */ 229 protected void send(TestContext context, String name, int count) 230 throws Exception { 231 Destination destination = getDestination(name); 232 if (destination == null) { 233 throw new Exception("No destination associated with name=" 234 + name + " exists"); 235 } 236 send(context, destination, count); 237 } 238 239 /*** 240 * Helper to send messages to a destination 241 * 242 * @param context the context to use 243 * @param destination the destination 244 * @param count the no. of messages to send 245 * @throws Exception if messages cannot be sent. 246 */ 247 protected void send(TestContext context, Destination destination, 248 int count) 249 throws Exception { 250 send(context, context.getMessage(), destination, count); 251 } 252 253 /*** 254 * Helper to send messages to a destination 255 * 256 * @param context the context to use 257 * @param message the message to send 258 * @param destination the destination 259 * @param count the no. of messages to send 260 * @throws Exception if messages cannot be sent. 261 */ 262 protected void send(TestContext context, Message message, 263 Destination destination, int count) 264 throws Exception { 265 MessagingHelper.send(context, message, destination, count); 266 } 267 268 /*** 269 * Helper to send a message to a destination, and return a message from 270 * the same destination. 271 * <p/> 272 * Note: the persistent state of the receiver is removed on completion, 273 * so tests for durable topic subcribers should not use this method if 274 * more than one message is expected. 275 * 276 * @param message the message to send 277 * @param destination the destination 278 * @return Message the message received from the destination 279 * @throws Exception if the message cannot be sent, no message is received, 280 * or more than one message is received. 281 */ 282 protected Message sendReceive(Message message, Destination destination) 283 throws Exception { 284 285 return MessagingHelper.sendReceive(getContext(), message, destination); 286 } 287 288 /*** 289 * Helper to send a message to a destination, and return a message from 290 * the same destination 291 * 292 * @param message the message to send 293 * @param name the destination name. This must correspond to a destination 294 * returned by {@link #getDestinations}. 295 * @return Message the message received from the destination 296 * @throws Exception if the message cannot be sent, no message is received, 297 * or more than one message is received. 298 */ 299 protected Message sendReceive(Message message, String name) 300 throws Exception { 301 302 Destination destination = getDestination(name); 303 if (destination == null) { 304 throw new Exception("No destination associated with name=" 305 + name + " exists"); 306 } 307 return sendReceive(message, destination); 308 } 309 310 311 /*** 312 * Helper to create a MessageReceiver for a single destination returned 313 * by {@link #getDestination}, using the default test context 314 * 315 * @param name the destination name 316 * @return the receiver 317 * @throws Exception for any error 318 */ 319 protected MessageReceiver createReceiver(String name) throws Exception { 320 return createReceiver(getContext(), name); 321 } 322 323 /*** 324 * Helper to create a MessageReceiver for a single destination returned 325 * by {@link #getDestination}, using the specified test context 326 * 327 * @param context the test context to use 328 * @param name the destination name 329 * @return the receiver 330 * @throws Exception for any error 331 */ 332 protected MessageReceiver createReceiver(TestContext context, String name) 333 throws Exception { 334 335 Destination destination = getDestination(name); 336 if (log.isDebugEnabled()) { 337 log.debug("Creating receiver for destination=" 338 + DestinationHelper.getName(destination)); 339 } 340 return SessionHelper.createReceiver(context, destination); 341 } 342 343 /*** 344 * Helper to create MessageReceiver instances for each destination returned 345 * by {@link #getDestinations} 346 * 347 * @return a list of message receivers 348 * @throws Exception for any error 349 */ 350 protected MessageReceiver[] createReceivers() throws Exception { 351 return createReceivers(getContext()); 352 } 353 354 /*** 355 * Helper to create MessageReceiver instances for each destination returned 356 * by {@link #getDestinations} 357 * 358 * @param context the test context to use 359 * @return a list of message receivers 360 * @throws Exception for any error 361 */ 362 protected MessageReceiver[] createReceivers(TestContext context) 363 throws Exception { 364 String[] destinations = getDestinations(); 365 MessageReceiver[] receivers = new MessageReceiver[destinations.length]; 366 for (int i = 0; i < destinations.length; ++i) { 367 receivers[i] = createReceiver(context, destinations[i]); 368 } 369 return receivers; 370 } 371 372 /*** 373 * Helper to recreate a MessageReceiver. This closes the receiver, 374 * and recreates it to consume from the same destination, selector, 375 * and for durable topic subscribers, subscription and no-local value 376 * 377 * @param receiver the receiver to re-create 378 * @return the re-created receiver 379 * @throws Exception for any error 380 */ 381 protected MessageReceiver recreate(MessageReceiver receiver) 382 throws Exception { 383 384 Destination destination = receiver.getDestination(); 385 String name = receiver.getName(); 386 String selector = receiver.getSelector(); 387 boolean noLocal = receiver.getNoLocal(); 388 receiver.close(); 389 return SessionHelper.createReceiver(getContext(), destination, name, 390 selector, noLocal); 391 } 392 393 /*** 394 * Helper to create a MessageSender for a single destination returned 395 * by {@link #getDestination}, using the default test context 396 * 397 * @param name the destination name 398 * @return the sender 399 * @throws Exception for any error 400 */ 401 protected MessageSender createSender(String name) throws Exception { 402 return createSender(getContext(), name); 403 } 404 405 /*** 406 * Helper to create a MessageSender for a single destination returned 407 * by {@link #getDestination}, using the specified test context 408 * 409 * @param context the test context to use 410 * @param name the destination name 411 * @return the sender 412 * @throws Exception for any error 413 */ 414 protected MessageSender createSender(TestContext context, String name) 415 throws Exception { 416 Destination destination = getDestination(name); 417 if (log.isDebugEnabled()) { 418 log.debug("Creating sender for destination=" 419 + DestinationHelper.getName(destination)); 420 } 421 return SessionHelper.createSender(context, destination); 422 } 423 424 /*** 425 * Helper to create MessageSender instances for each destination returned 426 * by {@link #getDestinations} 427 * 428 * @return a list of message senders 429 * @throws Exception for any error 430 */ 431 protected MessageSender[] createSenders() throws Exception { 432 return createSenders(getContext()); 433 } 434 435 /*** 436 * Helper to create MessageSender instances for each destination returned 437 * by {@link #getDestinations} 438 * 439 * @param context the test context to use 440 * @return a list of message senders 441 * @throws Exception for any error 442 */ 443 protected MessageSender[] createSenders(TestContext context) 444 throws Exception { 445 String[] destinations = getDestinations(); 446 MessageSender[] senders = new MessageSender[destinations.length]; 447 for (int i = 0; i < destinations.length; ++i) { 448 senders[i] = createSender(context, destinations[i]); 449 } 450 return senders; 451 } 452 453 /*** 454 * Helper to verify that each consumer receives the correct number of 455 * messages 456 * 457 * @param receivers a list of {@link MessageReceiver} instances 458 * @param count the number of expected messages 459 * @throws Exception for any error 460 */ 461 protected void receive(MessageReceiver[] receivers, int count) 462 throws Exception { 463 TestContext context = getContext(); 464 for (int i = 0; i < receivers.length; ++i) { 465 receive(context, receivers[i], count); 466 } 467 } 468 469 /*** 470 * Helper to verify that each consumer receives the correct number of 471 * messages 472 * 473 * @param context the test context to use 474 * @param receivers a list of {@link MessageReceiver} instances 475 * @param count the number of expected messages 476 * @throws Exception for any error 477 */ 478 protected void receive(TestContext context, MessageReceiver[] receivers, 479 int count) throws Exception { 480 for (int i = 0; i < receivers.length; ++i) { 481 receive(context, receivers[i], count); 482 } 483 } 484 485 /*** 486 * Helper to verify that a consumer receives a single message 487 * 488 * @param receiver the message receiver 489 * @return the received message 490 * @throws Exception for any error 491 */ 492 protected Message receive(MessageReceiver receiver) 493 throws Exception { 494 return receive(getContext(), receiver); 495 } 496 497 /*** 498 * Helper to verify that a consumer receives a single message 499 * 500 * @param context the test context to use 501 * @param receiver the message receiver 502 * @return the received message 503 * @throws Exception for any error 504 */ 505 protected Message receive(TestContext context, MessageReceiver receiver) 506 throws Exception { 507 List messages = receive(context, receiver, 1); 508 return (Message) messages.get(0); 509 } 510 511 /*** 512 * Helper to verify that a consumer receives the correct number of 513 * messages 514 * 515 * @param receiver the message receiver 516 * @param count the number of expected messages 517 * @return the list of messages received, or null, if none were expected 518 * @throws Exception for any error 519 */ 520 protected List receive(MessageReceiver receiver, int count) 521 throws Exception { 522 return receive(getContext(), receiver, count); 523 } 524 525 /*** 526 * Helper to verify that a consumer receives the correct number of 527 * messages 528 * 529 * @param context the test context to use 530 * @param receiver the message receiver 531 * @param count the number of expected messages 532 * @return the list of messages received, or null, if none were expected 533 * @throws Exception for any error 534 */ 535 protected List receive(TestContext context, MessageReceiver receiver, 536 int count) throws Exception { 537 String name = DestinationHelper.getName(receiver.getDestination()); 538 long timeout = context.getMessagingBehaviour().getTimeout(); 539 540 List result = receiver.receive(count, timeout); 541 if (result == null) { 542 if (count != 0) { 543 String msg = "Failed to receive any messages from " 544 + "destination=" + name + ". Test context=" + context; 545 fail(msg); 546 } 547 } else if (result.size() != count) { 548 String msg = "Expected " + count + " messages from destination=" 549 + name + ", but got " + result.size() + ". Test context=" 550 + context; 551 fail(msg); 552 } 553 return result; 554 } 555 556 /*** 557 * Helper to send messages to each destination 558 * 559 * @param senders a list of {@link MessageSender} instances 560 * @param message the message to send 561 * @param count the number of messages to send 562 * @throws Exception for any error 563 */ 564 protected void send(MessageSender[] senders, Message message, int count) 565 throws Exception { 566 567 for (int i = 0; i < senders.length; ++i) { 568 senders[i].send(message, count); 569 } 570 } 571 572 /*** 573 * Helper to close a list of MessageReceivers 574 * The references are removed from the list. 575 * 576 * @param receivers a list of {@link MessageReceiver} instances 577 * @throws Exception for any error 578 */ 579 protected void close(MessageReceiver[] receivers) throws Exception { 580 for (int i = 0; i < receivers.length; ++i) { 581 close(receivers[i]); 582 receivers[i] = null; 583 } 584 } 585 586 /*** 587 * Helper to close a MessageReceiver 588 * 589 * @param receiver the receiver 590 * @throws Exception for any error 591 */ 592 protected void close(MessageReceiver receiver) throws Exception { 593 if (receiver != null) { 594 if (log.isDebugEnabled()) { 595 Destination destination = receiver.getDestination(); 596 log.debug("Closing receiver for destination=" 597 + DestinationHelper.getName(destination)); 598 } 599 // invoke remove to destroy any durable topic subscription 600 receiver.remove(); 601 } 602 } 603 604 /*** 605 * Helper to close a list of MessageSenders. 606 * The references are removed from the list. 607 * 608 * @param senders a list of {@link MessageSender} instances 609 * @throws Exception for any error 610 */ 611 protected void close(MessageSender[] senders) throws Exception { 612 for (int i = 0; i < senders.length; ++i) { 613 close(senders[i]); 614 senders[i] = null; 615 } 616 } 617 618 /*** 619 * Helper to close a MessageSender 620 * 621 * @param sender the sender 622 * @throws Exception for any error 623 */ 624 protected void close(MessageSender sender) throws Exception { 625 if (sender != null) { 626 if (log.isDebugEnabled()) { 627 Destination destination = sender.getDestination(); 628 log.debug("Closing sender for destination=" 629 + DestinationHelper.getName(destination)); 630 } 631 sender.close(); 632 } 633 } 634 635 }

This page was automatically generated by Maven