diff options
author | Sébastien Lesaint <sebastien.lesaint@sonarsource.com> | 2017-09-14 11:19:17 +0200 |
---|---|---|
committer | Eric Hartmann <hartmann.eric@gmail.Com> | 2017-10-02 13:03:35 +0200 |
commit | a94a39eb02cc3064d9d13f539fcef01b9879fecc (patch) | |
tree | 25ee3d9f202076466e4f7c3d83f59787c81a5b29 /server | |
parent | 96f16d3ea0fe077642c9d3c83a8b2e52a1d9c859 (diff) | |
download | sonarqube-a94a39eb02cc3064d9d13f539fcef01b9879fecc.tar.gz sonarqube-a94a39eb02cc3064d9d13f539fcef01b9879fecc.zip |
SONAR-9725 grammar support for single rule in Builtin QP notification
Diffstat (limited to 'server')
2 files changed, 51 insertions, 21 deletions
diff --git a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java index 8572d30e8a1..641b18d5ded 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java +++ b/server/sonar-server/src/main/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplate.java @@ -69,15 +69,21 @@ public class BuiltInQualityProfilesNotificationTemplate extends EmailTemplate { .append("\n"); int newRules = profile.getNewRules(); if (newRules > 0) { - message.append(" ").append(newRules).append(" new rules\n"); + message.append(" ").append(newRules).append(" new rule") + .append(plural(newRules)) + .append('\n'); } int updatedRules = profile.getUpdatedRules(); if (updatedRules > 0) { - message.append(" ").append(updatedRules).append(" rules have been updated\n"); + message.append(" ").append(updatedRules).append(" rule") + .append(updatedRules > 1 ? "s have been updated" : " has been updated") + .append("\n"); } int removedRules = profile.getRemovedRules(); if (removedRules > 0) { - message.append(" ").append(removedRules).append(" rules removed\n"); + message.append(" ").append(removedRules).append(" rule") + .append(plural(removedRules)) + .append(" removed\n"); } message.append("\n"); }); @@ -92,6 +98,10 @@ public class BuiltInQualityProfilesNotificationTemplate extends EmailTemplate { .setMessage(message.toString()); } + private static String plural(int count) { + return count > 1 ? "s" : ""; + } + public String encode(String text) { try { return URLEncoder.encode(text, UTF_8.name()); diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplateTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplateTest.java index c3514e636a2..62d9955f833 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplateTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/BuiltInQualityProfilesNotificationTemplateTest.java @@ -63,7 +63,7 @@ public class BuiltInQualityProfilesNotificationTemplateTest { } @Test - public void notification_contains_list_of_new_rules() { + public void notification_contains_count_of_new_rules() { String profileName = newProfileName(); String languageKey = newLanguageKey(); String languageName = newLanguageName(); @@ -77,12 +77,11 @@ public class BuiltInQualityProfilesNotificationTemplateTest { EmailMessage emailMessage = underTest.format(notification.serialize()); - assertMessage(emailMessage, - "\n 2 new rules\n"); + assertMessage(emailMessage, "\n 2 new rules\n"); } @Test - public void notification_contains_list_of_updated_rules() { + public void notification_contains_count_of_updated_rules() { String profileName = newProfileName(); String languageKey = newLanguageKey(); String languageName = newLanguageName(); @@ -96,13 +95,11 @@ public class BuiltInQualityProfilesNotificationTemplateTest { EmailMessage emailMessage = underTest.format(notification.serialize()); - assertMessage(emailMessage, - "\n" + - " 2 rules have been updated\n"); + assertMessage(emailMessage, "\n 2 rules have been updated\n"); } @Test - public void notification_contains_list_of_removed_rules() { + public void notification_contains_count_of_removed_rules() { String profileName = newProfileName(); String languageKey = newLanguageKey(); String languageName = newLanguageName(); @@ -116,9 +113,30 @@ public class BuiltInQualityProfilesNotificationTemplateTest { EmailMessage emailMessage = underTest.format(notification.serialize()); - assertMessage(emailMessage, - "\n" + - " 2 rules removed\n"); + assertMessage(emailMessage, "\n 2 rules removed\n"); + } + + @Test + public void notification_supports_grammar_for_single_rule_added_removed_or_updated() { + String profileName = newProfileName(); + String languageKey = newLanguageKey(); + String languageName = newLanguageName(); + BuiltInQualityProfilesNotification notification = new BuiltInQualityProfilesNotification() + .addProfile(Profile.newBuilder() + .setProfileName(profileName) + .setLanguageKey(languageKey) + .setLanguageName(languageName) + .setNewRules(1) + .setUpdatedRules(1) + .setRemovedRules(1) + .build()); + + EmailMessage emailMessage = underTest.format(notification.serialize()); + + assertThat(emailMessage.getMessage()) + .contains("\n 1 new rule\n") + .contains("\n 1 rule has been updated\n") + .contains("\n 1 rule removed\n"); } @Test @@ -139,7 +157,7 @@ public class BuiltInQualityProfilesNotificationTemplateTest { EmailMessage emailMessage = underTest.format(notification.serialize()); assertMessage(emailMessage, - "\n" + + "\n" + " 2 new rules\n" + " 3 rules have been updated\n" + " 4 rules removed\n"); @@ -171,9 +189,9 @@ public class BuiltInQualityProfilesNotificationTemplateTest { assertThat(emailMessage.getMessage()).containsSequence("The following built-in profiles have been updated:\n", profileTitleText(profileName1, languageKey1, languageName1), - " 2 new rules\n", - profileTitleText(profileName2, languageKey2, languageName2), - " 13 new rules\n", + " 2 new rules\n", + profileTitleText(profileName2, languageKey2, languageName2), + " 13 new rules\n", "This is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles"); } @@ -236,9 +254,11 @@ public class BuiltInQualityProfilesNotificationTemplateTest { } private void assertMessage(EmailMessage emailMessage, String expectedProfileDetails) { - assertThat(emailMessage.getMessage()).containsSequence("The following built-in profiles have been updated:\n\n", - expectedProfileDetails, - "\nThis is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles"); + assertThat(emailMessage.getMessage()) + .containsSequence( + "The following built-in profiles have been updated:\n\n", + expectedProfileDetails, + "\nThis is a good time to review your quality profiles and update them to benefit from the latest evolutions: " + server.getPublicRootUrl() + "/profiles"); } private String profileTitleText(String profileName, String languageKey, String languageName) { |