1 16 17 package org.springframework.jms.core; 18 19 import java.io.PrintWriter ; 20 import java.io.StringWriter ; 21 22 import javax.jms.Connection ; 23 import javax.jms.ConnectionFactory ; 24 import javax.jms.DeliveryMode ; 25 import javax.jms.JMSException ; 26 import javax.jms.Message ; 27 import javax.jms.MessageConsumer ; 28 import javax.jms.MessageProducer ; 29 import javax.jms.Queue ; 30 import javax.jms.Session ; 31 import javax.jms.TextMessage ; 32 import javax.naming.Context ; 33 import javax.naming.NamingException ; 34 35 import junit.framework.TestCase; 36 import org.easymock.MockControl; 37 38 import org.springframework.jms.JmsException; 39 import org.springframework.jms.support.JmsUtils; 40 import org.springframework.jms.support.converter.SimpleMessageConverter; 41 import org.springframework.jms.support.destination.JndiDestinationResolver; 42 import org.springframework.jndi.JndiTemplate; 43 44 50 public class JmsTemplateTests extends TestCase { 51 52 private Context mockJndiContext; 53 private MockControl mockJndiControl; 54 55 private MockControl connectionFactoryControl; 56 private ConnectionFactory mockConnectionFactory; 57 58 private MockControl connectionControl; 59 private Connection mockConnection; 60 61 private MockControl sessionControl; 62 private Session mockSession; 63 64 private MockControl queueControl; 65 private Queue mockQueue; 66 67 private int deliveryMode = DeliveryMode.PERSISTENT; 68 private int priority = 9; 69 private int timeToLive = 10000; 70 71 72 75 protected void setUp() throws Exception { 76 mockJndiControl = MockControl.createControl(Context .class); 77 mockJndiContext = (Context ) this.mockJndiControl.getMock(); 78 79 createMockforDestination(); 80 81 mockJndiContext.close(); 82 mockJndiControl.replay(); 83 } 84 85 private void createMockforDestination() throws JMSException , NamingException { 86 connectionFactoryControl = 87 MockControl.createControl(ConnectionFactory .class); 88 mockConnectionFactory = 89 (ConnectionFactory ) connectionFactoryControl.getMock(); 90 91 connectionControl = MockControl.createControl(Connection .class); 92 mockConnection = (Connection ) connectionControl.getMock(); 93 94 sessionControl = MockControl.createControl(Session .class); 95 mockSession = (Session ) sessionControl.getMock(); 96 97 queueControl = MockControl.createControl(Queue .class); 98 mockQueue = (Queue ) queueControl.getMock(); 99 100 mockConnectionFactory.createConnection(); 101 connectionFactoryControl.setReturnValue(mockConnection); 102 connectionFactoryControl.replay(); 103 104 mockConnection.createSession(useTransactedTemplate(), Session.AUTO_ACKNOWLEDGE); 105 connectionControl.setReturnValue(mockSession); 106 mockSession.getTransacted(); 107 sessionControl.setReturnValue(useTransactedSession()); 108 109 mockJndiContext.lookup("testDestination"); 110 mockJndiControl.setReturnValue(mockQueue); 111 } 112 113 private JmsTemplate createTemplate() { 114 JmsTemplate template = new JmsTemplate(); 115 JndiDestinationResolver destMan = new JndiDestinationResolver(); 116 destMan.setJndiTemplate(new JndiTemplate() { 117 protected Context createInitialContext() throws NamingException { 118 return mockJndiContext; 119 } 120 }); 121 template.setDestinationResolver(destMan); 122 template.setSessionTransacted(useTransactedTemplate()); 123 return template; 124 } 125 126 protected boolean useTransactedSession() { 127 return false; 128 } 129 130 protected boolean useTransactedTemplate() { 131 return false; 132 } 133 134 135 public void testExceptionStackTrace() { 136 JMSException jmsEx = new JMSException ("could not connect"); 137 Exception innerEx = new Exception ("host not found"); 138 jmsEx.setLinkedException(innerEx); 139 JmsException springJmsEx = JmsUtils.convertJmsAccessException(jmsEx); 140 StringWriter sw = new StringWriter (); 141 PrintWriter out = new PrintWriter (sw); 142 springJmsEx.printStackTrace(out); 143 String trace = sw.toString(); 144 assertTrue("inner jms exception not found", trace.indexOf("host not found") > 0); 145 } 146 147 public void testProducerCallback() throws Exception { 148 JmsTemplate template = createTemplate(); 149 template.setConnectionFactory(mockConnectionFactory); 150 151 MockControl messageProducerControl = MockControl.createControl(MessageProducer .class); 152 MessageProducer mockMessageProducer = (MessageProducer ) messageProducerControl.getMock(); 153 154 mockSession.createProducer(null); 155 sessionControl.setReturnValue(mockMessageProducer); 156 157 mockMessageProducer.getPriority(); 158 messageProducerControl.setReturnValue(4); 159 160 mockMessageProducer.close(); 161 messageProducerControl.setVoidCallable(1); 162 mockSession.close(); 163 sessionControl.setVoidCallable(1); 164 mockConnection.close(); 165 connectionControl.setVoidCallable(1); 166 167 messageProducerControl.replay(); 168 sessionControl.replay(); 169 connectionControl.replay(); 170 171 template.execute(new ProducerCallback() { 172 public Object doInJms(Session session, MessageProducer msgProducer) throws JMSException { 173 boolean b = session.getTransacted(); 174 int i = msgProducer.getPriority(); 175 return null; 176 } 177 }); 178 179 connectionFactoryControl.verify(); 180 connectionControl.verify(); 181 sessionControl.verify(); 182 } 183 184 public void testProducerCallbackWithIdAndTimestampDisabled() throws Exception { 185 JmsTemplate template = createTemplate(); 186 template.setConnectionFactory(mockConnectionFactory); 187 template.setMessageIdEnabled(false); 188 template.setMessageTimestampEnabled(false); 189 190 MockControl messageProducerControl = MockControl.createControl(MessageProducer .class); 191 MessageProducer mockMessageProducer = (MessageProducer ) messageProducerControl.getMock(); 192 193 mockSession.createProducer(null); 194 sessionControl.setReturnValue(mockMessageProducer); 195 196 mockMessageProducer.setDisableMessageID(true); 197 messageProducerControl.setVoidCallable(1); 198 mockMessageProducer.setDisableMessageTimestamp(true); 199 messageProducerControl.setVoidCallable(1); 200 mockMessageProducer.getPriority(); 201 messageProducerControl.setReturnValue(4); 202 203 mockMessageProducer.close(); 204 messageProducerControl.setVoidCallable(1); 205 mockSession.close(); 206 sessionControl.setVoidCallable(1); 207 mockConnection.close(); 208 connectionControl.setVoidCallable(1); 209 210 messageProducerControl.replay(); 211 sessionControl.replay(); 212 connectionControl.replay(); 213 214 template.execute(new ProducerCallback() { 215 public Object doInJms(Session session, MessageProducer msgProducer) throws JMSException { 216 boolean b = session.getTransacted(); 217 int i = msgProducer.getPriority(); 218 return null; 219 } 220 }); 221 222 connectionFactoryControl.verify(); 223 connectionControl.verify(); 224 sessionControl.verify(); 225 } 226 227 230 public void testSessionCallback() throws Exception { 231 JmsTemplate template = createTemplate(); 232 template.setConnectionFactory(mockConnectionFactory); 233 234 mockSession.close(); 235 sessionControl.setVoidCallable(1); 236 237 mockConnection.close(); 238 connectionControl.setVoidCallable(1); 239 240 sessionControl.replay(); 241 connectionControl.replay(); 242 243 template.execute(new SessionCallback() { 244 public Object doInJms(Session session) throws JMSException { 245 boolean b = session.getTransacted(); 246 return null; 247 } 248 }); 249 250 connectionFactoryControl.verify(); 251 connectionControl.verify(); 252 sessionControl.verify(); 253 } 254 255 259 public void testSendDestination() throws Exception { 260 doTestSendDestination(true, false, true, false); 261 } 262 263 267 public void testSendDestinationName() throws Exception { 268 doTestSendDestination(false, false, true, false); 269 } 270 271 275 public void testSendDestinationWithQOS() throws Exception { 276 doTestSendDestination(true, false, false, true); 277 } 278 279 283 public void testSendDestinationNameWithQOS() throws Exception { 284 doTestSendDestination(false, false, false, true); 285 } 286 287 290 public void testSendDefaultDestination() throws Exception { 291 doTestSendDestination(true, true, true, true); 292 } 293 294 297 public void testSendDefaultDestinationName() throws Exception { 298 doTestSendDestination(false, true, true, true); 299 } 300 301 304 public void testSendDefaultDestinationWithQOS() throws Exception { 305 doTestSendDestination(true, true, false, false); 306 } 307 308 311 public void testSendDefaultDestinationNameWithQOS() throws Exception { 312 doTestSendDestination(false, true, false, false); 313 } 314 315 320 private void doTestSendDestination( 321 boolean explicitDestination, boolean useDefaultDestination, 322 boolean ignoreQOS, boolean disableIdAndTimestamp) throws Exception { 323 324 JmsTemplate template = createTemplate(); 325 template.setConnectionFactory(mockConnectionFactory); 326 327 String destinationName = "testDestination"; 328 329 if (useDefaultDestination) { 330 if (explicitDestination) { 331 template.setDefaultDestination(mockQueue); 332 } 333 else { 334 template.setDefaultDestinationName(destinationName); 335 } 336 } 337 if (disableIdAndTimestamp) { 338 template.setMessageIdEnabled(false); 339 template.setMessageTimestampEnabled(false); 340 } 341 342 MockControl messageProducerControl = MockControl.createControl(MessageProducer .class); 343 MessageProducer mockMessageProducer = (MessageProducer ) messageProducerControl.getMock(); 344 345 MockControl messageControl = MockControl.createControl(TextMessage .class); 346 TextMessage mockMessage = (TextMessage ) messageControl.getMock(); 347 348 mockSession.createProducer(mockQueue); 349 sessionControl.setReturnValue(mockMessageProducer); 350 mockSession.createTextMessage("just testing"); 351 sessionControl.setReturnValue(mockMessage); 352 353 if (useTransactedTemplate()) { 354 mockSession.commit(); 355 sessionControl.setVoidCallable(1); 356 } 357 358 if (disableIdAndTimestamp) { 359 mockMessageProducer.setDisableMessageID(true); 360 messageProducerControl.setVoidCallable(1); 361 mockMessageProducer.setDisableMessageTimestamp(true); 362 messageProducerControl.setVoidCallable(1); 363 } 364 365 if (ignoreQOS) { 366 mockMessageProducer.send(mockMessage); 367 } 368 else { 369 template.setExplicitQosEnabled(true); 370 template.setDeliveryMode(deliveryMode); 371 template.setPriority(priority); 372 template.setTimeToLive(timeToLive); 373 mockMessageProducer.send(mockMessage, deliveryMode, priority, timeToLive); 374 } 375 messageProducerControl.setVoidCallable(1); 376 377 mockMessageProducer.close(); 378 messageProducerControl.setVoidCallable(1); 379 mockSession.close(); 380 sessionControl.setVoidCallable(1); 381 mockConnection.close(); 382 connectionControl.setVoidCallable(1); 383 384 messageProducerControl.replay(); 385 sessionControl.replay(); 386 connectionControl.replay(); 387 388 if (useDefaultDestination) { 389 template.send(new MessageCreator() { 390 public Message createMessage(Session session) throws JMSException { 391 return session.createTextMessage("just testing"); 392 } 393 }); 394 } 395 else { 396 if (explicitDestination) { 397 template.send(mockQueue, new MessageCreator() { 398 public Message createMessage(Session session) 399 throws JMSException { 400 return session.createTextMessage("just testing"); 401 } 402 }); 403 } 404 else { 405 template.send(destinationName, new MessageCreator() { 406 public Message createMessage(Session session) 407 throws JMSException { 408 return session.createTextMessage("just testing"); 409 } 410 }); 411 } 412 } 413 414 connectionFactoryControl.verify(); 415 connectionControl.verify(); 416 sessionControl.verify(); 417 messageProducerControl.verify(); 418 } 419 420 public void testConverter() throws Exception { 421 JmsTemplate template = createTemplate(); 422 template.setConnectionFactory(mockConnectionFactory); 423 template.setMessageConverter(new SimpleMessageConverter()); 424 String s = "Hello world"; 425 426 MockControl messageProducerControl = MockControl.createControl(MessageProducer .class); 427 MessageProducer mockMessageProducer = (MessageProducer ) messageProducerControl.getMock(); 428 429 MockControl messageControl = MockControl.createControl(TextMessage .class); 430 TextMessage mockMessage = (TextMessage ) messageControl.getMock(); 431 432 mockSession.createProducer(mockQueue); 433 sessionControl.setReturnValue(mockMessageProducer); 434 mockSession.createTextMessage("Hello world"); 435 sessionControl.setReturnValue(mockMessage); 436 437 mockMessageProducer.send(mockMessage); 438 messageProducerControl.setVoidCallable(1); 439 mockMessageProducer.close(); 440 messageProducerControl.setVoidCallable(1); 441 442 if (useTransactedTemplate()) { 443 mockSession.commit(); 444 sessionControl.setVoidCallable(1); 445 } 446 447 mockSession.close(); 448 sessionControl.setVoidCallable(1); 449 mockConnection.close(); 450 connectionControl.setVoidCallable(1); 451 452 messageProducerControl.replay(); 453 sessionControl.replay(); 454 connectionControl.replay(); 455 456 template.convertAndSend(mockQueue, s); 457 458 messageProducerControl.verify(); 459 sessionControl.verify(); 460 connectionControl.verify(); 461 connectionFactoryControl.verify(); 462 } 463 464 public void testReceiveDefaultDestination() throws Exception { 465 doTestReceive(true, true, false, false, false, false, false); 466 } 467 468 public void testReceiveDefaultDestinationName() throws Exception { 469 doTestReceive(false, true, false, false, false, false, false); 470 } 471 472 public void testReceiveDestination() throws Exception { 473 doTestReceive(true, false, false, false, false, true, false); 474 } 475 476 public void testReceiveDestinationWithClientAcknowledge() throws Exception { 477 doTestReceive(true, false, false, true, false, false, true); 478 } 479 480 public void testReceiveDestinationName() throws Exception { 481 doTestReceive(false, false, false, false, false, true, true); 482 } 483 484 public void testReceiveDefaultDestinationWithSelector() throws Exception { 485 doTestReceive(true, true, false, false, true, true, true); 486 } 487 488 public void testReceiveDefaultDestinationNameWithSelector() throws Exception { 489 doTestReceive(false, true, false, false, true, true, true); 490 } 491 492 public void testReceiveDestinationWithSelector() throws Exception { 493 doTestReceive(true, false, false, false, true, false, true); 494 } 495 496 public void testReceiveDestinationWithClientAcknowledgeWithSelector() throws Exception { 497 doTestReceive(true, false, false, true, true, true, false); 498 } 499 500 public void testReceiveDestinationNameWithSelector() throws Exception { 501 doTestReceive(false, false, false, false, true, false, false); 502 } 503 504 public void testReceiveAndConvertDefaultDestination() throws Exception { 505 doTestReceive(true, true, true, false, false, false, true); 506 } 507 508 public void testReceiveAndConvertDefaultDestinationName() throws Exception { 509 doTestReceive(false, true, true, false, false, false, true); 510 } 511 512 public void testReceiveAndConvertDestinationName() throws Exception { 513 doTestReceive(false, false, true, false, false, true, false); 514 } 515 516 public void testReceiveAndConvertDestination() throws Exception { 517 doTestReceive(true, false, true, false, false, true, true); 518 } 519 520 public void testReceiveAndConvertDefaultDestinationWithSelector() throws Exception { 521 doTestReceive(true, true, true, false, true, true, true); 522 } 523 524 public void testReceiveAndConvertDestinationNameWithSelector() throws Exception { 525 doTestReceive(false, false, true, false, true, true, false); 526 } 527 528 public void testReceiveAndConvertDestinationWithSelector() throws Exception { 529 doTestReceive(true, false, true, false, true, false, true); 530 } 531 532 private void doTestReceive( 533 boolean explicitDestination, boolean useDefaultDestination, boolean testConverter, 534 boolean clientAcknowledge, boolean messageSelector, boolean noLocal, boolean timeout) 535 throws Exception { 536 537 JmsTemplate template = createTemplate(); 538 template.setConnectionFactory(mockConnectionFactory); 539 540 String destinationName = "testDestination"; 541 542 if (useDefaultDestination) { 543 if (explicitDestination) { 544 template.setDefaultDestination(mockQueue); 545 } 546 else { 547 template.setDefaultDestinationName(destinationName); 548 } 549 } 550 if (noLocal) { 551 template.setPubSubNoLocal(true); 552 } 553 if (timeout) { 554 template.setReceiveTimeout(1000); 555 } 556 557 mockConnection.start(); 558 connectionControl.setVoidCallable(1); 559 mockConnection.close(); 560 connectionControl.setVoidCallable(1); 561 562 MockControl messageConsumerControl = MockControl.createControl(MessageConsumer .class); 563 MessageConsumer mockMessageConsumer = (MessageConsumer ) messageConsumerControl.getMock(); 564 565 String selectorString = "selector"; 566 if (messageSelector) { 567 if (noLocal) { 568 mockSession.createConsumer(mockQueue, selectorString, true); 569 } 570 else { 571 mockSession.createConsumer(mockQueue, selectorString); 572 } 573 } 574 else { 575 if (noLocal) { 576 mockSession.createConsumer(mockQueue, null, true); 577 } 578 else { 579 mockSession.createConsumer(mockQueue); 580 } 581 } 582 sessionControl.setReturnValue(mockMessageConsumer); 583 584 if (useTransactedTemplate()) { 585 mockSession.commit(); 586 sessionControl.setVoidCallable(1); 587 } 588 else if (!useTransactedSession()) { 589 mockSession.getAcknowledgeMode(); 590 if (clientAcknowledge) { 591 sessionControl.setReturnValue(Session.CLIENT_ACKNOWLEDGE, 1); 592 } 593 else { 594 sessionControl.setReturnValue(Session.AUTO_ACKNOWLEDGE, 1); 595 } 596 } 597 598 mockSession.close(); 599 sessionControl.setVoidCallable(1); 600 601 MockControl messageControl = MockControl.createControl(TextMessage .class); 602 TextMessage mockMessage = (TextMessage ) messageControl.getMock(); 603 604 if (testConverter) { 605 mockMessage.getText(); 606 messageControl.setReturnValue("Hello World!"); 607 } 608 if (!useTransactedSession() && clientAcknowledge) { 609 mockMessage.acknowledge(); 610 messageControl.setVoidCallable(1); 611 } 612 613 sessionControl.replay(); 614 connectionControl.replay(); 615 messageControl.replay(); 616 617 if (timeout) { 618 mockMessageConsumer.receive(1000); 619 } 620 else { 621 mockMessageConsumer.receive(); 622 } 623 messageConsumerControl.setReturnValue(mockMessage); 624 mockMessageConsumer.close(); 625 messageConsumerControl.setVoidCallable(1); 626 627 messageConsumerControl.replay(); 628 629 Message message = null; 630 String textFromMessage = null; 631 632 if (useDefaultDestination) { 633 if (testConverter) { 634 textFromMessage = (String ) 635 (messageSelector ? template.receiveSelectedAndConvert(selectorString) : 636 template.receiveAndConvert()); 637 } 638 else { 639 message = (messageSelector ? template.receiveSelected(selectorString) : template.receive()); 640 } 641 } 642 else if (explicitDestination) { 643 if (testConverter) { 644 textFromMessage = (String ) 645 (messageSelector ? template.receiveSelectedAndConvert(mockQueue, selectorString) : 646 template.receiveAndConvert(mockQueue)); 647 } 648 else { 649 message = (messageSelector ? template.receiveSelected(mockQueue, selectorString) : 650 template.receive(mockQueue)); 651 } 652 } 653 else { 654 if (testConverter) { 655 textFromMessage = (String ) 656 (messageSelector ? template.receiveSelectedAndConvert(destinationName, selectorString) : 657 template.receiveAndConvert(destinationName)); 658 } 659 else { 660 message = (messageSelector ? template.receiveSelected(destinationName, selectorString) : 661 template.receive(destinationName)); 662 } 663 } 664 665 connectionFactoryControl.verify(); 666 connectionControl.verify(); 667 sessionControl.verify(); 668 messageConsumerControl.verify(); 669 messageControl.verify(); 670 671 if (testConverter) { 672 assertEquals("Message text should be equal", "Hello World!", textFromMessage); 673 } 674 else { 675 assertEquals("Messages should refer to the same object", message, mockMessage); 676 } 677 } 678 679 } 680 | Popular Tags |