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 static org.hamcrest.Matchers.is;
23 import static org.junit.Assert.assertThat;
25 import org.sonar.api.resources.Resource;
27 import org.junit.Before;
28 import org.junit.Test;
29 import static org.mockito.Mockito.*;
30 import org.sonar.api.batch.DecoratorContext;
31 import org.sonar.api.measures.CoreMetrics;
32 import org.sonar.api.measures.Measure;
33 import org.sonar.api.resources.Project;
35 public class LineCoverageDecoratorTest {
37 private Project project = null;
40 public void before() {
41 project = mock(Project.class);
42 when(project.getScope()).thenReturn(Resource.SCOPE_SET);
46 public void noCoverageWhenStaticAnalysis() {
47 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.STATIC);
48 assertThat(new LineCoverageDecorator().shouldExecuteOnProject(project), is(false));
50 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.REUSE_REPORTS);
51 assertThat(new LineCoverageDecorator().shouldExecuteOnProject(project), is(true));
53 when(project.getAnalysisType()).thenReturn(Project.AnalysisType.DYNAMIC);
54 assertThat(new LineCoverageDecorator().shouldExecuteOnProject(project), is(true));
58 public void lineCoverage() {
59 DecoratorContext context = mockContext(50, 10);
60 new LineCoverageDecorator().decorate(project, context);
62 // 50-10 covered lines / 50 lines
63 verify(context).saveMeasure(CoreMetrics.LINE_COVERAGE, 80.0);
68 public void noLineCoverageIfNoLines() {
69 DecoratorContext context = mockContext(null, null);
70 new LineCoverageDecorator().decorate(project, context);
72 verify(context, never()).saveMeasure(eq(CoreMetrics.LINE_COVERAGE), anyDouble());
76 public void zeroCoveredLines() {
77 DecoratorContext context = mockContext(50, 50);
78 new LineCoverageDecorator().decorate(project, context);
80 verify(context).saveMeasure(CoreMetrics.LINE_COVERAGE, 0.0);
84 public void allCoveredLines() {
85 DecoratorContext context = mockContext(50, 00);
86 new LineCoverageDecorator().decorate(project, context);
88 verify(context).saveMeasure(CoreMetrics.LINE_COVERAGE, 100.0);
91 private DecoratorContext mockContext(Integer lines, Integer uncoveredLines) {
92 DecoratorContext context = mock(DecoratorContext.class);
94 when(context.getMeasure(CoreMetrics.LINES_TO_COVER)).thenReturn(new Measure(CoreMetrics.LINES_TO_COVER, lines.doubleValue()));
96 if (uncoveredLines != null) {
97 when(context.getMeasure(CoreMetrics.UNCOVERED_LINES)).thenReturn(new Measure(CoreMetrics.UNCOVERED_LINES, uncoveredLines.doubleValue()));