From a65c759c562b567b28e0e3eac63895af20d01f41 Mon Sep 17 00:00:00 2001 From: Fabrice Bellingard Date: Thu, 31 Jan 2013 17:02:30 +0100 Subject: [PATCH] SONAR-3959 Add possibility to set default message to Notification class --- .../sonar/api/notifications/Notification.java | 42 +++++++++- .../notifications/NotificationTest.java | 77 +++++++++++++++++++ 2 files changed, 115 insertions(+), 4 deletions(-) create mode 100644 sonar-server/src/test/java/org/sonar/server/notifications/NotificationTest.java diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/notifications/Notification.java b/sonar-plugin-api/src/main/java/org/sonar/api/notifications/Notification.java index c69cc777d3f..ad27351ddab 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/notifications/Notification.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/notifications/Notification.java @@ -19,13 +19,12 @@ */ package org.sonar.api.notifications; -import java.io.Serializable; -import java.util.HashMap; - +import com.google.common.collect.Maps; import org.apache.commons.lang.builder.ReflectionToStringBuilder; import org.apache.commons.lang.builder.ToStringStyle; -import com.google.common.collect.Maps; +import java.io.Serializable; +import java.util.HashMap; /** *

@@ -33,11 +32,18 @@ import com.google.common.collect.Maps; * knowledge of the possible ways to be delivered (see {@link NotificationChannel}) or of the users who should * receive it (see {@link NotificationDispatcher}). *

+ *

+ * When creating a new notification, it is strongly advised to give a default message that can be used by channels + * that don't want to specifically format messages for different notification types. You can use + * {@link Notification#setDefaultMessage(String)} for that purpose. + *

* * @since 2.10 */ public class Notification implements Serializable { + private static final String DEFAULT_MESSAGE_KEY = "default_message"; + private String type; private HashMap fields = Maps.newHashMap(); // NOSONAR false-positive due to serialization : usage of HashMap instead of @@ -64,6 +70,34 @@ public class Notification implements Serializable { return type; } + /** + *

+ * When creating a new notification, it is strongly advised to give a default message that can be + * used by channels that don't want to specifically format messages for different notification types. + *

+ *

+ * This method is equivalent to setting a value for the field {@link #DEFAULT_MESSAGE_KEY} with + * {@link #setFieldValue(String, String)}. + *

+ * + * @since 3.5 + */ + public Notification setDefaultMessage(String value) { + setFieldValue(DEFAULT_MESSAGE_KEY, value); + return this; + } + + /** + * Returns the default message to display for this notification. + */ + public String getDefaultMessage() { + String defaultMessage = getFieldValue(DEFAULT_MESSAGE_KEY); + if (defaultMessage == null) { + defaultMessage = this.toString(); + } + return defaultMessage; + } + /** * Adds a field (kind of property) to the notification * diff --git a/sonar-server/src/test/java/org/sonar/server/notifications/NotificationTest.java b/sonar-server/src/test/java/org/sonar/server/notifications/NotificationTest.java new file mode 100644 index 00000000000..87b041e0984 --- /dev/null +++ b/sonar-server/src/test/java/org/sonar/server/notifications/NotificationTest.java @@ -0,0 +1,77 @@ +/* + * Sonar, open source software quality management tool. + * Copyright (C) 2008-2012 SonarSource + * mailto:contact AT sonarsource DOT com + * + * Sonar is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * Sonar is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with Sonar; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 + */ +package org.sonar.server.notifications; + +import org.junit.Before; +import org.junit.Test; +import org.sonar.api.notifications.Notification; + +import static org.fest.assertions.Assertions.assertThat; + +public class NotificationTest { + + private Notification notification; + + @Before + public void init() { + notification = new Notification("alerts").setDefaultMessage("There are new alerts").setFieldValue("alertCount", "42"); + } + + @Test + public void shouldReturnType() { + assertThat(notification.getType()).isEqualTo("alerts"); + } + + @Test + public void shouldReturnDefaultMessage() { + assertThat(notification.getDefaultMessage()).isEqualTo("There are new alerts"); + } + + @Test + public void shouldReturnToStringIfDefaultMessageNotSet() { + notification = new Notification("alerts").setFieldValue("alertCount", "42"); + System.out.println(notification); + assertThat(notification.getDefaultMessage()).contains("type=alerts"); + assertThat(notification.getDefaultMessage()).contains("fields={alertCount=42}"); + } + + @Test + public void shouldReturnField() { + assertThat(notification.getFieldValue("alertCount")).isEqualTo("42"); + assertThat(notification.getFieldValue("fake")).isNull(); + + // default message is stored as field as well + assertThat(notification.getFieldValue("default_message")).isEqualTo("There are new alerts"); + } + + @Test + public void shouldEqual() { + assertThat(notification.equals("")).isFalse(); + assertThat(notification.equals(null)).isFalse(); + assertThat(notification.equals(notification)).isTrue(); + + Notification otherNotif = new Notification("alerts").setDefaultMessage("There are new alerts").setFieldValue("alertCount", "42"); + assertThat(otherNotif).isEqualTo(notification); + + otherNotif = new Notification("alerts").setDefaultMessage("There are new alerts").setFieldValue("alertCount", "15000"); + assertThat(otherNotif).isNotEqualTo(notification); + } + +} -- 2.39.5