INT-1624 Add error-channel to imap idle inbound channel adapter XSD, Parser, Test

This commit is contained in:
Gary Russell
2010-11-17 15:53:13 -05:00
parent 984ce33106
commit 031ab1424e
5 changed files with 117 additions and 2 deletions

View File

@@ -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<Object>() {
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<Object>() {
public Object answer(InvocationOnMock invocation) throws Throwable {
return messages;
}
}).when(receiver).searchForNewMessages();
doAnswer(new Answer<Object>() {
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());
}
}

View File

@@ -37,6 +37,13 @@
auto-startup="false"
should-delete-messages="true"/>
<mail:imap-idle-channel-adapter id="simpleAdapterWithErrorChannel"
store-uri="imap:foo"
channel="channel"
error-channel="errorChannel"
auto-startup="false"
should-delete-messages="true"/>
<mail:imap-idle-channel-adapter id="simpleAdapterMarkAsRead"
store-uri="imap:foo"
channel="channel"

View File

@@ -64,6 +64,27 @@ public class ImapIdleChannelAdapterParserTests {
assertEquals(0, properties.size());
assertEquals(Boolean.TRUE, receiverAccessor.getPropertyValue("shouldDeleteMessages"));
assertEquals(Boolean.TRUE, receiverAccessor.getPropertyValue("shouldMarkMessagesAsRead"));
assertNull(adapterAccessor.getPropertyValue("errorChannel"));
}
@Test
public void simpleAdapterWithErrorChannel() {
Object adapter = context.getBean("simpleAdapterWithErrorChannel");
assertEquals(ImapIdleChannelAdapter.class, adapter.getClass());
DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
Object channel = context.getBean("channel");
assertSame(channel, adapterAccessor.getPropertyValue("outputChannel"));
assertNull(adapterAccessor.getPropertyValue("taskExecutor"));
assertEquals(Boolean.FALSE, adapterAccessor.getPropertyValue("autoStartup"));
Object receiver = adapterAccessor.getPropertyValue("mailReceiver");
assertEquals(ImapMailReceiver.class, receiver.getClass());
DirectFieldAccessor receiverAccessor = new DirectFieldAccessor(receiver);
Object url = receiverAccessor.getPropertyValue("url");
assertEquals(new URLName("imap:foo"), url);
Properties properties = (Properties) receiverAccessor.getPropertyValue("javaMailProperties");
assertEquals(0, properties.size());
assertEquals(Boolean.TRUE, receiverAccessor.getPropertyValue("shouldDeleteMessages"));
assertEquals(Boolean.TRUE, receiverAccessor.getPropertyValue("shouldMarkMessagesAsRead"));
assertSame(context.getBean("errorChannel"), adapterAccessor.getPropertyValue("errorChannel"));
}
@Test
public void simpleAdapterWithMarkeMessagesAsRead() {