Search in sources :

Example 6 with Store

use of jakarta.mail.Store in project spring-integration by spring-projects.

the class ImapMailReceiverTests method testImapLifecycleForRaceCondition.

// see INT-1801
@Test
public void testImapLifecycleForRaceCondition() throws Exception {
    for (int i = 0; i < 100; i++) {
        final ImapMailReceiver receiver = new ImapMailReceiver("imap://foo");
        Store store = mock(Store.class);
        Folder folder = mock(Folder.class);
        given(folder.exists()).willReturn(true);
        given(folder.isOpen()).willReturn(true);
        given(folder.search(Mockito.any())).willReturn(new Message[] {});
        given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
        given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.USER));
        DirectFieldAccessor df = new DirectFieldAccessor(receiver);
        df.setPropertyValue("store", store);
        receiver.setBeanFactory(mock(BeanFactory.class));
        receiver.afterPropertiesSet();
        new Thread(() -> {
            try {
                receiver.receive();
            } catch (jakarta.mail.MessagingException e) {
                if (e.getCause() instanceof NullPointerException) {
                    failed.getAndIncrement();
                }
            }
        }).start();
        new Thread(() -> {
            try {
                receiver.destroy();
            } catch (Exception ignore) {
            // ignore
            }
        }).start();
    }
    assertThat(failed.get()).isEqualTo(0);
}
Also used : MessagingException(jakarta.mail.MessagingException) URLName(jakarta.mail.URLName) Store(jakarta.mail.Store) Flags(jakarta.mail.Flags) Folder(jakarta.mail.Folder) IMAPFolder(com.sun.mail.imap.IMAPFolder) AddressException(jakarta.mail.internet.AddressException) MessagingException(jakarta.mail.MessagingException) IOException(java.io.IOException) FolderClosedException(jakarta.mail.FolderClosedException) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 7 with Store

use of jakarta.mail.Store in project spring-integration by spring-projects.

the class ImapMailReceiverTests method testNoInitialIdleDelayWhenRecentNotSupported.

