*/
package org.sonar.core.measure;
+import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
class MeasureFilterSort {
return Field.DATE.equals(field) || Field.PROJECT_CREATION_DATE.equals(field);
}
+ boolean isOnAlert() {
+ return metric != null && metric.getKey().equals(CoreMetrics.ALERT_STATUS_KEY);
+ }
+
boolean isAsc() {
return asc;
}
package org.sonar.core.measure;
import com.google.common.base.Function;
+import com.google.common.collect.ImmutableList;
import com.google.common.collect.Lists;
import com.google.common.collect.Ordering;
import org.apache.commons.dbutils.DbUtils;
rowProcessor = new NumericSortRowProcessor();
} else if (filter.sort().isOnDate()) {
rowProcessor = new DateSortRowProcessor();
+ } else if (filter.sort().isOnAlert()) {
+ rowProcessor = new AlertSortRowProcessor();
} else {
rowProcessor = new TextSortRowProcessor();
}
}
return ordering;
}
+ }
+ static class AlertSortRowProcessor extends TextSortRowProcessor {
+ Function sortFieldFunction() {
+ return new Function<MeasureFilterRow, Integer>() {
+ public Integer apply(MeasureFilterRow row) {
+ return ImmutableList.of("OK", "WARN", "ERROR").indexOf(row.getSortText());
+ }
+ };
+ }
+
+ @Override
+ Ordering sortFieldOrdering(boolean ascending) {
+ Ordering<Integer> ordering = Ordering.<Integer>natural().nullsLast();
+ if (!ascending) {
+ ordering = ordering.reverse();
+ }
+ return ordering;
+ }
}
static class NumericSortRowProcessor extends RowProcessor {
});
}
}
+
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
+import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Metric;
import org.sonar.api.utils.DateUtils;
import org.sonar.core.persistence.TestDatabase;
assertThat(DateUtils.formatDate(rows.get(1).getSortDate())).isEqualTo("2008-12-19");
}
+ @Test
+ public void sort_by_ascending_alert() throws SQLException {
+ db.prepareDbUnit(getClass(), "sort_by_alert.xml");
+
+ Metric alert = new Metric.Builder(CoreMetrics.ALERT_STATUS_KEY, "Alert", Metric.ValueType.LEVEL).create().setId(5);
+ MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setSortOnMetric(alert);
+ List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
+
+ // Php Project OK, Java Project WARN then Js Project ERROR
+ assertThat(rows).hasSize(3);
+ verifyPhpProject(rows.get(0));
+ verifyJavaProject(rows.get(1));
+ verifyProject(rows.get(2), 120L, 20L, 20L);
+ }
+
+ @Test
+ public void sort_by_descending_alert() throws SQLException {
+ db.prepareDbUnit(getClass(), "sort_by_alert.xml");
+
+ Metric alert = new Metric.Builder(CoreMetrics.ALERT_STATUS_KEY, "Alert", Metric.ValueType.LEVEL).create().setId(5);
+ MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setSortOnMetric(alert).setSortAsc(false);
+ List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
+
+ // Js Project ERROR, Java Project WARN, then Php Project OK
+ assertThat(rows).hasSize(3);
+ verifyProject(rows.get(0), 120L, 20L, 20L);
+ verifyJavaProject(rows.get(1));
+ verifyPhpProject(rows.get(2));
+ }
+
@Test
public void condition_on_numeric_measure() throws SQLException {
db.prepareDbUnit(getClass(), "shared.xml");
private void verifyJavaProject(MeasureFilterRow row) {
- assertThat(row.getSnapshotId()).isEqualTo(JAVA_PROJECT_SNAPSHOT_ID);
- assertThat(row.getResourceId()).isEqualTo(JAVA_PROJECT_ID);
- assertThat(row.getResourceRootId()).isEqualTo(JAVA_PROJECT_ID);
+ verifyProject(row, JAVA_PROJECT_SNAPSHOT_ID, JAVA_PROJECT_ID, JAVA_PROJECT_ID);
}
private void verifyJavaBigFile(MeasureFilterRow row) {
- assertThat(row.getSnapshotId()).isEqualTo(JAVA_FILE_BIG_SNAPSHOT_ID);
- assertThat(row.getResourceId()).isEqualTo(JAVA_FILE_BIG_ID);
- assertThat(row.getResourceRootId()).isEqualTo(JAVA_PROJECT_ID);
+ verifyProject(row, JAVA_FILE_BIG_SNAPSHOT_ID, JAVA_FILE_BIG_ID, JAVA_PROJECT_ID);
}
private void verifyJavaTinyFile(MeasureFilterRow row) {
- assertThat(row.getSnapshotId()).isEqualTo(JAVA_FILE_TINY_SNAPSHOT_ID);
- assertThat(row.getResourceId()).isEqualTo(JAVA_FILE_TINY_ID);
- assertThat(row.getResourceRootId()).isEqualTo(JAVA_PROJECT_ID);
+ verifyProject(row, JAVA_FILE_TINY_SNAPSHOT_ID, JAVA_FILE_TINY_ID, JAVA_PROJECT_ID);
}
private void verifyPhpProject(MeasureFilterRow row) {
- assertThat(row.getSnapshotId()).isEqualTo(PHP_SNAPSHOT_ID);
- assertThat(row.getResourceId()).isEqualTo(PHP_PROJECT_ID);
- assertThat(row.getResourceRootId()).isEqualTo(PHP_PROJECT_ID);
+ verifyProject(row, PHP_SNAPSHOT_ID, PHP_PROJECT_ID, PHP_PROJECT_ID);
+ }
+
+ private void verifyProject(MeasureFilterRow row, Long snashotId, Long resourceId, Long resourceRootId) {
+ assertThat(row.getSnapshotId()).isEqualTo(snashotId);
+ assertThat(row.getResourceId()).isEqualTo(resourceId);
+ assertThat(row.getResourceRootId()).isEqualTo(resourceRootId);
}
}
--- /dev/null
+<dataset>
+ <metrics id="5" name="alert_status" val_type="LEVEL" description="Alert" domain="General"
+ short_name="Alert" qualitative="[true]" user_managed="[false]" enabled="[true]" origin="JAV"
+ worst_value="[null]"
+ optimized_best_value="[true]" best_value="[null]" direction="1" hidden="[false]"
+ delete_historical_data="[null]"/>
+
+ <!-- java project -->
+ <projects kee="java_project" long_name="Java project" scope="PRJ" qualifier="TRK" name="Java project"
+ id="1" root_id="[null]"
+ description="[null]" enabled="[true]" language="java" copy_resource_id="[null]" person_id="[null]"
+ created_at="2008-12-19 00:00:00.00"/>
+
+ <snapshots id="101" project_id="1" root_project_id="1" root_snapshot_id="[null]" parent_snapshot_id="[null]"
+ scope="PRJ" qualifier="TRK" path="" depth="0"
+ purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]"
+ period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]"
+ period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ created_at="2008-12-20 00:00:00.00" build_date="2008-12-20 00:00:00.00"
+ version="1.0" status="P" islast="[true]"/>
+
+
+ <!-- alert -->
+ <project_measures id="1001" metric_id="5" value="510" snapshot_id="101"
+ url="[null]" variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]"
+ variation_value_4="[null]" variation_value_5="400"
+ rule_priority="[null]" alert_text="[null]" RULES_CATEGORY_ID="[null]"
+ RULE_ID="[null]" text_value="WARN" tendency="[null]" measure_date="[null]" project_id="[null]"
+ alert_status="[null]" description="[null]" characteristic_id="[null]"/>
+
+ <!-- php project -->
+ <projects kee="php_project" long_name="PHP project" scope="PRJ" qualifier="TRK" name="PHP project"
+ id="10" root_id="[null]"
+ description="[null]" enabled="[true]" language="php" copy_resource_id="[null]" person_id="[null]"
+ created_at="2012-12-12 04:06:00.00"/>
+
+
+ <snapshots id="110" project_id="10" root_project_id="10" root_snapshot_id="[null]" parent_snapshot_id="[null]"
+ scope="PRJ" qualifier="TRK" path="" depth="0"
+ purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]"
+ period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]"
+ period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ created_at="2012-12-13 04:06:00.00" build_date="2012-12-13 04:06:00.00"
+ version="3.0" status="P" islast="[true]"/>
+
+ <!-- alert -->
+ <project_measures id="1010" metric_id="5" value="5000" snapshot_id="110"
+ url="[null]" variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]"
+ variation_value_4="[null]" variation_value_5="[null]"
+ rule_priority="[null]" alert_text="[null]" RULES_CATEGORY_ID="[null]"
+ RULE_ID="[null]" text_value="OK" tendency="[null]" measure_date="[null]" project_id="[null]"
+ alert_status="[null]" description="[null]" characteristic_id="[null]"/>
+
+ <!-- js project -->
+ <projects kee="js_project" long_name="JS project" scope="PRJ" qualifier="TRK" name="JS project"
+ id="20" root_id="[null]"
+ description="[null]" enabled="[true]" language="js" copy_resource_id="[null]" person_id="[null]"
+ created_at="2012-12-12 04:06:00.00"/>
+
+
+ <snapshots id="120" project_id="20" root_project_id="20" root_snapshot_id="[null]" parent_snapshot_id="[null]"
+ scope="PRJ" qualifier="TRK" path="" depth="0"
+ purge_status="[null]" period1_mode="[null]" period1_param="[null]" period1_date="[null]"
+ period2_mode="[null]" period2_param="[null]" period2_date="[null]" period3_mode="[null]"
+ period3_param="[null]" period3_date="[null]" period4_mode="[null]" period4_param="[null]"
+ period4_date="[null]" period5_mode="[null]" period5_param="[null]" period5_date="[null]"
+ created_at="2012-12-13 04:06:00.00" build_date="2012-12-13 04:06:00.00"
+ version="3.0" status="P" islast="[true]"/>
+
+ <!-- alert -->
+ <project_measures id="1020" metric_id="5" value="5000" snapshot_id="120"
+ url="[null]" variation_value_1="[null]" variation_value_2="[null]" variation_value_3="[null]"
+ variation_value_4="[null]" variation_value_5="[null]"
+ rule_priority="[null]" alert_text="[null]" RULES_CATEGORY_ID="[null]"
+ RULE_ID="[null]" text_value="ERROR" tendency="[null]" measure_date="[null]" project_id="[null]"
+ alert_status="[null]" description="[null]" characteristic_id="[null]"/>
+
+
+</dataset>
\ No newline at end of file