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.resources.Project;
28 import org.sonar.api.resources.Scopes;
30 import static org.fest.assertions.Assertions.assertThat;
31 import static org.mockito.Matchers.anyDouble;
32 import static org.mockito.Matchers.eq;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.never;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
38 public class AllTestsCoverageDecoratorTest {
39 private final AllTestsCoverageDecorator decorator = new AllTestsCoverageDecorator();
40 private final Project project = mock(Project.class);
43 public void before() {
44 when(project.getScope()).thenReturn(Scopes.PROJECT);
48 public void noCoverageWhenStaticAnalysis() {
49 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
50 assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isFalse();
52 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
53 assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
55 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
56 assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
60 public void coverage() {
61 DecoratorContext context = mockContext(50, 40, 10, 8);
63 decorator.decorate(project, context);
65 // (50-40 covered lines + 10-8 covered conditions) / (50 lines + 10 conditions)
66 verify(context).saveMeasure(CoreMetrics.MERGED_COVERAGE, 20.0);
70 public void coverageCanBe0() {
71 DecoratorContext context = mockContext(50, 50, 5, 5);
73 decorator.decorate(project, context);
75 verify(context).saveMeasure(CoreMetrics.MERGED_COVERAGE, 0.0);
79 public void coverageCanBe100() {
80 DecoratorContext context = mockContext(50, 0, 5, 0);
82 decorator.decorate(project, context);
84 verify(context).saveMeasure(CoreMetrics.MERGED_COVERAGE, 100.0);
88 public void noCoverageIfNoElements() {
89 DecoratorContext context = mock(DecoratorContext.class);
91 decorator.decorate(project, context);
93 verify(context, never()).saveMeasure(eq(CoreMetrics.MERGED_COVERAGE), anyDouble());
96 private static DecoratorContext mockContext(int lines, int uncoveredLines, int conditions, int uncoveredConditions) {
97 DecoratorContext context = mock(DecoratorContext.class);
98 when(context.getMeasure(CoreMetrics.MERGED_LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.MERGED_LINES_TO_COVER, (double) lines));
99 when(context.getMeasure(CoreMetrics.MERGED_UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.MERGED_UNCOVERED_LINES, (double) uncoveredLines));
100 when(context.getMeasure(CoreMetrics.MERGED_CONDITIONS_TO_COVER)).thenReturn(new Measure(CoreMetrics.MERGED_CONDITIONS_TO_COVER, (double) conditions));
101 when(context.getMeasure(CoreMetrics.MERGED_UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.MERGED_UNCOVERED_CONDITIONS, (double) uncoveredConditions));