|
1 | 1 | /* |
2 | | - * Copyright 2002-2008 the original author or authors. |
| 2 | + * Copyright 2002-2009 the original author or authors. |
3 | 3 | * |
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | 5 | * you may not use this file except in compliance with the License. |
|
29 | 29 | import javax.jms.MessageListener; |
30 | 30 | import javax.jms.Session; |
31 | 31 |
|
| 32 | +import org.easymock.EasyMock; |
32 | 33 | import org.easymock.MockControl; |
33 | 34 | import org.easymock.internal.AlwaysMatcher; |
34 | 35 | import org.junit.Before; |
35 | 36 | import org.junit.Test; |
36 | 37 | import org.springframework.core.task.TaskExecutor; |
37 | 38 | import org.springframework.jms.StubQueue; |
| 39 | +import org.springframework.util.ErrorHandler; |
38 | 40 |
|
39 | 41 | /** |
40 | 42 | * @author Rick Evans |
41 | 43 | * @author Juergen Hoeller |
42 | 44 | * @author Chris Beams |
| 45 | + * @author Mark Fisher |
43 | 46 | */ |
44 | 47 | public class SimpleMessageListenerContainerTests extends AbstractMessageListenerContainerTests { |
45 | 48 |
|
@@ -380,6 +383,64 @@ public void onMessage(Message message, Session session) throws JMSException { |
380 | 383 | mockConnectionFactory.verify(); |
381 | 384 | } |
382 | 385 |
|
| 386 | + @Test |
| 387 | + public void testRegisteredErrorHandlerIsInvokedOnException() throws Exception { |
| 388 | + final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer(); |
| 389 | + |
| 390 | + Session session = EasyMock.createMock(Session.class); |
| 391 | + |
| 392 | + // Queue gets created in order to create MessageConsumer for that Destination... |
| 393 | + session.createQueue(DESTINATION_NAME); |
| 394 | + EasyMock.expectLastCall().andReturn(QUEUE_DESTINATION); |
| 395 | + // and then the MessageConsumer gets created... |
| 396 | + session.createConsumer(QUEUE_DESTINATION, null); // no MessageSelector... |
| 397 | + EasyMock.expectLastCall().andReturn(messageConsumer); |
| 398 | + // an exception is thrown, so the rollback logic is being applied here... |
| 399 | + session.getTransacted(); |
| 400 | + EasyMock.expectLastCall().andReturn(false); |
| 401 | + EasyMock.replay(session); |
| 402 | + |
| 403 | + Connection connection = EasyMock.createMock(Connection.class); |
| 404 | + connection.setExceptionListener(this.container); |
| 405 | + // session gets created in order to register MessageListener... |
| 406 | + connection.createSession(this.container.isSessionTransacted(), this.container.getSessionAcknowledgeMode()); |
| 407 | + EasyMock.expectLastCall().andReturn(session); |
| 408 | + // and the connection is start()ed after the listener is registered... |
| 409 | + connection.start(); |
| 410 | + EasyMock.replay(connection); |
| 411 | + |
| 412 | + ConnectionFactory connectionFactory = EasyMock.createMock(ConnectionFactory.class); |
| 413 | + connectionFactory.createConnection(); |
| 414 | + EasyMock.expectLastCall().andReturn(connection); |
| 415 | + EasyMock.replay(connectionFactory); |
| 416 | + |
| 417 | + final IllegalStateException theException = new IllegalStateException("intentional test failure"); |
| 418 | + |
| 419 | + this.container.setConnectionFactory(connectionFactory); |
| 420 | + this.container.setDestinationName(DESTINATION_NAME); |
| 421 | + this.container.setMessageListener(new SessionAwareMessageListener() { |
| 422 | + public void onMessage(Message message, Session session) throws JMSException { |
| 423 | + throw theException; |
| 424 | + } |
| 425 | + }); |
| 426 | + |
| 427 | + ErrorHandler errorHandler = EasyMock.createMock(ErrorHandler.class); |
| 428 | + errorHandler.handleError(theException); |
| 429 | + EasyMock.expectLastCall(); |
| 430 | + EasyMock.replay(errorHandler); |
| 431 | + this.container.setErrorHandler(errorHandler); |
| 432 | + this.container.afterPropertiesSet(); |
| 433 | + |
| 434 | + // manually trigger an Exception with the above bad MessageListener... |
| 435 | + Message message = EasyMock.createMock(Message.class); |
| 436 | + EasyMock.replay(message); |
| 437 | + |
| 438 | + // a Throwable from a MessageListener MUST simply be swallowed... |
| 439 | + messageConsumer.sendMessage(message); |
| 440 | + |
| 441 | + EasyMock.verify(errorHandler, message, session, connection, connectionFactory); |
| 442 | + } |
| 443 | + |
383 | 444 | @Test |
384 | 445 | public void testNoRollbackOccursIfSessionIsNotTransactedAndThatExceptionsDo_NOT_Propagate() throws Exception { |
385 | 446 | final SimpleMessageConsumer messageConsumer = new SimpleMessageConsumer(); |
|
0 commit comments