import org.sonar.core.graph.GraphUtil;
import java.util.List;
+import java.util.Map;
import java.util.SortedSet;
+import static com.google.common.collect.Maps.newHashMap;
+
public class DefaultTestable extends BeanVertex implements MutableTestable {
private static final String COVERS = "covers";
return number;
}
+ public Map<Integer, Integer> testCasesByLines() {
+ Map<Integer, Integer> testCasesByLines = newHashMap();
+ for (Integer line : testedLines()) {
+ testCasesByLines.put(line, countTestCasesOfLine(line));
+ }
+ return testCasesByLines;
+ }
+
public List<TestCase> testCasesOfLine(int line) {
ImmutableList.Builder<TestCase> cases = ImmutableList.builder();
for (Edge edge : coverEdges()) {
*/
package org.sonar.core.test;
+import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables;
import org.junit.Test;
import org.sonar.api.component.mock.MockSourceFile;
import static org.fest.assertions.Assertions.assertThat;
public class DefaultTestableTest {
+
@Test
- public void should_not_have_tested_lines() {
+ public void not_have_tested_lines() {
BeanGraph beanGraph = BeanGraph.createInMemory();
DefaultTestable testable = beanGraph.createVertex(DefaultTestable.class);
}
@Test
- public void should_have_tested_lines() {
+ public void tested_lines() {
BeanGraph beanGraph = BeanGraph.createInMemory();
DefaultTestable testable = beanGraph.createVertex(DefaultTestable.class);
}
@Test
- public void should_get_test_cases() {
+ public void get_test_cases() {
BeanGraph beanGraph = BeanGraph.createInMemory();
DefaultTestable testable = beanGraph.createVertex(DefaultTestable.class);
}
@Test
- public void should_get_test_case_by_key() {
+ public void get_test_case_by_key() {
BeanGraph beanGraph = BeanGraph.createInMemory();
DefaultTestPlan plan = beanGraph.createVertex(DefaultTestPlan.class);
}
@Test
- public void should_return_cover_of_testCase(){
+ public void return_cover_of_testCase(){
BeanGraph beanGraph = BeanGraph.createInMemory();
ScanGraph graph = ScanGraph.create();
assertThat(testable1.coverageBlock(testCase).testable()).isEqualTo(testable1);
assertThat(testable2.coverageBlock(testCase)).isNull();
}
+
+ @Test
+ public void test_cases_by_lines() {
+ BeanGraph beanGraph = BeanGraph.createInMemory();
+
+ DefaultTestable testable = beanGraph.createVertex(DefaultTestable.class);
+ DefaultTestCase testCase1 = beanGraph.createVertex(DefaultTestCase.class);
+ testCase1.setCoverageBlock(testable, Arrays.asList(10, 11, 12));
+ DefaultTestCase testCase2 = beanGraph.createVertex(DefaultTestCase.class);
+ testCase2.setCoverageBlock(testable, Arrays.asList(12, 48, 49));
+
+ assertThat(testable.testCasesByLines()).isEqualTo(ImmutableMap.of(49, 1, 48, 1, 10, 1, 11, 1, 12, 2));
+ }
}
import org.sonar.api.component.Perspective;
import java.util.List;
+import java.util.Map;
import java.util.SortedSet;
public interface Testable extends Perspective {
int countTestCasesOfLine(Integer line);
+ Map<Integer, Integer> testCasesByLines();
+
List<TestCase> testCasesOfLine(int line);
SortedSet<Integer> testedLines();
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.web.UserRole;
+import org.sonar.core.component.SnapshotPerspectives;
import org.sonar.core.measure.db.MeasureDataDao;
import org.sonar.core.measure.db.MeasureDataDto;
import org.sonar.server.user.UserSession;
import javax.annotation.CheckForNull;
+import java.util.Map;
+
public class CoverageService implements ServerComponent {
private final MeasureDataDao measureDataDao;
+ private final SnapshotPerspectives snapshotPerspectives;
- public CoverageService(MeasureDataDao measureDataDao) {
+ public CoverageService(MeasureDataDao measureDataDao, SnapshotPerspectives snapshotPerspectives) {
this.measureDataDao = measureDataDao;
+ this.snapshotPerspectives = snapshotPerspectives;
}
public void checkPermission(String fileKey) {
return findDataFromComponent(fileKey, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY);
}
+ /**
+ * Warning - does not check permission
+ */
+ @CheckForNull
+ public Map<Integer, Integer> getCoveredLines(String fileKey) {
+ Testable testable = snapshotPerspectives.as(MutableTestable.class, fileKey);
+ if (testable != null) {
+ return testable.testCasesByLines();
+ }
+ return null;
+ }
+
@CheckForNull
private String findDataFromComponent(String fileKey, String metricKey) {
MeasureDataDto data = measureDataDao.findByComponentKeyAndMetricKey(fileKey, metricKey);
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.server.test.CoverageService;
+import javax.annotation.Nullable;
+
import java.util.Map;
public class CoverageShowAction implements RequestHandler {
JsonWriter json = response.newJsonWriter().beginObject();
String hits = coverageService.getHitsData(fileKey);
+ Map<Integer, Integer> coveredLines = coverageService.getCoveredLines(fileKey);
if (hits != null) {
- Map<Integer, Integer> hitsByLine = KeyValueFormat.parseIntInt(hits);
- writeCoverage(hitsByLine, from, to, json);
+ Map<Integer, Integer> hitsByLine = KeyValueFormat.parseIntInt(hits);
+ writeCoverage(fileKey, hitsByLine, coveredLines, from, to, json);
}
json.endObject().close();
}
- private void writeCoverage(Map<Integer, Integer> hitsByLine, int from, int to, JsonWriter json) {
+ private void writeCoverage(String fileKey, Map<Integer, Integer> hitsByLine, @Nullable Map<Integer, Integer> coveredLines, int from, int to, JsonWriter json) {
json.name("coverage").beginArray();
for (Map.Entry<Integer, Integer> entry : hitsByLine.entrySet()) {
Integer line = entry.getKey();
json.beginArray();
json.value(line);
json.value(hits > 0);
+ json.value(coveredLines != null ? coveredLines.get(line) : null);
json.endArray();
}
}
import org.mockito.Mock;
import org.mockito.runners.MockitoJUnitRunner;
import org.sonar.api.measures.CoreMetrics;
+import org.sonar.api.test.MutableTestable;
import org.sonar.api.web.UserRole;
+import org.sonar.core.component.SnapshotPerspectives;
import org.sonar.core.measure.db.MeasureDataDao;
import org.sonar.server.user.MockUserSession;
import static org.fest.assertions.Assertions.assertThat;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
-import static org.mockito.Mockito.verify;
-import static org.mockito.Mockito.when;
+import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
public class CoverageServiceTest {
@Mock
MeasureDataDao measureDataDao;
+ @Mock
+ SnapshotPerspectives snapshotPerspectives;
+
static final String COMPONENT_KEY = "org.sonar.sample:Sample";
CoverageService service;
@Before
public void setUp() throws Exception {
- service = new CoverageService(measureDataDao);
+ service = new CoverageService(measureDataDao, snapshotPerspectives);
}
@Test
verify(measureDataDao).findByComponentKeyAndMetricKey(COMPONENT_KEY, CoreMetrics.COVERED_CONDITIONS_BY_LINE_KEY);
}
+ @Test
+ public void get_coverered_lines() throws Exception {
+ MutableTestable testable = mock(MutableTestable.class);
+ when(snapshotPerspectives.as(MutableTestable.class, COMPONENT_KEY)).thenReturn(testable);
+
+ service.getCoveredLines(COMPONENT_KEY);
+ verify(testable).testCasesByLines();
+ }
+
+ @Test
+ public void not_get_coverered_lines_if_no_testable() throws Exception {
+ when(snapshotPerspectives.as(MutableTestable.class, COMPONENT_KEY)).thenReturn(null);
+
+ assertThat(service.getCoveredLines(COMPONENT_KEY)).isNull();
+ }
+
}
package org.sonar.server.test.ws;
+import com.google.common.collect.ImmutableMap;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
public void show_coverage() throws Exception {
String fileKey = "src/Foo.java";
when(coverageService.getHitsData(fileKey)).thenReturn("1=1;2=1;3=0;4=1;5=1");
+ when(coverageService.getCoveredLines(fileKey)).thenReturn(ImmutableMap.of(4, 8, 1, 2));
WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey);
public void show_coverage_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.getCoveredLines(fileKey)).thenReturn(ImmutableMap.of(4, 8, 1, 2));
WsTester.TestRequest request = tester.newGetRequest("api/coverage", "show").setParam("key", fileKey).setParam("from", "3").setParam("to", "4");
{
"coverage": [
- [1, true],
- [2, true],
- [3, false],
- [4, true],
- [5, true]
+ [1, true, 2],
+ [2, true, null],
+ [3, false, null],
+ [4, true, 8],
+ [5, true, null]
]
}
{
"coverage": [
- [3, false],
- [4, true]
+ [3, false, null],
+ [4, true, 8],
]
}