Search in sources :

Example 1 with FolderClosedException

use of javax.mail.FolderClosedException in project spring-integration by spring-projects.

the class ImapMailReceiverTests method testIdleReconnects.

@Test
public void testIdleReconnects() throws Exception {
    ImapMailReceiver receiver = spy(new ImapMailReceiver("imap:foo"));
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();
    IMAPFolder folder = mock(IMAPFolder.class);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
    given(folder.isOpen()).willReturn(false).willReturn(true);
    given(folder.exists()).willReturn(true);
    given(folder.hasNewMessages()).willReturn(true);
    Field storeField = AbstractMailReceiver.class.getDeclaredField("store");
    storeField.setAccessible(true);
    Store store = mock(Store.class);
    given(store.isConnected()).willReturn(false);
    given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
    storeField.set(receiver, store);
    ImapIdleChannelAdapter adapter = new ImapIdleChannelAdapter(receiver);
    Log logger = spy(TestUtils.getPropertyValue(adapter, "logger", Log.class));
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    willDoNothing().given(logger).warn(anyString(), any(Throwable.class));
    willAnswer(i -> {
        i.callRealMethod();
        throw new FolderClosedException(folder, "test");
    }).given(receiver).waitForNewMessages();
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    adapter.setTaskScheduler(taskScheduler);
    adapter.setReconnectDelay(50);
    adapter.afterPropertiesSet();
    final CountDownLatch latch = new CountDownLatch(3);
    adapter.setApplicationEventPublisher(e -> {
        latch.countDown();
    });
    adapter.start();
    assertTrue(latch.await(60, TimeUnit.SECONDS));
    verify(store, atLeast(3)).connect();
    taskScheduler.shutdown();
}
Also used : Log(org.apache.commons.logging.Log) IMAPFolder(com.sun.mail.imap.IMAPFolder) URLName(javax.mail.URLName) FolderClosedException(javax.mail.FolderClosedException) Store(javax.mail.Store) Flags(javax.mail.Flags) CountDownLatch(java.util.concurrent.CountDownLatch) ThreadPoolTaskScheduler(org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler) Field(java.lang.reflect.Field) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) LongRunningIntegrationTest(org.springframework.integration.test.support.LongRunningIntegrationTest) Test(org.junit.Test)

Aggregations

IMAPFolder (com.sun.mail.imap.IMAPFolder)1 Field (java.lang.reflect.Field)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Flags (javax.mail.Flags)1 FolderClosedException (javax.mail.FolderClosedException)1 Store (javax.mail.Store)1 URLName (javax.mail.URLName)1 Log (org.apache.commons.logging.Log)1 Test (org.junit.Test)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 LongRunningIntegrationTest (org.springframework.integration.test.support.LongRunningIntegrationTest)1 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)1