private boolean exists(String escapedFolderName) throws MessagingException { try { // Since we don't care about RECENT, we'll use that for the check, because we're checking // a folder other than ourself, and don't want any untagged responses to cause a change // in our own fields connection.executeSimpleCommand(String.format("STATUS %s (RECENT)", escapedFolderName)); return true; } catch (IOException ioe) { throw ioExceptionHandler(connection, ioe); } catch (NegativeImapResponseException e) { return false; } }
private void requestCapabilities() throws IOException, MessagingException { List<ImapResponse> responses = extractCapabilities(executeSimpleCommand(Commands.CAPABILITY)); if (responses.size() != 2) { throw new MessagingException("Invalid CAPABILITY response received"); } }
List<ImapResponse> executeCommandWithIdSet(String commandPrefix, String commandSuffix, Set<Long> ids) throws IOException, MessagingException { GroupedIds groupedIds = IdGrouper.groupIds(ids); List<String> splitCommands = ImapCommandSplitter.splitCommand( commandPrefix, commandSuffix, groupedIds, getLineLengthLimit()); List<ImapResponse> responses = new ArrayList<>(); for (String splitCommand : splitCommands) { responses.addAll(executeSimpleCommand(splitCommand)); } return responses; }
private void setupSearchResponses(String... responses) throws MessagingException, IOException { List<ImapResponse> imapResponses = new ArrayList<>(responses.length); for (String response : responses) { imapResponses.add(createImapResponse(response)); } when(imapConnection.executeSimpleCommand(startsWith("SEARCH"))).thenReturn(imapResponses); }
@Test public void getPersonalNamespaces_withoutSpecialUseCapability_shouldUseSimpleListCommand() throws Exception { ImapConnection imapConnection = mock(ImapConnection.class); when(imapConnection.hasCapability(Capabilities.LIST_EXTENDED)).thenReturn(true); when(imapConnection.hasCapability(Capabilities.SPECIAL_USE)).thenReturn(false); imapStore.enqueueImapConnection(imapConnection); imapStore.getPersonalNamespaces(); verify(imapConnection, never()).executeSimpleCommand("LIST \"\" \"*\" RETURN (SPECIAL-USE)"); verify(imapConnection).executeSimpleCommand("LIST \"\" \"*\""); }
@Test public void getPersonalNamespaces_withoutListExtendedCapability_shouldUseSimpleListCommand() throws Exception { ImapConnection imapConnection = mock(ImapConnection.class); when(imapConnection.hasCapability(Capabilities.LIST_EXTENDED)).thenReturn(false); when(imapConnection.hasCapability(Capabilities.SPECIAL_USE)).thenReturn(true); imapStore.enqueueImapConnection(imapConnection); imapStore.getPersonalNamespaces(); verify(imapConnection, never()).executeSimpleCommand("LIST \"\" \"*\" RETURN (SPECIAL-USE)"); verify(imapConnection).executeSimpleCommand("LIST \"\" \"*\""); }
@Test public void getPersonalNamespaces_withoutException_shouldLeaveImapConnectionOpen() throws Exception { ImapConnection imapConnection = mock(ImapConnection.class); List<ImapResponse> imapResponses = Collections.singletonList(createImapResponse("5 OK Success")); when(imapConnection.executeSimpleCommand(anyString())).thenReturn(imapResponses); imapStore.enqueueImapConnection(imapConnection); imapStore.getPersonalNamespaces(); verify(imapConnection, never()).close(); }
@Test public void exists_withNegativeImapResponse_shouldReturnFalse() throws Exception { ImapFolder imapFolder = createFolder("Folder"); when(imapStore.getConnection()).thenReturn(imapConnection); doThrow(NegativeImapResponseException.class).when(imapConnection) .executeSimpleCommand("STATUS \"Folder\" (UIDVALIDITY)"); boolean folderExists = imapFolder.exists(); assertFalse(folderExists); }
@Test public void create_withNegativeImapResponse_shouldReturnFalse() throws Exception { ImapFolder imapFolder = createFolder("Folder"); when(imapStore.getConnection()).thenReturn(imapConnection); doThrow(NegativeImapResponseException.class).when(imapConnection).executeSimpleCommand("CREATE \"Folder\""); boolean success = imapFolder.create(); assertFalse(success); }
@Test public void create_withClosedFolder_shouldOpenConnectionAndIssueCreateCommand() throws Exception { ImapFolder imapFolder = createFolder("Folder"); when(imapStore.getConnection()).thenReturn(imapConnection); imapFolder.create(); verify(imapConnection).executeSimpleCommand("CREATE \"Folder\""); }
@Test public void exists_withClosedFolder_shouldOpenConnectionAndIssueStatusCommand() throws Exception { ImapFolder imapFolder = createFolder("Folder"); when(imapStore.getConnection()).thenReturn(imapConnection); imapFolder.exists(); verify(imapConnection).executeSimpleCommand("STATUS \"Folder\" (UIDVALIDITY)"); }
@Test public void getPersonalNamespaces_withIoException_shouldCloseImapConnection() throws Exception { ImapConnection imapConnection = mock(ImapConnection.class); doThrow(IOException.class).when(imapConnection).executeSimpleCommand("LIST \"\" \"*\""); imapStore.enqueueImapConnection(imapConnection); try { imapStore.getPersonalNamespaces(); fail("Expected exception"); } catch (MessagingException ignored) { } verify(imapConnection).close(); }
@Test public void open_withMessagingException_shouldThrowMessagingException() throws Exception { ImapFolder imapFolder = createFolder("Folder"); when(imapStore.getConnection()).thenReturn(imapConnection); doThrow(MessagingException.class).when(imapConnection).executeSimpleCommand("SELECT \"Folder\""); try { imapFolder.open(OPEN_MODE_RW); fail("Expected exception"); } catch (MessagingException ignored) { } }
@Test public void expungeUids_withoutUidPlus_shouldIssueExpungeCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RW); when(imapConnection.isUidPlusCapable()).thenReturn(false); folder.expungeUids(singletonList("1")); verify(imapConnection).executeSimpleCommand("EXPUNGE"); }
@Test public void open_withConnectionThrowingOnReUse_shouldCreateNewImapConnection() throws Exception { ImapFolder imapFolder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RW); imapFolder.open(OPEN_MODE_RW); doThrow(IOException.class).when(imapConnection).executeSimpleCommand(Commands.NOOP); imapFolder.open(OPEN_MODE_RW); verify(imapStore, times(2)).getConnection(); }
@Test public void expunge_shouldIssueExpungeCommand() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RW); folder.expunge(); verify(imapConnection).executeSimpleCommand("EXPUNGE"); }
@Test public void getHighestUid_imapConnectionThrowsNegativesResponse_shouldReturnMinusOne() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RW); doThrow(NegativeImapResponseException.class).when(imapConnection).executeSimpleCommand("UID SEARCH *:*"); folder.open(OPEN_MODE_RW); long highestUid = folder.getHighestUid(); assertEquals(-1L, highestUid); }
@Test public void getUnreadMessageCount_connectionThrowsIOException_shouldThrowMessagingException() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RW); when(imapConnection.executeSimpleCommand("SEARCH 1:* UNSEEN NOT DELETED")).thenThrow(new IOException()); folder.open(OPEN_MODE_RW); try { folder.getUnreadMessageCount(); fail("Expected exception"); } catch (MessagingException e) { assertEquals("IO Error", e.getMessage()); } }
@Test public void getHighestUid_imapConnectionThrowsIOException_shouldThrowMessagingException() throws Exception { ImapFolder folder = createFolder("Folder"); prepareImapFolderForOpen(OPEN_MODE_RW); doThrow(IOException.class).when(imapConnection).executeSimpleCommand("UID SEARCH *:*"); folder.open(OPEN_MODE_RW); try { folder.getHighestUid(); fail("Expected MessagingException"); } catch (MessagingException e) { assertEquals("IO Error", e.getMessage()); } }
@Test public void executeSingleCommand_withOkResponse_shouldReturnResult() throws Exception { MockImapServer server = new MockImapServer(); simpleOpenDialog(server, ""); server.expect("4 CREATE Folder"); server.output("4 OK Folder created"); ImapConnection imapConnection = startServerAndCreateImapConnection(server); List<ImapResponse> result = imapConnection.executeSimpleCommand("CREATE Folder"); assertEquals(result.size(), 1); server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }