]> source.dussan.org Git - sonarqube.git/blob
b5c4ef15c8a8b3754b55f53e114b3b9e0ce06951
[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.Collection;
32
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;
40
41 public class AllTestsCoverageDecoratorTest {
42   private final AllTestsCoverageDecorator decorator = new AllTestsCoverageDecorator();
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_use_metrics() {
52     Collection<Metric> metrics = decorator.usedMetrics();
53
54     assertThat(metrics).containsOnly(CoreMetrics.MERGED_LINES_TO_COVER, CoreMetrics.MERGED_UNCOVERED_LINES, CoreMetrics.NEW_MERGED_LINES_TO_COVER,
55         CoreMetrics.NEW_MERGED_UNCOVERED_LINES, CoreMetrics.MERGED_CONDITIONS_TO_COVER, CoreMetrics.MERGED_UNCOVERED_CONDITIONS,
56         CoreMetrics.NEW_MERGED_CONDITIONS_TO_COVER, CoreMetrics.NEW_MERGED_UNCOVERED_CONDITIONS);
57   }
58
59   @Test
60   public void noCoverageWhenStaticAnalysis() {
61     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
62     assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isFalse();
63
64     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
65     assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
66
67     when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
68     assertThat(new CoverageDecorator().shouldExecuteOnProject(project)).isTrue();
69   }
70
71   @Test
72   public void coverage() {
73     DecoratorContext context = mockContext(50, 40, 10, 8);
74
75     decorator.decorate(project, context);
76
77     // (50-40 covered lines + 10-8 covered conditions) / (50 lines + 10 conditions)
78     verify(context).saveMeasure(CoreMetrics.MERGED_COVERAGE, 20.0);
79   }
80
81   @Test
82   public void coverageCanBe0() {
83     DecoratorContext context = mockContext(50, 50, 5, 5);
84
85     decorator.decorate(project, context);
86
87     verify(context).saveMeasure(CoreMetrics.MERGED_COVERAGE, 0.0);
88   }
89
90   @Test
91   public void coverageCanBe100() {
92     DecoratorContext context = mockContext(50, 0, 5, 0);
93
94     decorator.decorate(project, context);
95
96     verify(context).saveMeasure(CoreMetrics.MERGED_COVERAGE, 100.0);
97   }
98
99   @Test
100   public void noCoverageIfNoElements() {
101     DecoratorContext context = mock(DecoratorContext.class);
102
103     decorator.decorate(project, context);
104
105     verify(context, never()).saveMeasure(eq(CoreMetrics.MERGED_COVERAGE), anyDouble());
106   }
107
108   private static DecoratorContext mockContext(int lines, int uncoveredLines, int conditions, int uncoveredConditions) {
109     DecoratorContext context = mock(DecoratorContext.class);
110     when(context.getMeasure(CoreMetrics.MERGED_LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.MERGED_LINES_TO_COVER, (double) lines));
111     when(context.getMeasure(CoreMetrics.MERGED_UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.MERGED_UNCOVERED_LINES, (double) uncoveredLines));
112     when(context.getMeasure(CoreMetrics.MERGED_CONDITIONS_TO_COVER)).thenReturn(new Measure(CoreMetrics.MERGED_CONDITIONS_TO_COVER, (double) conditions));
113     when(context.getMeasure(CoreMetrics.MERGED_UNCOVERED_CONDITIONS)).thenReturn(new Measure(CoreMetrics.MERGED_UNCOVERED_CONDITIONS, (double) uncoveredConditions));
114     return context;
115   }
116 }