From 28ae81e81f52fb4176ce091a8f6a0c2fd41b032b Mon Sep 17 00:00:00 2001 From: Evgeny Mandrikov Date: Fri, 30 Sep 2011 16:00:22 +0400 Subject: SONAR-2779 Explicitly specify UTF-8 for outgoing emails --- .../emailnotifications/EmailNotificationChannel.java | 1 + .../emailnotifications/EmailNotificationChannelTest.java | 16 ++++++++++++++-- 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java index df3f5ea7769..115191d9394 100644 --- a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java +++ b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java @@ -162,6 +162,7 @@ public class EmailNotificationChannel extends NotificationChannel { email.addHeader(LIST_ARCHIVE_HEADER, configuration.getServerBaseURL()); } // Set general information + email.setCharset("UTF-8"); email.setFrom(configuration.getFrom(), StringUtils.defaultIfBlank(emailMessage.getFrom(), FROM_NAME_DEFAULT)); email.addTo(emailMessage.getTo(), " "); String subject = StringUtils.defaultIfBlank(StringUtils.trimToEmpty(configuration.getPrefix()) + " ", "") diff --git a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/EmailNotificationChannelTest.java b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/EmailNotificationChannelTest.java index 1446e7d0b8f..0dc4182d0b3 100644 --- a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/EmailNotificationChannelTest.java +++ b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/EmailNotificationChannelTest.java @@ -22,6 +22,7 @@ package org.sonar.plugins.emailnotifications; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.nullValue; import static org.junit.Assert.assertThat; +import static org.junit.Assert.fail; import static org.junit.Assume.assumeThat; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -88,18 +89,25 @@ public class EmailNotificationChannelTest { assertThat(server.getReceivedEmailSize(), is(1)); SmtpMessage email = (SmtpMessage) server.getReceivedEmail().next(); + assertThat(email.getHeaderValue("Content-Type"), is("text/plain; charset=UTF-8")); + assertThat(email.getHeaderValue("From"), is("Sonar ")); assertThat(email.getHeaderValue("To"), is("")); assertThat(email.getHeaderValue("Subject"), is("[SONAR] Test Message from Sonar")); assertThat(email.getBody(), is("This is a test message from Sonar.")); } - @Test(expected = EmailException.class) + @Test public void shouldThrowAnExceptionWhenUnableToSendTestEmail() throws Exception { configure(); server.stop(); - channel.sendTestEmail("user@nowhere", "Test Message from Sonar", "This is a test message from Sonar."); + try { + channel.sendTestEmail("user@nowhere", "Test Message from Sonar", "This is a test message from Sonar."); + fail(); + } catch (EmailException e) { + // expected + } } @Test @@ -126,6 +134,8 @@ public class EmailNotificationChannelTest { assertThat(server.getReceivedEmailSize(), is(1)); SmtpMessage email = (SmtpMessage) server.getReceivedEmail().next(); + assertThat(email.getHeaderValue("Content-Type"), is("text/plain; charset=UTF-8")); + assertThat(email.getHeaderValue("In-Reply-To"), is("")); assertThat(email.getHeaderValue("References"), is("")); @@ -150,6 +160,8 @@ public class EmailNotificationChannelTest { assertThat(server.getReceivedEmailSize(), is(1)); SmtpMessage email = (SmtpMessage) server.getReceivedEmail().next(); + assertThat(email.getHeaderValue("Content-Type"), is("text/plain; charset=UTF-8")); + assertThat(email.getHeaderValue("In-Reply-To"), nullValue()); assertThat(email.getHeaderValue("References"), nullValue()); -- cgit v1.2.3