ImapMailReceiver extends AbstractMailClient implements MailReceiver { @Override public int handleMessages(MessageHandler handler) throws MailClientException { int numberOfMsgsHandled = 0; handlerInvocations++; if (LOG.isDebugEnabled()) { LOG.debug("handleMessages() invoked, (" + hashCode() + " : " + Thread.currentThread().getId() + "), [" + imapHost + "], handler: " + handler.getClass().getName() + ", invocation count: " + handlerInvocations); } else { LOG.info("handleMessages() invoked"); } Store store = getImapsStore(); Folder inbox = getInbox(store); Message[] messages = getMessages(store, inbox); for (Message message : messages) { boolean handledSuccessfully = handleMessage(handler, message); if (handledSuccessfully) { numberOfMsgsHandled++; } } LOG.info("Handled " + numberOfMsgsHandled + " messages."); MailUtils.closeQuietly(store, inbox, MailUtils.FOLDER_EXPUNGE_INBOX_TRUE); LOG.info("Call the message monitoring check here"); handleMessageMonitoring(); return numberOfMsgsHandled; } ImapMailReceiver(Properties mailServerProps); @Override int handleMessages(MessageHandler handler); @Override int getHandlerInvocations();  }
@Test public void willDeleteUnhandledMsgs() throws MailClientException, MessagingException { final ImapMailReceiver testMailReceiver = getTestMailReceiver(true); MessageHandler handler = getMockHandler(false); deliverMsgs(NUMBER_OF_MSGS); assertEquals("Expecting zero messages handled.", 0, testMailReceiver.handleMessages(handler)); expungeMissedMessages(); assertEquals(NUMBER_OF_MSGS_IN_BATCH + " should be removed from the server.", NUMBER_OF_MSGS - NUMBER_OF_MSGS_IN_BATCH, countRemainingMsgs()); }
@Test public void canLeaveUnhandledMsgsOnServer() throws MailClientException, MessagingException { ImapMailReceiver testMailReceiver = getTestMailReceiver(false); MessageHandler handler = getMockHandler(false); deliverMsgs(NUMBER_OF_MSGS); assertEquals("Expecting zero messages handled.", 0, testMailReceiver.handleMessages(handler)); expungeMissedMessages(); assertEquals("All messages should be left on the server.", NUMBER_OF_MSGS, countRemainingMsgs()); }
@Test public void canHandleNoMessages() throws MailClientException { ImapMailReceiver testMailReceiver = getTestMailReceiver(false); assertEquals("No messages on server returns zero, does not fail.", 0, testMailReceiver.handleMessages(getMockHandler(true))); }
@Test public void testHandleMessages() throws MailClientException, MessagingException { Message[] msgs = { message }; int result; when(properties.getProperty("connect.delete.unhandled.msgs")).thenReturn("false"); when(properties.getProperty("connect.max.msgs.in.batch", "25")).thenReturn("30"); when(mockStore.getFolder(MailUtils.FOLDER_NAME_INBOX)).thenReturn(mockFolder); when(mockFolder.getMessageCount()).thenReturn(1); when(mockFolder.getMessages(1, 1)).thenReturn(msgs); when(handler.handleMessage(message)).thenReturn(true); ImapMailReceiver receiver = new ImapMailReceiver(properties) { @Override Store getImapsStore() { return mockStore; } @Override protected void handleMessageMonitoring() { } }; result = receiver.handleMessages(handler); verify(mockStore, times(1)).connect(); verify(mockStore).getFolder(MailUtils.FOLDER_NAME_INBOX); verify(mockFolder).open(Folder.READ_WRITE); assertEquals(result, 1); }
@Test(expected = MailClientException.class) public void testHandleMessageExceptionOnStoreConnect() throws MessagingException, MailClientException { when(properties.getProperty("connect.delete.unhandled.msgs")).thenReturn("false"); when(properties.getProperty("connect.max.msgs.in.batch", "25")).thenReturn("30"); when(mockStore.getFolder(anyString())).thenThrow(new MessagingException()); ImapMailReceiver receiver = new ImapMailReceiver(properties) { @Override Store getImapsStore() { return mockStore; } @Override protected void handleMessageMonitoring() { } }; try { receiver.handleMessages(handler); } catch (MailClientException ex) { verify(mockStore, times(1)).close(); throw ex; } }