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.List;
33 import static org.fest.assertions.Assertions.assertThat;
34 import static org.mockito.Mockito.anyDouble;
35 import static org.mockito.Mockito.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 AllTestsLineCoverageDecoratorTest {
42 private final AllTestsLineCoverageDecorator decorator = new AllTestsLineCoverageDecorator();
43 private final Project project = mock(Project.class);
46 public void before() {
47 when(project.getScope()).thenReturn(Scopes.PROJECT);
51 public void should_depend_on_coverage_metrics() {
52 List<Metric> metrics = decorator.dependsUponMetrics();
54 assertThat(metrics).containsOnly(CoreMetrics.MERGED_UNCOVERED_LINES, CoreMetrics.MERGED_LINES_TO_COVER, CoreMetrics.NEW_MERGED_UNCOVERED_LINES,
55 CoreMetrics.NEW_MERGED_LINES_TO_COVER);
59 public void noCoverageWhenStaticAnalysis() {
60 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
61 assertThat(decorator.shouldExecuteOnProject(project)).isFalse();
63 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
64 assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
66 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
67 assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
71 public void lineCoverage() {
72 DecoratorContext context = mockContext(50, 10);
74 decorator.decorate(project, context);
76 // 50-10 covered lines / 50 lines
77 verify(context).saveMeasure(CoreMetrics.MERGED_LINE_COVERAGE, 80.0);
81 public void zeroCoveredLines() {
82 DecoratorContext context = mockContext(50, 50);
84 decorator.decorate(project, context);
86 verify(context).saveMeasure(CoreMetrics.MERGED_LINE_COVERAGE, 0.0);
90 public void allCoveredLines() {
91 DecoratorContext context = mockContext(50, 00);
93 decorator.decorate(project, context);
95 verify(context).saveMeasure(CoreMetrics.MERGED_LINE_COVERAGE, 100.0);
99 public void noLineCoverageIfNoLines() {
100 DecoratorContext context = mock(DecoratorContext.class);
102 decorator.decorate(project, context);
104 verify(context, never()).saveMeasure(eq(CoreMetrics.MERGED_LINE_COVERAGE), anyDouble());
107 private static DecoratorContext mockContext(int lines, int uncoveredLines) {
108 DecoratorContext context = mock(DecoratorContext.class);
109 when(context.getMeasure(CoreMetrics.MERGED_LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.MERGED_LINES_TO_COVER, (double) lines));
110 when(context.getMeasure(CoreMetrics.MERGED_UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.MERGED_UNCOVERED_LINES, (double) uncoveredLines));