Search in sources :

Example 1 with FolderClosedException

use of jakarta.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);
    LogAccessor logger = spy(TestUtils.getPropertyValue(adapter, "logger", LogAccessor.class));
    new DirectFieldAccessor(adapter).setPropertyValue("logger", logger);
    willDoNothing().given(logger).warn(any(Throwable.class), anyString());
    willAnswer(i -> {
        i.callRealMethod();
        throw new FolderClosedException(folder, "test");
    }).given(receiver).waitForNewMessages();
    ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler();
    taskScheduler.initialize();
    adapter.setTaskScheduler(taskScheduler);
    adapter.setReconnectDelay(1);
    adapter.afterPropertiesSet();
    final CountDownLatch latch = new CountDownLatch(3);
    adapter.setApplicationEventPublisher(e -> {
        latch.countDown();
    });
    adapter.start();
    assertThat(latch.await(60, TimeUnit.SECONDS)).isTrue();
    verify(store, atLeast(3)).connect();
    adapter.stop();
    taskScheduler.shutdown();
}
Also used : IMAPFolder(com.sun.mail.imap.IMAPFolder) URLName(jakarta.mail.URLName) FolderClosedException(jakarta.mail.FolderClosedException) Store(jakarta.mail.Store) Flags(jakarta.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) LogAccessor(org.springframework.core.log.LogAccessor) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Aggregations

ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)1 IMAPFolder (com.sun.mail.imap.IMAPFolder)1 Flags (jakarta.mail.Flags)1 FolderClosedException (jakarta.mail.FolderClosedException)1 Store (jakarta.mail.Store)1 URLName (jakarta.mail.URLName)1 Field (java.lang.reflect.Field)1 CountDownLatch (java.util.concurrent.CountDownLatch)1 Test (org.junit.jupiter.api.Test)1 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)1 BeanFactory (org.springframework.beans.factory.BeanFactory)1 LogAccessor (org.springframework.core.log.LogAccessor)1 ThreadPoolTaskScheduler (org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler)1