]> source.dussan.org Git - sonarqube.git/blob
0a51ab09c5e6f3ffa3dfa34711bb47fd5426bd22
[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.resources.Project;
28 import org.sonar.api.resources.Scopes;
29
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;
37
38 public class AllTestsCoverageDecoratorTest {
39   private final AllTestsCoverageDecorator decorator = new AllTestsCoverageDecorator();
40   private final Project project = mock(Project.class);
41
42   @Before
43   public void before() {
44     when(project.getScope()).thenReturn(Scopes.PROJECT);
45   }
46
47   @Test
48   public void noCoverageWhenStaticAnalysis() {
49     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
50     assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isFalse();
51
52     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
53     assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
54
55     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
56     assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
57   }
58
59   @Test
60   public void coverage() {
61     DecoratorContext context = mockContext(50, 40, 10, 8);
62
63     decorator.decorate(project, context);
64
65     // (50-40 covered lines + 10-8 covered conditions) / (50 lines + 10 conditions)
66     verify(context).saveMeasure(CoreMetrics.MERGED_COVERAGE, 20.0);
67   }
68
69   @Test
70   public void coverageCanBe0() {
71     DecoratorContext context = mockContext(50, 50, 5, 5);
72
73     decorator.decorate(project, context);
74
75     verify(context).saveMeasure(CoreMetrics.MERGED_COVERAGE, 0.0);
76   }
77
78   @Test
79   public void coverageCanBe100() {
80     DecoratorContext context = mockContext(50, 0, 5, 0);
81
82     decorator.decorate(project, context);
83
84     verify(context).saveMeasure(CoreMetrics.MERGED_COVERAGE, 100.0);
85   }
86
87   @Test
88   public void noCoverageIfNoElements() {
89     DecoratorContext context = mock(DecoratorContext.class);
90
91     decorator.decorate(project, context);
92
93     verify(context, never()).saveMeasure(eq(CoreMetrics.MERGED_COVERAGE), anyDouble());
94   }
95
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));
102     return context;
103   }
104 }