diff options
author | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-03-07 12:08:56 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@sonarsource.com> | 2016-03-07 12:08:56 +0100 |
commit | 6368d2292614f67492fb28ff5486b7aa3ef6c699 (patch) | |
tree | 8d1d885267f959d711a6da9338ce20c14ed8cd00 /server/sonar-server | |
parent | 597a4fed424d895bbd9b54287f90ad67ae5b5161 (diff) | |
download | sonarqube-6368d2292614f67492fb28ff5486b7aa3ef6c699.tar.gz sonarqube-6368d2292614f67492fb28ff5486b7aa3ef6c699.zip |
Improve allocation of new ports in integration tests
Diffstat (limited to 'server/sonar-server')
-rw-r--r-- | server/sonar-server/src/test/java/org/sonar/server/notification/email/EmailNotificationChannelTest.java | 60 |
1 files changed, 22 insertions, 38 deletions
diff --git a/server/sonar-server/src/test/java/org/sonar/server/notification/email/EmailNotificationChannelTest.java b/server/sonar-server/src/test/java/org/sonar/server/notification/email/EmailNotificationChannelTest.java index 5c7db9efd6a..a1dc5a7ab8c 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/notification/email/EmailNotificationChannelTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/notification/email/EmailNotificationChannelTest.java @@ -19,8 +19,6 @@ */ package org.sonar.server.notification.email; -import java.io.IOException; -import java.net.ServerSocket; import java.util.List; import javax.mail.internet.MimeMessage; import org.apache.commons.mail.EmailException; @@ -44,44 +42,30 @@ public class EmailNotificationChannelTest { @Rule public ExpectedException thrown = ExpectedException.none(); - private int port; - private Wiser server; + private Wiser smtpServer; private EmailSettings configuration; - private EmailNotificationChannel channel; - - private static int getNextAvailablePort() { - try { - ServerSocket socket = new ServerSocket(0); - int unusedPort = socket.getLocalPort(); - socket.close(); - return unusedPort; - } catch (IOException e) { - throw new RuntimeException("Error getting an available port from system", e); - } - } + private EmailNotificationChannel underTest; @Before public void setUp() { - port = getNextAvailablePort(); - server = new Wiser(); - server.setPort(port); - server.start(); + smtpServer = new Wiser(0); + smtpServer.start(); configuration = mock(EmailSettings.class); - channel = new EmailNotificationChannel(configuration, null, null); + underTest = new EmailNotificationChannel(configuration, null, null); } @After public void tearDown() { - server.stop(); + smtpServer.stop(); } @Test public void shouldSendTestEmail() throws Exception { configure(); - channel.sendTestEmail("user@nowhere", "Test Message from SonarQube", "This is a test message from SonarQube."); + underTest.sendTestEmail("user@nowhere", "Test Message from SonarQube", "This is a test message from SonarQube."); - List<WiserMessage> messages = server.getMessages(); + List<WiserMessage> messages = smtpServer.getMessages(); assertThat(messages).hasSize(1); MimeMessage email = messages.get(0).getMimeMessage(); @@ -95,10 +79,10 @@ public class EmailNotificationChannelTest { @Test public void shouldThrowAnExceptionWhenUnableToSendTestEmail() { configure(); - server.stop(); + smtpServer.stop(); try { - channel.sendTestEmail("user@nowhere", "Test Message from SonarQube", "This is a test message from SonarQube."); + underTest.sendTestEmail("user@nowhere", "Test Message from SonarQube", "This is a test message from SonarQube."); fail(); } catch (EmailException e) { // expected @@ -111,8 +95,8 @@ public class EmailNotificationChannelTest { .setTo("user@nowhere") .setSubject("Foo") .setMessage("Bar"); - channel.deliver(emailMessage); - assertThat(server.getMessages()).isEmpty(); + underTest.deliver(emailMessage); + assertThat(smtpServer.getMessages()).isEmpty(); } @Test @@ -124,9 +108,9 @@ public class EmailNotificationChannelTest { .setTo("user@nowhere") .setSubject("Review #3") .setMessage("I'll take care of this violation."); - channel.deliver(emailMessage); + underTest.deliver(emailMessage); - List<WiserMessage> messages = server.getMessages(); + List<WiserMessage> messages = smtpServer.getMessages(); assertThat(messages).hasSize(1); MimeMessage email = messages.get(0).getMimeMessage(); @@ -152,9 +136,9 @@ public class EmailNotificationChannelTest { .setTo("user@nowhere") .setSubject("Foo") .setMessage("Bar"); - channel.deliver(emailMessage); + underTest.deliver(emailMessage); - List<WiserMessage> messages = server.getMessages(); + List<WiserMessage> messages = smtpServer.getMessages(); assertThat(messages).hasSize(1); MimeMessage email = messages.get(0).getMimeMessage(); @@ -176,24 +160,24 @@ public class EmailNotificationChannelTest { @Test public void shouldNotThrowAnExceptionWhenUnableToSendEmail() { configure(); - server.stop(); + smtpServer.stop(); EmailMessage emailMessage = new EmailMessage() .setTo("user@nowhere") .setSubject("Foo") .setMessage("Bar"); - channel.deliver(emailMessage); + underTest.deliver(emailMessage); } @Test public void shouldSendTestEmailWithSTARTTLS() { - server.getServer().setEnableTLS(true); - server.getServer().setRequireTLS(true); + smtpServer.getServer().setEnableTLS(true); + smtpServer.getServer().setRequireTLS(true); configure(); when(configuration.getSecureConnection()).thenReturn("STARTTLS"); try { - channel.sendTestEmail("user@nowhere", "Test Message from SonarQube", "This is a test message from SonarQube."); + underTest.sendTestEmail("user@nowhere", "Test Message from SonarQube", "This is a test message from SonarQube."); fail("An SSL exception was expected a a proof that STARTTLS is enabled"); } catch (EmailException e) { // We don't have a SSL certificate so we are expecting a SSL error @@ -203,7 +187,7 @@ public class EmailNotificationChannelTest { private void configure() { when(configuration.getSmtpHost()).thenReturn("localhost"); - when(configuration.getSmtpPort()).thenReturn(port); + when(configuration.getSmtpPort()).thenReturn(smtpServer.getServer().getPort()); when(configuration.getFrom()).thenReturn("server@nowhere"); when(configuration.getPrefix()).thenReturn("[SONARQUBE]"); when(configuration.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org"); |