diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2012-12-10 22:56:04 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2012-12-10 22:56:04 +0100 |
commit | 5de4c53c916ff31b63f74339abd1b852d1524834 (patch) | |
tree | 042217a0f11223c052c4c9ae14d29bb76bc7651a /sonar-server | |
parent | 54ad3e74c218d12178f08882e0a7c337e042c078 (diff) | |
download | sonarqube-5de4c53c916ff31b63f74339abd1b852d1524834.tar.gz sonarqube-5de4c53c916ff31b63f74339abd1b852d1524834.zip |
SONAR-4002 Display widget properties in the same order than the annotations @WidgetProperty
Diffstat (limited to 'sonar-server')
4 files changed, 48 insertions, 94 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 ae3c1b29798..de678d2d3e8 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 @@ -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(); } } diff --git a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb index 769eb22b4c0..81684742838 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb +++ b/sonar-server/src/main/webapp/WEB-INF/app/helpers/application_helper.rb @@ -541,67 +541,6 @@ module ApplicationHelper 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 # * <tt>:width</tt> - The width suffixed with unit, for example '300px' or '100%'. Default is '250px' diff --git a/sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_widget_properties.html.erb b/sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_widget_properties.html.erb index 8c5fd718aeb..4257bf72677 100644 --- a/sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_widget_properties.html.erb +++ b/sonar-server/src/main/webapp/WEB-INF/app/views/dashboard/_widget_properties.html.erb @@ -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() -%>"> 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 f1b0d056772..6c196599d9e 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 @@ -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() { |