]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-1929 minor refactoring + add some tests
authorSimon Brandhof <simon.brandhof@gmail.com>
Fri, 16 Dec 2011 07:32:48 +0000 (08:32 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Fri, 16 Dec 2011 07:33:01 +0000 (08:33 +0100)
sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/Dashboard.java
sonar-plugin-api/src/test/java/org/sonar/api/web/dashboard/DashboardTest.java
sonar-plugin-api/src/test/java/org/sonar/api/web/dashboard/WidgetTest.java [deleted file]
sonar-server/src/main/java/org/sonar/server/startup/RegisterNewDashboards.java
sonar-server/src/test/java/org/sonar/server/startup/RegisterNewDashboardsTest.java

index 9f56b90eab635b0b51c1880f30cea33acde4ff3f..e1fa4b1df3b9dbbb7486448e55137cf2f52e8f6b 100644 (file)
@@ -48,7 +48,7 @@ public final class Dashboard {
   }
 
   /**
-   * Creates a new {@link Dashboard}-
+   * Creates a new {@link Dashboard}.
    */
   public static Dashboard create(String id, String name) {
     return new Dashboard()
@@ -70,6 +70,9 @@ public final class Dashboard {
 
   /**
    * Add a widget with the given parameters, and return the newly created {@link Widget} object if one wants to add parameters to it.
+   *
+   * @param widgetId id of an existing widget
+   * @param columnId column starts with 1. The widget is ignored if the column id does not match the layout.
    */
   public Widget addWidget(String widgetId, int columnId) {
     if (columnId < 1) {
@@ -98,9 +101,6 @@ public final class Dashboard {
     return id;
   }
 
-  /**
-   * @param id the id to set
-   */
   private Dashboard setId(String id) {
     if (StringUtils.isBlank(id)) {
       throw new IllegalArgumentException("Dashboard id can not be blank");
@@ -148,7 +148,7 @@ public final class Dashboard {
   }
 
   /**
-   * Returns the layout of the dashboard.
+   * Returns the layout. Default value is the 2 columns mode with width 50%/50%.
    *
    * @return the layout
    */
@@ -191,6 +191,10 @@ public final class Dashboard {
       return properties;
     }
 
+    public String getProperty(String key) {
+      return properties.get(key);
+    }
+
     /**
      * Returns the identifier of this widget.
      *
index b35a740c2ea6dde70fe81e1d36a145d521a12033..08c1b535cae5726b50d0cdb92a8138b3fe0a3692 100644 (file)
@@ -87,6 +87,21 @@ public class DashboardTest {
 
   @Test
   public void shouldAddSeveralTimesTheSameWidget() {
-    // TODO
+    Dashboard dashboard = Dashboard.createByName("Fake");
+    dashboard.addWidget("most_violated_rules", 1);
+    dashboard.addWidget("most_violated_rules", 1).setProperty("foo", "bar");
+
+    assertThat(dashboard.getWidgets().size(), is(2));
+    assertThat(dashboard.getWidgetsOfColumn(1).get(0).getProperties().size(), is(0));
+    assertThat(dashboard.getWidgetsOfColumn(1).get(1).getProperty("foo"), is("bar"));
+  }
+
+  @Test
+  public void shouldSetWidgetProperty() {
+    Dashboard dashboard = Dashboard.createByName("Fake");
+    Dashboard.Widget widget = dashboard.addWidget("fake-widget", 1);
+    widget.setProperty("foo", "bar");
+
+    assertThat(widget.getProperties().get("foo"), is("bar"));
   }
 }
diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/web/dashboard/WidgetTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/web/dashboard/WidgetTest.java
deleted file mode 100644 (file)
index 95af669..0000000
+++ /dev/null
@@ -1,48 +0,0 @@
-/*
- * Sonar, open source software quality management tool.
- * Copyright (C) 2008-2011 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.api.web.dashboard;
-
-import org.junit.Test;
-
-import java.util.Map.Entry;
-import java.util.Set;
-
-import static org.hamcrest.Matchers.is;
-import static org.junit.Assert.assertThat;
-
-public class WidgetTest {
-
-  @Test
-  public void shouldCreateWidget() {
-    Dashboard dashboard = Dashboard.createByName("Fake");
-    Dashboard.Widget widget = dashboard.addWidget("fake-widget", 1);
-    assertThat(widget.getId(), is("fake-widget"));
-  }
-
-  @Test
-  public void shouldSetProperty() {
-    Dashboard dashboard = Dashboard.createByName("Fake");
-    Dashboard.Widget widget = dashboard.addWidget("fake-widget", 1);
-    widget.setProperty("foo", "bar");
-
-    assertThat(widget.getProperties().get("foo"), is("bar"));
-  }
-
-}
index 66d356278afb66f27535be1d43c30378537f6fcc..7add3b89c7da44fdd11eb70d73fd35bbb4b38315 100644 (file)
@@ -68,8 +68,8 @@ public final class RegisterNewDashboards {
     DashboardDto defaultDashboard = null;
     for (DashboardTemplate dashboardTemplate : dashboardTemplates) {
       Dashboard dashboard = dashboardTemplate.createDashboard();
-      if (shouldBeRegistered(dashboard)) {
-        DashboardDto dashboardDto = loadDashboard(dashboard);
+      if (shouldRegister(dashboard)) {
+        DashboardDto dashboardDto = registerDashboard(dashboard);
         if (DEFAULT_DASHBOARD_ID.equals(dashboard.getId())) {
           defaultDashboard = dashboardDto;
         } else {
@@ -105,7 +105,7 @@ public final class RegisterNewDashboards {
     LOG.info("New dashboard '" + dashboardDto.getName() + "' registered");
   }
 
-  protected DashboardDto loadDashboard(Dashboard dashboard) {
+  protected DashboardDto registerDashboard(Dashboard dashboard) {
     DashboardDto dto = createDtoFromExtension(dashboard);
     // save the new dashboard
     dashboardDao.insert(dto);
@@ -150,7 +150,7 @@ public final class RegisterNewDashboards {
     return dashboardDto;
   }
 
-  protected boolean shouldBeRegistered(Dashboard dashboard) {
+  protected boolean shouldRegister(Dashboard dashboard) {
     return loadedTemplateDao.countByTypeAndKey(LoadedTemplateDto.DASHBOARD_TYPE, dashboard.getId()) == 0;
   }
 
index 9e2b466ae2e369ee42f4a59e39f833d6c605d8e2..d41719808d323eef3063361e39a31f11e7951221 100644 (file)
@@ -72,17 +72,17 @@ public class RegisterNewDashboardsTest {
   @Test
   public void testShouldNotBeRegistered() throws Exception {
     when(loadedTemplateDao.countByTypeAndKey(LoadedTemplateDto.DASHBOARD_TYPE, "fake-dashboard")).thenReturn(1);
-    assertThat(registerNewDashboards.shouldBeRegistered(fakeDashboardTemplate.createDashboard()), is(false));
+    assertThat(registerNewDashboards.shouldRegister(fakeDashboardTemplate.createDashboard()), is(false));
   }
 
   @Test
   public void testShouldBeLoaded() throws Exception {
-    assertThat(registerNewDashboards.shouldBeRegistered(fakeDashboardTemplate.createDashboard()), is(true));
+    assertThat(registerNewDashboards.shouldRegister(fakeDashboardTemplate.createDashboard()), is(true));
   }
 
   @Test
   public void shouldLoadDasboard() throws Exception {
-    DashboardDto dashboardDto = registerNewDashboards.loadDashboard(fakeDashboardTemplate.createDashboard());
+    DashboardDto dashboardDto = registerNewDashboards.registerDashboard(fakeDashboardTemplate.createDashboard());
     assertNotNull(dashboardDto);
     verify(dashboardDao).insert(dashboardDto);
     verify(loadedTemplateDao).insert(eq(new LoadedTemplateDto("fake-dashboard", LoadedTemplateDto.DASHBOARD_TYPE)));