private void respondToFetchEnvelopesWithMessage(final Message message) throws MessagingException { doAnswer(new Answer() { @Override public Void answer(InvocationOnMock invocation) { FetchProfile fetchProfile = (FetchProfile) invocation.getArguments()[1]; if (invocation.getArguments()[2] != null) { MessageRetrievalListener listener = (MessageRetrievalListener) invocation.getArguments()[2]; if (fetchProfile.contains(FetchProfile.Item.ENVELOPE)) { listener.messageStarted("UID", 1, 1); listener.messageFinished(message, 1, 1); listener.messagesFinished(1); } } return null; } }).when(remoteFolder).fetch(any(List.class), any(FetchProfile.class), nullable(MessageRetrievalListener.class)); }
@Test public void fetch_withEmptyMessageListArgument_shouldDoNothing() throws Exception { ImapFolder folder = createFolder("Folder"); FetchProfile fetchProfile = createFetchProfile(); folder.fetch(Collections.<ImapMessage>emptyList(), fetchProfile, null); verifyNoMoreInteractions(imapStore); }
@Test public void fetch_withNullMessageListArgument_shouldDoNothing() throws Exception { ImapFolder folder = createFolder("Folder"); FetchProfile fetchProfile = createFetchProfile(); folder.fetch(null, fetchProfile, null); verifyNoMoreInteractions(imapStore); }
@Test public void sync_shouldFetchUnsynchronizedMessagesListAndFlags() throws Exception { messageCountInRemoteFolder(1); hasUnsyncedRemoteMessage(); when(remoteFolder.supportsFetchingFlags()).thenReturn(true); imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); verify(remoteFolder, atLeastOnce()).fetch(any(List.class), fetchProfileCaptor.capture(), nullable(MessageRetrievalListener.class)); assertTrue(fetchProfileCaptor.getAllValues().get(0).contains(FetchProfile.Item.FLAGS)); assertTrue(fetchProfileCaptor.getAllValues().get(0).contains(FetchProfile.Item.ENVELOPE)); assertEquals(2, fetchProfileCaptor.getAllValues().get(0).size()); }
@Test public void sync_withUnsyncedNewSmallMessage_shouldFetchBodyOfSmallMessage() throws Exception { Message smallMessage = buildSmallNewMessage(); messageCountInRemoteFolder(1); hasUnsyncedRemoteMessage(); when(remoteFolder.supportsFetchingFlags()).thenReturn(false); respondToFetchEnvelopesWithMessage(smallMessage); imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); verify(remoteFolder, atLeast(2)).fetch(any(List.class), fetchProfileCaptor.capture(), nullable(MessageRetrievalListener.class)); assertEquals(1, fetchProfileCaptor.getAllValues().get(1).size()); assertTrue(fetchProfileCaptor.getAllValues().get(1).contains(FetchProfile.Item.BODY)); }
@Test public void sync_withUnsyncedNewSmallMessage_shouldFetchStructureAndLimitedBodyOfLargeMessage() throws Exception { Message largeMessage = buildLargeNewMessage(); messageCountInRemoteFolder(1); hasUnsyncedRemoteMessage(); when(remoteFolder.supportsFetchingFlags()).thenReturn(false); respondToFetchEnvelopesWithMessage(largeMessage); imapSync.sync(FOLDER_NAME, syncConfig, listener, remoteFolder); //TODO: Don't bother fetching messages of a size we don't have verify(remoteFolder, atLeast(4)).fetch(any(List.class), fetchProfileCaptor.capture(), nullable(MessageRetrievalListener.class)); assertEquals(1, fetchProfileCaptor.getAllValues().get(2).size()); assertEquals(FetchProfile.Item.STRUCTURE, fetchProfileCaptor.getAllValues().get(2).get(0)); assertEquals(1, fetchProfileCaptor.getAllValues().get(3).size()); assertEquals(FetchProfile.Item.BODY_SANE, fetchProfileCaptor.getAllValues().get(3).get(0)); }
@Test public void fetch_withBodySaneFetchProfile_shouldIssueRespectiveCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); when(imapConnection.readResponse(nullable(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK")); List<ImapMessage> messages = createImapMessages("1"); FetchProfile fetchProfile = createFetchProfile(Item.BODY_SANE); when(storeConfig.getMaximumAutoDownloadMessageSize()).thenReturn(4096); folder.fetch(messages, fetchProfile, null); verify(imapConnection).sendCommand("UID FETCH 1 (UID BODY.PEEK[]<0.4096>)", false); }
@Test public void fetch_withBodySaneFetchProfileAndNoMaximumDownloadSize_shouldIssueRespectiveCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); when(imapConnection.readResponse(nullable(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK")); List<ImapMessage> messages = createImapMessages("1"); FetchProfile fetchProfile = createFetchProfile(Item.BODY_SANE); when(storeConfig.getMaximumAutoDownloadMessageSize()).thenReturn(0); folder.fetch(messages, fetchProfile, null); verify(imapConnection).sendCommand("UID FETCH 1 (UID BODY.PEEK[])", false); }
@Test public void fetch_withStructureFetchProfile_shouldSetContentType() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); String bodyStructure = "(\"TEXT\" \"PLAIN\" (\"CHARSET\" \"US-ASCII\") NIL NIL \"7BIT\" 2279 48)"; when(imapConnection.readResponse(nullable(ImapResponseCallback.class))) .thenReturn(createImapResponse("* 1 FETCH (BODYSTRUCTURE "+bodyStructure+" UID 1)")) .thenReturn(createImapResponse("x OK")); List<ImapMessage> messages = createImapMessages("1"); FetchProfile fetchProfile = createFetchProfile(Item.STRUCTURE); folder.fetch(messages, fetchProfile, null); verify(messages.get(0)).setHeader(MimeHeader.HEADER_CONTENT_TYPE, "text/plain;\r\n CHARSET=\"US-ASCII\""); }
@Test public void fetch_withFlagsFetchProfile_shouldSetFlags() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); List<ImapMessage> messages = createImapMessages("1"); FetchProfile fetchProfile = createFetchProfile(Item.FLAGS); when(imapConnection.readResponse(nullable(ImapResponseCallback.class))) .thenReturn(createImapResponse("* 1 FETCH (FLAGS (\\Seen) UID 1)")) .thenReturn(createImapResponse("x OK")); folder.fetch(messages, fetchProfile, null); ImapMessage imapMessage = messages.get(0); verify(imapMessage).setFlagInternal(Flag.SEEN, true); }
@Test public void fetch_withFlagsFetchProfile_shouldIssueRespectiveCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); when(imapConnection.readResponse(nullable(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK")); List<ImapMessage> messages = createImapMessages("1"); FetchProfile fetchProfile = createFetchProfile(Item.FLAGS); folder.fetch(messages, fetchProfile, null); verify(imapConnection).sendCommand("UID FETCH 1 (UID FLAGS)", false); }
@Test public void fetch_withStructureFetchProfile_shouldIssueRespectiveCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); when(imapConnection.readResponse(nullable(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK")); List<ImapMessage> messages = createImapMessages("1"); FetchProfile fetchProfile = createFetchProfile(Item.STRUCTURE); folder.fetch(messages, fetchProfile, null); verify(imapConnection).sendCommand("UID FETCH 1 (UID BODYSTRUCTURE)", false); }
@Test public void fetch_withEnvelopeFetchProfile_shouldIssueRespectiveCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); when(imapConnection.readResponse(nullable(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK")); List<ImapMessage> messages = createImapMessages("1"); FetchProfile fetchProfile = createFetchProfile(Item.ENVELOPE); folder.fetch(messages, fetchProfile, null); verify(imapConnection).sendCommand("UID FETCH 1 (UID INTERNALDATE RFC822.SIZE BODY.PEEK[HEADER.FIELDS " + "(date subject from content-type to cc reply-to message-id references in-reply-to X-K9mail-Identity)]" + ")", false); }
@Test public void fetch_withBodyFetchProfileAndNoMaximumDownloadSize_shouldIssueRespectiveCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RO); folder.open(OPEN_MODE_RO); when(imapConnection.readResponse(nullable(ImapResponseCallback.class))).thenReturn(createImapResponse("x OK")); List<ImapMessage> messages = createImapMessages("1"); FetchProfile fetchProfile = createFetchProfile(Item.BODY); folder.fetch(messages, fetchProfile, null); verify(imapConnection).sendCommand("UID FETCH 1 (UID BODY.PEEK[])", false); }