]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-5285 Manage type parameter to return coverage for unit tests, integration tests...
authorJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 14 May 2014 12:59:18 +0000 (14:59 +0200)
committerJulien Lancelot <julien.lancelot@sonarsource.com>
Wed, 14 May 2014 12:59:25 +0000 (14:59 +0200)
sonar-server/src/main/java/org/sonar/server/test/CoverageService.java
sonar-server/src/main/java/org/sonar/server/test/ws/CoverageShowAction.java
sonar-server/src/test/java/org/sonar/server/test/CoverageServiceTest.java
sonar-server/src/test/java/org/sonar/server/test/ws/CoverageShowActionTest.java
sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_integration_test.json [new file with mode: 0644]
sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_overall_test.json [new file with mode: 0644]

index 52ff6e42381e184c5ad0936608e2859398a75619..7a723a8e2acdf1455865c676b5776651dbcd47cc 100644 (file)
 
 package org.sonar.server.test;
 
+import com.google.common.collect.Maps;
 import org.sonar.api.ServerComponent;
 import org.sonar.api.measures.CoreMetrics;
 import org.sonar.api.test.MutableTestable;
 import org.sonar.api.test.Testable;
+import org.sonar.api.utils.KeyValueFormat;
 import org.sonar.api.web.UserRole;
 import org.sonar.core.component.SnapshotPerspectives;
 import org.sonar.core.measure.db.MeasureDataDao;
