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;
public class CoverageService implements ServerComponent {
+ public enum TYPE {
+ UT, IT, OVERALL
+ }
+
private final MeasureDataDao measureDataDao;
private final SnapshotPerspectives snapshotPerspectives;
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();
}
}
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;
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) {
.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();
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;
@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
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());
}
}
@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");
}
@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");
+ }
+
}
--- /dev/null
+{
+ "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]
+ ]
+}
--- /dev/null
+{
+ "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]
+ ]
+}