@Before
public void before() {
executor = new MeasureFilterExecutor(getMyBatis(), getDatabase(), new ResourceDao(getMyBatis()));
- setupData("shared");
}
@Test
public void should_return_empty_results_if_empty_filter() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter();
assertThat(filter.isEmpty()).isTrue();
@Test
public void invalid_filter_should_not_return_results() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setUserFavourites(true);
// anonymous user does not have favourites
assertThat(executor.execute(filter, new MeasureFilterContext())).isEmpty();
@Test
public void filter_is_not_valid_if_missing_base_snapshot() {
+ setupData("shared");
MeasureFilterContext context = new MeasureFilterContext();
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setOnBaseResourceChildren(true);
assertThat(MeasureFilterExecutor.isValid(filter, context)).isFalse();
@Test
public void filter_is_not_valid_if_condition_on_unknown_metric() {
+ setupData("shared");
MeasureFilterContext context = new MeasureFilterContext();
MeasureFilter filter = new MeasureFilter().addCondition(new MeasureFilterCondition(null, MeasureFilterCondition.Operator.LESS, 3.0));
assertThat(MeasureFilterExecutor.isValid(filter, context)).isFalse();
@Test
public void filter_is_not_valid_if_sorting_on_unknown_metric() {
+ setupData("shared");
MeasureFilterContext context = new MeasureFilterContext();
MeasureFilter filter = new MeasureFilter().setSortOnMetric(null);
assertThat(MeasureFilterExecutor.isValid(filter, context)).isFalse();
@Test
public void filter_is_not_valid_if_anonymous_favourites() {
+ setupData("shared");
MeasureFilterContext context = new MeasureFilterContext();
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setUserFavourites(true);
assertThat(MeasureFilterExecutor.isValid(filter, context)).isFalse();
@Test
public void projects_without_measure_conditions() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setSortOn(MeasureFilterSort.Field.LANGUAGE);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void test_default_sort() {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA"));
assertThat(filter.sort().isAsc()).isTrue();
@Test
public void sort_by_ascending_resource_name() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA")).setSortAsc(true);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void sort_by_ascending_resource_key() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA")).setSortAsc(true).setSortOn(MeasureFilterSort.Field.KEY);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void sort_by_ascending_resource_version() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setSortAsc(true).setSortOn(MeasureFilterSort.Field.VERSION);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void sort_by_descending_resource_name() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA")).setSortAsc(false);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void sort_by_ascending_text_measure() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setSortOnMetric(METRIC_PROFILE);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void sort_by_descending_text_measure() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setSortOnMetric(METRIC_PROFILE).setSortAsc(false);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void sort_by_missing_text_measure() throws SQLException {
+ setupData("shared");
// the metric 'profile' is not set on files
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA")).setSortOnMetric(METRIC_PROFILE);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void sort_by_ascending_numeric_measure() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA")).setSortOnMetric(METRIC_LINES);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void sort_by_descending_numeric_measure() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA")).setSortOnMetric(METRIC_LINES).setSortAsc(false);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void null_measures_are_ordered_after_descending_numeric_measures() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK"))
.setSortOnMetric(METRIC_COVERAGE).setSortAsc(false);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
}
@Test
- public void null_measures_are_ordered_after_ascending_numeric_measures() throws SQLException {
- MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK"))
- .setSortOnMetric(METRIC_COVERAGE).setSortAsc(true);
- List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
+ public void null_measures_are_ordered_after_ascending_numeric_measures() throws SQLException {
+ setupData("shared");
+ MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK"))
+ .setSortOnMetric(METRIC_COVERAGE).setSortAsc(true);
+ List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
- // Java project has coverage but not PHP
- assertThat(rows).hasSize(2);
- verifyJavaProject(rows.get(0));
- verifyPhpProject(rows.get(1));
- }
+ // Java project has coverage but not PHP
+ assertThat(rows).hasSize(2);
+ verifyJavaProject(rows.get(0));
+ verifyPhpProject(rows.get(1));
+ }
@Test
public void sort_by_missing_numeric_measure() throws SQLException {
+ setupData("shared");
// coverage measures are not computed
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA")).setSortOnMetric(METRIC_UNKNOWN);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void sort_by_ascending_variation() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setSortOnMetric(METRIC_LINES).setSortOnPeriod(5);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void sort_by_descending_variation() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK"))
.setSortOnMetric(METRIC_LINES).setSortOnPeriod(5).setSortAsc(false);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void sort_by_ascending_date() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setSortOn(MeasureFilterSort.Field.DATE);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void sort_by_descending_date() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setSortOn(MeasureFilterSort.Field.DATE).setSortAsc(false);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void condition_on_numeric_measure() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA"))
.setSortOnMetric(METRIC_LINES)
.addCondition(new MeasureFilterCondition(METRIC_LINES, MeasureFilterCondition.Operator.GREATER, 200));
@Test
public void condition_on_measure_variation() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK"))
.setSortOnMetric(METRIC_LINES)
.addCondition(new MeasureFilterCondition(METRIC_LINES, MeasureFilterCondition.Operator.GREATER, 1000).setPeriod(5));
@Test
public void multiple_conditions_on_numeric_measures() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA"))
.setSortOnMetric(METRIC_LINES)
.addCondition(new MeasureFilterCondition(METRIC_LINES, MeasureFilterCondition.Operator.GREATER, 2))
@Test
public void filter_by_language() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setResourceLanguages(Arrays.asList("java", "cobol"));
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void filter_by_min_date() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setFromDate(DateUtils.parseDate("2012-12-13"));
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void filter_by_range_of_dates() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK"))
.setFromDate(DateUtils.parseDate("2007-01-01"))
.setToDate(DateUtils.parseDate("2010-01-01"));
@Test
public void filter_by_resource_name() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setResourceName("PHP Proj");
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void filter_by_resource_key_star_regexp() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setResourceKeyRegexp("java*");
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void filter_by_resource_key_exclamation_mark() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).setResourceKeyRegexp("JaV?_proje*");
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
assertThat(rows).hasSize(1);
@Test
public void filter_by_base_resource() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("CLA")).setBaseResourceKey("java_project");
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void filter_by_parent_resource() throws SQLException {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setBaseResourceKey("java_project").setOnBaseResourceChildren(true);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void filter_by_parent_without_children() throws Exception {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK", "PAC", "CLA")).setBaseResourceKey("java_project:org.sonar.foo.Big").setOnBaseResourceChildren(true);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
@Test
public void filter_by_user_favourites() throws Exception {
+ setupData("shared");
MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK", "FIL")).setUserFavourites(true);
List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext().setUserId(50L));
verifyPhpProject(rows.get(1));
}
+ @Test
+ public void ignore_person_measures() throws Exception {
+ setupData("ignore_person_measures");
+ MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).addCondition(
+ new MeasureFilterCondition(new Metric("ncloc").setId(1), MeasureFilterCondition.Operator.GREATER, 0.0)
+ );
+ List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext().setUserId(50L));
+
+ assertThat(rows).hasSize(1);
+ assertThat(rows.get(0).getSnapshotId()).isEqualTo(101L);
+ }
+
+ @Test
+ public void ignore_quality_model_measures() throws Exception {
+ setupData("ignore_quality_model_measures");
+ MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK")).addCondition(
+ new MeasureFilterCondition(new Metric("ncloc").setId(1), MeasureFilterCondition.Operator.GREATER, 0.0)
+ );
+ List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext().setUserId(50L));
+
+ assertThat(rows).hasSize(1);
+ assertThat(rows.get(0).getSnapshotId()).isEqualTo(101L);
+ }
+
private void verifyJavaProject(MeasureFilterRow row) {
assertThat(row.getSnapshotId()).isEqualTo(JAVA_PROJECT_SNAPSHOT_ID);
assertThat(row.getResourceId()).isEqualTo(JAVA_PROJECT_ID);
--- /dev/null
+<dataset>
+ <metrics id="1" name="lines" val_type="FLOAT" description="Lines" domain="Size"
+ short_name="Lines" qualitative="[false]" user_managed="[false]" enabled="[true]" origin="JAV" worst_value="[null]"
+ optimized_best_value="[null]" best_value="[null]" direction="1" hidden="[false]"
+ delete_historical_data="[null]"/>
+
+ <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]"/>
+
+ <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]"/>
+
+
+ <!-- standard measure -->
+ <project_measures id="1001" metric_id="1" value="500" snapshot_id="101" person_id="[null]"
+ 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="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+ alert_status="[null]" description="[null]" characteristic_id="[null]"/>
+
+ <!-- details of the measure by person -->
+ <project_measures id="1002" metric_id="1" value="300" snapshot_id="101" person_id="30000"
+ 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="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+ alert_status="[null]" description="[null]" characteristic_id="[null]"/>
+
+ <project_measures id="1003" metric_id="1" value="200" snapshot_id="101" person_id="40000"
+ 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="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+ alert_status="[null]" description="[null]" characteristic_id="[null]"/>
+
+</dataset>
\ No newline at end of file
--- /dev/null
+<dataset>
+ <metrics id="1" name="lines" val_type="FLOAT" description="Lines" domain="Size"
+ short_name="Lines" qualitative="[false]" user_managed="[false]" enabled="[true]" origin="JAV" worst_value="[null]"
+ optimized_best_value="[null]" best_value="[null]" direction="1" hidden="[false]"
+ delete_historical_data="[null]"/>
+
+ <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]"/>
+
+ <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]"/>
+
+
+ <!-- standard measure -->
+ <project_measures id="1001" metric_id="1" value="500" snapshot_id="101" characteristic_id="[null]"
+ 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="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+ alert_status="[null]" description="[null]" person_id="[null]"/>
+
+ <!-- details of the measure by model characteristic -->
+ <project_measures id="1002" metric_id="1" value="300" snapshot_id="101" characteristic_id="30000"
+ 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="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+ alert_status="[null]" description="[null]" person_id="[null]"/>
+
+ <project_measures id="1003" metric_id="1" value="200" snapshot_id="101" characteristic_id="40000"
+ 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="[null]" tendency="[null]" measure_date="[null]" project_id="[null]"
+ alert_status="[null]" description="[null]" person_id="[null]"/>
+
+</dataset>
\ No newline at end of file