]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-4002 Display widget properties in the same order than the annotations @WidgetPr...
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 10 Dec 2012 21:56:04 +0000 (22:56 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 10 Dec 2012 21:56:04 +0000 (22:56 +0100)
sonar-server/src/main/java/org/sonar/server/ui/ViewProxy.java
sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb
sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_widget_properties.html.erb
sonar-server/src/test/java/org/sonar/server/ui/ViewProxyTest.java

index ae3c1b297981ffb59dd281020c4cdea85a28c988..de678d2d3e86cd26eae33b0c4d1aab065dcd1f46 100644 (file)
@@ -59,7 +59,7 @@ public class ViewProxy<V extends View> implements Comparable<ViewProxy> {
   private String[] resourceLanguages = {};
   private String[] defaultForMetrics = {};
   private String description = "";
-  private Map<String, WidgetProperty> widgetPropertiesByKey = Maps.newHashMap();
+  private Map<String, WidgetProperty> widgetPropertiesByKey = Maps.newLinkedHashMap();
   private String[] widgetCategories = {};
   private WidgetLayoutType widgetLayout = WidgetLayoutType.DEFAULT;
   private boolean isDefaultTab = false;
@@ -200,7 +200,7 @@ public class ViewProxy<V extends View> implements Comparable<ViewProxy> {
 
   public boolean isController() {
     String id = view.getId();
-    return id !=null && id.length()>0 && id.charAt(0)=='/';
+    return id != null && id.length() > 0 && id.charAt(0) == '/';
   }
 
   public String getTitle() {
@@ -254,7 +254,7 @@ public class ViewProxy<V extends View> implements Comparable<ViewProxy> {
   public boolean supportsMetric(String metricKey) {
     return ArrayUtils.contains(defaultForMetrics, metricKey);
   }
-  
+
   public boolean acceptsAvailableMeasures(String[] availableMeasures) {
     for (String mandatoryMeasure : mandatoryMeasures) {
       if (!ArrayUtils.contains(availableMeasures, mandatoryMeasure)) {
@@ -321,29 +321,29 @@ public class ViewProxy<V extends View> implements Comparable<ViewProxy> {
     }
     ViewProxy rhs = (ViewProxy) obj;
     return new EqualsBuilder()
-        .append(getId(), rhs.getId())
-        .isEquals();
+      .append(getId(), rhs.getId())
+      .isEquals();
   }
 
   @Override
   public String toString() {
     return new ToStringBuilder(this)
-        .append("id", view.getId())
-        .append("sections", sections)
-        .append("userRoles", userRoles)
-        .append("scopes", resourceScopes)
-        .append("qualifiers", resourceQualifiers)
-        .append("languages", resourceLanguages)
-        .append("metrics", defaultForMetrics)
-        .toString();
+      .append("id", view.getId())
+      .append("sections", sections)
+      .append("userRoles", userRoles)
+      .append("scopes", resourceScopes)
+      .append("qualifiers", resourceQualifiers)
+      .append("languages", resourceLanguages)
+      .append("metrics", defaultForMetrics)
+      .toString();
 
   }
 
   public int compareTo(ViewProxy other) {
     return new CompareToBuilder()
-        .append(getTitle(), other.getTitle())
-        .append(getId(), other.getId())
-        .toComparison();
+      .append(getTitle(), other.getTitle())
+      .append(getId(), other.getId())
+      .toComparison();
 
   }
 }
index 769eb22b4c072e5b4702db66e8a35289b32c3310..816847428387557fa23b1d0e1d43212da1f43d24 100644 (file)
@@ -540,67 +540,6 @@ module ApplicationHelper
     html += "\">X</a></span>"
   end
 
-  #
-  # Compares 2 strings using the human natural order.
-  # For instance, the following array:
-  #    ['foo10', 'foo2', foo1']
-  # , will be sorted this way:
-  #    ['foo1', 'foo2', foo10']
-  # , instead of (with standard String sort):
-  #    ['foo1', 'foo10', foo2']
-  # 
-  # Source: http://www.justskins.com/forums/natural-order-sort-101199.html
-  # 
-  def natural_comparison(str1, str2, caseInsensitive=false)
-    str1, str2 = str1.dup, str2.dup
-    compareExpression = /^(\D*)(\d*)(.*)$/
-
-    if caseInsensitive
-      str1.downcase!
-      str2.downcase!
-    end
-
-    # Remove all whitespace
-    str1.gsub!(/\s*/, '')
-    str2.gsub!(/\s*/, '')
-
-    while (str1.length > 0) or (str2.length > 0) do
-      # Extract non-digits, digits and rest of string
-      str1 =~ compareExpression
-      chars1, num1, str1 = $1.dup, $2.dup, $3.dup
-
-      str2 =~ compareExpression
-      chars2, num2, str2 = $1.dup, $2.dup, $3.dup
-
-      # Compare the non-digits
-      case (chars1 <=> chars2)
-        when 0
-          # Non-digits are the same, compare the digits...
-          # If either number begins with a zero, then compare
-          # alphabetically, otherwise compare numerically
-          if (num1[0] != 48) and (num2[0] != 48)
-            num1, num2 = num1.to_i, num2.to_i
-          end
-
-          case (num1 <=> num2)
-            when -1 then
-              return -1
-            when 1 then
-              return 1
-          end
-        when -1 then
-          return -1
-        when 1 then
-          return 1
-      end # case
-
-    end # while
-
-    # Strings are naturally equal
-    return 0
-  end
-
-
   #
   # Creates an enhanced dropdown selection box of resources. Values are loaded on the fly via Ajax requests.
   # ==== Options
index 8c5fd718aeb087a65f4085b3f7a914fe6b9c9ba7..4257bf72677a42a3c7435dee8e3b4cfb957a269b 100644 (file)
@@ -20,7 +20,7 @@
       </tr>
     <% end %>
 
-    <% widget.java_definition.getWidgetProperties().sort { |w1, w2| natural_comparison(w1.key, w2.key) }.each do |property_def| %>
+    <% widget.java_definition.getWidgetProperties().each do |property_def| %>
       <tr>
         <td class="form-key-cell"><%= property_def.key() -%><%= " *" unless property_def.optional() -%></td>
         <td class="form-val-cell" id="row_<%= property_def.key() -%>">
index f1b0d05677270332638f73a2ab5e093bbf4eab33..6c196599d9e889075959f0cbb1dbc9343c4af560 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.server.ui;
 
+import com.google.common.collect.Lists;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.rules.ExpectedException;
@@ -33,6 +34,8 @@ import org.sonar.api.web.WidgetProperty;
 import org.sonar.api.web.WidgetPropertyType;
 import org.sonar.api.web.WidgetScope;
 
+import java.util.List;
+
 import static com.google.common.collect.Iterables.getOnlyElement;
 import static org.fest.assertions.Assertions.assertThat;
 import static org.hamcrest.CoreMatchers.is;
@@ -68,8 +71,8 @@ public class ViewProxyTest {
     ViewProxy proxy = new ViewProxy<View>(view);
 
     assertThat(proxy.getTarget(), is(view));
-    assertArrayEquals(proxy.getSections(), new String[] {NavigationSection.RESOURCE});
-    assertArrayEquals(proxy.getUserRoles(), new String[] {UserRole.ADMIN});
+    assertArrayEquals(proxy.getSections(), new String[]{NavigationSection.RESOURCE});
+    assertArrayEquals(proxy.getUserRoles(), new String[]{UserRole.ADMIN});
   }
 
   @Test
@@ -84,7 +87,7 @@ public class ViewProxyTest {
     ViewProxy proxy = new ViewProxy<View>(view);
 
     assertThat(proxy.getTarget(), is(view));
-    assertArrayEquals(proxy.getSections(), new String[] {NavigationSection.HOME});
+    assertArrayEquals(proxy.getSections(), new String[]{NavigationSection.HOME});
     assertThat(proxy.getUserRoles().length, org.hamcrest.Matchers.is(0));
   }
 
@@ -127,7 +130,7 @@ public class ViewProxyTest {
     ViewProxy proxy = new ViewProxy<MyView>(new MyView());
 
     assertThat(proxy.isDefaultTab(), is(false));
-    assertThat(proxy.getDefaultTabForMetrics(), is(new String[] {"ncloc", "coverage"}));
+    assertThat(proxy.getDefaultTabForMetrics(), is(new String[]{"ncloc", "coverage"}));
   }
 
   @Test
@@ -135,7 +138,18 @@ public class ViewProxyTest {
     ViewProxy<Widget> proxy = new ViewProxy<Widget>(new EditableWidget());
 
     assertThat(proxy.isEditable()).isTrue();
-    assertThat(proxy.getWidgetProperties()).hasSize(2);
+    assertThat(proxy.getWidgetProperties()).hasSize(3);
+  }
+
+  @Test
+  public void load_widget_properties_in_the_same_order_than_annotations() {
+    ViewProxy<Widget> proxy = new ViewProxy<Widget>(new EditableWidget());
+
+    List<WidgetProperty> widgetProperties = Lists.newArrayList(proxy.getWidgetProperties());
+    assertThat(widgetProperties).hasSize(3);
+    assertThat(widgetProperties.get(0).key()).isEqualTo("first_prop");
+    assertThat(widgetProperties.get(1).key()).isEqualTo("second_prop");
+    assertThat(widgetProperties.get(2).key()).isEqualTo("third_prop");
   }
 
   @Test
@@ -164,7 +178,7 @@ public class ViewProxyTest {
     exception.expect(IllegalArgumentException.class);
     exception.expectMessage("INVALID");
     exception.expectMessage("WidgetWithInvalidScope");
-    
+
     new ViewProxy<Widget>(new WidgetWithInvalidScope());
   }
 
@@ -189,7 +203,7 @@ public class ViewProxyTest {
     }
     ViewProxy proxy = new ViewProxy<MyView>(new MyView());
 
-    assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "ncloc", "coverage"}), is(true));
+    assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "ncloc", "coverage"}), is(true));
   }
 
   @Test
