*/
package org.sonar.plugins.core.sensors;
+import org.sonar.api.CoreProperties;
import org.sonar.api.batch.Decorator;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependedUpon;
+import org.sonar.api.config.Settings;
+import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
+import org.sonar.api.measures.MeasureUtils;
import org.sonar.api.measures.Metric;
import org.sonar.api.resources.Project;
import org.sonar.api.resources.Resource;
public abstract class AbstractCoverageDecorator implements Decorator {
+ private Settings settings;
+
+ public AbstractCoverageDecorator(Settings settings) {
+ this.settings = settings;
+ }
+
+ protected boolean isCoverageComputationForced() {
+ return !settings.getBoolean(CoreProperties.COVERAGE_UNFORCED_KEY);
+ }
+
+ protected long getLinesToCover(DecoratorContext context) {
+ Measure linesToCover = context.getMeasure(CoreMetrics.LINES_TO_COVER);
+ if (MeasureUtils.hasValue(linesToCover)) {
+ return linesToCover.getValue().longValue();
+ } else {
+ if (isCoverageComputationForced()) {
+ // SONAR-5772 as an approximation we consider that every ncloc is a line to cover
+ return MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.NCLOC), 0L);
+ } else {
+ return 0L;
+ }
+ }
+ }
+
@Override
public boolean shouldExecuteOnProject(Project project) {
return true;
}
protected boolean shouldDecorate(final Resource resource) {
- return !ResourceUtils.isUnitTestClass(resource);
+ return !ResourceUtils.isUnitTestFile(resource);
}
private void computeMeasure(DecoratorContext context) {
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.MeasureUtils;
import org.sonar.api.measures.Metric;
import java.util.Collection;
public final class BranchCoverageDecorator extends AbstractCoverageDecorator {
+
+ public BranchCoverageDecorator(Settings settings) {
+ super(settings);
+ }
+
@DependsUpon
public Collection<Metric> dependsUponMetrics() {
return CoverageConstants.BRANCH_COVERAGE_METRICS;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.MeasureUtils;
import org.sonar.api.measures.Metric;
import java.util.Collection;
public final class CoverageDecorator extends AbstractCoverageDecorator {
+
+ public CoverageDecorator(Settings settings) {
+ super(settings);
+ }
+
@DependsUpon
public Collection<Metric> usedMetrics() {
return CoverageConstants.COVERAGE_METRICS;
@Override
protected Long countElements(DecoratorContext context) {
- long lines = MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.LINES_TO_COVER), 0L);
+ long linesToCover = getLinesToCover(context);
long conditions = MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.CONDITIONS_TO_COVER), 0L);
- return lines + conditions;
+ return linesToCover + conditions;
}
@Override
import com.google.common.collect.ImmutableList;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.MeasureUtils;
import org.sonar.api.measures.Metric;
public final class ItBranchCoverageDecorator extends AbstractCoverageDecorator {
+ public ItBranchCoverageDecorator(Settings settings) {
+ super(settings);
+ }
+
@DependsUpon
public List<Metric> dependsUponMetrics() {
return ImmutableList.<Metric>of(CoreMetrics.IT_UNCOVERED_CONDITIONS, CoreMetrics.IT_CONDITIONS_TO_COVER,
import com.google.common.collect.ImmutableList;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.MeasureUtils;
import org.sonar.api.measures.Metric;
import java.util.Collection;
public final class ItCoverageDecorator extends AbstractCoverageDecorator {
+
+ public ItCoverageDecorator(Settings settings) {
+ super(settings);
+ }
+
@DependsUpon
public Collection<Metric> usedMetrics() {
return ImmutableList.<Metric>of(CoreMetrics.IT_LINES_TO_COVER, CoreMetrics.IT_UNCOVERED_LINES, CoreMetrics.NEW_IT_LINES_TO_COVER,
import com.google.common.collect.ImmutableList;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.MeasureUtils;
import org.sonar.api.measures.Metric;
import java.util.List;
public final class ItLineCoverageDecorator extends AbstractCoverageDecorator {
+
+ public ItLineCoverageDecorator(Settings settings) {
+ super(settings);
+ }
+
@DependsUpon
public List<Metric> dependsUponMetrics() {
return ImmutableList.<Metric>of(CoreMetrics.IT_UNCOVERED_LINES, CoreMetrics.IT_LINES_TO_COVER, CoreMetrics.NEW_IT_UNCOVERED_LINES,
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.MeasureUtils;
import org.sonar.api.measures.Metric;
import java.util.Collection;
public final class LineCoverageDecorator extends AbstractCoverageDecorator {
+
+ public LineCoverageDecorator(Settings settings) {
+ super(settings);
+ }
+
@DependsUpon
public Collection<Metric> dependsUponMetrics() {
return CoverageConstants.LINE_COVERAGE_METRICS;
@Override
protected Long countElements(DecoratorContext context) {
- return MeasureUtils.getValueAsLong(context.getMeasure(CoreMetrics.LINES_TO_COVER), 0L);
+ return getLinesToCover(context);
}
@Override
import com.google.common.collect.ImmutableList;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.MeasureUtils;
import org.sonar.api.measures.Metric;
import java.util.List;
public final class OverallBranchCoverageDecorator extends AbstractCoverageDecorator {
+
+ public OverallBranchCoverageDecorator(Settings settings) {
+ super(settings);
+ }
+
@DependsUpon
public List<Metric> dependsUponMetrics() {
return ImmutableList.<Metric>of(CoreMetrics.OVERALL_UNCOVERED_CONDITIONS, CoreMetrics.OVERALL_CONDITIONS_TO_COVER,
import com.google.common.collect.ImmutableList;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.MeasureUtils;
import org.sonar.api.measures.Metric;
import java.util.Collection;
public final class OverallCoverageDecorator extends AbstractCoverageDecorator {
+
+ public OverallCoverageDecorator(Settings settings) {
+ super(settings);
+ }
+
@DependsUpon
public Collection<Metric> usedMetrics() {
return ImmutableList.<Metric>of(CoreMetrics.OVERALL_LINES_TO_COVER, CoreMetrics.OVERALL_UNCOVERED_LINES, CoreMetrics.NEW_OVERALL_LINES_TO_COVER,
import com.google.common.collect.ImmutableList;
import org.sonar.api.batch.DecoratorContext;
import org.sonar.api.batch.DependsUpon;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.MeasureUtils;
import org.sonar.api.measures.Metric;
import java.util.List;
public final class OverallLineCoverageDecorator extends AbstractCoverageDecorator {
+
+ public OverallLineCoverageDecorator(Settings settings) {
+ super(settings);
+ }
+
@DependsUpon
public List<Metric> dependsUponMetrics() {
return ImmutableList.<Metric>of(CoreMetrics.OVERALL_UNCOVERED_LINES, CoreMetrics.OVERALL_LINES_TO_COVER, CoreMetrics.NEW_OVERALL_UNCOVERED_LINES,
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Project;
import static org.mockito.Mockito.when;
public class BranchCoverageDecoratorTest {
- private final BranchCoverageDecorator decorator = new BranchCoverageDecorator();
+ private final BranchCoverageDecorator decorator = new BranchCoverageDecorator(new Settings());
private final Project resource = mock(Project.class);
@Before
import org.junit.Before;
import org.junit.Test;
+import org.sonar.api.CoreProperties;
import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import java.util.Collection;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.anyDouble;
-import static org.mockito.Mockito.eq;
+import static org.mockito.Matchers.anyDouble;
+import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class CoverageDecoratorTest {
- private final CoverageDecorator decorator = new CoverageDecorator();
+ private Settings settings;
+ private CoverageDecorator decorator;
private final Project project = mock(Project.class);
@Before
public void before() {
when(project.getScope()).thenReturn(Scopes.PROJECT);
+ settings = new Settings();
+ decorator = new CoverageDecorator(settings);
}
@Test
Collection<Metric> metrics = decorator.usedMetrics();
assertThat(metrics).containsOnly(CoreMetrics.LINES_TO_COVER, CoreMetrics.UNCOVERED_LINES, CoreMetrics.NEW_LINES_TO_COVER,
- CoreMetrics.NEW_UNCOVERED_LINES, CoreMetrics.CONDITIONS_TO_COVER, CoreMetrics.UNCOVERED_CONDITIONS,
- CoreMetrics.NEW_CONDITIONS_TO_COVER, CoreMetrics.NEW_UNCOVERED_CONDITIONS);
+ CoreMetrics.NEW_UNCOVERED_LINES, CoreMetrics.CONDITIONS_TO_COVER, CoreMetrics.UNCOVERED_CONDITIONS,
+ CoreMetrics.NEW_CONDITIONS_TO_COVER, CoreMetrics.NEW_UNCOVERED_CONDITIONS);
}
@Test
verify(context).saveMeasure(CoreMetrics.COVERAGE, 20.0);
}
+ @Test
+ public void forceCoverageByDefault() {
+ DecoratorContext context = mock(DecoratorContext.class);
+ when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 100.0));
+
+ decorator.decorate(project, context);
+
+ verify(context).saveMeasure(CoreMetrics.COVERAGE, 0.0);
+ }
+
+ @Test
+ public void dontForceCoverage() {
+ settings.setProperty(CoreProperties.COVERAGE_UNFORCED_KEY, "true");
+
+ DecoratorContext context = mock(DecoratorContext.class);
+ when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 100.0));
+
+ decorator.decorate(project, context);
+
+ verify(context, never()).saveMeasure(eq(CoreMetrics.COVERAGE), anyDouble());
+ }
+
@Test
public void coverageCanBe0() {
DecoratorContext context = mockContext(50, 50, 5, 5);
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Project;
import static org.mockito.Mockito.when;
public class ItBranchCoverageDecoratorTest {
- private final ItBranchCoverageDecorator decorator = new ItBranchCoverageDecorator();
+ private final ItBranchCoverageDecorator decorator = new ItBranchCoverageDecorator(new Settings());
private final Project resource = mock(Project.class);
@Before
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import static org.mockito.Mockito.when;
public class ItCoverageDecoratorTest {
- private final ItCoverageDecorator decorator = new ItCoverageDecorator();
+ private final ItCoverageDecorator decorator = new ItCoverageDecorator(new Settings());
private final Project project = mock(Project.class);
@Before
Collection<Metric> metrics = decorator.usedMetrics();
assertThat(metrics).containsOnly(CoreMetrics.IT_LINES_TO_COVER, CoreMetrics.IT_UNCOVERED_LINES, CoreMetrics.NEW_IT_LINES_TO_COVER,
- CoreMetrics.NEW_IT_UNCOVERED_LINES, CoreMetrics.IT_CONDITIONS_TO_COVER, CoreMetrics.IT_UNCOVERED_CONDITIONS,
- CoreMetrics.NEW_IT_CONDITIONS_TO_COVER, CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS);
+ CoreMetrics.NEW_IT_UNCOVERED_LINES, CoreMetrics.IT_CONDITIONS_TO_COVER, CoreMetrics.IT_UNCOVERED_CONDITIONS,
+ CoreMetrics.NEW_IT_CONDITIONS_TO_COVER, CoreMetrics.NEW_IT_UNCOVERED_CONDITIONS);
}
@Test
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.anyDouble;
-import static org.mockito.Mockito.eq;
+import static org.mockito.Matchers.anyDouble;
+import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class ItLineCoverageDecoratorTest {
- private final ItLineCoverageDecorator decorator = new ItLineCoverageDecorator();
+ private final ItLineCoverageDecorator decorator = new ItLineCoverageDecorator(new Settings());
private final Project project = mock(Project.class);
@Before
import org.junit.Before;
import org.junit.Test;
+import org.sonar.api.CoreProperties;
import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Project;
import static org.mockito.Mockito.when;
public class LineCoverageDecoratorTest {
- private final LineCoverageDecorator decorator = new LineCoverageDecorator();
+
+ private Settings settings;
+ private LineCoverageDecorator decorator;
private final Project project = mock(Project.class);
@Before
public void before() {
when(project.getScope()).thenReturn(Scopes.PROJECT);
+ settings = new Settings();
+ decorator = new LineCoverageDecorator(settings);
}
@Test
verify(context).saveMeasure(CoreMetrics.LINE_COVERAGE, 0.0);
}
+ @Test
+ public void forceCoverageByDefault() {
+ DecoratorContext context = mock(DecoratorContext.class);
+ when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 100.0));
+
+ decorator.decorate(project, context);
+
+ verify(context).saveMeasure(CoreMetrics.LINE_COVERAGE, 0.0);
+ }
+
+ @Test
+ public void dontForceCoverage() {
+ settings.setProperty(CoreProperties.COVERAGE_UNFORCED_KEY, "true");
+
+ DecoratorContext context = mock(DecoratorContext.class);
+ when(context.getMeasure(CoreMetrics.NCLOC)).thenReturn(new Measure(CoreMetrics.NCLOC, 100.0));
+
+ decorator.decorate(project, context);
+
+ verify(context, never()).saveMeasure(eq(CoreMetrics.LINE_COVERAGE), anyDouble());
+ }
+
@Test
public void allCoveredLines() {
DecoratorContext context = mockContext(50, 00);
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.resources.Project;
import static org.mockito.Mockito.when;
public class OverallBranchCoverageDecoratorTest {
- private final OverallBranchCoverageDecorator decorator = new OverallBranchCoverageDecorator();
+ private final OverallBranchCoverageDecorator decorator = new OverallBranchCoverageDecorator(new Settings());
private final Project resource = mock(Project.class);
@Before
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import static org.mockito.Mockito.when;
public class OverallCoverageDecoratorTest {
- private final OverallCoverageDecorator decorator = new OverallCoverageDecorator();
+ private final OverallCoverageDecorator decorator = new OverallCoverageDecorator(new Settings());
private final Project project = mock(Project.class);
@Before
Collection<Metric> metrics = decorator.usedMetrics();
assertThat(metrics).containsOnly(CoreMetrics.OVERALL_LINES_TO_COVER, CoreMetrics.OVERALL_UNCOVERED_LINES, CoreMetrics.NEW_OVERALL_LINES_TO_COVER,
- CoreMetrics.NEW_OVERALL_UNCOVERED_LINES, CoreMetrics.OVERALL_CONDITIONS_TO_COVER, CoreMetrics.OVERALL_UNCOVERED_CONDITIONS,
- CoreMetrics.NEW_OVERALL_CONDITIONS_TO_COVER, CoreMetrics.NEW_OVERALL_UNCOVERED_CONDITIONS);
+ CoreMetrics.NEW_OVERALL_UNCOVERED_LINES, CoreMetrics.OVERALL_CONDITIONS_TO_COVER, CoreMetrics.OVERALL_UNCOVERED_CONDITIONS,
+ CoreMetrics.NEW_OVERALL_CONDITIONS_TO_COVER, CoreMetrics.NEW_OVERALL_UNCOVERED_CONDITIONS);
}
@Test
import org.junit.Before;
import org.junit.Test;
import org.sonar.api.batch.DecoratorContext;
+import org.sonar.api.config.Settings;
import org.sonar.api.measures.CoreMetrics;
import org.sonar.api.measures.Measure;
import org.sonar.api.measures.Metric;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.mockito.Mockito.anyDouble;
-import static org.mockito.Mockito.eq;
+import static org.mockito.Matchers.anyDouble;
+import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
public class OverallLineCoverageDecoratorTest {
- private final OverallLineCoverageDecorator decorator = new OverallLineCoverageDecorator();
+ private final OverallLineCoverageDecorator decorator = new OverallLineCoverageDecorator(new Settings());
private final Project project = mock(Project.class);
@Before
List<Metric> metrics = decorator.dependsUponMetrics();
assertThat(metrics).containsOnly(CoreMetrics.OVERALL_UNCOVERED_LINES, CoreMetrics.OVERALL_LINES_TO_COVER, CoreMetrics.NEW_OVERALL_UNCOVERED_LINES,
- CoreMetrics.NEW_OVERALL_LINES_TO_COVER);
+ CoreMetrics.NEW_OVERALL_LINES_TO_COVER);
}
@Test
.category(CoreProperties.CATEGORY_EXCLUSIONS)
.subCategory(CoreProperties.SUBCATEGORY_DUPLICATIONS_EXCLUSIONS)
.multiValues(true)
+ .build(),
+ PropertyDefinition.builder(CoreProperties.COVERAGE_UNFORCED_KEY)
+ .defaultValue("false")
+ .type(PropertyType.BOOLEAN)
+ .name("Don't force computation of test coverage")
+ .description("By default test coverage is computed for all languages even if no coverage information is provided. "
+ + "If test coverage is not applicable to your situation and you don't want to see 0% coverage on your dashboards then set this property to 'true' "
+ + "and coverage measures will only be computed when some input data are provided.")
+ .onQualifiers(Qualifiers.PROJECT)
+ .category(CoreProperties.CATEGORY_CODE_COVERAGE)
.build()
));
return defs;
* @since 5.1
*/
String IMPORT_UNKNOWN_FILES_KEY = "sonar.import_unknown_files";
+
+ /**
+ * @since 5.1
+ */
+ String COVERAGE_UNFORCED_KEY = "sonar.coverage.unforced";
}