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);
}
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();
}
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();
}
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();
}
Aggregations