Search in sources :

Example 1 with ImapIdleChannelAdapter

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();
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) Field(java.lang.reflect.Field) ImapMailReceiver(org.springframework.integration.mail.ImapMailReceiver) Message(javax.mail.Message) ClassPathXmlApplicationContext(org.springframework.context.support.ClassPathXmlApplicationContext) ImapIdleChannelAdapter(org.springframework.integration.mail.ImapIdleChannelAdapter) Folder(javax.mail.Folder) CountDownLatch(java.util.concurrent.CountDownLatch) Test(org.junit.Test)

Aggregations

Field (java.lang.reflect.Field)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 Folder (javax.mail.Folder)1 Message (javax.mail.Message)1 Test (org.junit.Test)1 ClassPathXmlApplicationContext (org.springframework.context.support.ClassPathXmlApplicationContext)1 ImapIdleChannelAdapter (org.springframework.integration.mail.ImapIdleChannelAdapter)1 ImapMailReceiver (org.springframework.integration.mail.ImapMailReceiver)1