]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3529 Property field validation
authorDavid Gageot <david@gageot.net>
Mon, 1 Oct 2012 14:18:19 +0000 (16:18 +0200)
committerDavid Gageot <david@gageot.net>
Mon, 1 Oct 2012 14:36:58 +0000 (16:36 +0200)
sonar-plugin-api/src/test/java/org/sonar/api/config/PropertyDefinitionTest.java
sonar-server/src/main/webapp/WEB-INF/app/controllers/settings_controller.rb
sonar-server/src/main/webapp/WEB-INF/app/helpers/settings_helper.rb
sonar-server/src/main/webapp/WEB-INF/app/models/property.rb
sonar-server/src/main/webapp/WEB-INF/app/views/settings/_error.html.erb [new file with mode: 0644]
sonar-server/src/main/webapp/WEB-INF/app/views/settings/_properties.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/settings/_set_instance.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/settings/_single_value.html.erb
sonar-server/src/main/webapp/WEB-INF/app/views/settings/_type_SINGLE_SELECT_LIST.html.erb
sonar-server/src/main/webapp/stylesheets/style.css

index caea3a388b74ddc4268cb8b2a2b7a266e95b1170..3a4fcbea15635815c7f8d4d593cc0b02bac5c0b7 100644 (file)
@@ -81,7 +81,7 @@ public class PropertyDefinitionTest {
   }
 
   @Properties(@Property(key = "hello", name = "Hello", fields = {
-    @PropertyField(key = "first", name = "First"),
+    @PropertyField(key = "first", name = "First", description = "Description", options = {"A", "B"}),
     @PropertyField(key = "second", name = "Second", type = PropertyType.INTEGER)}))
   static class WithPropertySet {
   }
@@ -96,10 +96,13 @@ public class PropertyDefinitionTest {
     assertThat(def.getFields()).hasSize(2);
     assertThat(def.getFields()[0].getKey()).isEqualTo("first");
     assertThat(def.getFields()[0].getName()).isEqualTo("First");
+    assertThat(def.getFields()[0].getDescription()).isEqualTo("Description");
     assertThat(def.getFields()[0].getType()).isEqualTo(PropertyType.STRING);
+    assertThat(def.getFields()[0].getOptions()).containsOnly("A", "B");
     assertThat(def.getFields()[1].getKey()).isEqualTo("second");
     assertThat(def.getFields()[1].getName()).isEqualTo("Second");
     assertThat(def.getFields()[1].getType()).isEqualTo(PropertyType.INTEGER);
+    assertThat(def.getFields()[1].getOptions()).isEmpty();
   }
 
   @Properties(@Property(key = "hello", name = "Hello"))
index 0db50cd74b9133570fc28256f3666d16b89bedf6..bbb052d42618e121ab0f937090256bc6bb9a9ea3 100644 (file)
@@ -38,6 +38,8 @@ class SettingsController < ApplicationController
     access_denied if (@resource.nil? && !is_admin?)
 
     load_properties()
+
+    @updated_properties = {}
     save_properties(resource_id)
     save_property_sets(resource_id)
 
@@ -46,7 +48,6 @@ class SettingsController < ApplicationController
 
   private
 
-  # TODO: Validation
   def save_property_sets(resource_id)
     (params[:property_sets] || []).each do |key, value|
       set_keys = drop_trailing_blank_values(value)
@@ -57,15 +58,16 @@ class SettingsController < ApplicationController
       params[key].each do |field_key, field_values|
         field_values.each_with_index do |field_value, index|
           set_key = set_keys[index]
-          Property.set("#{key}.#{set_key}.#{field_key}", field_value, resource_id) if set_key
+          if set_key
+            field_property_key = "#{key}.#{set_key}.#{field_key}"
+            @updated_properties[field_property_key] = Property.set(field_property_key, field_value, resource_id)
+          end
         end
       end
     end
   end
 
   def save_properties(resource_id)
-    @updated_properties = {}
-
     (params[:settings] || []).each do |key, value|
       if value.kind_of? Array
         value = drop_trailing_blank_values(value)
index 3376c8807d2d5a3fe2f6f3b9e2537d60f2b63cac..6e3500af73d2e1a3b5f1319afe14d51765a99251 100644 (file)
@@ -38,6 +38,14 @@ module SettingsHelper
     message("field.#{property.key}.#{field.key}.description", :default => field.description)
   end
 
+  def option_name(property, field, option)
+    if field
+      message("option.#{property.key}.#{field.key}.#{option}.name", :default => option)
+    else
+      message("option.#{property.key}.#{option}.name", :default => option)
+    end
+  end
+
   def property_help(property)
     message("property.#{property.key}.help", :default => '')
   end
index e4bec485aaf32a59404f95b88d99a78d1582e73b..3ebf1126a2ce3c2710337a5c2f680002e0d5b7dd 100644 (file)
@@ -137,6 +137,18 @@ class Property < ActiveRecord::Base
       end
   end
 
