summaryrefslogtreecommitdiffstats
path: root/sonar-plugin-api
diff options
context:
space:
mode:
authorSimon Brandhof <simon.brandhof@gmail.com>2011-12-15 18:44:21 +0100
committerSimon Brandhof <simon.brandhof@gmail.com>2011-12-15 18:44:21 +0100
commit16232e514c5b0d064a114906fb31441b97a480ae (patch)
treeffdea318d342f10e6396ae5120ea073d0acc1738 /sonar-plugin-api
parentb84f33030464fcc3e6e70affb1680381516a19e6 (diff)
downloadsonarqube-16232e514c5b0d064a114906fb31441b97a480ae.tar.gz
sonarqube-16232e514c5b0d064a114906fb31441b97a480ae.zip
SONAR-1929 refactor the extension point to define dashboards
Diffstat (limited to 'sonar-plugin-api')
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/Dashboard.java90
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/DashboardLayout.java28
-rw-r--r--sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/Widget.java94
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/web/dashboard/DashboardTest.java68
-rw-r--r--sonar-plugin-api/src/test/java/org/sonar/api/web/dashboard/WidgetTest.java24
5 files changed, 153 insertions, 151 deletions
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 5e91fa7f8b0..9f56b90eab6 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
@@ -19,9 +19,14 @@
*/
package org.sonar.api.web.dashboard;
-import com.google.common.collect.Lists;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.ListMultimap;
+import com.google.common.collect.Maps;
+import org.apache.commons.lang.StringUtils;
import java.util.Collection;
+import java.util.List;
+import java.util.Map;
/**
* Definition of a dashboard.
@@ -37,7 +42,7 @@ public final class Dashboard {
private String name;
private String description;
private DashboardLayout layout = DashboardLayout.TWO_COLUMNS;
- private Collection<Widget> widgets = Lists.newArrayList();
+ private ListMultimap<Integer, Widget> widgetsByColumn = ArrayListMultimap.create();
private Dashboard() {
}
@@ -52,21 +57,36 @@ 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.
+ * The id is deduced from the name.
*/
- public Widget addWidget(String id, int columnId, int rowId) {
- Widget widget = new Widget(id, columnId, rowId);
- widgets.add(widget);
- return widget;
+ public static Dashboard createByName(String name) {
+ String id = StringUtils.trimToEmpty(name);
+ id = StringUtils.lowerCase(id);
+ id = StringUtils.replaceChars(id, ' ', '_');
+ return new Dashboard()
+ .setId(id)
+ .setName(name);
}
/**
- * Returns the list of widgets.
- *
- * @return the widgets of this dashboard
+ * Add a widget with the given parameters, and return the newly created {@link Widget} object if one wants to add parameters to it.
*/
+ public Widget addWidget(String widgetId, int columnId) {
+ if (columnId < 1) {
+ throw new IllegalArgumentException("Widget column starts with 1");
+ }
+
+ Widget widget = new Widget(widgetId);
+ widgetsByColumn.put(columnId, widget);
+ return widget;
+ }
+
public Collection<Widget> getWidgets() {
- return widgets;
+ return widgetsByColumn.values();
+ }
+
+ public List<Widget> getWidgetsOfColumn(int columnId) {
+ return widgetsByColumn.get(columnId);
}
/**
@@ -82,6 +102,9 @@ public final class Dashboard {
* @param id the id to set
*/
private Dashboard setId(String id) {
+ if (StringUtils.isBlank(id)) {
+ throw new IllegalArgumentException("Dashboard id can not be blank");
+ }
this.id = id;
return this;
}
@@ -133,12 +156,49 @@ public final class Dashboard {
return layout;
}
+ public Dashboard setLayout(DashboardLayout dl) {
+ if (dl == null) {
+ throw new IllegalArgumentException("The layout of the dashboard '" + getId() + "' can not be null");
+ }
+ this.layout = dl;
+ return this;
+ }
+
+
/**
- * @param layout the layout to set
+ * Note that this class is an inner class to avoid confusion with the extension point org.sonar.api.web.Widget.
*/
- public Dashboard setLayout(DashboardLayout layout) {
- this.layout = layout;
- return this;
+ public static final class Widget {
+ private String id;
+ private Map<String, String> properties;
+
+ Widget(String id) {
+ this.id = id;
+ this.properties = Maps.newHashMap();
+ }
+
+ public Widget setProperty(String key, String value) {
+ properties.put(key, value);
+ return this;
+ }
+
+ /**
+ * Returns the properties of this widget.
+ *
+ * @return the properties
+ */
+ public Map<String, String> getProperties() {
+ return properties;
+ }
+
+ /**
+ * Returns the identifier of this widget.
+ *
+ * @return the id
+ */
+ public String getId() {
+ return id;
+ }
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/DashboardLayout.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/DashboardLayout.java
index 02d83565008..3b698476dc7 100644
--- a/sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/DashboardLayout.java
+++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/DashboardLayout.java
@@ -29,37 +29,47 @@ public enum DashboardLayout {
/**
* Only 1 column that take all the page
*/
- ONE_COLUMN("100%"),
+ ONE_COLUMN("100%", 1),
/**
* 2 columns of the same width
*/
- TWO_COLUMNS("50%-50%"),
+ TWO_COLUMNS("50%-50%", 2),
/**
* 2 columns with the first one smaller than the second
*/
- TWO_COLUMNS_30_70("30%-70%"),
+ TWO_COLUMNS_30_70("30%-70%", 2),
/**
* 2 columns with the first one bigger than the second
*/
- TWO_COLUMNS_70_30("70%-30%"),
+ TWO_COLUMNS_70_30("70%-30%", 2),
/**
* 3 columns of the same width
*/
- TREE_COLUMNS("33%-33%-33%");
+ TREE_COLUMNS("33%-33%-33%", 3);
- private String layout;
+ private String code;
+ private int columns;
- private DashboardLayout(String layout) {
- this.layout = layout;
+ private DashboardLayout(String code, int columns) {
+ this.code = code;
+ this.columns = columns;
+ }
+
+ public String getCode() {
+ return code;
+ }
+
+ public int getColumns() {
+ return columns;
}
@Override
public String toString() {
- return layout;
+ return code;
}
}
diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/Widget.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/Widget.java
deleted file mode 100644
index 8929f4f60f9..00000000000
--- a/sonar-plugin-api/src/main/java/org/sonar/api/web/dashboard/Widget.java
+++ /dev/null
@@ -1,94 +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 java.util.Map;
-
-import com.google.common.collect.Maps;
-
-/**
- *
- * Definition of a widget inside a dashboard.
- *
- * @since 2.13
- */
-public final class Widget {
-
- private String id;
- private int columnIndex;
- private int rowIndex;
- private Map<String, String> properties;
-
- Widget(String id, int columnIndex, int rowIndex) {
- this.id = id;
- this.columnIndex = columnIndex;
- this.rowIndex = rowIndex;
- this.properties = Maps.newHashMap();
- }
-
- /**
- * Adds a property to this widget.
- *
- * @param key
- * the id of the property
- * @param value
- * the value of the property
- */
- public void addProperty(String key, String value) {
- properties.put(key, value);
- }
-
- /**
- * Returns the properties of this widget.
- *
- * @return the properties
- */
- public Map<String, String> getProperties() {
- return properties;
- }
-
- /**
- * Returns the identifier of this widget.
- *
- * @return the id
- */
- public String getId() {
- return id;
- }
-
- /**
- * Returns the column index of this widget.
- *
- * @return the columnIndex
- */
- public int getColumnIndex() {
- return columnIndex;
- }
-
- /**
- * Returns the row index of this widget.
- *
- * @return the rowIndex
- */
- public int getRowIndex() {
- return rowIndex;
- }
-
-}
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 3555d786c4e..b35a740c2ea 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
@@ -19,11 +19,9 @@
*/
package org.sonar.api.web.dashboard;
+import org.hamcrest.core.Is;
import org.junit.Test;
-import java.util.Map.Entry;
-import java.util.Set;
-
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertThat;
@@ -31,36 +29,64 @@ import static org.junit.Assert.assertThat;
public class DashboardTest {
@Test
- public void shouldCreateDashboardAndWidget() throws Exception {
+ public void shouldCreateDashboard() {
Dashboard dashboard = Dashboard.create("fake-dashboard", "Fake");
assertThat(dashboard.getId(), is("fake-dashboard"));
assertThat(dashboard.getName(), is("Fake"));
assertThat(dashboard.getLayout(), is(DashboardLayout.TWO_COLUMNS));
assertThat(dashboard.getDescription(), nullValue());
+ }
+
+ @Test
+ public void shouldAddWidgets() {
+ Dashboard dashboard = Dashboard.createByName("Fake");
+ Dashboard.Widget mostViolatedRules = dashboard.addWidget("most_violated_rules", 1);
+ assertThat(mostViolatedRules.getId(), is("most_violated_rules"));
+ assertThat(dashboard.getWidgets().size(), is(1));
+ assertThat(dashboard.getWidgetsOfColumn(1).size(), is(1));
- Widget widget = dashboard.addWidget("fake-widget", 12, 13);
- assertThat(widget.getId(), is("fake-widget"));
- assertThat(widget.getColumnIndex(), is(12));
- assertThat(widget.getRowIndex(), is(13));
+ dashboard.addWidget("hotspots", 1);
+ assertThat(dashboard.getWidgets().size(), is(2));
+ assertThat(dashboard.getWidgetsOfColumn(1).size(), is(2));
- widget.addProperty("fake-property", "fake_metric");
- Set<Entry<String, String>> properties = widget.getProperties().entrySet();
- assertThat(properties.size(), is(1));
- Entry<String, String> property = properties.iterator().next();
- assertThat(property.getKey(), is("fake-property"));
- assertThat(property.getValue(), is("fake_metric"));
+ // widgets are sorted by order of insertion
+ assertThat(dashboard.getWidgetsOfColumn(1).get(1).getId(), is("hotspots"));
}
@Test
- public void shouldAddWidget() throws Exception {
- Dashboard dashboard = Dashboard.create("fake-dashboard", "Fake");
- dashboard.addWidget("fake-widget", 12, 13);
+ public void shouldAddWidgetsOnDifferentColumns() {
+ Dashboard dashboard = Dashboard.createByName("Fake");
+
+ dashboard.addWidget("most_violated_rules", 1);
+ assertThat(dashboard.getWidgets().size(), is(1));
+ assertThat(dashboard.getWidgetsOfColumn(1).size(), is(1));
- Widget widget = dashboard.getWidgets().iterator().next();
+ dashboard.addWidget("hotspots", 2);
+ assertThat(dashboard.getWidgets().size(), is(2));
+ assertThat(dashboard.getWidgetsOfColumn(2).size(), is(1));
+ }
- assertThat(widget.getId(), is("fake-widget"));
- assertThat(widget.getColumnIndex(), is(12));
- assertThat(widget.getRowIndex(), is(13));
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldFailIfBlankId() {
+ Dashboard.create(" ", "Fake");
}
+ @Test(expected = IllegalArgumentException.class)
+ public void shouldFailToDeduceIdFromName() {
+ Dashboard.createByName(" ");
+ }
+
+ @Test
+ public void shouldCreateByName() {
+ Dashboard dashboard = Dashboard.createByName("Fake");
+ assertThat(dashboard.getId(), Is.is("fake"));
+
+ dashboard = Dashboard.createByName(" Fake You ");
+ assertThat(dashboard.getId(), Is.is("fake_you"));
+ }
+
+ @Test
+ public void shouldAddSeveralTimesTheSameWidget() {
+ // TODO
+ }
}
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
index a463fc81bf9..95af6699d0f 100644
--- 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
@@ -30,19 +30,19 @@ import static org.junit.Assert.assertThat;
public class WidgetTest {
@Test
- public void shouldCreateWidgetWithProperties() throws Exception {
- Dashboard dashboard = Dashboard.create("fake-dashboard", "Fake");
- Widget widget = dashboard.addWidget("fake-widget", 12, 13);
+ public void shouldCreateWidget() {
+ Dashboard dashboard = Dashboard.createByName("Fake");
+ Dashboard.Widget widget = dashboard.addWidget("fake-widget", 1);
assertThat(widget.getId(), is("fake-widget"));
- assertThat(widget.getColumnIndex(), is(12));
- assertThat(widget.getRowIndex(), is(13));
-
- widget.addProperty("fake-property", "fake_metric");
- Set<Entry<String, String>> properties = widget.getProperties().entrySet();
- assertThat(properties.size(), is(1));
- Entry<String, String> property = properties.iterator().next();
- assertThat(property.getKey(), is("fake-property"));
- assertThat(property.getValue(), is("fake_metric"));
+ }
+
+ @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"));
}
}