]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7800 add override of MeasureDao#selectByQuery with ResultHandler
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Wed, 6 Jul 2016 14:06:03 +0000 (16:06 +0200)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 7 Jul 2016 15:07:00 +0000 (17:07 +0200)
sonar-db/src/main/java/org/sonar/db/measure/MeasureDao.java
sonar-db/src/main/java/org/sonar/db/measure/MeasureMapper.java
sonar-db/src/test/java/org/sonar/db/measure/MeasureDaoTest.java

index a50dfb98672a96324734c7667ed070e26df8d2e0..0536a8389d6a57307fde7ddf952210892f9a185d 100644 (file)
@@ -25,10 +25,12 @@ import java.util.Collection;
 import java.util.Collections;
 import java.util.List;
 import java.util.Optional;
+import org.apache.ibatis.session.ResultHandler;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 
 import static org.sonar.db.DatabaseUtils.executeLargeInputs;
+import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput;
 
 public class MeasureDao implements Dao {
 
@@ -63,6 +65,23 @@ public class MeasureDao implements Dao {
     });
   }
 
+  public void selectByQuery(DbSession dbSession, MeasureQuery query, ResultHandler resultHandler) {
+    if (query.returnsEmpty()) {
+      return;
+    }
+    if (query.getComponentUuids() == null) {
+      mapper(dbSession).selectByQuery(query, resultHandler);
+    } else {
+      executeLargeInputsWithoutOutput(
+        query.getComponentUuids(),
+        componentUuids -> {
+          MeasureQuery pageQuery = MeasureQuery.copyWithSubsetOfComponentUuids(query, componentUuids);
+          mapper(dbSession).selectByQuery(pageQuery, resultHandler);
+          return null;
+        });
+    }
+  }
+
   public List<PastMeasureDto> selectPastMeasures(DbSession dbSession,
     String componentUuid,
     String analysisUuid,
index a1bc7f9448d893da5b94ad40e2d9f49d903b86fb..68cfe0c20501b218af0d36859b40abc7d101038f 100644 (file)
@@ -22,11 +22,14 @@ package org.sonar.db.measure;
 import java.util.Collection;
 import java.util.List;
 import org.apache.ibatis.annotations.Param;
+import org.apache.ibatis.session.ResultHandler;
 
 public interface MeasureMapper {
 
   List<MeasureDto> selectByQuery(@Param("query") MeasureQuery query);
 
+  void selectByQuery(@Param("query") MeasureQuery query, ResultHandler resultHandler);
+
   List<PastMeasureDto> selectPastMeasures(@Param("componentUuid") String componentUuid, @Param("analysisUuid") String analysisUuid, @Param("metricIds") List<Integer> metricIds);
 
   List<MeasureDto> selectProjectMeasuresOfDeveloper(@Param("developerId") long developerId, @Param("metricIds") Collection<Integer> metricIds);
index b0f6db7870d9a5d07f20f47a8ba0c37443d3dad5..9437e42b3e4cb4a0c57758597a5e7805ce112986 100644 (file)
@@ -19,6 +19,7 @@
  */
 package org.sonar.db.measure;
 
+import java.util.ArrayList;
 import java.util.List;
 import java.util.Optional;
 import org.junit.Rule;
@@ -157,6 +158,73 @@ public class MeasureDaoTest {
     verifyMeasures(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID).setPersonId(A_PERSON_ID), "M4");
   }
 
+  @Test
+  public void selectByQuery_with_handler() {
+    insertAnalysis(LAST_ANALYSIS_UUID, true);
+    insertAnalysis(OTHER_ANALYSIS_UUID, false);
+    // component C1
+    insertMeasure("M1", OTHER_ANALYSIS_UUID, "C1", NCLOC_METRIC_ID);
+    insertMeasure("M2", LAST_ANALYSIS_UUID, "C1", NCLOC_METRIC_ID);
+    insertMeasure("M3", LAST_ANALYSIS_UUID, "C1", COVERAGE_METRIC_ID);
+    insertMeasureOnPerson("M4", LAST_ANALYSIS_UUID, "C1", NCLOC_METRIC_ID, A_PERSON_ID);
+    insertMeasureOnPerson("M5", OTHER_ANALYSIS_UUID, "C1", NCLOC_METRIC_ID, 123L);
+    // component C2
+    insertMeasure("M6", LAST_ANALYSIS_UUID, "C2", NCLOC_METRIC_ID);
+    db.commit();
+
+    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuids(emptyList()));
+    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("MISSING_COMPONENT"));
+
+    // all measures of component C1 of last analysis
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1"), "M2", "M3");
+    // all measures of component C1 of non last analysis
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(OTHER_ANALYSIS_UUID), "M1");
+    // all measures of component C1 of last analysis by UUID
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID), "M2", "M3");
+
+    // ncloc measure of component C1 of last analysis
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setMetricId(NCLOC_METRIC_ID), "M2");
+    // ncloc measure of component C1 of non last analysis
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(OTHER_ANALYSIS_UUID).setMetricId(NCLOC_METRIC_ID), "M1");
+    // ncloc measure of component C1 of last analysis by UUID
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID).setMetricId(NCLOC_METRIC_ID), "M2");
+
+    // multiple measures of component C1 of last analysis
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setMetricIds(asList(NCLOC_METRIC_ID, COVERAGE_METRIC_ID)), "M2", "M3");
+    // multiple measures of component C1 of non last analysis
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(OTHER_ANALYSIS_UUID).setMetricIds(asList(NCLOC_METRIC_ID, COVERAGE_METRIC_ID)), "M1");
+    // multiple measures of component C1 of last analysis by UUID
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID).setMetricIds(asList(NCLOC_METRIC_ID, COVERAGE_METRIC_ID)), "M2", "M3");
+
+    // missing measure of component C1 of last analysis
+    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setMetricId(COMPLEXITY_METRIC_ID));
+    // missing measure of component C1 of non last analysis
+    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(OTHER_ANALYSIS_UUID).setMetricId(COMPLEXITY_METRIC_ID));
+    // missing measure of component C1 of last analysis by UUID
+    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID).setMetricId(COMPLEXITY_METRIC_ID));
+
+    // ncloc measures of components C1, C2 and C3 (which does not exist) of last analysis
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuids(asList("C1", "C2", "C3")), "M2", "M3", "M6");
+    // ncloc measures of components C1, C2 and C3 (which does not exist) of non last analysis
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuids(asList("C1", "C2", "C3")).setAnalysisUuid(OTHER_ANALYSIS_UUID), "M1");
+    // ncloc measures of components C1, C2 and C3 (which does not exist) of last analysis by UUID
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuids(asList("C1", "C2", "C3")).setAnalysisUuid(LAST_ANALYSIS_UUID), "M2", "M3", "M6");
+
+    // measures of missing developer of component C1 of last analysis
+    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setPersonId(123L));
+    // measures of missing developer of component C1 of non last analysis
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(OTHER_ANALYSIS_UUID).setPersonId(123L), "M5");
+    // measures of missing developer of component C1 of last analysis by UUID
+    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID).setPersonId(123L));
+
+    // developer measures of component C1 of last analysis
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setPersonId(A_PERSON_ID), "M4");
+    // developer measures of component C1 of non last analysis
+    verifyZeroMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(OTHER_ANALYSIS_UUID).setPersonId(A_PERSON_ID));
+    // developer measures of component C1 of last analysis by UUID
+    verifyMeasuresWithHandler(MeasureQuery.builder().setComponentUuid("C1").setAnalysisUuid(LAST_ANALYSIS_UUID).setPersonId(A_PERSON_ID), "M4");
+  }
+
   @Test
   public void selectSingle() {
     insertAnalysis(LAST_ANALYSIS_UUID, true);
@@ -185,10 +253,28 @@ public class MeasureDaoTest {
     assertThat(measures).extracting(MeasureDto::getData).containsOnly(expectedIds);
   }
 
+  private void verifyMeasuresWithHandler(MeasureQuery.Builder query, String... expectedIds) {
+    List<MeasureDto> measures = getMeasuresWithHandler(query);
+    assertThat(measures).extracting(MeasureDto::getData).containsOnly(expectedIds);
+  }
+
   private void verifyZeroMeasures(MeasureQuery.Builder query) {
     assertThat(underTest.selectByQuery(db.getSession(), query.build())).isEmpty();
   }
 
+  private void verifyZeroMeasuresWithHandler(MeasureQuery.Builder query) {
+    List<MeasureDto> measures = getMeasuresWithHandler(query);
+    assertThat(measures).isEmpty();
+  }
+
+  private List<MeasureDto> getMeasuresWithHandler(MeasureQuery.Builder query) {
+    List<MeasureDto> measures = new ArrayList<>();
+    underTest.selectByQuery(db.getSession(), query.build(), resultContext ->
+      measures.add((MeasureDto) resultContext.getResultObject())
+    );
+    return measures;
+  }
+
   private void insertMeasure(String id, String analysisUuid, String componentUuid, int metricId) {
     MeasureDto measure = MeasureTesting.newMeasure()
       .setAnalysisUuid(analysisUuid)