]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3959 Add possibility to set default message to Notification class
authorFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Thu, 31 Jan 2013 16:02:30 +0000 (17:02 +0100)
committerFabrice Bellingard <fabrice.bellingard@sonarsource.com>
Thu, 31 Jan 2013 16:02:30 +0000 (17:02 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/notifications/Notification.java
sonar-server/src/test/java/org/sonar/server/notifications/NotificationTest.java [new file with mode: 0644]

index c69cc777d3fd7dbbb675cf010c91171f987c1981..ad27351ddab7bbfd4ba691336263fc298b7c72e1 100644 (file)
  */
 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;
 
 /**
  * <p>
@@ -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}).
  * </p> 
+ * <p>
+ * 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.
+ * </p>
  * 
  * @since 2.10
  */
 public class Notification implements Serializable {
 
+  private static final String DEFAULT_MESSAGE_KEY = "default_message";
+
   private String type;
 
   private HashMap<String, String> 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;
   }
 
+  /**
+   * <p>
+   * 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.
+   * </p>
+   * <p>
+   * This method is equivalent to setting a value for the field {@link #DEFAULT_MESSAGE_KEY} with 
+   * {@link #setFieldValue(String, String)}.
+   * </p> 
+   * 
+   * @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 (file)
index 0000000..87b041e
--- /dev/null
@@ -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);
+  }
+
+}