From 794e495aaa2cb8c848a3fd0cb48c502abdb72963 Mon Sep 17 00:00:00 2001 From: David Gageot Date: Fri, 6 Jul 2012 14:24:23 +0200 Subject: [PATCH] SONAR-3432 Support Regular expressions --- .../org/sonar/plugins/checkstyle/rules.xml | 50 +++++++++---------- .../resources/org/sonar/plugins/pmd/rules.xml | 2 +- ...uleParametersFromARuleParameter-result.xml | 4 +- ...ActiveRuleParametersFromARuleParameter.xml | 4 +- .../dao/RulesDaoTest/shouldGetRuleParams.xml | 6 +-- .../jpa/dao/RulesDaoTest/shouldGetRules.xml | 4 +- .../main/java/org/sonar/api/PropertyType.java | 9 +++- .../org/sonar/api/web/WidgetPropertyType.java | 9 +++- .../WEB-INF/app/helpers/properties_helper.rb | 25 ++++++---- .../app/helpers/rules_configuration_helper.rb | 35 ++++++------- .../webapp/WEB-INF/app/models/api/utils.rb | 2 +- .../WEB-INF/app/models/property_type.rb | 22 +++++--- .../_type_REGULAR_EXPRESSION.html.erb | 2 + .../shouldActivateInChildren-result.xml | 2 +- .../shouldActivateInChildren.xml | 2 +- .../RuleChangeTest/changeParentProfile.xml | 2 +- .../RuleChangeTest/initialData.xml | 2 +- .../RuleChangeTest/ruleReverted.xml | 4 +- 18 files changed, 104 insertions(+), 82 deletions(-) create mode 100644 sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_REGULAR_EXPRESSION.html.erb diff --git a/plugins/sonar-checkstyle-plugin/src/main/resources/org/sonar/plugins/checkstyle/rules.xml b/plugins/sonar-checkstyle-plugin/src/main/resources/org/sonar/plugins/checkstyle/rules.xml index 9b250bb4d51..e8de5fb04c7 100644 --- a/plugins/sonar-checkstyle-plugin/src/main/resources/org/sonar/plugins/checkstyle/rules.xml +++ b/plugins/sonar-checkstyle-plugin/src/main/resources/org/sonar/plugins/checkstyle/rules.xml @@ -273,7 +273,7 @@ - + ^Abstract.*$|^.*Factory$ @@ -380,7 +380,7 @@ MULTIPLE - + ^[A-Z][A-Z0-9]*(_[A-Z0-9]+)*$ @@ -524,7 +524,7 @@ - + @@ -576,7 +576,7 @@ - + @@ -658,7 +658,7 @@ - + @@ -686,7 +686,7 @@ - + @@ -847,10 +847,10 @@ - + - + @@ -900,7 +900,7 @@ - + @@ -917,7 +917,7 @@ - + ^[a-z][a-zA-Z0-9]*$ @@ -927,7 +927,7 @@ - + ^[a-z][a-zA-Z0-9]*$ @@ -956,7 +956,7 @@ - + ^[a-z][a-zA-Z0-9]*$ @@ -997,7 +997,7 @@ - + ^[a-z][a-zA-Z0-9]*$ @@ -1059,7 +1059,7 @@ - + @@ -1079,7 +1079,7 @@ - + @@ -1195,7 +1195,7 @@ - + ^[a-z]+(\.[a-zA-Z_][a-zA-Z0-9_]*)*$ @@ -1211,7 +1211,7 @@ - + ^[a-z][a-zA-Z0-9]*$ @@ -1269,7 +1269,7 @@ MULTIPLE - + @@ -1314,7 +1314,7 @@ - + @@ -1353,7 +1353,7 @@ MULTIPLE - + ^[a-z][a-zA-Z0-9]*$ @@ -1424,7 +1424,7 @@ MULTIPLE - + @@ -1464,7 +1464,7 @@ MULTIPLE - + ^[A-Z][a-zA-Z0-9]*$ @@ -1508,7 +1508,7 @@ - + @@ -1548,7 +1548,7 @@ - + @@ -1590,7 +1590,7 @@ - + diff --git a/plugins/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules.xml b/plugins/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules.xml index 402f37088c9..36d5aee2b8f 100644 --- a/plugins/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules.xml +++ b/plugins/sonar-pmd-plugin/src/main/resources/org/sonar/plugins/pmd/rules.xml @@ -1661,7 +1661,7 @@ - + diff --git a/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldDeleteActiveRuleParametersFromARuleParameter-result.xml b/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldDeleteActiveRuleParametersFromARuleParameter-result.xml index f5004757f82..0d8e296a25a 100644 --- a/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldDeleteActiveRuleParametersFromARuleParameter-result.xml +++ b/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldDeleteActiveRuleParametersFromARuleParameter-result.xml @@ -3,8 +3,8 @@ - - + + diff --git a/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldDeleteActiveRuleParametersFromARuleParameter.xml b/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldDeleteActiveRuleParametersFromARuleParameter.xml index ad93402fa5b..0a6b2ee0e2c 100644 --- a/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldDeleteActiveRuleParametersFromARuleParameter.xml +++ b/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldDeleteActiveRuleParametersFromARuleParameter.xml @@ -3,8 +3,8 @@ - - + + diff --git a/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldGetRuleParams.xml b/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldGetRuleParams.xml index 8bb3979e8ad..fa092663e15 100644 --- a/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldGetRuleParams.xml +++ b/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldGetRuleParams.xml @@ -7,11 +7,11 @@ plugin_rule_key="checkstyle.new2" plugin_name="PLUGIN_KEY" enabled="true" cardinality="SINGLE" parent_id="[null]"/> - - - \ No newline at end of file diff --git a/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldGetRules.xml b/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldGetRules.xml index 35d4172d221..04a15024e0a 100644 --- a/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldGetRules.xml +++ b/sonar-core/src/test/resources/org/sonar/jpa/dao/RulesDaoTest/shouldGetRules.xml @@ -5,9 +5,9 @@ - - \ No newline at end of file diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java b/sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java index 6e0a40ea088..39d0bb63b54 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/PropertyType.java @@ -66,5 +66,12 @@ public enum PropertyType { /** * SonarSource license */ - LICENSE + LICENSE, + + /** + * Regular expression + * + * @since 3.2 + */ + REGULAR_EXPRESSION } diff --git a/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetPropertyType.java b/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetPropertyType.java index d34ed2dadb2..edff6ee319f 100644 --- a/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetPropertyType.java +++ b/sonar-plugin-api/src/main/java/org/sonar/api/web/WidgetPropertyType.java @@ -66,5 +66,12 @@ public enum WidgetPropertyType { * * @since 3.2 */ - PASSWORD + PASSWORD, + + /** + * Regular expression + * + * @since 3.2 + */ + REGULAR_EXPRESSION } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb index 999d06c0f7a..f9f215479c0 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/properties_helper.rb @@ -20,21 +20,30 @@ module PropertiesHelper def property_value(key, type, value, options = {}) - if type==PropertyType::TYPE_INTEGER - text_field_tag key, value, {:size => 10}.update(options) + if type==PropertyType::TYPE_STRING + text_field_tag key, value, {:size => 25}.update(options) + + elsif type==PropertyType::TYPE_TEXT + text_area_tag key, value, {:size => '40x6'}.update(options) + + elsif type==PropertyType::TYPE_PASSWORD + password_field_tag key, value, {:size => 25}.update(options) elsif type==PropertyType::TYPE_BOOLEAN check_box_tag key, "true", value=='true', options - elsif type==PropertyType::TYPE_FLOAT + elsif type==PropertyType::TYPE_INTEGER text_field_tag key, value, {:size => 10}.update(options) - elsif type==PropertyType::TYPE_STRING - text_field_tag key, value, {:size => 25}.update(options) + elsif type==PropertyType::TYPE_FLOAT + text_field_tag key, value, {:size => 10}.update(options) elsif type==PropertyType::TYPE_METRIC select_tag key, options_grouped_by_domain(Metric.all.select{|m| m.display?}.sort_by(&:short_name), value, :include_empty => true), options + elsif type==PropertyType::TYPE_REGULAR_EXPRESSION + text_field_tag key, value, {:size => 25}.update(options) + elsif type==PropertyType::TYPE_FILTER user_filters = options_key(value, ::Filter.find(:all, :conditions => ['user_id=?', current_user.id]).sort_by(&:id)) shared_filters = options_key(value, ::Filter.find(:all, :conditions => ['(user_id<>? or user_id is null) and shared=?', current_user.id, true]).sort_by(&:id)) @@ -44,12 +53,6 @@ module PropertiesHelper "#{filters_combo} #{filter_link}" - elsif type==PropertyType::TYPE_TEXT - text_area_tag key, value, {:size => '40x6'}.update(options) - - elsif type==PropertyType::TYPE_PASSWORD - password_field_tag key, value, {:size => 25}.update(options) - else hidden_field_tag key, options end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_configuration_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_configuration_helper.rb index 1a8fb4051d3..aa4beee3dbf 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_configuration_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/rules_configuration_helper.rb @@ -22,7 +22,6 @@ module RulesConfigurationHelper PARAM_TYPE_STRING_LIST = "s{}" PARAM_TYPE_INTEGER_LIST = "i{}" - PARAM_TYPE_REGEXP = "r" # Kept for compatibility with old rule param type def type_with_compatibility(type) @@ -31,23 +30,27 @@ module RulesConfigurationHelper return PropertyType::TYPE_INTEGER if type == 'i' return PropertyType::TYPE_INTEGER if type == PARAM_TYPE_INTEGER_LIST return PropertyType::TYPE_BOOLEAN if type == 'b' - return PropertyType::TYPE_STRING if type == PARAM_TYPE_REGEXP + return PropertyType::TYPE_REGULAR_EXPRESSION if type == 'r' return PropertyType::TYPE_STRING if is_set(type) type end - def readable_type(type) - return "Set of comma delimited strings" if type == PARAM_TYPE_STRING_LIST - return "Number" if type_with_compatibility(type) == PropertyType::TYPE_INTEGER - return "Set of comma delimited numbers" if type == PARAM_TYPE_INTEGER_LIST - return "Regular expression" if type == PARAM_TYPE_REGEXP - return "Set of comma delimited values" if is_set(type) + def readable_type(param_type) + type=type_with_compatibility(param_type) + + return "Set of comma delimited strings" if param_type == PARAM_TYPE_STRING_LIST + return "Number" if type == PropertyType::TYPE_INTEGER + return "Set of comma delimited numbers" if param_type == PARAM_TYPE_INTEGER_LIST + return "Regular expression" if type == PropertyType::TYPE_REGULAR_EXPRESSION + return "Set of comma delimited values" if is_set(param_type) "" end def param_value_input(parameter, value, options = {}) - property_value 'value', type_with_compatibility(parameter.param_type), value, {:id => parameter.id}.update(options) + type=type_with_compatibility(parameter.param_type) + + property_value 'value', type, value, {:id => parameter.id}.update(options) end def is_set(type) @@ -71,18 +74,12 @@ module RulesConfigurationHelper errors.add("#{value}", "'#{n}' must be an integer.") end end - elsif param_type == RulesConfigurationHelper::PARAM_TYPE_REGEXP - if !Api::Utils.is_regexp?(attribute) - errors.add("#{value}", "'#{attribute}' must be a regular expression") - end + elsif type == PropertyType::TYPE_REGULAR_EXPRESSION + errors.add("#{value}", "'#{attribute}' must be a regular expression") unless Api::Utils.is_regexp?(attribute) elsif type == PropertyType::TYPE_INTEGER - if !Api::Utils.is_integer?(attribute) - errors.add("#{value}", "'#{attribute}' must be an integer.") - end + errors.add("#{value}", "'#{attribute}' must be an integer.") unless Api::Utils.is_integer?(attribute) elsif type == PropertyType::TYPE_BOOLEAN - if !Api::Utils.is_boolean?(attribute) - errors.add("#{value}", "'#{attribute}' must be one of : true,false") - end + errors.add("#{value}", "'#{attribute}' must be one of : true,false") unless Api::Utils.is_boolean?(attribute) end end end diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb index dc6764f7850..1fe7ec88aa4 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/api/utils.rb @@ -47,7 +47,7 @@ class Api::Utils def self.is_regexp?(s) begin - Regexp.new(S) + Regexp.new(s) true rescue false diff --git a/sonar-server/src/main/webapp/WEB-INF/app/models/property_type.rb b/sonar-server/src/main/webapp/WEB-INF/app/models/property_type.rb index 9a9cd071e8c..8fcb9f06da3 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/models/property_type.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/models/property_type.rb @@ -18,14 +18,18 @@ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02 # class PropertyType - TYPE_INTEGER = 'INTEGER' + TYPE_STRING = 'STRING' + TYPE_TEXT = 'TEXT' + TYPE_PASSWORD = 'PASSWORD' TYPE_BOOLEAN = 'BOOLEAN' + TYPE_INTEGER = 'INTEGER' TYPE_FLOAT = 'FLOAT' - TYPE_STRING = 'STRING' + #TYPE_SINGLE_SELECT_LIST = 'SINGLE_SELECT_LIST' TYPE_METRIC = 'METRIC' + TYPE_LICENSE = 'LICENSE' + TYPE_REGULAR_EXPRESSION = 'REGULAR_EXPRESSION' + TYPE_FILTER = 'FILTER' - TYPE_TEXT = 'TEXT' - TYPE_PASSWORD = 'PASSWORD' def self.text_to_value(text, type) case type @@ -46,10 +50,12 @@ class PropertyType errors.add_to_base("Unknown type for property #{key}") unless type if text_value.empty? errors.add_to_base("#{key} is empty") unless optional - else - errors.add_to_base("#{key} is not an integer") if type==PropertyType::TYPE_INTEGER && !Api::Utils.is_integer?(text_value) - errors.add_to_base("#{key} is not a decimal number") if type==PropertyType::TYPE_FLOAT && !Api::Utils.is_number?(text_value) - errors.add_to_base("#{key} is not a boolean") if type==PropertyType::TYPE_BOOLEAN && !(text_value=="true" || text_value=="false") + return end + + errors.add_to_base("#{key} is not an integer") if type==TYPE_INTEGER && !Api::Utils.is_integer?(text_value) + errors.add_to_base("#{key} is not a decimal number") if type==TYPE_FLOAT && !Api::Utils.is_number?(text_value) + errors.add_to_base("#{key} is not a boolean") if type==TYPE_BOOLEAN && !Api::Utils.is_boolean?(text_value) + errors.add_to_base("#{key} is not a regular expression") if type==TYPE_REGULAR_EXPRESSION && !Api::Utils.is_regexp?(text_value) end end 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 new file mode 100644 index 00000000000..b4c3b451f4d --- /dev/null +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_REGULAR_EXPRESSION.html.erb @@ -0,0 +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/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldActivateInChildren-result.xml b/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldActivateInChildren-result.xml index f34a0b494fd..cc9d1869478 100644 --- a/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldActivateInChildren-result.xml +++ b/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldActivateInChildren-result.xml @@ -3,7 +3,7 @@ - + diff --git a/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldActivateInChildren.xml b/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldActivateInChildren.xml index 62081abc403..75c3d712a0f 100644 --- a/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldActivateInChildren.xml +++ b/sonar-server/src/test/resources/org/sonar/server/configuration/InheritedProfilesTest/shouldActivateInChildren.xml @@ -3,7 +3,7 @@ - + diff --git a/sonar-server/src/test/resources/org/sonar/server/configuration/RuleChangeTest/changeParentProfile.xml b/sonar-server/src/test/resources/org/sonar/server/configuration/RuleChangeTest/changeParentProfile.xml index 7c4d381feea..b5d2d18e454 100644 --- a/sonar-server/src/test/resources/org/sonar/server/configuration/RuleChangeTest/changeParentProfile.xml +++ b/sonar-server/src/test/resources/org/sonar/server/configuration/RuleChangeTest/changeParentProfile.xml @@ -5,7 +5,7 @@ - + diff --git a/sonar-server/src/test/resources/org/sonar/server/configuration/RuleChangeTest/initialData.xml b/sonar-server/src/test/resources/org/sonar/server/configuration/RuleChangeTest/initialData.xml index aa0cb98276e..7aeb11f9b99 100644 --- a/sonar-server/src/test/resources/org/sonar/server/configuration/RuleChangeTest/initialData.xml +++ b/sonar-server/src/test/resources/org/sonar/server/configuration/RuleChangeTest/initialData.xml @@ -5,7 +5,7 @@ - + diff --git a/sonar-server/src/test/resources/org/sonar/server/configuration/RuleChangeTest/ruleReverted.xml b/sonar-server/src/test/resources/org/sonar/server/configuration/RuleChangeTest/ruleReverted.xml index 2ec87b2cd7b..e682eb176a8 100644 --- a/sonar-server/src/test/resources/org/sonar/server/configuration/RuleChangeTest/ruleReverted.xml +++ b/sonar-server/src/test/resources/org/sonar/server/configuration/RuleChangeTest/ruleReverted.xml @@ -2,8 +2,8 @@ - - + + -- 2.39.5