]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-3825 display null values at the end of results, whatever the asc/desc sorting
authorSimon Brandhof <simon.brandhof@gmail.com>
Mon, 10 Dec 2012 22:06:29 +0000 (23:06 +0100)
committerSimon Brandhof <simon.brandhof@gmail.com>
Mon, 10 Dec 2012 22:06:29 +0000 (23:06 +0100)
sonar-core/src/main/java/org/sonar/core/measure/MeasureFilterSql.java
sonar-core/src/test/java/org/sonar/core/measure/MeasureFilterExecutorTest.java

index e1ec30a4291fdacedb37d834b65be1ce0cbd6e8f..54b0d00a0074e1272b51c057fd38bd38581dbf23 100644 (file)
@@ -99,8 +99,11 @@ class MeasureFilterSql {
       }
     }
     if (filter.sort().isSortedByDatabase()) {
-      sql.append(" ORDER BY sortval");
-      sql.append(filter.sort().isAsc() ? " ASC " : " DESC ");
+      if (filter.sort().isAsc()) {
+        sql.append(" ORDER BY sortval");
+      } else {
+        sql.append(" ORDER BY CASE WHEN max(sortval) IS NULL THEN 1 ELSE 0 END, sortval DESC");
+      }
     }
   }
 
index 3bc247897a21960816a8a2e92279fc1e34c5f189..3416b62fa8ec1f1b19e21e590eb27e48226ddb53 100644 (file)
@@ -218,6 +218,30 @@ public class MeasureFilterExecutorTest extends AbstractDaoTestCase {
     verifyJavaTinyFile(rows.get(1));
   }
 
+  @Test
+  public void null_measures_are_ordered_after_descending_numeric_measures() throws SQLException {
+    MeasureFilter filter = new MeasureFilter().setResourceQualifiers(Arrays.asList("TRK"))
+      .setSortOnMetric(METRIC_COVERAGE).setSortAsc(false);
+    List<MeasureFilterRow> rows = executor.execute(filter, new MeasureFilterContext());
+
+    // Java has coverage but not PHP
+    assertThat(rows).hasSize(2);
+    verifyJavaProject(rows.get(0));
+    verifyPhpProject(rows.get(1));
+  }
+
+  @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());
+
+      // Java 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 {
     // coverage measures are not computed