From 031ab1424e281f912c1e6f8c7a3dc3d559e7ab70 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Wed, 17 Nov 2010 15:53:13 -0500 Subject: [PATCH] INT-1624 Add error-channel to imap idle inbound channel adapter XSD, Parser, Test --- spring-integration-mail/.springBeans | 13 ++++ .../config/spring-integration-mail-2.0.xsd | 15 +++++ .../mail/ImapMailReceiverTests.java | 63 ++++++++++++++++++- ...pIdleChannelAdapterParserTests-context.xml | 7 +++ .../ImapIdleChannelAdapterParserTests.java | 21 +++++++ 5 files changed, 117 insertions(+), 2 deletions(-) create mode 100644 spring-integration-mail/.springBeans diff --git a/spring-integration-mail/.springBeans b/spring-integration-mail/.springBeans new file mode 100644 index 0000000000..6f4c287497 --- /dev/null +++ b/spring-integration-mail/.springBeans @@ -0,0 +1,13 @@ + + + 1 + + + + + + + + + + diff --git a/spring-integration-mail/src/main/resources/org/springframework/integration/mail/config/spring-integration-mail-2.0.xsd b/spring-integration-mail/src/main/resources/org/springframework/integration/mail/config/spring-integration-mail-2.0.xsd index 6d35bc7d6a..6f7629ec20 100644 --- a/spring-integration-mail/src/main/resources/org/springframework/integration/mail/config/spring-integration-mail-2.0.xsd +++ b/spring-integration-mail/src/main/resources/org/springframework/integration/mail/config/spring-integration-mail-2.0.xsd @@ -108,6 +108,21 @@ + + + + + + + + + If a (synchronous) downstream exception is thrown and an error-channel is specified, + the MessageHandlingException will be sent to this channel. Otherwise, any such exception + will simply be logged as a warning by the channel adapter. + + + diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java b/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java index 6feb01f1f0..2be8e63c20 100644 --- a/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/ImapMailReceiverTests.java @@ -15,8 +15,8 @@ */ package org.springframework.integration.mail; -import static junit.framework.Assert.assertEquals; -import static junit.framework.Assert.assertNotNull; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; import static org.mockito.Mockito.doAnswer; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; @@ -39,7 +39,10 @@ import org.mockito.stubbing.Answer; import org.springframework.beans.DirectFieldAccessor; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; +import org.springframework.integration.channel.DirectChannel; +import org.springframework.integration.channel.QueueChannel; import org.springframework.integration.core.PollableChannel; +import org.springframework.integration.handler.AbstractReplyProducingMessageHandler; import org.springframework.integration.history.MessageHistory; import org.springframework.integration.mail.config.ImapIdleChannelAdapterParserTests; import org.springframework.integration.test.util.TestUtils; @@ -48,6 +51,7 @@ import com.sun.mail.imap.IMAPFolder; /** * @author Oleg Zhurakousky + * @author Gary Russell * */ public class ImapMailReceiverTests { @@ -285,4 +289,59 @@ public class ImapMailReceiverTests { assertNotNull(componentHistoryRecord); assertEquals("mail:imap-idle-channel-adapter", componentHistoryRecord.get("type")); } + + @Test + public void testIdleChannelAdapterException() throws Exception{ + ApplicationContext context = + new ClassPathXmlApplicationContext("ImapIdleChannelAdapterParserTests-context.xml", ImapIdleChannelAdapterParserTests.class); + ImapIdleChannelAdapter adapter = context.getBean("simpleAdapter", ImapIdleChannelAdapter.class); + + DirectChannel channel = new DirectChannel(); + channel.subscribe(new AbstractReplyProducingMessageHandler() { + protected Object handleRequestMessage(org.springframework.integration.Message requestMessage) { + throw new RuntimeException("Failed"); + } + }); + adapter.setOutputChannel(channel); + QueueChannel errorChannel = new QueueChannel(); + adapter.setErrorChannel(errorChannel); + + AbstractMailReceiver receiver = new ImapMailReceiver(); + receiver = spy(receiver); + receiver.afterPropertiesSet(); + DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter); + adapterAccessor.setPropertyValue("mailReceiver", receiver); + + MimeMessage mailMessage = mock(MimeMessage.class); + Flags flags = mock(Flags.class); + when(mailMessage.getFlags()).thenReturn(flags); + final Message[] messages = new Message[]{mailMessage}; + + doAnswer(new Answer() { + public Object answer(InvocationOnMock invocation) throws Throwable { + DirectFieldAccessor accesor = new DirectFieldAccessor((invocation.getMock())); + IMAPFolder folder = mock(IMAPFolder.class); + accesor.setPropertyValue("folder", folder); + when(folder.hasNewMessages()).thenReturn(true); + return null; + } + }).when(receiver).openFolder(); + + doAnswer(new Answer() { + public Object answer(InvocationOnMock invocation) throws Throwable { + return messages; + } + }).when(receiver).searchForNewMessages(); + + doAnswer(new Answer() { + public Object answer(InvocationOnMock invocation) throws Throwable { + return null; + } + }).when(receiver).fetchMessages(messages); + + adapter.start(); + org.springframework.integration.Message replMessage = errorChannel.receive(10000); + assertNotNull(replMessage); + assertEquals("Failed", ((Exception) replMessage.getPayload()).getCause().getMessage()); + } } diff --git a/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/ImapIdleChannelAdapterParserTests-context.xml b/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/ImapIdleChannelAdapterParserTests-context.xml index 17635a94e9..2705f23146 100644 --- a/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/ImapIdleChannelAdapterParserTests-context.xml +++ b/spring-integration-mail/src/test/java/org/springframework/integration/mail/config/ImapIdleChannelAdapterParserTests-context.xml @@ -37,6 +37,13 @@ auto-startup="false" should-delete-messages="true"/> + +