@@ -36,6 +38,10 @@ import java.util.Map;
 
 public class CoverageService implements ServerComponent {
 
+  public enum TYPE {
+    UT, IT, OVERALL
+  }
+
   private final MeasureDataDao measureDataDao;
   private final SnapshotPerspectives snapshotPerspectives;
 
@@ -48,48 +54,58 @@ public class CoverageService implements ServerComponent {
     UserSession.get().checkComponentPermission(UserRole.CODEVIEWER, fileKey);
   }
 
-  /**
-   * Warning - does not check permission
-   */
-  @CheckForNull
-  public String getHitsData(String fileKey) {
-    return findDataFromComponent(fileKey, CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY);
+  public Map<Integer, Integer> getHits(String fileKey, CoverageService.TYPE type) {
+    switch (type) {
+      case IT:
+        return findDataFromComponent(fileKey, CoreMetrics.IT_COVERAGE_LINE_HITS_DATA_KEY);
+      case OVERALL:
+        return findDataFromComponent(fileKey, CoreMetrics.OVERALL_COVERAGE_LINE_HITS_DATA_KEY);
+      default:
+        return findDataFromComponent(fileKey, CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY);
+    }
   }
 
-  /**
-   * Warning - does not check permission
-   */
-  @CheckForNull
-  public String getConditionsData(String fileKey) {
-    return findDataFromComponent(fileKey, CoreMetrics.CONDITIONS_BY_LINE_KEY);
+  public Map<Integer, Integer> getConditions(String fileKey, CoverageService.TYPE type) {
+    switch (type) {
+      case IT:
+        return findDataFromComponent(fileKey, CoreMetrics.IT_CONDITIONS_BY_LINE_KEY);
+      case OVERALL:
+        return findDataFromComponent(fileKey, CoreMetrics.OVERALL_CONDITIONS_BY_LINE_KEY);
+      default:
+        return findDataFromComponent(fileKey, CoreMetrics.CONDITIONS_BY_LINE_KEY);
+    }
   }
 
-  /**
-   * Warning - does not check permission
-   */
-  @CheckForNull
-  public String getCoveredConditionsData(String fileKey) {
-    return findDataFromComponent(fileKey, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY);
+  public Map<Integer, Integer> getCoveredConditions(String fileKey, CoverageService.TYPE type) {
+    switch (type) {
+      case IT:
+        return findDataFromComponent(fileKey, CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE_KEY);
+      case OVERALL:
+        return findDataFromComponent(fileKey, CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE_KEY);
+      default:
+        return findDataFromComponent(fileKey, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY);
+    }
   }
 
   /**
-   * Warning - does not check permission
+   * Test cases is only return for unit tests
    */
-  @CheckForNull
-  public Map<Integer, Integer> getTestCasesByLines(String fileKey) {
-    Testable testable = snapshotPerspectives.as(MutableTestable.class, fileKey);
-    if (testable != null) {
-      return testable.testCasesByLines();
+  public Map<Integer, Integer> getTestCases(String fileKey, CoverageService.TYPE type) {
+    if (TYPE.UT.equals(type)) {
+      Testable testable = snapshotPerspectives.as(MutableTestable.class, fileKey);
+      if (testable != null) {
+        return testable.testCasesByLines();
+      }
     }
-    return null;
+    return Maps.newHashMap();
   }
 
   @CheckForNull
-  private String findDataFromComponent(String fileKey, String metricKey) {
+  private Map<Integer, Integer> findDataFromComponent(String fileKey, String metricKey) {
     MeasureDataDto data = measureDataDao.findByComponentKeyAndMetricKey(fileKey, metricKey);
     if (data != null) {
-      return data.getData();
+      return KeyValueFormat.parseIntInt(data.getData());
     }
-    return null;
+    return Maps.newHashMap();
   }
 }
index cab2d31ef69e082c5ea1537eabc1f0ea138a03f2..e385920a9277aab788747bd58b3a53e37be842b4 100644 (file)
 
 package org.sonar.server.test.ws;
 
-import com.google.common.collect.Maps;
 import com.google.common.io.Resources;
 import org.apache.commons.lang.ObjectUtils;
 import org.sonar.api.server.ws.Request;
 import org.sonar.api.server.ws.RequestHandler;
 import org.sonar.api.server.ws.Response;
 import org.sonar.api.server.ws.WebService;
-import org.sonar.api.utils.KeyValueFormat;
 import org.sonar.api.utils.text.JsonWriter;
 import org.sonar.server.test.CoverageService;
 
@@ -35,6 +33,11 @@ import java.util.Map;
 
 public class CoverageShowAction implements RequestHandler {
 
+  private static final String KEY = "key";
+  private static final String FROM = "from";
+  private static final String TO = "to";
+  private static final String TYPE = "type";
+
   private final CoverageService coverageService;
 
   public CoverageShowAction(CoverageService coverageService) {
@@ -49,54 +52,51 @@ public class CoverageShowAction implements RequestHandler {
       .setHandler(this);
 
     action
-      .createParam("key")
+      .createParam(KEY)
       .setRequired(true)
       .setDescription("File key")
       .setExampleValue("my_project:/src/foo/Bar.php");
 
     action
-      .createParam("from")
+      .createParam(FROM)
       .setDescription("First line to return. Starts at 1")
       .setExampleValue("10")
       .setDefaultValue("1");
 
     action
-      .createParam("to")
+      .createParam(TO)
       .setDescription("Last line to return (inclusive)")
       .setExampleValue("20");
 
     action
-      .createParam("type")
+      .createParam(TYPE)
       .setDescription("Type of coverage info to return :" +
         "<ul>" +
         "<li>UT : Unit Tests</li>" +
         "<li>IT : Integration Tests</li>" +
         "<li>OVERALL : Unit and Integration Tests</li>" +
         "</ul>")
-      .setPossibleValues("UT", "IT", "OVERALL")
-      .setDefaultValue("UT");
+      .setPossibleValues(CoverageService.TYPE.values())
+      .setDefaultValue(CoverageService.TYPE.UT.name());
   }
 
   @Override
   public void handle(Request request, Response response) {
-    String fileKey = request.mandatoryParam("key");
+    String fileKey = request.mandatoryParam(KEY);
     coverageService.checkPermission(fileKey);
 
-    int from = Math.max(request.mandatoryParamAsInt("from"), 1);
-    int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt("to"), Integer.MAX_VALUE);
+    int from = Math.max(request.mandatoryParamAsInt(FROM), 1);
+    int to = (Integer) ObjectUtils.defaultIfNull(request.paramAsInt(TO), Integer.MAX_VALUE);
+    CoverageService.TYPE type = CoverageService.TYPE.valueOf(request.param(TYPE));
 
     JsonWriter json = response.newJsonWriter().beginObject();
 
-    String hits = coverageService.getHitsData(fileKey);
-    Map<Integer, Integer> testCases = coverageService.getTestCasesByLines(fileKey);
-    String conditions = coverageService.getConditionsData(fileKey);
-    String coveredConditions = coverageService.getCoveredConditionsData(fileKey);
-    if (hits != null) {
-      Map<Integer, Integer> hitsByLine = KeyValueFormat.parseIntInt(hits);
-      Map<Integer, Integer> testCasesByLine = testCases != null ? testCases : Maps.<Integer, Integer>newHashMap();
-      Map<Integer, Integer> conditionsByLine = conditions != null ? KeyValueFormat.parseIntInt(conditions) : Maps.<Integer, Integer>newHashMap();
-      Map<Integer, Integer> coveredConditionsByLine = coveredConditions != null ? KeyValueFormat.parseIntInt(coveredConditions) : Maps.<Integer, Integer>newHashMap();
-      writeCoverage(fileKey, hitsByLine, testCasesByLine, conditionsByLine, coveredConditionsByLine, from, to, json);
+    Map<Integer, Integer> hits = coverageService.getHits(fileKey, type);
+    if (!hits.isEmpty()) {
+      Map<Integer, Integer> testCases = coverageService.getTestCases(fileKey, type);
+      Map<Integer, Integer> conditions = coverageService.getConditions(fileKey, type);
+      Map<Integer, Integer> coveredConditions = coverageService.getCoveredConditions(fileKey, type);
+      writeCoverage(fileKey, hits, testCases, conditions, coveredConditions, from, to, json);
     }
 
     json.endObject().close();
index f9d2c3c6906ef8ca50580e0ba60a0e4c3382eb86..9a158755c59b3471eec58d59d015a278a6660dde 100644 (file)
@@ -33,6 +33,8 @@ import org.sonar.core.component.SnapshotPerspectives;
 import org.sonar.core.measure.db.MeasureDataDao;
 import org.sonar.server.user.MockUserSession;
 
+import java.util.Collections;
+
 import static org.fest.assertions.Assertions.assertThat;
 import static org.mockito.Matchers.anyString;
 import static org.mockito.Matchers.eq;
@@ -70,26 +72,44 @@ public class CoverageServiceTest {
 
   @Test
   public void get_hits_data() throws Exception {
-    service.getHitsData(COMPONENT_KEY);
+    service.getHits(COMPONENT_KEY, CoverageService.TYPE.UT);
     verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.COVERAGE_LINE_HITS_DATA_KEY);
+
+    service.getHits(COMPONENT_KEY, CoverageService.TYPE.IT);
+    verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.IT_COVERAGE_LINE_HITS_DATA_KEY);
+
+    service.getHits(COMPONENT_KEY, CoverageService.TYPE.OVERALL);
+    verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.OVERALL_COVERAGE_LINE_HITS_DATA_KEY);
   }
 
   @Test
   public void not_get_hits_data_if_no_data() throws Exception {
     when(measureDataDao.findByComponentKeyAndMetricKey(eq(COMPONENT_KEY), anyString())).thenReturn(null);
-    assertThat(service.getHitsData(COMPONENT_KEY)).isNull();
+    assertThat(service.getHits(COMPONENT_KEY, CoverageService.TYPE.UT)).isEqualTo(Collections.emptyMap());
   }
 
   @Test
   public void get_conditions_data() throws Exception {
-    service.getConditionsData(COMPONENT_KEY);
+    service.getConditions(COMPONENT_KEY, CoverageService.TYPE.UT);
     verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.CONDITIONS_BY_LINE_KEY);
+
+    service.getConditions(COMPONENT_KEY, CoverageService.TYPE.IT);
+    verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.IT_CONDITIONS_BY_LINE_KEY);
+
+    service.getConditions(COMPONENT_KEY, CoverageService.TYPE.OVERALL);
+    verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.OVERALL_CONDITIONS_BY_LINE_KEY);
   }
 
   @Test
-  public void get_coverered_conditions_data() throws Exception {
-    service.getCoveredConditionsData(COMPONENT_KEY);
+  public void get_covered_conditions_data() throws Exception {
+    service.getCoveredConditions(COMPONENT_KEY, CoverageService.TYPE.UT);
     verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY);
+
+    service.getCoveredConditions(COMPONENT_KEY, CoverageService.TYPE.IT);
+    verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.IT_COVERED_CONDITIONS_BY_LINE_KEY);
+
+    service.getCoveredConditions(COMPONENT_KEY, CoverageService.TYPE.OVERALL);
+    verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.OVERALL_COVERED_CONDITIONS_BY_LINE_KEY);
   }
 
   @Test
@@ -97,15 +117,17 @@ public class CoverageServiceTest {
     MutableTestable testable = mock(MutableTestable.class);
     when(snapshotPerspectives.as(MutableTestable.class, COMPONENT_KEY)).thenReturn(testable);
 
-    service.getTestCasesByLines(COMPONENT_KEY);
+    service.getTestCases(COMPONENT_KEY, CoverageService.TYPE.UT);
     verify(testable).testCasesByLines();
+
+    assertThat(service.getTestCases(COMPONENT_KEY, CoverageService.TYPE.IT)).isEmpty();
   }
 
   @Test
   public void not_get_test_cases_by_lines_if_no_testable() throws Exception {
     when(snapshotPerspectives.as(MutableTestable.class, COMPONENT_KEY)).thenReturn(null);
 
-    assertThat(service.getTestCasesByLines(COMPONENT_KEY)).isNull();
+    assertThat(service.getTestCases(COMPONENT_KEY, CoverageService.TYPE.UT)).isEqualTo(Collections.emptyMap());
   }
 
 }
index 5da2ea3b625e9f5c8a931dbd7facc3f0348a4dba..e4c7c881cbddb645e499ae61f105911cedbb0ad9 100644 (file)
@@ -47,12 +47,12 @@ public class CoverageShowActionTest {
   @Test
   public void show_coverage_for_unit_test() throws Exception {
     String fileKey = "src/Foo.java";
-    when(coverageService.getHitsData(fileKey)).thenReturn("1=1;2=1;3=0;4=1;5=1");
-    when(coverageService.getTestCasesByLines(fileKey)).thenReturn(ImmutableMap.of(4, 8, 1, 2));
-    when(coverageService.getConditionsData(fileKey)).thenReturn("2=3;3=2");
-    when(coverageService.getCoveredConditionsData(fileKey)).thenReturn("2=1;3=2");
+    when(coverageService.getHits(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(1, 1, 2, 1, 3, 0, 4, 1, 5 , 1));
+    when(coverageService.getTestCases(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(4, 8, 1, 2));
+    when(coverageService.getConditions(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(2, 3, 3, 2));
+    when(coverageService.getCoveredConditions(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(2, 1, 3, 2));
 
-    WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey);
+    WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey).setParam("type", "UT");
 
     request.execute().assertJson(getClass(), "show_coverage.json");
   }
@@ -60,14 +60,38 @@ public class CoverageShowActionTest {
   @Test
   public void show_coverage_for_unit_test_with_from_and_to() throws Exception {
     String fileKey = "src/Foo.java";
-    when(coverageService.getHitsData(fileKey)).thenReturn("1=1;2=1;3=0;4=1;5=1");
-    when(coverageService.getTestCasesByLines(fileKey)).thenReturn(ImmutableMap.of(4, 8, 1, 2));
-    when(coverageService.getConditionsData(fileKey)).thenReturn("2=3;3=2");
-    when(coverageService.getCoveredConditionsData(fileKey)).thenReturn("2=1;3=2");
+    when(coverageService.getHits(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(1, 1, 2, 1, 3, 0, 4, 1, 5 , 1));
+    when(coverageService.getTestCases(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(4, 8, 1, 2));
+    when(coverageService.getConditions(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(2, 3, 3, 2));
+    when(coverageService.getCoveredConditions(fileKey, CoverageService.TYPE.UT)).thenReturn(ImmutableMap.of(2, 1, 3, 2));
 
-    WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey).setParam("from", "3").setParam("to", "4");
+    WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey).setParam("from", "3").setParam("to", "4").setParam("type", "UT");
 
     request.execute().assertJson(getClass(), "show_coverage_with_from_and_to.json");
   }
 
+  @Test
+  public void show_coverage_for_integration_test() throws Exception {
+    String fileKey = "src/Foo.java";
+    when(coverageService.getHits(fileKey, CoverageService.TYPE.IT)).thenReturn(ImmutableMap.of(1, 1, 2, 1, 3, 0, 4, 1, 5 , 1));
+    when(coverageService.getConditions(fileKey, CoverageService.TYPE.IT)).thenReturn(ImmutableMap.of(2, 3, 3, 2));
+    when(coverageService.getCoveredConditions(fileKey, CoverageService.TYPE.IT)).thenReturn(ImmutableMap.of(2, 1, 3, 2));
+
+    WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey).setParam("type", "IT");
+
+    request.execute().assertJson(getClass(), "show_coverage_for_integration_test.json");
+  }
+
+  @Test
+  public void show_coverage_for_overall_test() throws Exception {
+    String fileKey = "src/Foo.java";
+    when(coverageService.getHits(fileKey, CoverageService.TYPE.OVERALL)).thenReturn(ImmutableMap.of(1, 1, 2, 1, 3, 0, 4, 1, 5 , 1));
+    when(coverageService.getConditions(fileKey, CoverageService.TYPE.OVERALL)).thenReturn(ImmutableMap.of(2, 3, 3, 2));
+    when(coverageService.getCoveredConditions(fileKey, CoverageService.TYPE.OVERALL)).thenReturn(ImmutableMap.of(2, 1, 3, 2));
+
+    WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey).setParam("type", "OVERALL");
+
+    request.execute().assertJson(getClass(), "show_coverage_for_overall_test.json");
+  }
+
 }
diff --git a/sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_integration_test.json b/sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_integration_test.json
new file mode 100644 (file)
index 0000000..e393012
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "coverage": [
+    [1, true, null, null, null],
+    [2, true, null, 3, 1],
+    [3, false, null, 2, 2],
+    [4, true, null, null, null],
+    [5, true, null, null, null]
+  ]
+}
diff --git a/sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_overall_test.json b/sonar-server/src/test/resources/org/sonar/server/test/ws/CoverageShowActionTest/show_coverage_for_overall_test.json
new file mode 100644 (file)
index 0000000..e393012
--- /dev/null
@@ -0,0 +1,9 @@
+{
+  "coverage": [
+    [1, true, null, null, null],
+    [2, true, null, 3, 1],
+    [3, false, null, 2, 2],
+    [4, true, null, null, null],
+    [5, true, null, null, null]
+  ]
+}