@SuppressWarnings("resource")
@Test
public void testNoInitialIdleDelayWhenRecentNotSupported() throws Exception {
    ImapIdleChannelAdapter adapter = this.context.getBean("simpleAdapter", ImapIdleChannelAdapter.class);
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    adapter.setReconnectDelay(1);
    ImapMailReceiver receiver = new ImapMailReceiver("imap:foo");
    receiver = spy(receiver);
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();
    final 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);
    DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
    adapterAccessor.setPropertyValue("mailReceiver", receiver);
    Field storeField = AbstractMailReceiver.class.getDeclaredField("store");
    storeField.setAccessible(true);
    Store store = mock(Store.class);
    given(store.isConnected()).willReturn(true);
    given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
    storeField.set(receiver, store);
    willAnswer(invocation -> folder).given(receiver).getFolder();
    MimeMessage mailMessage = mock(MimeMessage.class);
    Flags flags = mock(Flags.class);
    given(mailMessage.getFlags()).willReturn(flags);
    final Message[] messages = new Message[] { mailMessage };
    final AtomicInteger shouldFindMessagesCounter = new AtomicInteger(2);
    willAnswer(invocation -> {
        /*
			 * Return the message from first invocation of waitForMessages()
			 * and in receive(); then return false in the next call to
			 * waitForMessages() so we enter idle(); counter will be reset
			 * to 1 in the mocked idle().
			 */
        if (shouldFindMessagesCounter.decrementAndGet() >= 0) {
            return messages;
        } else {
            return new Message[0];
        }
    }).given(receiver).searchForNewMessages();
    willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
    willAnswer(invocation -> {
        Thread.sleep(300);
        shouldFindMessagesCounter.set(1);
        return null;
    }).given(folder).idle(true);
    adapter.start();
    /*
		 * Idle takes 5 seconds; if all is well, we should receive the first message
		 * before then.
		 */
    assertThat(channel.receive(3000)).isNotNull();
    // We should not receive any more until the next idle elapses
    assertThat(channel.receive(100)).isNull();
    assertThat(channel.receive(6000)).isNotNull();
    adapter.stop();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MimeMessage(jakarta.mail.internet.MimeMessage) Message(jakarta.mail.Message) IMAPFolder(com.sun.mail.imap.IMAPFolder) URLName(jakarta.mail.URLName) Store(jakarta.mail.Store) Flags(jakarta.mail.Flags) Field(java.lang.reflect.Field) MimeMessage(jakarta.mail.internet.MimeMessage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 8 with Store

use of jakarta.mail.Store in project spring-integration by spring-projects.

the class ImapMailReceiverTests method testInitialIdleDelayWhenRecentIsSupported.

@Test
public void testInitialIdleDelayWhenRecentIsSupported() throws Exception {
    ImapIdleChannelAdapter adapter = this.context.getBean("simpleAdapter", ImapIdleChannelAdapter.class);
    QueueChannel channel = new QueueChannel();
    adapter.setOutputChannel(channel);
    adapter.setReconnectDelay(1);
    ImapMailReceiver receiver = new ImapMailReceiver("imap:foo");
    receiver.setCancelIdleInterval(1);
    receiver = spy(receiver);
    receiver.setBeanFactory(mock(BeanFactory.class));
    receiver.afterPropertiesSet();
    final IMAPFolder folder = mock(IMAPFolder.class);
    given(folder.getPermanentFlags()).willReturn(new Flags(Flags.Flag.RECENT));
    given(folder.isOpen()).willReturn(false).willReturn(true);
    given(folder.exists()).willReturn(true);
    DirectFieldAccessor adapterAccessor = new DirectFieldAccessor(adapter);
    adapterAccessor.setPropertyValue("mailReceiver", receiver);
    Field storeField = AbstractMailReceiver.class.getDeclaredField("store");
    storeField.setAccessible(true);
    Store store = mock(Store.class);
    given(store.isConnected()).willReturn(true);
    given(store.getFolder(Mockito.any(URLName.class))).willReturn(folder);
    storeField.set(receiver, store);
    willAnswer(invocation -> folder).given(receiver).getFolder();
    MimeMessage mailMessage = mock(MimeMessage.class);
    Flags flags = mock(Flags.class);
    given(mailMessage.getFlags()).willReturn(flags);
    final Message[] messages = new Message[] { mailMessage };
    willAnswer(invocation -> messages).given(receiver).searchForNewMessages();
    willAnswer(invocation -> null).given(receiver).fetchMessages(messages);
    final CountDownLatch idles = new CountDownLatch(2);
    willAnswer(invocation -> {
        idles.countDown();
        Thread.sleep(500);
        return null;
    }).given(folder).idle(true);
    adapter.start();
    /*
		 * Idle takes 5 seconds; since this server supports RECENT, we should
		 * not receive any early messages.
		 */
    assertThat(channel.receive(100)).isNull();
    assertThat(channel.receive(5000)).isNotNull();
    assertThat(idles.await(5, TimeUnit.SECONDS)).isTrue();
    adapter.stop();
}
Also used : QueueChannel(org.springframework.integration.channel.QueueChannel) MimeMessage(jakarta.mail.internet.MimeMessage) Message(jakarta.mail.Message) IMAPFolder(com.sun.mail.imap.IMAPFolder) URLName(jakarta.mail.URLName) Store(jakarta.mail.Store) Flags(jakarta.mail.Flags) CountDownLatch(java.util.concurrent.CountDownLatch) Field(java.lang.reflect.Field) MimeMessage(jakarta.mail.internet.MimeMessage) DirectFieldAccessor(org.springframework.beans.DirectFieldAccessor) BeanFactory(org.springframework.beans.factory.BeanFactory) Test(org.junit.jupiter.api.Test) ServerSetupTest(com.icegreen.greenmail.util.ServerSetupTest)

Example 9 with Store

use of jakarta.mail.Store 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

Store (jakarta.mail.Store)9 DirectFieldAccessor (org.springframework.beans.DirectFieldAccessor)6 IMAPFolder (com.sun.mail.imap.IMAPFolder)5 Flags (jakarta.mail.Flags)5 URLName (jakarta.mail.URLName)5 Test (org.junit.jupiter.api.Test)5 BeanFactory (org.springframework.beans.factory.BeanFactory)5 ServerSetupTest (com.icegreen.greenmail.util.ServerSetupTest)4 Folder (jakarta.mail.Folder)4 Message (jakarta.mail.Message)4 MessagingException (jakarta.mail.MessagingException)4 MimeMessage (jakarta.mail.internet.MimeMessage)3 Field (java.lang.reflect.Field)3 XPathException (org.exist.xquery.XPathException)3 IntegerValue (org.exist.xquery.value.IntegerValue)3 FolderClosedException (jakarta.mail.FolderClosedException)2 Session (jakarta.mail.Session)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 QueueChannel (org.springframework.integration.channel.QueueChannel)2 AddressException (jakarta.mail.internet.AddressException)1