2 * Sonar, open source software quality management tool.
3 * Copyright (C) 2008-2012 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * Sonar is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * Sonar is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with Sonar; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02
20 package org.sonar.plugins.core.sensors;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.sonar.api.batch.DecoratorContext;
25 import org.sonar.api.measures.CoreMetrics;
26 import org.sonar.api.measures.Measure;
27 import org.sonar.api.measures.Metric;
28 import org.sonar.api.resources.Project;
29 import org.sonar.api.resources.Scopes;
31 import java.util.Collection;
33 import static org.fest.assertions.Assertions.assertThat;
34 import static org.mockito.Matchers.anyDouble;
35 import static org.mockito.Matchers.eq;
36 import static org.mockito.Mockito.mock;
37 import static org.mockito.Mockito.never;
38 import static org.mockito.Mockito.verify;
39 import static org.mockito.Mockito.when;
41 public class OverallCoverageDecoratorTest {
42 private final OverallCoverageDecorator decorator = new OverallCoverageDecorator();
43 private final Project project = mock(Project.class);
46 public void before() {
47 when(project.getScope()).thenReturn(Scopes.PROJECT);
51 public void should_use_metrics() {
52 Collection<Metric> metrics = decorator.usedMetrics();
54 assertThat(metrics).containsOnly(CoreMetrics.OVERALL_LINES_TO_COVER, CoreMetrics.OVERALL_UNCOVERED_LINES, CoreMetrics.NEW_OVERALL_LINES_TO_COVER,
55 CoreMetrics.NEW_OVERALL_UNCOVERED_LINES, CoreMetrics.OVERALL_CONDITIONS_TO_COVER, CoreMetrics.OVERALL_UNCOVERED_CONDITIONS,
56 CoreMetrics.NEW_OVERALL_CONDITIONS_TO_COVER, CoreMetrics.NEW_OVERALL_UNCOVERED_CONDITIONS);
60 public void noCoverageWhenStaticAnalysis() {
61 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
62 assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isFalse();
64 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
65 assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
67 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
68 assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
72 public void coverage() {
73 DecoratorContext context = mockContext(50, 40, 10, 8);
75 decorator.decorate(project, context);
77 // (50-40 covered lines + 10-8 covered conditions) / (50 lines + 10 conditions)
78 verify(context).saveMeasure(CoreMetrics.OVERALL_COVERAGE, 20.0);
82 public void coverageCanBe0() {
83 DecoratorContext context = mockContext(50, 50, 5, 5);
85 decorator.decorate(project, context);
87 verify(context).saveMeasure(CoreMetrics.OVERALL_COVERAGE, 0.0);
91 public void coverageCanBe100() {
92 DecoratorContext context = mockContext(50, 0, 5, 0);
94 decorator.decorate(project, context);
96 verify(context).saveMeasure(CoreMetrics.OVERALL_COVERAGE, 100.0);
100 public void noCoverageIfNoElements() {
101 DecoratorContext context = mock(DecoratorContext.class);
103 decorator.decorate(project, context);
105 verify(context, never()).saveMeasure(eq(CoreMetrics.OVERALL_COVERAGE), anyDouble());
108 private static DecoratorContext mockContext(int lines, int uncoveredLines, int conditions, int uncoveredConditions) {
109 DecoratorContext context = mock(DecoratorContext.class);
110 when(context.getMeasure(CoreMetrics.OVERALL_LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.OVERALL_LINES_TO_COVER, (double) lines));
111 when(context.getMeasure(CoreMetrics.OVERALL_UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.OVERALL_UNCOVERED_LINES, (double) uncoveredLines));
112 when(context.getMeasure(CoreMetrics.OVERALL_CONDITIONS_TO_COVER)).thenReturn(new Measure(CoreMetrics.OVERALL_CONDITIONS_TO_COVER, (double) conditions));
113 when(context.getMeasure(CoreMetrics.OVERALL_UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.OVERALL_UNCOVERED_CONDITIONS, (double) uncoveredConditions));