void releaseConnection(ImapConnection connection) { if (connection != null && connection.isConnected()) { synchronized (connections) { connections.offer(connection); } } }
private List<ImapResponse> saslAuthPlainWithLoginFallback() throws IOException, MessagingException { try { return saslAuthPlain(); } catch (AuthenticationFailedException e) { if (!isConnected()) { throw e; } return login(); } }
@Test public void isConnected_withoutPreviousOpen_shouldReturnFalse() throws Exception { ImapConnection imapConnection = createImapConnection( settings, socketFactory, connectivityManager, oAuth2TokenProvider); boolean result = imapConnection.isConnected(); assertFalse(result); }
@Test public void getConnection_withDeadConnectionInPool_shouldReturnNewImapConnectionInstance() throws Exception { ImapConnection imapConnectionOne = mock(ImapConnection.class); ImapConnection imapConnectionTwo = mock(ImapConnection.class); imapStore.enqueueImapConnection(imapConnectionOne); imapStore.enqueueImapConnection(imapConnectionTwo); imapStore.getConnection(); when(imapConnectionOne.isConnected()).thenReturn(true); doThrow(IOException.class).when(imapConnectionOne).executeSimpleCommand(Commands.NOOP); imapStore.releaseConnection(imapConnectionOne); ImapConnection result = imapStore.getConnection(); assertSame(imapConnectionTwo, result); }
@Test public void getConnection_calledAfterRelease_shouldReturnCachedImapConnection() throws Exception { ImapConnection imapConnection = mock(ImapConnection.class); when(imapConnection.isConnected()).thenReturn(true); imapStore.enqueueImapConnection(imapConnection); ImapConnection connection = imapStore.getConnection(); imapStore.releaseConnection(connection); ImapConnection result = imapStore.getConnection(); assertSame(imapConnection, result); }
@Test public void getConnection_calledAfterReleaseWithAClosedConnection_shouldReturnNewImapConnectionInstance() throws Exception { ImapConnection imapConnectionOne = mock(ImapConnection.class); ImapConnection imapConnectionTwo = mock(ImapConnection.class); imapStore.enqueueImapConnection(imapConnectionOne); imapStore.enqueueImapConnection(imapConnectionTwo); imapStore.getConnection(); when(imapConnectionOne.isConnected()).thenReturn(false); imapStore.releaseConnection(imapConnectionOne); ImapConnection result = imapStore.getConnection(); assertSame(imapConnectionTwo, result); }
@Test public void isConnected_afterOpen_shouldReturnTrue() throws Exception { MockImapServer server = new MockImapServer(); ImapConnection imapConnection = simpleOpen(server); boolean result = imapConnection.isConnected(); assertTrue(result); server.verifyConnectionStillOpen(); server.shutdown(); }
@Test public void open_withInvalidHostname_shouldThrow() throws Exception { settings.setHost("host name"); settings.setPort(143); ImapConnection imapConnection = createImapConnection( settings, socketFactory, connectivityManager, oAuth2TokenProvider); try { imapConnection.open(); fail("Expected exception"); } catch (UnknownHostException ignored) { } assertFalse(imapConnection.isConnected()); }
@Test public void isConnected_afterOpenAndClose_shouldReturnFalse() throws Exception { MockImapServer server = new MockImapServer(); ImapConnection imapConnection = simpleOpen(server); imapConnection.close(); boolean result = imapConnection.isConnected(); assertFalse(result); server.verifyConnectionClosed(); server.shutdown(); }
@Test public void open_authPlainFailureAndDisconnect_shouldThrow() throws Exception { settings.setAuthType(AuthType.PLAIN); MockImapServer server = new MockImapServer(); preAuthenticationDialog(server, "AUTH=PLAIN"); server.expect("2 AUTHENTICATE PLAIN"); server.output("+"); server.expect(ByteString.encodeUtf8("\000" + USERNAME + "\000" + PASSWORD).base64()); server.output("2 NO [UNAVAILABLE] Maximum number of connections from user+IP exceeded"); server.closeConnection(); ImapConnection imapConnection = startServerAndCreateImapConnection(server); try { imapConnection.open(); fail("Expected exception"); } catch (NegativeImapResponseException e) { assertThat(e.getMessage(), containsString("Maximum number of connections from user+IP exceeded")); } assertFalse(imapConnection.isConnected()); server.verifyConnectionClosed(); server.verifyInteractionCompleted(); }