From c4d46ddae98dfcd7dbf34cdc1acd2416c0baef4d Mon Sep 17 00:00:00 2001 From: Simon Brandhof Date: Fri, 16 Dec 2011 08:32:48 +0100 Subject: [PATCH] SONAR-1929 minor refactoring + add some tests --- .../sonar/api/web/dashboard/Dashboard.java | 14 ++++-- .../api/web/dashboard/DashboardTest.java | 17 ++++++- .../sonar/api/web/dashboard/WidgetTest.java | 48 ------------------- .../server/startup/RegisterNewDashboards.java | 8 ++-- .../startup/RegisterNewDashboardsTest.java | 6 +-- 5 files changed, 32 insertions(+), 61 deletions(-) delete mode 100644 sonar-plugin-api/src/test/java/org/sonar/api/web/dashboard/WidgetTest.java diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/Dashboard.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/Dashboard.java index 9f56b90eab6..e1fa4b1df3b 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/Dashboard.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/Dashboard.java @@ -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. * diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/web/dashboard/DashboardTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/web/dashboard/DashboardTest.java index b35a740c2ea..08c1b535cae 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/web/dashboard/DashboardTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/web/dashboard/DashboardTest.java @@ -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 index 95af6699d0f..00000000000 --- a/sonar-plugin-api/src/test/java/org/sonar/api/web/dashboard/WidgetTest.java +++ /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")); - } - -} diff --git a/sonar-server/src/main/java/org/sonar/server/startup/RegisterNewDashboards.java b/sonar-server/src/main/java/org/sonar/server/startup/RegisterNewDashboards.java index 66d356278af..7add3b89c7d 100644 --- a/sonar-server/src/main/java/org/sonar/server/startup/RegisterNewDashboards.java +++ b/sonar-server/src/main/java/org/sonar/server/startup/RegisterNewDashboards.java @@ -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; } diff --git a/sonar-server/src/test/java/org/sonar/server/startup/RegisterNewDashboardsTest.java b/sonar-server/src/test/java/org/sonar/server/startup/RegisterNewDashboardsTest.java index 9e2b466ae2e..d41719808d3 100644 --- a/sonar-server/src/test/java/org/sonar/server/startup/RegisterNewDashboardsTest.java +++ b/sonar-server/src/test/java/org/sonar/server/startup/RegisterNewDashboardsTest.java @@ -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))); -- 2.39.5