From dffab878ddf772d36f56b94ee1a739bc3fc1a984 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Mon, 24 Sep 2012 18:45:21 +0200 Subject: [PATCH] SONAR-3529 API: ability to define property sets. --- .../org/sonar/plugins/core/CorePlugin.java | 1 + .../src/main/java/org/sonar/api/Property.java | 7 ++++++ .../sonar/api/config/PropertyDefinition.java | 9 +++++++ .../api/config/PropertyDefinitionTest.java | 18 +++++++++++++- .../WEB-INF/app/helpers/settings_helper.rb | 8 +++++++ .../webapp/WEB-INF/app/models/property_set.rb | 24 +++++++++++++++++++ .../app/views/settings/_properties.html.erb | 6 ++--- .../app/views/settings/_type_BOOLEAN.html.erb | 2 +- .../app/views/settings/_type_FLOAT.html.erb | 2 +- .../app/views/settings/_type_INTEGER.html.erb | 2 +- .../app/views/settings/_type_LICENSE.html.erb | 2 +- .../app/views/settings/_type_METRIC.html.erb | 2 +- .../views/settings/_type_PASSWORD.html.erb | 2 +- .../settings/_type_PROPERTY_SET.html.erb | 8 ++++++- .../_type_REGULAR_EXPRESSION.html.erb | 2 +- .../_type_SINGLE_SELECT_LIST.html.erb | 2 +- .../app/views/settings/_type_STRING.html.erb | 2 +- .../app/views/settings/_type_TEXT.html.erb | 2 +- 18 files changed, 86 insertions(+), 15 deletions(-) create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/models/property_set.rb diff --git a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java index 710fdd66929..f5338025e33 100644 --- a/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java +++ b/plugins/sonar-core-plugin/src/main/java/org/sonar/plugins/core/CorePlugin.java @@ -63,6 +63,7 @@ import java.util.List; name = "Toto", global = true, type = PropertyType.PROPERTY_SET, + property_set_name = "myset", category = CoreProperties.CATEGORY_GENERAL), @Property( key = CoreProperties.PROJECT_LANGUAGE_PROPERTY, diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/Property.java b/sonar-plugin-api/src/main/java/org/sonar/api/Property.java index d76dc019a88..64037142ac2 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/Property.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/Property.java @@ -98,4 +98,11 @@ public @interface Property { * @since 3.3 */ boolean multiValues() default false; + + /** + * Name of the property set. Used only when type = PropertyType.PROPERTY_SET. + * + * @since 3.3 + */ + String property_set_name() default ""; } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java index 69a8f16c879..5464b2f3cdd 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/config/PropertyDefinition.java @@ -67,6 +67,7 @@ public final class PropertyDefinition { private boolean onModule = false; private boolean isGlobal = true; private boolean multiValues; + private String property_set_name; private PropertyDefinition(Property annotation) { this.key = annotation.key(); @@ -80,6 +81,7 @@ public final class PropertyDefinition { this.type = fixType(annotation.key(), annotation.type()); this.options = annotation.options(); this.multiValues = annotation.multiValues(); + this.property_set_name = annotation.property_set_name(); } private static PropertyType fixType(String key, PropertyType type) { @@ -180,4 +182,11 @@ public final class PropertyDefinition { public boolean isMultiValues() { return multiValues; } + + /** + * @since 3.3 + */ + public String getProperty_set_name() { + return property_set_name; + } } diff --git a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java index d65d3af3c86..061bb99a575 100644 --- a/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java +++ b/sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java @@ -47,6 +47,7 @@ public class PropertyDefinitionTest { assertThat(def.isOnProject()).isTrue(); assertThat(def.isOnModule()).isTrue(); assertThat(def.isMultiValues()).isTrue(); + assertThat(def.getProperty_set_name()).isEmpty(); } @Properties(@Property(key = "hello", name = "Hello", defaultValue = "world", description = "desc", @@ -74,6 +75,21 @@ public class PropertyDefinitionTest { assertThat(def.isMultiValues()).isFalse(); } + @Properties(@Property(key = "hello", name = "Hello", type = PropertyType.PROPERTY_SET, property_set_name = "set1")) + static class WithPropertySet { + } + + @Test + public void should_support_property_sets() { + Properties props = AnnotationUtils.getAnnotation(WithPropertySet.class, Properties.class); + Property prop = props.value()[0]; + + PropertyDefinition def = PropertyDefinition.create(prop); + + assertThat(def.getType()).isEqualTo(PropertyType.PROPERTY_SET); + assertThat(def.getProperty_set_name()).isEqualTo("set1"); + } + @Properties(@Property(key = "hello", name = "Hello")) static class DefaultValues { } @@ -131,7 +147,7 @@ public class PropertyDefinitionTest { @Test public void validate_single_select_list() { - PropertyDefinition def = PropertyDefinition.create("foo", PropertyType.SINGLE_SELECT_LIST, new String[] {"de", "en"}); + PropertyDefinition def = PropertyDefinition.create("foo", PropertyType.SINGLE_SELECT_LIST, new String[]{"de", "en"}); assertThat(def.validate(null).isValid()).isTrue(); assertThat(def.validate("").isValid()).isTrue(); diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/settings_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/settings_helper.rb index fe7ab2ea4ee..d64fd1d3744 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/settings_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/settings_helper.rb @@ -53,4 +53,12 @@ module SettingsHelper def by_name(categories) categories.sort_by { |category| category_name(category) } end + + def input_name(property) + h(property.key) + (property.multi_values ? '[]' : '') + end + + def property_set_values(property) + PropertySet.findAll(property.property_set_name); + end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/property_set.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/property_set.rb new file mode 100644 index 00000000000..61007210470 --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/property_set.rb @@ -0,0 +1,24 @@ +# +# Sonar, entreprise quality control 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 {library}; if not, write to the Free Software +# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 +# +class PropertySet + def self.findAll(property_set_name) + [property_set_name + '1', property_set_name + '2'] + end +end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_properties.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_properties.html.erb index cb3ee1a3b31..ed62ce8d565 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_properties.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_properties.html.erb @@ -29,15 +29,15 @@ <% value = property_value(property) -%> <% if property.multi_values -%> <% value.each do |sub_value| -%> - <%= render "settings/multi_value", :property => property, :value => sub_value -%> + <%= render "settings/multi_value", :property => property, :value => sub_value, :multi => true -%> <% end -%>
<% else -%> - <%= render "settings/type_#{property_type(property, value)}", :property => property, :value => value -%> + <%= render "settings/type_#{property_type(property, value)}", :property => property, :value => value, :multi => false -%> <% end -%> <% p = @updated_properties[property.key] if @updated_properties -%> diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_BOOLEAN.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_BOOLEAN.html.erb index 9271bbd0c92..b1643852f5c 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_BOOLEAN.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_BOOLEAN.html.erb @@ -1,4 +1,4 @@ - diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_FLOAT.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_FLOAT.html.erb index 163677b4478..9694f05758e 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_FLOAT.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_FLOAT.html.erb @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_INTEGER.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_INTEGER.html.erb index 5603dfe4a75..767aa39ad63 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_INTEGER.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_INTEGER.html.erb @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_LICENSE.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_LICENSE.html.erb index 69996fa4419..566f2c37959 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_LICENSE.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_LICENSE.html.erb @@ -6,7 +6,7 @@ date = license.getExpirationDateAsString() %>
- +
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_METRIC.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_METRIC.html.erb index 45ed790ecf6..ac5d599482c 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_METRIC.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_METRIC.html.erb @@ -1,4 +1,4 @@ - <% metrics_per_domain={} diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_PASSWORD.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_PASSWORD.html.erb index e8b70971e02..5b3a604ca8d 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_PASSWORD.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_PASSWORD.html.erb @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_PROPERTY_SET.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_PROPERTY_SET.html.erb index a27d0b0dc2c..ad61e35f16b 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_PROPERTY_SET.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_PROPERTY_SET.html.erb @@ -1 +1,7 @@ -NOT IMPLEMENTED YET \ No newline at end of file + diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_REGULAR_EXPRESSION.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_REGULAR_EXPRESSION.html.erb index b4c3b451f4d..b5e2126fc19 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_REGULAR_EXPRESSION.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_REGULAR_EXPRESSION.html.erb @@ -1,2 +1,2 @@ - + <%= link_to_function(image_tag('zoom.png'), "enlargeTextInput('#{property.getKey()}')", :class => 'nolink') -%> \ No newline at end of file diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_SINGLE_SELECT_LIST.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_SINGLE_SELECT_LIST.html.erb index ac0e0d3d867..1e6839f1524 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_SINGLE_SELECT_LIST.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_SINGLE_SELECT_LIST.html.erb @@ -1,4 +1,4 @@ - <% property.options.each do |option| %> diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_STRING.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_STRING.html.erb index 07d91a2313e..ee1ec69bd15 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_STRING.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_STRING.html.erb @@ -1,4 +1,4 @@ - + <% unless property.multi_values -%> <%= link_to_function(image_tag('zoom.png'), "enlargeTextInput('input_#{property.key.parameterize}')", :class => 'nolink') -%> <% end -%> diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_TEXT.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_TEXT.html.erb index dfefb03af9f..ce59e4a8a07 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_TEXT.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_TEXT.html.erb @@ -1 +1 @@ - \ No newline at end of file + \ No newline at end of file -- 2.39.5