private void assertCheckOpenErrorMessage(String folderName, MessagingException e) { assertEquals("Folder " + folderName + " is not open.", e.getMessage()); }
@Test public void copyMessages_withoutDestinationFolderOfWrongType_shouldThrow() throws Exception { ImapFolder sourceFolder = createFolder("Source"); Folder destinationFolder = mock(Folder.class); List<ImapMessage> messages = singletonList(mock(ImapMessage.class)); try { sourceFolder.copyMessages(messages, destinationFolder); fail("Expected exception"); } catch (MessagingException e) { assertEquals("ImapFolder.copyMessages passed non-ImapFolder", e.getMessage()); } }
@Test public void search_withRemoteSearchDisabled_shouldThrow() throws Exception { ImapFolder folder = createFolder("Folder"); when(storeConfig.isAllowRemoteSearch()).thenReturn(false); try { folder.search("query", Collections.<Flag>emptySet(), Collections.<Flag>emptySet()); fail("Expected exception"); } catch (MessagingException e) { assertEquals("Your settings do not allow remote searching of this account", e.getMessage()); } }
@Test public void copyMessages_withClosedFolder_shouldThrow() throws Exception { ImapFolder sourceFolder = createFolder("Source"); ImapFolder destinationFolder = createFolder("Destination"); when(imapStore.getConnection()).thenReturn(imapConnection); when(imapStore.getCombinedPrefix()).thenReturn(""); List<ImapMessage> messages = singletonList(mock(ImapMessage.class)); try { sourceFolder.copyMessages(messages, destinationFolder); fail("Expected exception"); } catch (MessagingException e) { assertEquals("Folder Source is not open.", e.getMessage()); } }
@Test public void open_withoutExistsResponse_shouldThrowMessagingException() throws Exception { ImapFolder imapFolder = createFolder("Folder"); when(imapStore.getConnection()).thenReturn(imapConnection); List<ImapResponse> selectResponses = asList( createImapResponse("* OK [UIDNEXT 57576] Predicted next UID"), createImapResponse("2 OK [READ-WRITE] Select completed.") ); when(imapConnection.executeSimpleCommand("SELECT \"Folder\"")).thenReturn(selectResponses); try { imapFolder.open(OPEN_MODE_RW); fail("Expected exception"); } catch (MessagingException e) { assertEquals("Did not find message count during open", e.getMessage()); } }
@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 checkSettings_withOpenThrowing_shouldThrowMessagingException() throws Exception { ImapConnection imapConnection = mock(ImapConnection.class); doThrow(IOException.class).when(imapConnection).open(); imapStore.enqueueImapConnection(imapConnection); try { imapStore.checkSettings(); fail("Expected exception"); } catch (MessagingException e) { assertEquals("Unable to connect", e.getMessage()); assertNotNull(e.getCause()); assertEquals(IOException.class, e.getCause().getClass()); } }
@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 getMessages_withEndArgumentSmallerThanStartArgument_shouldThrow() throws Exception { ImapFolder folder = createFolder("Folder"); try { folder.getMessages(10, 5, null, null); fail("Expected exception"); } catch (MessagingException e) { assertEquals("Invalid message set 10 5", e.getMessage()); } }
@Test public void getMessages_withInvalidStartArgument_shouldThrow() throws Exception { ImapFolder folder = createFolder("Folder"); try { folder.getMessages(0, 10, null, null); fail("Expected exception"); } catch (MessagingException e) { assertEquals("Invalid message set 0 10", e.getMessage()); } }
@Test public void getMessages_withInvalidEndArgument_shouldThrow() throws Exception { ImapFolder folder = createFolder("Folder"); try { folder.getMessages(10, 0, null, null); fail("Expected exception"); } catch (MessagingException e) { assertEquals("Invalid message set 10 0", e.getMessage()); } }
@Test public void sendMessage_withMessageTooLarge_shouldThrow() throws Exception { Message message = getDefaultMessageBuilder() .setHasAttachments(true) .messageSize(1234L) .build(); MockSmtpServer server = createServerAndSetupForPlainAuthentication("SIZE 1000"); SmtpTransport transport = startServerAndCreateSmtpTransport(server); try { transport.sendMessage(message); fail("Expected message too large error"); } catch (MessagingException e) { assertTrue(e.isPermanentFailure()); assertEquals("Message too large for server", e.getMessage()); } //FIXME: Make sure connection was closed //server.verifyConnectionClosed(); }
private Cursor getAccountUnread(int accountNumber) { MatrixCursor cursor = new MatrixCursor(UNREAD_PROJECTION); Account myAccount; AccountStats myAccountStats; Object[] values = new Object[2]; Context context = getContext(); MessagingController controller = MessagingController.getInstance(context); Collection<Account> accounts = Preferences.getPreferences(context).getAvailableAccounts(); for (Account account : accounts) { if (account.getAccountNumber() == accountNumber) { myAccount = account; try { myAccountStats = controller.getAccountStats(account); values[0] = myAccount.getDescription(); if (myAccountStats == null) { values[1] = 0; } else { values[1] = myAccountStats.unreadMessageCount; } } catch (MessagingException e) { Timber.e(e.getMessage()); values[0] = "Unknown"; values[1] = 0; } cursor.addRow(values); } } return cursor; } }
@Test public void open_withConnectionError_shouldThrow() throws Exception { settings.setHost("127.1.2.3"); settings.setPort(143); ImapConnection imapConnection = createImapConnection( settings, socketFactory, connectivityManager, oAuth2TokenProvider); try { imapConnection.open(); fail("Expected exception"); } catch (MessagingException e) { assertEquals("Cannot connect to host", e.getMessage()); assertTrue(e.getCause() instanceof IOException); } }
@Test public void open_authCramMd5WithoutAuthCramMd5Capability_shouldThrow() throws Exception { settings.setAuthType(AuthType.CRAM_MD5); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "AUTH=PLAIN"); ImapConnection imapConnection = startServerAndCreateImapConnection(server); try { imapConnection.open(); fail("Expected exception"); } catch (MessagingException e) { assertEquals("Server doesn't support encrypted passwords using CRAM-MD5.", e.getMessage()); } server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }
@Test public void open_withoutLoginAndPlainAuthExtensions_shouldThrow() throws Exception { MockSmtpServer server = new MockSmtpServer(); server.output("220 localhost Simple Mail Transfer Service Ready"); server.expect("EHLO [127.0.0.1]"); server.output("250-localhost Hello client.localhost"); server.output("250 AUTH"); server.expect("QUIT"); server.output("221 BYE"); SmtpTransport transport = startServerAndCreateSmtpTransport(server, AuthType.PLAIN, ConnectionSecurity.NONE); try { transport.open(); fail("Exception expected"); } catch (MessagingException e) { assertEquals("Authentication methods SASL PLAIN and LOGIN are unavailable.", e.getMessage()); } server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); }
@Test public void open_withoutXoauth2Extension_shouldThrow() throws Exception { MockSmtpServer server = new MockSmtpServer(); server.output("220 localhost Simple Mail Transfer Service Ready"); server.expect("EHLO [127.0.0.1]"); server.output("250-localhost Hello client.localhost"); server.output("250 AUTH PLAIN LOGIN"); server.expect("QUIT"); server.output("221 BYE"); SmtpTransport transport = startServerAndCreateSmtpTransport(server, AuthType.XOAUTH2, ConnectionSecurity.NONE); try { transport.open(); fail("Exception expected"); } catch (MessagingException e) { assertEquals("Authentication method XOAUTH2 is unavailable.", e.getMessage()); } server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }
@Test public void open_withoutCramMd5AuthExtension_shouldThrow() throws Exception { MockSmtpServer server = new MockSmtpServer(); server.output("220 localhost Simple Mail Transfer Service Ready"); server.expect("EHLO [127.0.0.1]"); server.output("250-localhost Hello client.localhost"); server.output("250 AUTH PLAIN LOGIN"); server.expect("QUIT"); server.output("221 BYE"); SmtpTransport transport = startServerAndCreateSmtpTransport(server, AuthType.CRAM_MD5, ConnectionSecurity.NONE); try { transport.open(); fail("Exception expected"); } catch (MessagingException e) { assertEquals("Authentication method CRAM-MD5 is unavailable.", e.getMessage()); } server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }
@Test public void open_withAutomaticAuthAndNoTransportSecurityAndAuthPlainExtension_shouldThrow() throws Exception { MockSmtpServer server = new MockSmtpServer(); server.output("220 localhost Simple Mail Transfer Service Ready"); server.expect("EHLO [127.0.0.1]"); server.output("250-localhost Hello client.localhost"); server.output("250 AUTH PLAIN LOGIN"); server.expect("QUIT"); server.output("221 BYE"); SmtpTransport transport = startServerAndCreateSmtpTransport(server, AuthType.AUTOMATIC, ConnectionSecurity.NONE); try { transport.open(); fail("Exception expected"); } catch (MessagingException e) { assertEquals("Update your outgoing server authentication setting. AUTOMATIC auth. is unavailable.", e.getMessage()); } server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }
@Test public void open_authPlainWithLoginDisabled_shouldThrow() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "LOGINDISABLED"); ImapConnection imapConnection = startServerAndCreateImapConnection(server); try { imapConnection.open(); fail("Expected exception"); } catch (MessagingException e) { assertEquals("Server doesn't support unencrypted passwords using AUTH=PLAIN and LOGIN is disabled.", e.getMessage()); } server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }