2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.emailnotifications;
22 import org.apache.commons.mail.EmailException;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.sonar.api.platform.EmailSettings;
27 import org.sonar.plugins.emailnotifications.api.EmailMessage;
28 import org.subethamail.wiser.Wiser;
29 import org.subethamail.wiser.WiserMessage;
31 import javax.mail.internet.MimeMessage;
33 import java.io.IOException;
34 import java.net.ServerSocket;
35 import java.util.List;
37 import static org.hamcrest.Matchers.is;
38 import static org.hamcrest.Matchers.nullValue;
39 import static org.hamcrest.Matchers.startsWith;
40 import static org.junit.Assert.assertThat;
41 import static org.junit.Assert.fail;
42 import static org.mockito.Mockito.mock;
43 import static org.mockito.Mockito.when;
45 public class EmailNotificationChannelTest {
49 private EmailSettings configuration;
50 private EmailNotificationChannel channel;
52 private static int getNextAvailablePort() {
54 ServerSocket socket = new ServerSocket(0);
55 int unusedPort = socket.getLocalPort();
58 } catch (IOException e) {
59 throw new RuntimeException("Error getting an available port from system", e);
65 port = getNextAvailablePort();
70 configuration = mock(EmailSettings.class);
71 channel = new EmailNotificationChannel(configuration, null, null);
75 public void tearDown() {
80 public void shouldSendTestEmail() throws Exception {
82 channel.sendTestEmail("user@nowhere", "Test Message from Sonar", "This is a test message from Sonar.");
84 List<WiserMessage> messages = server.getMessages();
85 assertThat(messages.size(), is(1));
87 MimeMessage email = messages.get(0).getMimeMessage();
88 assertThat(email.getHeader("Content-Type", null), is("text/plain; charset=UTF-8"));
89 assertThat(email.getHeader("From", ","), is("Sonar <server@nowhere>"));
90 assertThat(email.getHeader("To", null), is("<user@nowhere>"));
91 assertThat(email.getHeader("Subject", null), is("[SONAR] Test Message from Sonar"));
92 assertThat((String) email.getContent(), startsWith("This is a test message from Sonar."));
96 public void shouldThrowAnExceptionWhenUnableToSendTestEmail() throws Exception {
101 channel.sendTestEmail("user@nowhere", "Test Message from Sonar", "This is a test message from Sonar.");
103 } catch (EmailException e) {
109 public void shouldNotSendEmailWhenHostnameNotConfigured() throws Exception {
110 EmailMessage emailMessage = new EmailMessage()
111 .setTo("user@nowhere")
114 channel.deliver(emailMessage);
115 assertThat(server.getMessages().size(), is(0));
119 public void shouldSendThreadedEmail() throws Exception {
121 EmailMessage emailMessage = new EmailMessage()
122 .setMessageId("reviews/view/1")
123 .setFrom("Full Username")
124 .setTo("user@nowhere")
125 .setSubject("Review #3")
126 .setMessage("I'll take care of this violation.");
127 channel.deliver(emailMessage);
129 List<WiserMessage> messages = server.getMessages();
130 assertThat(messages.size(), is(1));
132 MimeMessage email = messages.get(0).getMimeMessage();
134 assertThat(email.getHeader("Content-Type", null), is("text/plain; charset=UTF-8"));
136 assertThat(email.getHeader("In-Reply-To", null), is("<reviews/view/1@nemo.sonarsource.org>"));
137 assertThat(email.getHeader("References", null), is("<reviews/view/1@nemo.sonarsource.org>"));
139 assertThat(email.getHeader("List-ID", null), is("Sonar <sonar.nemo.sonarsource.org>"));
140 assertThat(email.getHeader("List-Archive", null), is("http://nemo.sonarsource.org"));
142 assertThat(email.getHeader("From", ","), is("\"Full Username (Sonar)\" <server@nowhere>"));
143 assertThat(email.getHeader("To", null), is("<user@nowhere>"));
144 assertThat(email.getHeader("Subject", null), is("[SONAR] Review #3"));
145 assertThat((String) email.getContent(), startsWith("I'll take care of this violation."));
149 public void shouldSendNonThreadedEmail() throws Exception {
151 EmailMessage emailMessage = new EmailMessage()
152 .setTo("user@nowhere")
155 channel.deliver(emailMessage);
157 List<WiserMessage> messages = server.getMessages();
158 assertThat(messages.size(), is(1));
160 MimeMessage email = messages.get(0).getMimeMessage();
162 assertThat(email.getHeader("Content-Type", null), is("text/plain; charset=UTF-8"));
164 assertThat(email.getHeader("In-Reply-To", null), nullValue());
165 assertThat(email.getHeader("References", null), nullValue());
167 assertThat(email.getHeader("List-ID", null), is("Sonar <sonar.nemo.sonarsource.org>"));
168 assertThat(email.getHeader("List-Archive", null), is("http://nemo.sonarsource.org"));
170 assertThat(email.getHeader("From", null), is("Sonar <server@nowhere>"));
171 assertThat(email.getHeader("To", null), is("<user@nowhere>"));
172 assertThat(email.getHeader("Subject", null), is("[SONAR] Foo"));
173 assertThat((String) email.getContent(), startsWith("Bar"));
177 public void shouldNotThrowAnExceptionWhenUnableToSendEmail() throws Exception {
181 EmailMessage emailMessage = new EmailMessage()
182 .setTo("user@nowhere")
185 channel.deliver(emailMessage);
188 private void configure() {
189 when(configuration.getSmtpHost()).thenReturn("localhost");
190 when(configuration.getSmtpPort()).thenReturn(Integer.toString(port));
191 when(configuration.getFrom()).thenReturn("server@nowhere");
192 when(configuration.getPrefix()).thenReturn("[SONAR]");
193 when(configuration.getServerBaseURL()).thenReturn("http://nemo.sonarsource.org");