+  def java_field_definition
+    @java_field_definition ||=
+      begin
+        if /(.*)\..*\.(.*)/.match(key)
+          property_definition = Java::OrgSonarServerUi::JRubyFacade.getInstance().getPropertyDefinitions().get(Regexp.last_match(1))
+          if property_definition
+            property_definition.fields.find { |field| field.key == Regexp.last_match(2) }
+          end
+        end
+      end
+  end
+
   def validation_error_message
     msg=''
     errors.each_full do |error|
@@ -156,9 +168,21 @@ class Property < ActiveRecord::Base
   end
 
   def validate
+    validate_property()
+    validate_field()
+  end
+
+  def validate_property
     if java_definition
       validation_result = java_definition.validate(text_value)
       errors.add_to_base(validation_result.getErrorKey()) unless validation_result.isValid()
     end
   end
+
+  def validate_field
+    if java_field_definition
+      validation_result = java_field_definition.validate(text_value)
+      errors.add_to_base(validation_result.getErrorKey()) unless validation_result.isValid()
+    end
+  end
 end
diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_error.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/settings/_error.html.erb
new file mode 100644 (file)
index 0000000..c1f5cf0
--- /dev/null
@@ -0,0 +1,7 @@
+<% if @updated_properties -%>
+  <% p = @updated_properties[key] -%>
+
+  <% if p && !p.valid? -%>
+    <div class="error"><%= p.validation_error_message -%></div>
+  <% end -%>
+<% end -%>
index 5d2e340b5c380676e4fd6ae3ce0faf87e4ddd9a3..9626071b3eeada858228bf971e6c915883caedba 100644 (file)
               <%= render "settings/single_value", :property => property, :value => value -%>
             <% end -%>
 
-            <% p = @updated_properties[property.key] if @updated_properties -%>
-            <% if p && !p.valid? -%>
-              <div class="error"><%= p.validation_error_message -%></div>
-            <% end -%>
+            <%= render "settings/error", :key => property.key -%>
 
             <% default_prop_value = (@resource ? Property.value(property.key, nil, property.defaultValue) : property.defaultValue) -%>
             <% unless default_prop_value.blank? -%>
index 4cafb504ce0df1bb7de0b42028c703fc038c4288..bc3ad9282a8716232cfbcb7fd5be631552f4f34d 100644 (file)
@@ -6,16 +6,19 @@
   </div>
 
   <% property.fields.each do |field| -%>
+    <% key = "#{property.key}.#{set_key}.#{field.key}" if set_key -%>
+    <% value = Property.value(key, resource_id) if set_key -%>
+
     <div class="field">
       <label><%= field_name(property, field) -%>: </label>
-
-      <% value = Property.value("#{property.key}.#{set_key}.#{field.key}", resource_id) if set_key -%>
-      <%= render "settings/type_#{field.type}", :property => field, :value => value, :name => "#{property.key}[#{field.key}][]", :id => "input_#{h field.key}" -%>
+      <%= render "settings/type_#{field.type}", :property => field, :field => field, :value => value, :name => "#{property.key}[#{field.key}][]", :id => "input_#{h field.key}" -%>
 
       <% desc=field_description(property, field) -%>
       <% unless desc.blank? %>
         <p class="note"><%= desc -%></p>
       <% end -%>
+
+      <%= render "settings/error", :key => key -%>
     </div>
   <% end -%>
   <br/>
index a08cc708b38415d4aef8f0cedd2727af68280569..f9a0bcaa091eb629ab28137cb104ad4e03be7a08 100644 (file)
@@ -1 +1 @@
-<%= render "settings/type_#{property_type(property, value)}", :property => property, :value => value, :name => input_name(property), :id => "input_#{h property.key}" -%>
+<%= render "settings/type_#{property_type(property, value)}", :property => property, :field => nil, :value => value, :name => input_name(property), :id => "input_#{h property.key}" -%>
index e2ffefee9eca045ed1092ae4ddcaa773bda07198..2737ec732fc9355290fcc7dc5ac8c919bb364555 100644 (file)
@@ -1,6 +1,6 @@
 <select name="<%= name -%>" id="<%= id -%>">
   <option value=""><%= message('default') -%></option>
   <% property.options.each do |option| %>
-    <option value="<%= h option -%>" <%= 'selected' if value && value==option -%>><%= h option -%></option>
+    <option value="<%= h option -%>" <%= 'selected' if value && value==option -%>><%= option_name(property, field, option) -%></option>
   <% end %>
 </select>
\ No newline at end of file
index 3eefcc481bc7fdaadca5c0dc70a7f0179e6e2ce4..9d2140866669f00586bebd4fc90c63f04c05eab7 100644 (file)
@@ -2440,7 +2440,7 @@ textarea.width100 {
   padding-right: 5px;
 }
 
-.field .note {
+.field .note, .field .error  {
   margin-top: 3px;
-  margin-left: 90px;
+  margin-left: 88px;
 }