@@ -202,8 +216,8 @@ public class ViewProxyTest {
     }
     ViewProxy proxy = new ViewProxy<MyView>(new MyView());
 
-    assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "ncloc", "coverage"}), is(true));
-    assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "coverage"}), is(false));
+    assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "ncloc", "coverage"}), is(true));
+    assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "coverage"}), is(false));
   }
 
   @Test
@@ -216,8 +230,8 @@ public class ViewProxyTest {
     }
     ViewProxy proxy = new ViewProxy<MyView>(new MyView());
 
-    assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "coverage"}), is(true));
-    assertThat(proxy.acceptsAvailableMeasures(new String[] {"complexity", "coverage"}), is(false));
+    assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "coverage"}), is(true));
+    assertThat(proxy.acceptsAvailableMeasures(new String[]{"complexity", "coverage"}), is(false));
   }
 
   @Test
@@ -231,11 +245,11 @@ public class ViewProxyTest {
     ViewProxy proxy = new ViewProxy<MyView>(new MyView());
 
     // ok, mandatory measures and 1 needed measure
-    assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "ncloc", "coverage", "duplications"}), is(true));
+    assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "ncloc", "coverage", "duplications"}), is(true));
     // ko, one of the needed measures but not all of the mandatory ones
-    assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "coverage", "duplications"}), is(false));
+    assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "coverage", "duplications"}), is(false));
     // ko, mandatory measures but no one of the needed measures
-    assertThat(proxy.acceptsAvailableMeasures(new String[] {"lines", "nloc", "coverage", "dsm"}), is(false));
+    assertThat(proxy.acceptsAvailableMeasures(new String[]{"lines", "nloc", "coverage", "dsm"}), is(false));
   }
 
 }
@@ -258,8 +272,9 @@ class FakeView implements View {
 }
 
 @WidgetProperties({
-  @WidgetProperty(key = "foo", optional = false),
-  @WidgetProperty(key = "bar", defaultValue = "30", type = WidgetPropertyType.INTEGER)
+  @WidgetProperty(key = "first_prop", optional = false),
+  @WidgetProperty(key = "second_prop", defaultValue = "30", type = WidgetPropertyType.INTEGER),
+  @WidgetProperty(key = "third_prop", type = WidgetPropertyType.INTEGER)
 })
 class EditableWidget implements Widget {
   public String getId() {