use of org.springframework.integration.mail.ImapIdleChannelAdapter in project spring-integration by spring-projects.
the class ImapIdleIntegrationTests method testWithTransactionSynchronization.
@SuppressWarnings("resource")
@Test
public void testWithTransactionSynchronization() throws Exception {
final AtomicBoolean block = new AtomicBoolean(false);
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("imap-idle-mock-integration-config.xml", this.getClass());
PostTransactionProcessor processor = context.getBean("syncProcessor", PostTransactionProcessor.class);
ImapIdleChannelAdapter adapter = context.getBean("customAdapter", ImapIdleChannelAdapter.class);
assertNotNull(TestUtils.getPropertyValue(adapter, "applicationEventPublisher"));
ImapMailReceiver receiver = TestUtils.getPropertyValue(adapter, "mailReceiver", ImapMailReceiver.class);
// setup mock scenario
receiver = spy(receiver);
doAnswer(invocation -> {
// to emulate the behavior of IDLE
if (block.get()) {
Thread.sleep(5000);
}
block.set(true);
return null;
}).when(receiver).waitForNewMessages();
Message m1 = mock(Message.class);
doReturn(new Message[] { m1 }).when(receiver).receive();
Folder folder = mock(Folder.class);
when(folder.isOpen()).thenReturn(true);
Field folderField = ReflectionUtils.findField(ImapMailReceiver.class, "folder");
folderField.setAccessible(true);
folderField.set(receiver, folder);
Field mrField = ImapIdleChannelAdapter.class.getDeclaredField("mailReceiver");
mrField.setAccessible(true);
mrField.set(adapter, receiver);
// end mock setup
final CountDownLatch txProcessorLatch = new CountDownLatch(1);
doAnswer(invocation -> {
txProcessorLatch.countDown();
return null;
}).when(processor).process(any(Message.class));
adapter.start();
assertTrue(txProcessorLatch.await(10, TimeUnit.SECONDS));
adapter.stop();
context.close();
}
Aggregations