From 632c88629c99be66b63e04d8c5498c3d2ef23cf6 Mon Sep 17 00:00:00 2001 From: simonbrandhof Date: Mon, 1 Nov 2010 10:23:27 +0000 Subject: [PATCH] SONAR-1643 widgets require to be configured only if it has mandatory properties without default value --- .../java/org/sonar/server/ui/ViewProxy.java | 11 ++++ .../app/controllers/dashboard_controller.rb | 2 +- .../org/sonar/server/ui/ViewProxyTest.java | 54 +++++++++++++++++-- 3 files changed, 62 insertions(+), 5 deletions(-) diff --git a/sonar-server/src/main/java/org/sonar/server/ui/ViewProxy.java b/sonar-server/src/main/java/org/sonar/server/ui/ViewProxy.java index 8a7e569dc10..0f15f3ad8ae 100644 --- a/sonar-server/src/main/java/org/sonar/server/ui/ViewProxy.java +++ b/sonar-server/src/main/java/org/sonar/server/ui/ViewProxy.java @@ -20,6 +20,7 @@ package org.sonar.server.ui; import org.apache.commons.lang.ArrayUtils; +import org.apache.commons.lang.StringUtils; import org.apache.commons.lang.builder.CompareToBuilder; import org.apache.commons.lang.builder.EqualsBuilder; import org.apache.commons.lang.builder.ToStringBuilder; @@ -153,6 +154,16 @@ public class ViewProxy implements Comparable { return !ArrayUtils.isEmpty(properties); } + public boolean hasRequiredProperties() { + boolean requires = false; + for (WidgetProperty property : properties) { + if (!property.optional() && StringUtils.isEmpty(property.defaultValue())) { + requires = true; + } + } + return requires; + } + @Override public int hashCode() { return getId().hashCode(); diff --git a/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb b/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb index 3811e1c6b83..5fc5729aba6 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/controllers/dashboard_controller.rb @@ -93,7 +93,7 @@ class DashboardController < ApplicationController :name => definition.getTitle(), :column_index => dashboard.number_of_columns, :row_index => dashboard.column_size(dashboard.number_of_columns) + 1, - :configured => !definition.isEditable()) + :configured => !definition.hasRequiredProperties()) widget_id=new_widget.id end end diff --git a/sonar-server/src/test/java/org/sonar/server/ui/ViewProxyTest.java b/sonar-server/src/test/java/org/sonar/server/ui/ViewProxyTest.java index 410b2897f18..52eb5197aa7 100644 --- a/sonar-server/src/test/java/org/sonar/server/ui/ViewProxyTest.java +++ b/sonar-server/src/test/java/org/sonar/server/ui/ViewProxyTest.java @@ -20,10 +20,7 @@ package org.sonar.server.ui; import org.junit.Test; -import org.sonar.api.web.DefaultTab; -import org.sonar.api.web.NavigationSection; -import org.sonar.api.web.UserRole; -import org.sonar.api.web.View; +import org.sonar.api.web.*; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.Matchers.lessThan; @@ -117,6 +114,25 @@ public class ViewProxyTest { assertThat(proxy.isDefaultTab(), is(false)); assertThat(proxy.getDefaultTabForMetrics(), is(new String[]{"ncloc", "coverage"})); } + + @Test + public void widgetShouldBeEditable() { + ViewProxy proxy = new ViewProxy(new EditableWidget()); + assertThat(proxy.isEditable(), is(true)); + assertThat(proxy.getProperties().length, is(2)); + } + + @Test + public void widgetShouldRequireMandatoryProperties() { + ViewProxy proxy = new ViewProxy(new EditableWidget()); + assertThat(proxy.hasRequiredProperties(), is(true)); + } + + @Test + public void widgetShouldDefineOnlyOptionalProperties() { + ViewProxy proxy = new ViewProxy(new WidgetWithOptionalProperties()); + assertThat(proxy.hasRequiredProperties(), is(false)); + } } class FakeView implements View { @@ -135,3 +151,33 @@ class FakeView implements View { return id; } } + +@WidgetProperties({ + @WidgetProperty(key="foo", optional = false), + @WidgetProperty(key="bar", defaultValue = "30", type = "INTEGER") +}) +class EditableWidget implements Widget { + + public String getId() { + return "w1"; + } + + public String getTitle() { + return "W1"; + } +} + +@WidgetProperties({ + @WidgetProperty(key="foo"), + @WidgetProperty(key="bar") +}) +class WidgetWithOptionalProperties implements Widget { + + public String getId() { + return "w2"; + } + + public String getTitle() { + return "W2"; + } +} -- 2.39.5