From 82bc18673caef6d91e2595f4c001f72ba5111717 Mon Sep 17 00:00:00 2001 From: cketti Date: Sun, 16 Oct 2016 18:10:34 +0200 Subject: [PATCH] Code style fixes --- .../k9/mail/transport/SmtpTransportTest.java | 188 ++++++++---------- .../transport/mockServer/MockSmtpServer.java | 17 +- .../transport/mockServer/TestMessage.java | 11 +- .../testHelpers/TestTrustedSocketFactory.java | 6 +- 4 files changed, 94 insertions(+), 128 deletions(-) diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/transport/SmtpTransportTest.java b/k9mail-library/src/test/java/com/fsck/k9/mail/transport/SmtpTransportTest.java index 5daa8e1b0..a886b364a 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/transport/SmtpTransportTest.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/transport/SmtpTransportTest.java @@ -1,13 +1,18 @@ package com.fsck.k9.mail.transport; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + import com.fsck.k9.mail.Address; import com.fsck.k9.mail.AuthType; -import com.fsck.k9.mail.AuthenticationFailedException; import com.fsck.k9.mail.CertificateValidationException; import com.fsck.k9.mail.ConnectionSecurity; import com.fsck.k9.mail.Message; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.ServerSettings; +import com.fsck.k9.mail.ServerSettings.Type; import com.fsck.k9.mail.filter.Base64; import com.fsck.k9.mail.internet.MimeMessage; import com.fsck.k9.mail.ssl.TrustedSocketFactory; @@ -16,30 +21,22 @@ import com.fsck.k9.mail.transport.mockServer.MockSmtpServer; import com.fsck.k9.mail.transport.mockServer.TestMessage; import com.fsck.k9.mailstore.BinaryMemoryBody; import com.fsck.k9.testHelpers.TestTrustedSocketFactory; - import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.robolectric.RobolectricTestRunner; import org.robolectric.annotation.Config; -import java.io.IOException; -import java.util.ArrayList; -import java.util.List; - import static junit.framework.Assert.assertTrue; import static junit.framework.Assert.fail; import static org.junit.Assert.assertEquals; -import static org.mockito.Matchers.anyLong; -import static org.mockito.Matchers.eq; -import static org.mockito.Mockito.inOrder; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; + @RunWith(RobolectricTestRunner.class) @Config(manifest = Config.NONE, sdk = 21) public class SmtpTransportTest { - private String host; private int port; private ConnectionSecurity connectionSecurity; @@ -51,71 +48,24 @@ public class SmtpTransportTest { private StoreConfig storeConfig = mock(StoreConfig.class); private TrustedSocketFactory socketFactory; + @Before public void before() { socketFactory = new TestTrustedSocketFactory(); resetConnectionParameters(); } - private void resetConnectionParameters() { - host = null; - port = -1; - username = null; - password = null; - authenticationType = null; - clientCertificateAlias = null; - connectionSecurity = null; - extensions = new ArrayList<>(); - } - - private SmtpTransport startServerAndCreateSmtpTransport(MockSmtpServer server) - throws IOException, MessagingException { - server.start(); - host = server.getHost(); - port = server.getPort(); - String uri = SmtpTransport.createUri(new ServerSettings( - ServerSettings.Type.SMTP, host, port, connectionSecurity, authenticationType, - username, password, clientCertificateAlias)); - when(storeConfig.getTransportUri()).thenReturn(uri); - return createSmtpTransport(storeConfig, socketFactory); - } - - private SmtpTransport createSmtpTransport( - StoreConfig storeConfig, TrustedSocketFactory socketFactory) - throws MessagingException { - return new SmtpTransport(storeConfig, socketFactory); - } - - private void setupConnectAndPlainAuthentication(MockSmtpServer server) { - username = "user"; - password = "password"; - authenticationType = AuthType.PLAIN; - connectionSecurity = ConnectionSecurity.NONE; - - server.output("220 localhost Simple Mail Transfer Service Ready"); - server.expect("EHLO localhost"); - server.output("250-localhost Hello client.localhost"); - for (String extension: extensions) { - server.output("250-"+extension); - } - server.output("250 AUTH LOGIN PLAIN CRAM-MD5"); - server.expect("AUTH PLAIN AHVzZXIAcGFzc3dvcmQ="); - server.output("235 2.7.0 Authentication successful"); - } - @Test - public void SmtpTransport_withValidUri_canBeCreated() throws MessagingException { + public void SmtpTransport_withValidUri_canBeCreated() throws Exception { StoreConfig storeConfig = mock(StoreConfig.class); - when(storeConfig.getTransportUri()).thenReturn( - "smtp://user:password:CRAM_MD5@server:123456"); + when(storeConfig.getTransportUri()).thenReturn("smtp://user:password:CRAM_MD5@server:123456"); TrustedSocketFactory trustedSocketFactory = mock(TrustedSocketFactory.class); new SmtpTransport(storeConfig, trustedSocketFactory); } @Test(expected = MessagingException.class) - public void SmtpTransport_withInvalidUri_throwsMessagingException() - throws MessagingException { + public void SmtpTransport_withInvalidUri_throwsMessagingException() throws Exception { StoreConfig storeConfig = mock(StoreConfig.class); when(storeConfig.getTransportUri()).thenReturn("smpt://"); TrustedSocketFactory trustedSocketFactory = mock(TrustedSocketFactory.class); @@ -124,8 +74,7 @@ public class SmtpTransportTest { } @Test - public void open_withNoSecurityOrPasswordPlainAuth_connectsToServer_withoutLogin() - throws MessagingException, IOException, InterruptedException { + public void open_withNoSecurityOrPasswordPlainAuth_connectsToServer_withoutLogin() throws Exception { username = "user"; authenticationType = AuthType.PLAIN; connectionSecurity = ConnectionSecurity.NONE; @@ -144,8 +93,7 @@ public class SmtpTransportTest { } @Test - public void open_withNoSecurityPlainAuth_connectsToServer() - throws MessagingException, IOException, InterruptedException { + public void open_withNoSecurityPlainAuth_connectsToServer() throws Exception { username = "user"; password = "password"; authenticationType = AuthType.PLAIN; @@ -167,8 +115,7 @@ public class SmtpTransportTest { } @Test - public void open_withNoSecurityPlainAuth_usesLoginIfPlainUnavailable() - throws MessagingException, IOException, InterruptedException { + public void open_withNoSecurityPlainAuth_usesLoginIfPlainUnavailable() throws Exception { username = "user"; password = "password"; authenticationType = AuthType.PLAIN; @@ -194,8 +141,7 @@ public class SmtpTransportTest { } @Test - public void open_withNoSecurityPlainAuth_withNeither_throwsException() - throws MessagingException, IOException, InterruptedException { + public void open_withNoSecurityPlainAuth_withNeither_throwsException() throws Exception { username = "user"; password = "password"; authenticationType = AuthType.PLAIN; @@ -212,8 +158,7 @@ public class SmtpTransportTest { transport.open(); fail("Exception expected"); } catch (MessagingException e) { - assertEquals("Authentication methods SASL PLAIN and LOGIN are unavailable.", - e.getMessage()); + assertEquals("Authentication methods SASL PLAIN and LOGIN are unavailable.", e.getMessage()); } server.verifyConnectionStillOpen(); @@ -221,8 +166,7 @@ public class SmtpTransportTest { } @Test - public void open_withNoSecurityCramMd5Auth_connectsToServer() - throws MessagingException, IOException, InterruptedException { + public void open_withNoSecurityCramMd5Auth_connectsToServer() throws Exception { username = "user"; password = "password"; authenticationType = AuthType.CRAM_MD5; @@ -246,8 +190,7 @@ public class SmtpTransportTest { } @Test - public void open_withNoSecurityCramMd5Auth_withNoSupport_throwsException() - throws MessagingException, IOException, InterruptedException { + public void open_withNoSecurityCramMd5Auth_withNoSupport_throwsException() throws Exception { username = "user"; password = "password"; authenticationType = AuthType.CRAM_MD5; @@ -264,8 +207,7 @@ public class SmtpTransportTest { transport.open(); fail("Exception expected"); } catch (MessagingException e) { - assertEquals("Authentication method CRAM-MD5 is unavailable.", - e.getMessage()); + assertEquals("Authentication method CRAM-MD5 is unavailable.", e.getMessage()); } server.verifyConnectionStillOpen(); @@ -273,8 +215,7 @@ public class SmtpTransportTest { } @Test - public void open_withNoSecurityExternalAuth_connectsToServer() - throws MessagingException, IOException, InterruptedException { + public void open_withNoSecurityExternalAuth_connectsToServer() throws Exception { username = "user"; password = "password"; authenticationType = AuthType.EXTERNAL; @@ -296,8 +237,7 @@ public class SmtpTransportTest { } @Test - public void open_withNoSecurityExternal_withNoSupport_throwsException() - throws MessagingException, IOException, InterruptedException { + public void open_withNoSecurityExternal_withNoSupport_throwsException() throws Exception { username = "user"; password = "password"; authenticationType = AuthType.EXTERNAL; @@ -320,9 +260,9 @@ public class SmtpTransportTest { server.verifyConnectionStillOpen(); server.verifyInteractionCompleted(); } + @Test - public void open_withNoSecurityAutomatic_connectsToServerWithCramMD5IfSupported() - throws MessagingException, IOException, InterruptedException { + public void open_withNoSecurityAutomatic_connectsToServerWithCramMD5IfSupported() throws Exception { username = "user"; password = "password"; authenticationType = AuthType.AUTOMATIC; @@ -346,8 +286,7 @@ public class SmtpTransportTest { } @Test - public void open_withNoSecurityAutomatic_withCramMD5Unsupported_throwsException() - throws MessagingException, IOException, InterruptedException { + public void open_withNoSecurityAutomatic_withCramMD5Unsupported_throwsException() throws Exception { username = "user"; password = "password"; authenticationType = AuthType.AUTOMATIC; @@ -373,8 +312,7 @@ public class SmtpTransportTest { } @Test - public void open_triesHELO_whenServerDoesntSupportEHLO() - throws MessagingException, IOException, InterruptedException { + public void open_triesHELO_whenServerDoesntSupportEHLO() throws Exception { username = "user"; authenticationType = AuthType.PLAIN; connectionSecurity = ConnectionSecurity.NONE; @@ -394,9 +332,7 @@ public class SmtpTransportTest { } @Test - public void sendMessage_withNoAddressToSendTo_doesntOpenConnection() - throws MessagingException, IOException, InterruptedException { - + public void sendMessage_withNoAddressToSendTo_doesntOpenConnection() throws Exception { MimeMessage message = new MimeMessage(); MockSmtpServer server = new MockSmtpServer(); @@ -409,12 +345,10 @@ public class SmtpTransportTest { } @Test - public void sendMessage_withToAddressToSendTo_opensConnection() - throws MessagingException, IOException, InterruptedException { - + public void sendMessage_withToAddressToSendTo_opensConnection() throws Exception { TestMessage message = new TestMessage(); message.setFrom(new Address("user@localhost")); - message.setRecipients(Message.RecipientType.TO, new Address[]{new Address("user2@localhost")}); + message.setRecipients(Message.RecipientType.TO, new Address[] { new Address("user2@localhost") }); MockSmtpServer server = new MockSmtpServer(); setupConnectAndPlainAuthentication(server); @@ -429,19 +363,17 @@ public class SmtpTransportTest { server.output("250 OK: queued as 12345"); server.expect("QUIT"); server.output("221 BYE"); - - + SmtpTransport transport = startServerAndCreateSmtpTransport(server); transport.sendMessage(message); } @Test - public void sendMessage_with8BitEncoding_usesEncoding() - throws MessagingException, IOException, InterruptedException { + public void sendMessage_with8BitEncoding_usesEncoding() throws Exception { extensions.add("8BITMIME"); TestMessage message = new TestMessage(); message.setFrom(new Address("user@localhost")); - message.setRecipients(Message.RecipientType.TO, new Address[]{new Address("user2@localhost")}); + message.setRecipients(Message.RecipientType.TO, new Address[] { new Address("user2@localhost") }); MockSmtpServer server = new MockSmtpServer(); setupConnectAndPlainAuthentication(server); @@ -456,19 +388,17 @@ public class SmtpTransportTest { server.output("250 OK: queued as 12345"); server.expect("QUIT"); server.output("221 BYE"); - - + SmtpTransport transport = startServerAndCreateSmtpTransport(server); transport.sendMessage(message); } @Test - public void sendMessage_withMessageTooLarge_throwsException() - throws MessagingException, IOException, InterruptedException { + public void sendMessage_withMessageTooLarge_throwsException() throws Exception { extensions.add("SIZE 1000"); TestMessage message = new TestMessage(); message.setFrom(new Address("user@localhost")); - message.setRecipients(Message.RecipientType.TO, new Address[]{new Address("user2@localhost")}); + message.setRecipients(Message.RecipientType.TO, new Address[] { new Address("user2@localhost") }); message.setAttachmentCount(1); message.setBody(new BinaryMemoryBody(new byte[1001], "US-ASCII")); @@ -487,11 +417,10 @@ public class SmtpTransportTest { } @Test - public void sendMessage_withNegativeReply_throwsException() - throws MessagingException, IOException, InterruptedException { + public void sendMessage_withNegativeReply_throwsException() throws Exception { TestMessage message = new TestMessage(); message.setFrom(new Address("user@localhost")); - message.setRecipients(Message.RecipientType.TO, new Address[]{new Address("user2@localhost")}); + message.setRecipients(Message.RecipientType.TO, new Address[] { new Address("user2@localhost") }); MockSmtpServer server = new MockSmtpServer(); setupConnectAndPlainAuthentication(server); @@ -518,4 +447,49 @@ public class SmtpTransportTest { assertEquals("4.7.0 Temporary system problem", e.getReplyText()); } } + + private void resetConnectionParameters() { + host = null; + port = -1; + username = null; + password = null; + authenticationType = null; + clientCertificateAlias = null; + connectionSecurity = null; + extensions = new ArrayList<>(); + } + + private SmtpTransport startServerAndCreateSmtpTransport(MockSmtpServer server) throws IOException, + MessagingException { + server.start(); + host = server.getHost(); + port = server.getPort(); + ServerSettings serverSettings = new ServerSettings(Type.SMTP, host, port, connectionSecurity, + authenticationType, username, password, clientCertificateAlias); + String uri = SmtpTransport.createUri(serverSettings); + when(storeConfig.getTransportUri()).thenReturn(uri); + return createSmtpTransport(storeConfig, socketFactory); + } + + private SmtpTransport createSmtpTransport(StoreConfig storeConfig, TrustedSocketFactory socketFactory) + throws MessagingException { + return new SmtpTransport(storeConfig, socketFactory); + } + + private void setupConnectAndPlainAuthentication(MockSmtpServer server) { + username = "user"; + password = "password"; + authenticationType = AuthType.PLAIN; + connectionSecurity = ConnectionSecurity.NONE; + + server.output("220 localhost Simple Mail Transfer Service Ready"); + server.expect("EHLO localhost"); + server.output("250-localhost Hello client.localhost"); + for (String extension : extensions) { + server.output("250-" + extension); + } + server.output("250 AUTH LOGIN PLAIN CRAM-MD5"); + server.expect("AUTH PLAIN AHVzZXIAcGFzc3dvcmQ="); + server.output("235 2.7.0 Authentication successful"); + } } diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/transport/mockServer/MockSmtpServer.java b/k9mail-library/src/test/java/com/fsck/k9/mail/transport/mockServer/MockSmtpServer.java index 01ebd2456..9d6416cd6 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/transport/mockServer/MockSmtpServer.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/transport/mockServer/MockSmtpServer.java @@ -1,13 +1,6 @@ package com.fsck.k9.mail.transport.mockServer; -import android.annotation.SuppressLint; - -import com.jcraft.jzlib.JZlib; -import com.jcraft.jzlib.ZOutputStream; - -import org.apache.commons.io.IOUtils; - import java.io.EOFException; import java.io.IOException; import java.io.InputStream; @@ -28,14 +21,18 @@ import java.util.concurrent.TimeUnit; import java.util.zip.Inflater; import java.util.zip.InflaterInputStream; +import android.annotation.SuppressLint; + +import com.jcraft.jzlib.JZlib; +import com.jcraft.jzlib.ZOutputStream; import javax.net.ssl.KeyManagerFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocket; import javax.net.ssl.SSLSocketFactory; - import okio.BufferedSink; import okio.BufferedSource; import okio.Okio; +import org.apache.commons.io.IOUtils; @SuppressLint("NewApi") @@ -185,10 +182,12 @@ public class MockSmtpServer { public interface Logger { void log(String message); + void log(String format, Object... args); } - private interface SmtpInteraction {} + private interface SmtpInteraction { + } private static class ExpectedCommand implements SmtpInteraction { private final String command; diff --git a/k9mail-library/src/test/java/com/fsck/k9/mail/transport/mockServer/TestMessage.java b/k9mail-library/src/test/java/com/fsck/k9/mail/transport/mockServer/TestMessage.java index 0b9301709..208167ee2 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/mail/transport/mockServer/TestMessage.java +++ b/k9mail-library/src/test/java/com/fsck/k9/mail/transport/mockServer/TestMessage.java @@ -1,28 +1,23 @@ package com.fsck.k9.mail.transport.mockServer; -import android.content.ContentValues; -import android.database.Cursor; -import android.database.sqlite.SQLiteDatabase; - import com.fsck.k9.mail.Address; import com.fsck.k9.mail.Flag; import com.fsck.k9.mail.MessagingException; import com.fsck.k9.mail.internet.MimeMessage; -public class TestMessage extends MimeMessage { +public class TestMessage extends MimeMessage { private long mId; private int mAttachmentCount; private String mSubject; - private String mPreview = ""; - private long mThreadId; private long mRootId; private long messagePartId; private String mimeType; + public long getMessagePartId() { return messagePartId; } @@ -72,7 +67,6 @@ public class TestMessage extends MimeMessage { this.mFrom = new Address[] { from }; } - @Override public void setReplyTo(Address[] replyTo) { if (replyTo == null || replyTo.length == 0) { @@ -82,7 +76,6 @@ public class TestMessage extends MimeMessage { } } - /* * For performance reasons, we add headers instead of setting them (see super implementation) * which removes (expensive) them before adding them diff --git a/k9mail-library/src/test/java/com/fsck/k9/testHelpers/TestTrustedSocketFactory.java b/k9mail-library/src/test/java/com/fsck/k9/testHelpers/TestTrustedSocketFactory.java index d375598c8..01b1e143d 100644 --- a/k9mail-library/src/test/java/com/fsck/k9/testHelpers/TestTrustedSocketFactory.java +++ b/k9mail-library/src/test/java/com/fsck/k9/testHelpers/TestTrustedSocketFactory.java @@ -1,18 +1,18 @@ package com.fsck.k9.testHelpers; -import com.fsck.k9.mail.MessagingException; -import com.fsck.k9.mail.ssl.TrustedSocketFactory; -import com.fsck.k9.mail.store.imap.ImapConnectionTest; import java.io.IOException; import java.net.Socket; import java.security.KeyManagementException; import java.security.NoSuchAlgorithmException; +import com.fsck.k9.mail.MessagingException; +import com.fsck.k9.mail.ssl.TrustedSocketFactory; import javax.net.ssl.SSLContext; import javax.net.ssl.SSLSocketFactory; import javax.net.ssl.TrustManager; + public class TestTrustedSocketFactory implements TrustedSocketFactory { @Override public Socket createSocket(Socket socket, String host, int port, String clientCertificateAlias)