]> source.dussan.org Git - sonarqube.git/blob
d9eb3f67744164388cfb1134f7c1433db58064bc
[sonarqube.git] /
1 /*
2  * Sonar, open source software quality management tool.
3  * Copyright (C) 2008-2012 SonarSource
4  * mailto:contact AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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
19  */
20 package org.sonar.plugins.core.sensors;
21
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;
30
31 import java.util.List;
32
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;
40
41 public class AllTestsLineCoverageDecoratorTest {
42   private final AllTestsLineCoverageDecorator decorator = new AllTestsLineCoverageDecorator();
43   private final Project project = mock(Project.class);
44
45   @Before
46   public void before() {
47     when(project.getScope()).thenReturn(Scopes.PROJECT);
48   }
49
50   @Test
51   public void should_depend_on_coverage_metrics() {
52     List<Metric> metrics = decorator.dependsUponMetrics();
53
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);
56   }
57
58   @Test
59   public void noCoverageWhenStaticAnalysis() {
60     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
61     assertThat(decorator.shouldExecuteOnProject(project)).isFalse();
62
63     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
64     assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
65
66     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
67     assertThat(decorator.shouldExecuteOnProject(project)).isTrue();
68   }
69
70   @Test
71   public void lineCoverage() {
72     DecoratorContext context = mockContext(50, 10);
73
74     decorator.decorate(project, context);
75
76     // 50-10 covered lines / 50 lines
77     verify(context).saveMeasure(CoreMetrics.MERGED_LINE_COVERAGE, 80.0);
78   }
79
80   @Test
81   public void zeroCoveredLines() {
82     DecoratorContext context = mockContext(50, 50);
83
84     decorator.decorate(project, context);
85
86     verify(context).saveMeasure(CoreMetrics.MERGED_LINE_COVERAGE, 0.0);
87   }
88
89   @Test
90   public void allCoveredLines() {
91     DecoratorContext context = mockContext(50, 00);
92
93     decorator.decorate(project, context);
94
95     verify(context).saveMeasure(CoreMetrics.MERGED_LINE_COVERAGE, 100.0);
96   }
97
98   @Test
99   public void noLineCoverageIfNoLines() {
100     DecoratorContext context = mock(DecoratorContext.class);
101
102     decorator.decorate(project, context);
103
104     verify(context, never()).saveMeasure(eq(CoreMetrics.MERGED_LINE_COVERAGE), anyDouble());
105   }
106
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));
111     return context;
112   }
113 }