aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-server
diff options
context:
space:
mode:
Diffstat (limited to 'sonar-server')
-rw-r--r--sonar-server/src/main/java/org/sonar/server/filters/Filter.java28
-rw-r--r--sonar-server/src/main/java/org/sonar/server/filters/FilterResult.java38
-rw-r--r--sonar-server/src/test/java/org/sonar/server/filters/FilterExecutorTest.java37
-rw-r--r--sonar-server/src/test/java/org/sonar/server/filters/FilterResultTest.java2
4 files changed, 65 insertions, 40 deletions
diff --git a/sonar-server/src/main/java/org/sonar/server/filters/Filter.java b/sonar-server/src/main/java/org/sonar/server/filters/Filter.java
index cc7722acad8..1e576e1864c 100644
--- a/sonar-server/src/main/java/org/sonar/server/filters/Filter.java
+++ b/sonar-server/src/main/java/org/sonar/server/filters/Filter.java
@@ -26,7 +26,6 @@ import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.commons.lang.builder.ToStringStyle;
import org.sonar.api.resources.Qualifiers;
-import org.sonar.api.resources.Resource;
import java.util.List;
import java.util.Set;
@@ -59,7 +58,7 @@ public class Filter {
private boolean sortedByName;
private boolean sortedByDate;
private boolean sortedByVersion;
- private boolean useMeasureValueToSort = true; // only if sortedMetricId is not null
+ private boolean isNumericMetric = true;
private boolean ascendingSort = true;
public Filter setPath(Integer rootSnapshotId, Integer snapshotId, String snapshotPath, boolean isViewContext) {
@@ -174,18 +173,22 @@ public class Filter {
return sortedMetricId;
}
- public boolean useMeasureValueToSort() {
- return useMeasureValueToSort;
+ public boolean isNumericMetric() {
+ return isNumericMetric;
}
- public Filter setSortedMetricId(Integer id) {
- return setSortedMetricId(id, true, false);
+ public boolean isTextSort() {
+ return !isNumericMetric || sortedByLanguage || sortedByName || sortedByVersion;
}
+// public Filter setSortedMetricId(Integer id) {
+// return setSortedMetricId(id, true, false);
+// }
+
public Filter setSortedMetricId(Integer id, boolean isNumericValue, Boolean isVariation) {
unsetSorts();
this.sortedMetricId = id;
- this.useMeasureValueToSort = isNumericValue;
+ this.isNumericMetric = isNumericValue;
this.sortedByMeasureVariation = isVariation;
return this;
}
@@ -240,7 +243,7 @@ public class Filter {
this.sortedByName = false;
this.sortedMetricId = null;
this.sortedByVersion = false;
- this.useMeasureValueToSort = true;
+ this.isNumericMetric = true;
}
public List<MeasureCriterion> getMeasureCriteria() {
@@ -353,7 +356,7 @@ public class Filter {
String getColumnToSort() {
String col = "text_value";
- if (useMeasureValueToSort()) {
+ if (isNumericMetric()) {
col = (sortedByMeasureVariation == Boolean.TRUE ? getVariationColumn(periodIndex) : "value");
}
return col;
@@ -375,8 +378,9 @@ public class Filter {
}
public static Filter createForAllQualifiers() {
- return new Filter().setQualifiers(Resource.QUALIFIER_VIEW, Resource.QUALIFIER_SUBVIEW,
- Resource.QUALIFIER_PROJECT, Resource.QUALIFIER_MODULE, Resource.QUALIFIER_DIRECTORY, Resource.QUALIFIER_PACKAGE,
- Resource.QUALIFIER_FILE, Resource.QUALIFIER_CLASS, Qualifiers.UNIT_TEST_FILE, Resource.QUALIFIER_LIB);
+ return new Filter().setQualifiers(
+ Qualifiers.VIEW, Qualifiers.SUBVIEW,
+ Qualifiers.PROJECT, Qualifiers.MODULE, Qualifiers.DIRECTORY, Qualifiers.PACKAGE,
+ Qualifiers.FILE, Qualifiers.CLASS, Qualifiers.UNIT_TEST_FILE, Qualifiers.LIBRARY, Qualifiers.PARAGRAPH);
}
}
diff --git a/sonar-server/src/main/java/org/sonar/server/filters/FilterResult.java b/sonar-server/src/main/java/org/sonar/server/filters/FilterResult.java
index 9dfc0a5c260..ae0ebf15c53 100644
--- a/sonar-server/src/main/java/org/sonar/server/filters/FilterResult.java
+++ b/sonar-server/src/main/java/org/sonar/server/filters/FilterResult.java
@@ -33,7 +33,7 @@ public class FilterResult {
this.rows = new ArrayList(rows);
this.filter = filter;
}
-
+
/**
* @return a list of arrays
*/
@@ -71,7 +71,7 @@ public class FilterResult {
public void sort() {
if (filter.isSorted()) {
- Comparator comparator = new RowComparator(SORTED_COLUMN_INDEX);
+ Comparator comparator = (filter.isTextSort() ? new StringIgnoreCaseComparator(SORTED_COLUMN_INDEX) : new NumericComparator(SORTED_COLUMN_INDEX));
if (!filter.isAscendingSort()) {
comparator = new ReverseComparator(comparator);
}
@@ -81,14 +81,14 @@ public class FilterResult {
public void removeUnvalidRows() {
int numberOfCriteria = filter.getMeasureCriteria().size();
- if (numberOfCriteria>0) {
+ if (numberOfCriteria > 0) {
int fromColumnIndex = (filter.isSorted() ? SORTED_COLUMN_INDEX + 1 : SORTED_COLUMN_INDEX);
- for (Iterator<Object[]> it=rows.iterator() ; it.hasNext() ; ) {
+ for (Iterator<Object[]> it = rows.iterator(); it.hasNext(); ) {
Object[] row = it.next();
boolean remove = false;
- for (int index=0 ; index<numberOfCriteria ; index++) {
- if (row[fromColumnIndex+index]==null) {
- remove=true;
+ for (int index = 0; index < numberOfCriteria; index++) {
+ if (row[fromColumnIndex + index] == null) {
+ remove = true;
}
}
if (remove) {
@@ -98,11 +98,11 @@ public class FilterResult {
}
}
- static final class RowComparator implements Comparator, Serializable {
+ static final class NumericComparator implements Comparator, Serializable {
private static final long serialVersionUID = 4627704879575964978L;
private int index;
- RowComparator(int index) {
+ NumericComparator(int index) {
this.index = index;
}
@@ -112,5 +112,25 @@ public class FilterResult {
return (c1 == null ? -1 : (o2 == null ? 1 : c1.compareTo(o2)));
}
}
+
+ static final class StringIgnoreCaseComparator implements Comparator, Serializable {
+ private int index;
+
+ StringIgnoreCaseComparator(int index) {
+ this.index = index;
+ }
+
+ public int compare(Object o1, Object o2) {
+ String s1 = (String)((Object[]) o1)[index];
+ if (s1 == null) {
+ return -1;
+ }
+ String s2 = (String)((Object[]) o2)[index];
+ if (s2 == null) {
+ return 1;
+ }
+ return s1.compareToIgnoreCase(s2);
+ }
+ }
}
diff --git a/sonar-server/src/test/java/org/sonar/server/filters/FilterExecutorTest.java b/sonar-server/src/test/java/org/sonar/server/filters/FilterExecutorTest.java
index c8c539274a3..e1c5d336c44 100644
--- a/sonar-server/src/test/java/org/sonar/server/filters/FilterExecutorTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/filters/FilterExecutorTest.java
@@ -21,6 +21,7 @@ package org.sonar.server.filters;
import com.google.common.collect.Sets;
import org.junit.Test;
+import org.sonar.api.resources.Qualifiers;
import org.sonar.jpa.test.AbstractDbUnitTestCase;
import org.sonar.api.resources.Resource;
@@ -129,8 +130,8 @@ public class FilterExecutorTest extends AbstractDbUnitTestCase {
setupData("shared", "measures");
FilterExecutor executor = new FilterExecutor(getSession());
Filter filter = new Filter()
- .setQualifiers(Sets.newHashSet(Resource.QUALIFIER_CLASS))
- .setSortedMetricId(2);
+ .setQualifiers(Sets.newHashSet(Qualifiers.CLASS))
+ .setSortedMetricId(2, true, false);
FilterResult result = executor.execute(filter);
assertSortedSnapshotIds(result, 6, 5);
@@ -141,8 +142,8 @@ public class FilterExecutorTest extends AbstractDbUnitTestCase {
setupData("shared", "measures");
FilterExecutor executor = new FilterExecutor(getSession());
Filter filter = new Filter()
- .setQualifiers(Sets.newHashSet(Resource.QUALIFIER_CLASS))
- .setSortedMetricId(2)
+ .setQualifiers(Sets.newHashSet(Qualifiers.CLASS))
+ .setSortedMetricId(2, true, false)
.setAscendingSort(false);
FilterResult result = executor.execute(filter);
@@ -154,7 +155,7 @@ public class FilterExecutorTest extends AbstractDbUnitTestCase {
setupData("shared", "measures");
FilterExecutor executor = new FilterExecutor(getSession());
Filter filter = new Filter()
- .setQualifiers(Sets.newHashSet(Resource.QUALIFIER_CLASS))
+ .setQualifiers(Sets.newHashSet(Qualifiers.CLASS))
.addMeasureCriterion(new MeasureCriterion(2, ">", 50.0, false));
FilterResult result = executor.execute(filter);
@@ -166,7 +167,7 @@ public class FilterExecutorTest extends AbstractDbUnitTestCase {
setupData("shared", "measures");
FilterExecutor executor = new FilterExecutor(getSession());
Filter filter = new Filter()
- .setQualifiers(Sets.newHashSet(Resource.QUALIFIER_CLASS))
+ .setQualifiers(Sets.newHashSet(Qualifiers.CLASS))
.addMeasureCriterion(new MeasureCriterion(2, ">", 50.0, false))
.addMeasureCriterion(new MeasureCriterion(1, ">", 100.0, false));
@@ -179,7 +180,7 @@ public class FilterExecutorTest extends AbstractDbUnitTestCase {
setupData("shared", "measures");
FilterExecutor executor = new FilterExecutor(getSession());
Filter filter = new Filter()
- .setQualifiers(Sets.newHashSet(Resource.QUALIFIER_CLASS))
+ .setQualifiers(Sets.newHashSet(Qualifiers.CLASS))
.addMeasureCriterion(new MeasureCriterion(2, ">", 50.0, false))
.addMeasureCriterion(new MeasureCriterion(1, "<", 100.0, false));
@@ -192,10 +193,10 @@ public class FilterExecutorTest extends AbstractDbUnitTestCase {
setupData("shared", "measures");
FilterExecutor executor = new FilterExecutor(getSession());
Filter filter = new Filter()
- .setQualifiers(Sets.newHashSet(Resource.QUALIFIER_CLASS))
+ .setQualifiers(Sets.newHashSet(Qualifiers.CLASS))
.addMeasureCriterion(new MeasureCriterion(2, ">", 5.0, false))
.addMeasureCriterion(new MeasureCriterion(1, ">", 5.0, false))
- .setSortedMetricId(2); // sort by coverage
+ .setSortedMetricId(2, true, false); // sort by coverage
FilterResult result = executor.execute(filter);
assertSnapshotIds(result, 6, 5);
@@ -206,10 +207,10 @@ public class FilterExecutorTest extends AbstractDbUnitTestCase {
setupData("shared", "measures");
FilterExecutor executor = new FilterExecutor(getSession());
Filter filter = new Filter()
- .setQualifiers(Sets.newHashSet(Resource.QUALIFIER_CLASS))
+ .setQualifiers(Sets.newHashSet(Qualifiers.CLASS))
.addMeasureCriterion(new MeasureCriterion(2, ">", 5.0, false)) // filter on coverage
.addMeasureCriterion(new MeasureCriterion(1, ">", 5.0, false)) // filter on lines
- .setSortedMetricId(2) // sort by coverage
+ .setSortedMetricId(2, true, false) // sort by coverage
.setAscendingSort(false);
FilterResult result = executor.execute(filter);
@@ -237,7 +238,7 @@ public class FilterExecutorTest extends AbstractDbUnitTestCase {
setupData("shared", "measures");
FilterExecutor executor = new FilterExecutor(getSession());
Filter filter = new Filter()
- .setQualifiers(Sets.newHashSet(Resource.QUALIFIER_CLASS))
+ .setQualifiers(Sets.newHashSet(Qualifiers.CLASS))
.addMeasureCriterion(new MeasureCriterion(3, ">", 0.0, false)); // filter on duplicated lines
FilterResult result = executor.execute(filter);
@@ -249,7 +250,7 @@ public class FilterExecutorTest extends AbstractDbUnitTestCase {
setupData("shared", "measures");
FilterExecutor executor = new FilterExecutor(getSession());
Filter filter = new Filter()
- .setQualifiers(Sets.newHashSet(Resource.QUALIFIER_CLASS))
+ .setQualifiers(Sets.newHashSet(Qualifiers.CLASS))
.addMeasureCriterion(new MeasureCriterion(1, ">", 0.0, false)) // filter on lines
.addMeasureCriterion(new MeasureCriterion(3, ">", 0.0, false)); // filter on duplicated lines
@@ -262,8 +263,8 @@ public class FilterExecutorTest extends AbstractDbUnitTestCase {
setupData("shared", "measures");
FilterExecutor executor = new FilterExecutor(getSession());
Filter filter = new Filter()
- .setQualifiers(Sets.newHashSet(Resource.QUALIFIER_CLASS))
- .setSortedMetricId(3); // sort by duplicated lines
+ .setQualifiers(Sets.newHashSet(Qualifiers.CLASS))
+ .setSortedMetricId(3, true, false); // sort by duplicated lines
FilterResult result = executor.execute(filter);
assertSnapshotIds(result, 5, 6);
@@ -274,9 +275,9 @@ public class FilterExecutorTest extends AbstractDbUnitTestCase {
setupData("shared", "measures");
FilterExecutor executor = new FilterExecutor(getSession());
Filter filter = new Filter()
- .setQualifiers(Sets.newHashSet(Resource.QUALIFIER_CLASS))
+ .setQualifiers(Sets.newHashSet(Qualifiers.CLASS))
.addMeasureCriterion(new MeasureCriterion(1, ">", 0.0, false)) // lines > 0
- .setSortedMetricId(2); // sort by coverage
+ .setSortedMetricId(2, true, false); // sort by coverage
FilterResult result = executor.execute(filter);
assertSnapshotIds(result, 6, 5);
@@ -287,7 +288,7 @@ public class FilterExecutorTest extends AbstractDbUnitTestCase {
setupData("shared", "measures");
FilterExecutor executor = new FilterExecutor(getSession());
Filter filter = new Filter()
- .setQualifiers(Sets.newHashSet(Resource.QUALIFIER_CLASS))
+ .setQualifiers(Sets.newHashSet(Qualifiers.CLASS))
.addMeasureCriterion(new MeasureCriterion(1, ">", 400.0, false)) // lines > 400
.addMeasureCriterion(new MeasureCriterion(1, "<", 600.0, false)); // lines > 400
diff --git a/sonar-server/src/test/java/org/sonar/server/filters/FilterResultTest.java b/sonar-server/src/test/java/org/sonar/server/filters/FilterResultTest.java
index 27caf4452c6..399baff42e1 100644
--- a/sonar-server/src/test/java/org/sonar/server/filters/FilterResultTest.java
+++ b/sonar-server/src/test/java/org/sonar/server/filters/FilterResultTest.java
@@ -34,7 +34,7 @@ public class FilterResultTest {
@Test
public void sortWithNullElements() {
List<String[]> list = Arrays.asList(new String[]{"foo"}, new String[]{null}, new String[]{"bar"}, new String[]{null}, new String[]{null}, new String[]{"toto"});
- Collections.sort(list, new FilterResult.RowComparator(0));
+ Collections.sort(list, new FilterResult.NumericComparator(0));
assertThat(list.get(0)[0], nullValue());
assertThat(list.get(1)[0], nullValue());